8
8
use DateTimeZone ;
9
9
use Exception ;
10
10
use FilesystemIterator ;
11
+ use Generator ;
11
12
use Inhere \Console \Util \Helper ;
12
13
use InvalidArgumentException ;
13
14
use Iterator ;
@@ -116,20 +117,29 @@ class PharCompiler
116
117
private $ suffixes = ['.php ' ];
117
118
118
119
/**
119
- * @var array Want to exclude directory/file name list
120
+ * Want to exclude directory/file name list
121
+ *
122
+ * ```php
120
123
* [
121
124
* '/test/', // exclude all contains '/test/' path
122
125
* ]
126
+ * ```
127
+ *
128
+ * @var array
123
129
*/
124
130
private $ excludes = [];
125
131
126
132
/**
127
- * @var array The directory paths, will collect files in there.
133
+ * The directory paths, will collect files in there.
134
+ *
135
+ * @var array
128
136
*/
129
137
private $ directories = [];
130
138
131
139
/**
132
- * @var Closure[] Some events. if you want to get some info on packing.
140
+ * Some events. if you want to get some info on packing.
141
+ *
142
+ * @var Closure[]
133
143
*/
134
144
private $ events = [];
135
145
@@ -178,6 +188,9 @@ class PharCompiler
178
188
*/
179
189
private $ versionFile = '' ;
180
190
191
+ /**
192
+ * @var string
193
+ */
181
194
private $ versionFileContent = '' ;
182
195
183
196
// -------------------- internal properties --------------------
@@ -210,6 +223,22 @@ class PharCompiler
210
223
*/
211
224
private $ fileQueue ;
212
225
226
+ /**
227
+ * @throws RuntimeException
228
+ */
229
+ private static function checkEnv (): void
230
+ {
231
+ if (!class_exists (Phar::class, false )) {
232
+ throw new RuntimeException ("The 'phar' extension is required for build phar package " );
233
+ }
234
+
235
+ if (ini_get ('phar.readonly ' )) {
236
+ throw new RuntimeException (
237
+ "The 'phar.readonly' is 'On', build phar must setting it 'Off' or exec with 'php -d phar.readonly=0' "
238
+ );
239
+ }
240
+ }
241
+
213
242
/**
214
243
* @param string $pharFile
215
244
* @param string $extractTo
@@ -220,7 +249,7 @@ class PharCompiler
220
249
* @throws BadMethodCallException
221
250
* @throws RuntimeException
222
251
*/
223
- public static function unpack (string $ pharFile , string $ extractTo , $ files = null , $ overwrite = false ): bool
252
+ public static function unpack (string $ pharFile , string $ extractTo , $ files = null , bool $ overwrite = false ): bool
224
253
{
225
254
self ::checkEnv ();
226
255
@@ -229,22 +258,6 @@ public static function unpack(string $pharFile, string $extractTo, $files = null
229
258
return $ phar ->extractTo ($ extractTo , $ files , $ overwrite );
230
259
}
231
260
232
- /**
233
- * @throws RuntimeException
234
- */
235
- private static function checkEnv (): void
236
- {
237
- if (!class_exists (Phar::class, false )) {
238
- throw new RuntimeException ("The 'phar' extension is required for build phar package " );
239
- }
240
-
241
- if (ini_get ('phar.readonly ' )) {
242
- throw new RuntimeException (
243
- "The 'phar.readonly' is 'On', build phar must setting it 'Off' or exec with 'php -d phar.readonly=0' "
244
- );
245
- }
246
- }
247
-
248
261
/**
249
262
* PharCompiler constructor.
250
263
*
@@ -256,7 +269,7 @@ public function __construct(string $basePath)
256
269
{
257
270
self ::checkEnv ();
258
271
259
- $ this ->basePath = realpath ($ basePath );
272
+ $ this ->basePath = File:: realpath ($ basePath );
260
273
$ this ->fileQueue = new SplQueue ();
261
274
262
275
if (!is_dir ($ this ->basePath )) {
@@ -352,7 +365,7 @@ public function setExcludes(array $excludes): self
352
365
}
353
366
354
367
/**
355
- * @param bool $value
368
+ * @param bool|string|int $value
356
369
*
357
370
* @return PharCompiler
358
371
*/
@@ -363,7 +376,7 @@ public function stripComments($value): self
363
376
}
364
377
365
378
/**
366
- * @param bool $value
379
+ * @param bool|string|int $value
367
380
*
368
381
* @return PharCompiler
369
382
*/
@@ -578,9 +591,9 @@ protected function collectFileInfo(SplFileInfo $file): void
578
591
*
579
592
* @throws RuntimeException
580
593
*/
581
- public function findChangedByGit ()
594
+ public function findChangedByGit (): ? Generator
582
595
{
583
- // -u expand dir's files
596
+ // -u expand dir files
584
597
[, $ output ,] = Sys::run ('git status -s -u ' , $ this ->basePath );
585
598
586
599
// 'D some.file' deleted
@@ -608,10 +621,10 @@ public function findChangedByGit()
608
621
/**
609
622
* @param string $directory
610
623
*
611
- * @return Iterator|SplFileInfo[]
624
+ * @return Iterator
612
625
* @throws InvalidArgumentException
613
626
*/
614
- protected function findFiles (string $ directory )
627
+ protected function findFiles (string $ directory ): Iterator
615
628
{
616
629
return Helper::directoryIterator (
617
630
$ directory ,
@@ -711,19 +724,21 @@ private function createStub(): string
711
724
$ stub = "$ shebang \n$ stub " ;
712
725
}
713
726
714
- if ($ this ->cliIndex && $ this ->webIndex ) {
727
+ $ cliIndex = $ this ->cliIndex ;
728
+ $ webIndex = $ this ->webIndex ;
729
+ if ($ cliIndex && $ webIndex ) {
715
730
$ stub .= <<<EOF
716
731
// for command line
717
732
if (PHP_SAPI === 'cli') {
718
- require 'phar:// $ pharName/ { $ this -> cliIndex } ';
733
+ require 'phar:// $ pharName/ $ cliIndex';
719
734
} else {
720
- require 'phar:// $ pharName/ { $ this -> webIndex } ';
735
+ require 'phar:// $ pharName/ $ webIndex';
721
736
}
722
737
EOF ;
723
- } elseif ($ this -> cliIndex ) {
724
- $ stub .= "\nrequire 'phar:// $ pharName/ { $ this -> cliIndex } '; \n" ;
725
- } elseif ($ this -> webIndex ) {
726
- $ stub .= "\nrequire 'phar:// $ pharName/ { $ this -> webIndex } '; \n" ;
738
+ } elseif ($ cliIndex ) {
739
+ $ stub .= "\nrequire 'phar:// $ pharName/ $ cliIndex'; \n" ;
740
+ } elseif ($ webIndex ) {
741
+ $ stub .= "\nrequire 'phar:// $ pharName/ $ webIndex'; \n" ;
727
742
} else {
728
743
throw new RuntimeException ("'cliIndex' and 'webIndex', please set at least one " );
729
744
}
@@ -989,11 +1004,13 @@ public function setVersionFile(string $versionFile): PharCompiler
989
1004
}
990
1005
991
1006
/**
1007
+ * @param bool $abbrev
1008
+ *
992
1009
* @return string
993
1010
*/
994
- public function getLastCommit (): string
1011
+ public function getLastCommit (bool $ abbrev = true ): string
995
1012
{
996
- return $ this ->lastCommit ;
1013
+ return $ abbrev ? substr ( $ this -> lastCommit , 0 , 7 ) : $ this ->lastCommit ;
997
1014
}
998
1015
999
1016
/**
0 commit comments