18
18
use DateTime ;
19
19
use Phar ;
20
20
use RuntimeException ;
21
+ use SplFileInfo ;
21
22
use Symfony \Component \Finder \Finder ;
22
23
use Symfony \Component \Process \Process ;
23
24
@@ -44,118 +45,52 @@ class Compiler
44
45
* Compiles composer into a single phar file
45
46
*
46
47
* @throws RuntimeException
47
- *
48
- * @param string $pharFile The full path to the file to create
49
48
*/
50
- public function compile ($ pharFile = ' php-formatter.phar ' )
49
+ public function compile ()
51
50
{
52
- if (file_exists ($ pharFile )) {
53
- unlink ($ pharFile );
54
- }
51
+ $ pharFilePath = dirname (__FILE__ ) . '/../../../build/php-formatter.phar ' ;
55
52
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 );
62
55
}
63
- $ this ->version = trim ($ process ->getOutput ());
64
56
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 ();
77
58
78
59
/**
79
60
* Creating phar object
80
61
*/
81
- $ phar = new Phar ($ pharFile , 0 , 'php-formatter.phar ' );
62
+ $ phar = new Phar ($ pharFilePath , 0 , 'php-formatter.phar ' );
82
63
$ phar ->setSignatureAlgorithm (\Phar::SHA1 );
83
64
84
65
$ phar ->startBuffering ();
85
66
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 );
140
74
141
75
$ phar ->stopBuffering ();
142
76
143
- /**
144
- * Adding LICENSE
145
- */
146
- $ this ->addFile ($ phar , new \SplFileInfo (__DIR__ . '/../../../LICENSE ' ), false );
147
-
148
77
unset($ phar );
149
78
}
150
79
151
80
/**
152
81
* Add a file into the phar package
153
82
*
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
157
88
*/
158
- protected function addFile (Phar $ phar , $ file , $ strip = true )
89
+ protected function addFile (
90
+ Phar $ phar ,
91
+ SplFileInfo $ file ,
92
+ $ strip = true
93
+ )
159
94
{
160
95
$ path = strtr (str_replace (dirname (dirname (dirname (__DIR__ ))) . DIRECTORY_SEPARATOR , '' , $ file ->getRealPath ()), '\\' , '/ ' );
161
96
$ content = file_get_contents ($ file );
@@ -171,18 +106,24 @@ protected function addFile(Phar $phar, $file, $strip = true)
171
106
}
172
107
173
108
$ phar ->addFromString ($ path , $ content );
109
+
110
+ return $ this ;
174
111
}
175
112
176
113
/**
177
114
* Add bin into Phar
178
115
*
179
116
* @param Phar $phar Phar
117
+ *
118
+ * @return Compiler self Object
180
119
*/
181
120
protected function addBin (Phar $ phar )
182
121
{
183
122
$ content = file_get_contents (__DIR__ . '/../../../bin/php-formatter ' );
184
123
$ content = preg_replace ('{^#!/usr/bin/env php\s*} ' , '' , $ content );
185
124
$ phar ->addFromString ('bin/php-formatter ' , $ content );
125
+
126
+ return $ this ;
186
127
}
187
128
188
129
/**
@@ -220,9 +161,9 @@ protected function stripWhitespace($source)
220
161
return $ output ;
221
162
}
222
163
223
- protected function getStub ( )
164
+ protected function addStub ( Phar $ phar )
224
165
{
225
- return <<<'EOF'
166
+ $ stub = <<<'EOF'
226
167
#!/usr/bin/env php
227
168
<?php
228
169
@@ -245,5 +186,138 @@ protected function getStub()
245
186
246
187
__HALT_COMPILER();
247
188
EOF;
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 ;
248
322
}
249
323
}
0 commit comments