1818use DateTime ;
1919use Phar ;
2020use RuntimeException ;
21+ use SplFileInfo ;
2122use Symfony \Component \Finder \Finder ;
2223use Symfony \Component \Process \Process ;
2324
@@ -44,118 +45,52 @@ class Compiler
4445 * Compiles composer into a single phar file
4546 *
4647 * @throws RuntimeException
47- *
48- * @param string $pharFile The full path to the file to create
4948 */
50- public function compile ($ pharFile = ' php-formatter.phar ' )
49+ public function compile ()
5150 {
52- if (file_exists ($ pharFile )) {
53- unlink ($ pharFile );
54- }
51+ $ pharFilePath = dirname (__FILE__ ) . '/../../../build/php-formatter.phar ' ;
5552
56- /**
57- * Loading versions
58- */
59- $ process = new Process ('git log --pretty="%H" -n1 HEAD ' , __DIR__ );
60- if ($ process ->run () != 0 ) {
61- throw new \RuntimeException ('Can \'t run git log. You must ensure to run compile from php-formatter git repository clone and that git binary is available. ' );
53+ if (file_exists ($ pharFilePath )) {
54+ unlink ($ pharFilePath );
6255 }
63- $ this ->version = trim ($ process ->getOutput ());
6456
65- $ process = new Process ('git log -n1 --pretty=%ci HEAD ' , __DIR__ );
66- if ($ process ->run () != 0 ) {
67- throw new \RuntimeException ('Can \'t run git log. You must ensure to run compile from php-formatter git repository clone and that git binary is available. ' );
68- }
69- $ date = new \DateTime (trim ($ process ->getOutput ()));
70- $ date ->setTimezone (new \DateTimeZone ('UTC ' ));
71- $ this ->versionDate = $ date ->format ('Y-m-d H:i:s ' );
72-
73- $ process = new Process ('git describe --tags HEAD ' );
74- if ($ process ->run () == 0 ) {
75- $ this ->version = trim ($ process ->getOutput ());
76- }
57+ $ this ->loadVersion ();
7758
7859 /**
7960 * Creating phar object
8061 */
81- $ phar = new Phar ($ pharFile , 0 , 'php-formatter.phar ' );
62+ $ phar = new Phar ($ pharFilePath , 0 , 'php-formatter.phar ' );
8263 $ phar ->setSignatureAlgorithm (\Phar::SHA1 );
8364
8465 $ phar ->startBuffering ();
8566
86- /**
87- * All *.php files
88- */
89- $ finder = new Finder ();
90- $ finder
91- ->files ()
92- ->ignoreVCS (true )
93- ->name ('*.php ' )
94- ->notName ('Compiler.php ' )
95- ->notName ('ClassLoader.php ' )
96- ->in (realpath (__DIR__ . '/../../../src ' ));
97-
98- foreach ($ finder as $ file ) {
99- $ this ->addFile ($ phar , $ file );
100- }
101-
102- /**
103- * All vendors (ignoring tests)
104- */
105- $ finder = new Finder ();
106- $ finder
107- ->files ()
108- ->ignoreVCS (true )
109- ->name ('*.php ' )
110- ->exclude ('Tests ' )
111- ->in (realpath (__DIR__ . '/../../../vendor/symfony/ ' ));
112-
113- foreach ($ finder as $ file ) {
114- $ this ->addFile ($ phar , $ file );
115- }
116-
117- /**
118- * Adding composer vendor files
119- */
120- $ this ->addFile ($ phar , new \SplFileInfo (__DIR__ . '/../../../vendor/autoload.php ' ));
121- $ this ->addFile ($ phar , new \SplFileInfo (__DIR__ . '/../../../vendor/composer/autoload_namespaces.php ' ));
122- $ this ->addFile ($ phar , new \SplFileInfo (__DIR__ . '/../../../vendor/composer/autoload_psr4.php ' ));
123- $ this ->addFile ($ phar , new \SplFileInfo (__DIR__ . '/../../../vendor/composer/autoload_classmap.php ' ));
124- $ this ->addFile ($ phar , new \SplFileInfo (__DIR__ . '/../../../vendor/composer/autoload_real.php ' ));
125- $ this ->addFile ($ phar , new \SplFileInfo (__DIR__ . '/../../../vendor/composer/ClassLoader.php ' ));
126-
127- if (file_exists (__DIR__ . '/../../../vendor/composer/include_paths.php ' )) {
128- $ this ->addFile ($ phar , new \SplFileInfo (__DIR__ . '/../../../vendor/composer/include_paths.php ' ));
129- }
130-
131- /**
132- * Adding bin
133- */
134- $ this ->addBin ($ phar );
135-
136- /**
137- * Adding stubs
138- */
139- $ phar ->setStub ($ this ->getStub ());
67+ $ this
68+ ->addPHPFiles ($ phar )
69+ ->addVendorFiles ($ phar )
70+ ->addComposerVendorFiles ($ phar )
71+ ->addBin ($ phar )
72+ ->addStub ($ phar )
73+ ->addLicense ($ phar );
14074
14175 $ phar ->stopBuffering ();
14276
143- /**
144- * Adding LICENSE
145- */
146- $ this ->addFile ($ phar , new \SplFileInfo (__DIR__ . '/../../../LICENSE ' ), false );
147-
14877 unset($ phar );
14978 }
15079
15180 /**
15281 * Add a file into the phar package
15382 *
154- * @param Phar $phar Phar object
155- * @param string $file File to add
156- * @param bool $strip strip
83+ * @param Phar $phar Phar object
84+ * @param SplFileInfo $file File to add
85+ * @param bool $strip strip
86+ *
87+ * @return Compiler self Object
15788 */
158- protected function addFile (Phar $ phar , $ file , $ strip = true )
89+ protected function addFile (
90+ Phar $ phar ,
91+ SplFileInfo $ file ,
92+ $ strip = true
93+ )
15994 {
16095 $ path = strtr (str_replace (dirname (dirname (dirname (__DIR__ ))) . DIRECTORY_SEPARATOR , '' , $ file ->getRealPath ()), '\\' , '/ ' );
16196 $ content = file_get_contents ($ file );
@@ -171,18 +106,24 @@ protected function addFile(Phar $phar, $file, $strip = true)
171106 }
172107
173108 $ phar ->addFromString ($ path , $ content );
109+
110+ return $ this ;
174111 }
175112
176113 /**
177114 * Add bin into Phar
178115 *
179116 * @param Phar $phar Phar
117+ *
118+ * @return Compiler self Object
180119 */
181120 protected function addBin (Phar $ phar )
182121 {
183122 $ content = file_get_contents (__DIR__ . '/../../../bin/php-formatter ' );
184123 $ content = preg_replace ('{^#!/usr/bin/env php\s*} ' , '' , $ content );
185124 $ phar ->addFromString ('bin/php-formatter ' , $ content );
125+
126+ return $ this ;
186127 }
187128
188129 /**
@@ -220,9 +161,9 @@ protected function stripWhitespace($source)
220161 return $ output ;
221162 }
222163
223- protected function getStub ( )
164+ protected function addStub ( Phar $ phar )
224165 {
225- return <<<'EOF'
166+ $ stub = <<<'EOF'
226167#!/usr/bin/env php
227168<?php
228169
@@ -245,5 +186,138 @@ protected function getStub()
245186
246187__HALT_COMPILER();
247188EOF;
189+ $ phar ->setStub ($ stub );
190+
191+ return $ this ;
192+ }
193+
194+ /**
195+ * Add php files
196+ *
197+ * @param Phar $phar Phar instance
198+ *
199+ * @return Compiler self Object
200+ */
201+ private function addPHPFiles (Phar $ phar )
202+ {
203+ /**
204+ * All *.php files
205+ */
206+ $ finder = new Finder ();
207+ $ finder
208+ ->files ()
209+ ->ignoreVCS (true )
210+ ->name ('*.php ' )
211+ ->notName ('Compiler.php ' )
212+ ->notName ('ClassLoader.php ' )
213+ ->in (realpath (__DIR__ . '/../../../src ' ));
214+
215+ foreach ($ finder as $ file ) {
216+
217+ $ this ->addFile ($ phar , $ file );
218+ }
219+
220+ return $ this ;
221+ }
222+
223+ /**
224+ * Add vendor files
225+ *
226+ * @param Phar $phar Phar instance
227+ *
228+ * @return Compiler self Object
229+ */
230+ private function addVendorFiles (Phar $ phar )
231+ {
232+ $ vendorPath = __DIR__ . '/../../../vendor/ ' ;
233+
234+ /**
235+ * All *.php files
236+ */
237+ $ finder = new Finder ();
238+ $ finder
239+ ->files ()
240+ ->ignoreVCS (true )
241+ ->name ('*.php ' )
242+ ->exclude ('Tests ' )
243+ ->in (realpath ($ vendorPath . 'symfony/ ' ));
244+
245+ foreach ($ finder as $ file ) {
246+
247+ $ this ->addFile ($ phar , $ file );
248+ }
249+
250+ return $ this ;
251+ }
252+
253+ /**
254+ * Add composer vendor files
255+ *
256+ * @param Phar $phar Phar
257+ *
258+ * @return Compiler self Object
259+ */
260+ private function addComposerVendorFiles (Phar $ phar )
261+ {
262+ $ vendorPath = __DIR__ . '/../../../vendor/ ' ;
263+
264+ /**
265+ * Adding composer vendor files
266+ */
267+ $ this
268+ ->addFile ($ phar , new \SplFileInfo ($ vendorPath . 'autoload.php ' ))
269+ ->addFile ($ phar , new \SplFileInfo ($ vendorPath . 'composer/autoload_namespaces.php ' ))
270+ ->addFile ($ phar , new \SplFileInfo ($ vendorPath . 'composer/autoload_psr4.php ' ))
271+ ->addFile ($ phar , new \SplFileInfo ($ vendorPath . 'composer/autoload_classmap.php ' ))
272+ ->addFile ($ phar , new \SplFileInfo ($ vendorPath . 'composer/autoload_real.php ' ))
273+ ->addFile ($ phar , new \SplFileInfo ($ vendorPath . 'composer/ClassLoader.php ' ));
274+
275+ if (file_exists ($ vendorPath . 'composer/include_paths.php ' )) {
276+
277+ $ this ->addFile ($ phar , new \SplFileInfo ($ vendorPath . 'composer/include_paths.php ' ));
278+ }
279+
280+ return $ this ;
281+ }
282+
283+ /**
284+ * Add license
285+ *
286+ * @param Phar $phar Phar
287+ *
288+ * @return Compiler self Object
289+ */
290+ private function addLicense (Phar $ phar )
291+ {
292+ $ this ->addFile ($ phar , new \SplFileInfo (__DIR__ . '/../../../LICENSE ' ), false );
293+
294+ return $ this ;
295+ }
296+
297+ /**
298+ * Load versions
299+ */
300+ private function loadVersion ()
301+ {
302+ $ process = new Process ('git log --pretty="%H" -n1 HEAD ' , __DIR__ );
303+ if ($ process ->run () != 0 ) {
304+ throw new \RuntimeException ('Can \'t run git log. You must ensure to run compile from php-formatter git repository clone and that git binary is available. ' );
305+ }
306+ $ this ->version = trim ($ process ->getOutput ());
307+
308+ $ process = new Process ('git log -n1 --pretty=%ci HEAD ' , __DIR__ );
309+ if ($ process ->run () != 0 ) {
310+ throw new \RuntimeException ('Can \'t run git log. You must ensure to run compile from php-formatter git repository clone and that git binary is available. ' );
311+ }
312+ $ date = new \DateTime (trim ($ process ->getOutput ()));
313+ $ date ->setTimezone (new \DateTimeZone ('UTC ' ));
314+ $ this ->versionDate = $ date ->format ('Y-m-d H:i:s ' );
315+
316+ $ process = new Process ('git describe --tags HEAD ' );
317+ if ($ process ->run () == 0 ) {
318+ $ this ->version = trim ($ process ->getOutput ());
319+ }
320+
321+ return $ this ;
248322 }
249323}
0 commit comments