Skip to content

Commit 5181a37

Browse files
committed
update some for phar building
1 parent 28f6cd0 commit 5181a37

File tree

4 files changed

+64
-47
lines changed

4 files changed

+64
-47
lines changed

phar.build.inc

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,17 @@ $compiler
1414
->setShebang(true)
1515
->addExclude([
1616
'demo',
17+
'example',
18+
'runtime',
19+
'node_modules',
1720
'test',
1821
'tmp',
1922
])
2023
->addFile([
2124
'LICENSE',
2225
'composer.json',
2326
'README.md',
24-
'test/boot.php',
27+
'test/bootstrap.php',
2528
])
2629
->setCliIndex('examples/app')
2730
// ->setWebIndex('web/index.php')
@@ -30,7 +33,7 @@ $compiler
3033

3134
// Console 下的 Command Controller 命令类不去除注释,注释上是命令帮助信息
3235
$compiler->setStripFilter(static function ($file) {
33-
/** @var \SplFileInfo $file */
36+
/** @var SplFileInfo $file */
3437
$name = $file->getFilename();
3538

3639
return false === strpos($name, 'Command.php') && false === strpos($name, 'Controller.php');

src/Application.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
use Inhere\Console\IO\Output;
1515
use Inhere\Console\Util\Helper;
1616
use InvalidArgumentException;
17-
use ReflectionException;
1817
use RuntimeException;
1918
use SplFileInfo;
2019
use function class_exists;
@@ -285,7 +284,7 @@ public function dispatch(string $name, bool $detachedRun = false)
285284
}
286285

287286
$commands = $this->router->getAllNames();
288-
$this->output->error("The command '{$name}' is not exists!");
287+
$this->output->error("The command '$name' is not exists!");
289288

290289
// find similar command names by similar_text()
291290
if ($similar = Helper::findSimilar($name, $commands)) {
@@ -360,7 +359,6 @@ protected function runCommand(string $name, $handler, array $options)
360359
* @param bool $detachedRun
361360
*
362361
* @return mixed
363-
* @throws ReflectionException
364362
*/
365363
protected function runAction(array $info, array $options, bool $detachedRun = false)
366364
{

src/BuiltIn/PharController.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,10 @@ protected function packConfigure(Input $input): void
7272
* default is current work-dir(default: <cyan>{workDir}</cyan>)
7373
* -c, --config STRING Use the custom config file for build phar(default: <cyan>./phar.build.inc</cyan>)
7474
* -o, --output STRING Setting the output file name(<cyan>{defaultPkgName}</cyan>)
75-
* --fast Fast build. only add modified files by <cyan>git status -s</cyan>
76-
* --refresh Whether build vendor folder files on phar file exists(<cyan>False</cyan>)
77-
* --files STRING Only pack the list files to the exist phar, multi use ',' split
78-
* --no-progress Disable output progress on the runtime
75+
* --fast Fast build. only add modified files by <cyan>git status -s</cyan>
76+
* --refresh Whether build vendor folder files on phar file exists(<cyan>False</cyan>)
77+
* --files STRING Only pack the list files to the exist phar, multi use ',' split
78+
* --no-progress Disable output progress on the runtime
7979
*
8080
* @param Input $input
8181
* @param Output $output
@@ -92,7 +92,7 @@ protected function packConfigure(Input $input): void
9292
* only update the input files:
9393
* php -d phar.readonly=0 {binFile} phar:pack -o=mycli.phar --debug --files app/Command/ServeCommand.php
9494
*/
95-
public function packCommand($input, $output): int
95+
public function packCommand(Input $input, Output $output): int
9696
{
9797
$startAt = microtime(true);
9898
$workDir = $input->getPwd();
@@ -169,7 +169,6 @@ protected function configCompiler(string $dir): PharCompiler
169169
$configFile = $this->input->getSameOpt(['c', 'config']) ?: $dir . '/phar.build.inc';
170170

171171
if ($configFile && is_file($configFile)) {
172-
/** @noinspection PhpIncludeInspection */
173172
require $configFile;
174173
return $compiler->in($dir);
175174
}

src/Component/PharCompiler.php

Lines changed: 53 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use DateTimeZone;
99
use Exception;
1010
use FilesystemIterator;
11+
use Generator;
1112
use Inhere\Console\Util\Helper;
1213
use InvalidArgumentException;
1314
use 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
717732
if (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
}
722737
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";
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

Comments
 (0)