88use DateTimeZone ;
99use Exception ;
1010use FilesystemIterator ;
11+ use Generator ;
1112use Inhere \Console \Util \Helper ;
1213use InvalidArgumentException ;
1314use Iterator ;
@@ -116,20 +117,29 @@ class PharCompiler
116117 private $ suffixes = ['.php ' ];
117118
118119 /**
119- * @var array Want to exclude directory/file name list
120+ * Want to exclude directory/file name list
121+ *
122+ * ```php
120123 * [
121124 * '/test/', // exclude all contains '/test/' path
122125 * ]
126+ * ```
127+ *
128+ * @var array
123129 */
124130 private $ excludes = [];
125131
126132 /**
127- * @var array The directory paths, will collect files in there.
133+ * The directory paths, will collect files in there.
134+ *
135+ * @var array
128136 */
129137 private $ directories = [];
130138
131139 /**
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[]
133143 */
134144 private $ events = [];
135145
@@ -178,6 +188,9 @@ class PharCompiler
178188 */
179189 private $ versionFile = '' ;
180190
191+ /**
192+ * @var string
193+ */
181194 private $ versionFileContent = '' ;
182195
183196 // -------------------- internal properties --------------------
@@ -210,6 +223,22 @@ class PharCompiler
210223 */
211224 private $ fileQueue ;
212225
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+
213242 /**
214243 * @param string $pharFile
215244 * @param string $extractTo
@@ -220,7 +249,7 @@ class PharCompiler
220249 * @throws BadMethodCallException
221250 * @throws RuntimeException
222251 */
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
224253 {
225254 self ::checkEnv ();
226255
@@ -229,22 +258,6 @@ public static function unpack(string $pharFile, string $extractTo, $files = null
229258 return $ phar ->extractTo ($ extractTo , $ files , $ overwrite );
230259 }
231260
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-
248261 /**
249262 * PharCompiler constructor.
250263 *
@@ -256,7 +269,7 @@ public function __construct(string $basePath)
256269 {
257270 self ::checkEnv ();
258271
259- $ this ->basePath = realpath ($ basePath );
272+ $ this ->basePath = File:: realpath ($ basePath );
260273 $ this ->fileQueue = new SplQueue ();
261274
262275 if (!is_dir ($ this ->basePath )) {
@@ -352,7 +365,7 @@ public function setExcludes(array $excludes): self
352365 }
353366
354367 /**
355- * @param bool $value
368+ * @param bool|string|int $value
356369 *
357370 * @return PharCompiler
358371 */
@@ -363,7 +376,7 @@ public function stripComments($value): self
363376 }
364377
365378 /**
366- * @param bool $value
379+ * @param bool|string|int $value
367380 *
368381 * @return PharCompiler
369382 */
@@ -578,9 +591,9 @@ protected function collectFileInfo(SplFileInfo $file): void
578591 *
579592 * @throws RuntimeException
580593 */
581- public function findChangedByGit ()
594+ public function findChangedByGit (): ? Generator
582595 {
583- // -u expand dir's files
596+ // -u expand dir files
584597 [, $ output ,] = Sys::run ('git status -s -u ' , $ this ->basePath );
585598
586599 // 'D some.file' deleted
@@ -608,10 +621,10 @@ public function findChangedByGit()
608621 /**
609622 * @param string $directory
610623 *
611- * @return Iterator|SplFileInfo[]
624+ * @return Iterator
612625 * @throws InvalidArgumentException
613626 */
614- protected function findFiles (string $ directory )
627+ protected function findFiles (string $ directory ): Iterator
615628 {
616629 return Helper::directoryIterator (
617630 $ directory ,
@@ -711,19 +724,21 @@ private function createStub(): string
711724 $ stub = "$ shebang \n$ stub " ;
712725 }
713726
714- if ($ this ->cliIndex && $ this ->webIndex ) {
727+ $ cliIndex = $ this ->cliIndex ;
728+ $ webIndex = $ this ->webIndex ;
729+ if ($ cliIndex && $ webIndex ) {
715730 $ stub .= <<<EOF
716731// for command line
717732if (PHP_SAPI === 'cli') {
718- require 'phar:// $ pharName/ { $ this -> cliIndex } ';
733+ require 'phar:// $ pharName/ $ cliIndex';
719734} else {
720- require 'phar:// $ pharName/ { $ this -> webIndex } ';
735+ require 'phar:// $ pharName/ $ webIndex';
721736}
722737EOF ;
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" ;
727742 } else {
728743 throw new RuntimeException ("'cliIndex' and 'webIndex', please set at least one " );
729744 }
@@ -989,11 +1004,13 @@ public function setVersionFile(string $versionFile): PharCompiler
9891004 }
9901005
9911006 /**
1007+ * @param bool $abbrev
1008+ *
9921009 * @return string
9931010 */
994- public function getLastCommit (): string
1011+ public function getLastCommit (bool $ abbrev = true ): string
9951012 {
996- return $ this ->lastCommit ;
1013+ return $ abbrev ? substr ( $ this -> lastCommit , 0 , 7 ) : $ this ->lastCommit ;
9971014 }
9981015
9991016 /**
0 commit comments