Skip to content

Commit 17928ce

Browse files
committed
up: update some logic for phar build and verb env handle
1 parent 27c1048 commit 17928ce

File tree

7 files changed

+111
-37
lines changed

7 files changed

+111
-37
lines changed

phar.build.inc

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,20 @@ $compiler
1313
// ->stripComments(false)
1414
->setShebang(true)
1515
->addExclude([
16-
'demo',
17-
'example',
18-
'runtime',
19-
'node_modules',
20-
'test',
21-
'tmp',
16+
'demo/',
17+
'docs/',
18+
'example/',
19+
'runtime/',
20+
'node_modules/',
21+
'test/',
22+
'tmp/',
23+
'/console/resource/',
2224
])
2325
->addFile([
2426
'LICENSE',
2527
'composer.json',
2628
'README.md',
2729
'test/bootstrap.php',
28-
'examples/commands.php'
2930
])
3031
->setCliIndex('examples/app')
3132
// ->setWebIndex('web/index.php')

src/AbstractApplication.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -829,10 +829,8 @@ public function getVerbLevel(): int
829829
$optKey = GlobalOption::DEBUG;
830830

831831
// feat: support set debug level by ENV var: CONSOLE_DEBUG
832-
$envVal = OS::getEnvStrVal(Console::DEBUG_ENV_KEY);
833-
if ($envVal !== '') {
834-
$setVal = (int)$envVal;
835-
} else {
832+
$setVal = Console::getLevelFromENV(-1);
833+
if ($setVal < 0) {
836834
$setVal = (int)$this->config[$optKey];
837835
}
838836

src/BuiltIn/PharController.php

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,9 @@ protected function packConfigure(Input $input): void
7676
* -o, --output Setting the output file name(<cyan>{defaultPkgName}</cyan>)
7777
* --fast bool;Fast build. only add modified files by <cyan>git status -s</cyan>
7878
* --refresh bool;Whether build vendor folder files on phar file exists(<cyan>False</cyan>)
79-
* --files Only pack the list files to the exist phar, multi use ',' split
80-
* --no-progress bool;Disable output progress on the runtime
79+
* --files Only pack the list files to exist phar, multi use ',' split
80+
* --no-progress bool;Disable output progress on the runtime.
81+
* --debug bool;Show debug information for collect files
8182
*
8283
* @param Input $input
8384
* @param Output $output
@@ -137,9 +138,8 @@ public function packCommand(Input $input, Output $output, FlagsParser $fs): int
137138

138139
$output->colored('Collect Pack files', 'comment');
139140

140-
if (!$fs->getOpt('no-progress')) {
141-
$this->outputProgress($cpr);
142-
}
141+
$showProgress = !$fs->getOpt('no-progress');
142+
$this->outputProgress($cpr, $fs->getOpt('debug'), $showProgress);
143143

144144
// packing ...
145145
$cpr->pack($pharFile, $refresh);
@@ -186,22 +186,24 @@ protected function configCompiler(string $dir, string $confFile): PharCompiler
186186

187187
/**
188188
* @param PharCompiler $cpr
189+
* @param bool $debug
190+
* @param bool $showProgress
189191
*
190192
* @return void
191193
*/
192-
private function outputProgress(PharCompiler $cpr): void
194+
private function outputProgress(PharCompiler $cpr, bool $debug, bool $showProgress): void
193195
{
194-
if ($this->isDebug()) {
196+
if ($debug || $this->isDebug()) {
195197
// $output->info('Pack file to Phar ... ...');
196198
$cpr->onAdd(function (string $path): void {
197199
$this->writeln(" <info>+</info> $path");
198200
});
199201

200-
$cpr->on('skip', function (string $path, bool $isFile): void {
202+
$cpr->on(PharCompiler::ON_SKIP, function (string $path, bool $isFile): void {
201203
$mark = $isFile ? '[F]' : '[D]';
202204
$this->writeln(" <red>-</red> $path <info>$mark</info>");
203205
});
204-
} else {
206+
} else if ($showProgress) {
205207
$counter = Show::counterTxt('Collecting ...', 'Done.');
206208
$cpr->onAdd(static function () use ($counter): void {
207209
$counter->send(1);

src/Component/PharCompiler.php

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ class PharCompiler
6363
{
6464
public const ON_ADD = 'add';
6565

66+
/**
67+
* skip dir or file.
68+
* - fn: `function(string $path, bool $isFile) {}`
69+
*/
6670
public const ON_SKIP = 'skip';
6771

6872
public const ON_ERROR = 'error';
@@ -283,7 +287,7 @@ public function __construct(string $basePath)
283287
{
284288
self::checkEnv();
285289

286-
$this->basePath = File::realpath($basePath);
290+
$this->basePath = File::pathFormat(File::realpath($basePath), false);
287291
$this->fileQueue = new SplQueue();
288292

289293
if (!is_dir($this->basePath)) {
@@ -768,7 +772,13 @@ private function getIteratorFilter(): Closure
768772
if (!$this->fileFilter) {
769773
$this->fileFilter = function (SplFileInfo $file) {
770774
$name = $file->getFilename();
771-
$path = $file->getPathname();
775+
$path = File::pathFormat($file->getPathname(), false);
776+
// remove basePath prefix
777+
if (str_starts_with($path, $this->basePath)) {
778+
$noFullPath = substr($path, strlen($this->basePath)+1);
779+
} else {
780+
$noFullPath = $path;
781+
}
772782

773783
// Skip hidden files and directories.
774784
if (str_starts_with($name, '.')) {
@@ -779,13 +789,23 @@ private function getIteratorFilter(): Closure
779789
if ($file->isDir()) {
780790
foreach ($this->excludes as $exclude) {
781791
if (strpos($path . '/', $exclude) > 0) {
782-
$this->fire(self::ON_SKIP, $path, false);
792+
$this->fire(self::ON_SKIP, $noFullPath . '/', false);
783793
return false;
784794
}
785795
}
786796
return true;
787797
}
788798

799+
// Exclude file check
800+
if ($this->excludes) {
801+
foreach ($this->excludes as $exclude) {
802+
if (strpos($path, $exclude) > 0) {
803+
$this->fire(self::ON_SKIP, $noFullPath, true);
804+
return false;
805+
}
806+
}
807+
}
808+
789809
// File ext check
790810
if ($this->suffixes) {
791811
foreach ($this->suffixes as $suffix) {
@@ -794,7 +814,7 @@ private function getIteratorFilter(): Closure
794814
}
795815
}
796816

797-
$this->fire(self::ON_SKIP, $path, true);
817+
$this->fire(self::ON_SKIP, $noFullPath, true);
798818
return false;
799819
}
800820

@@ -856,8 +876,8 @@ private function collectInformation(): void
856876
*/
857877
private function getRelativeFilePath(SplFileInfo $file): string
858878
{
859-
$realPath = $file->getRealPath();
860-
$pathPrefix = $this->basePath . DIRECTORY_SEPARATOR;
879+
$realPath = File::pathFormat($file->getRealPath(), false);
880+
$pathPrefix = $this->basePath . '/';
861881

862882
$pos = strpos($realPath, $pathPrefix);
863883
$path = $pos !== false ? substr_replace($realPath, '', $pos, strlen($pathPrefix)) : $realPath;

src/Component/Router.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ public function addCommand(string $name, string|Closure|CommandInterface|null $h
189189

190190
if (!$name || !$handler) {
191191
$handlerClass = is_object($handler) ? get_class($handler) : $handler;
192-
throw new InvalidArgumentException("Command 'name' and 'handler' cannot be empty! name: $name, handler: $handlerClass");
192+
throw new InvalidArgumentException("Command 'name' and 'handler' cannot be empty! (name: $name, handler: $handlerClass)");
193193
}
194194

195195
Assert::isFalse(isset($this->commands[$name]), "Command '$name' have been registered!");

src/Console.php

Lines changed: 61 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
use Toolkit\Cli\Cli;
1515
use Toolkit\Cli\Color\ColorTag;
1616
use Toolkit\Stdlib\Json;
17+
use Toolkit\Stdlib\OS;
18+
use Toolkit\Stdlib\Str;
1719
use function date;
1820
use function debug_backtrace;
1921
use function implode;
@@ -55,6 +57,19 @@ class Console extends Cli
5557
self::VERB_CRAZY => 'CRAZY',
5658
];
5759

60+
// name => level
61+
public const NAME2LEVEL = [
62+
'QUIET' => self::VERB_QUIET,
63+
'ERR' => self::VERB_ERROR, // alias
64+
'ERROR' => self::VERB_ERROR,
65+
'WARN' => self::VERB_WARN,
66+
'WARNING' => self::VERB_WARN, // alias
67+
'INFO' => self::VERB_INFO,
68+
'DEBUG' => self::VERB_DEBUG,
69+
'CRAZY' => self::VERB_CRAZY,
70+
];
71+
72+
// level => color name
5873
public const LEVEL2TAG = [
5974
self::VERB_QUIET => 'normal',
6075
self::VERB_ERROR => 'error',
@@ -64,7 +79,7 @@ class Console extends Cli
6479
self::VERB_CRAZY => 'magenta',
6580
];
6681

67-
public const CMD_GROUP = 1;
82+
public const CMD_GROUP = 1;
6883

6984
public const CMD_SINGLE = 2;
7085

@@ -76,6 +91,49 @@ class Console extends Cli
7691
*/
7792
private static ?Application $app;
7893

94+
/**
95+
* get debug level from ENV var: `CONSOLE_DEBUG`. if not set, return `$defaultLevel`
96+
*
97+
* @param int $defaultLevel
98+
*
99+
* @return integer
100+
*/
101+
public static function getLevelFromENV(int $defaultLevel = self::VERB_ERROR): int
102+
{
103+
// feat: support set debug level by ENV var: CONSOLE_DEBUG
104+
$level = $defaultLevel;
105+
$envVal = OS::getEnvStrVal(Console::DEBUG_ENV_KEY);
106+
107+
if ($envVal !== '') {
108+
if (is_numeric($envVal)) {
109+
$level = (int)$envVal;
110+
} else {
111+
$level = self::nameToLevel($envVal, $defaultLevel);
112+
}
113+
}
114+
115+
return $level;
116+
}
117+
118+
/**
119+
* level name to level number
120+
*
121+
* @param string $levelName
122+
* @param int $defaultLevel
123+
*
124+
* @return int
125+
*/
126+
public static function nameToLevel(string $levelName, int $defaultLevel = self::VERB_INFO): int
127+
{
128+
$levelName = Str::upper($levelName);
129+
130+
if (isset(self::NAME2LEVEL[$levelName])) {
131+
return self::NAME2LEVEL[$levelName];
132+
}
133+
134+
return $defaultLevel;
135+
}
136+
79137
/**
80138
* @return Application
81139
*/
@@ -184,7 +242,8 @@ public static function log(int $level, string $msg, array $data = [], array $opt
184242
$backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, self::$traceIndex + 2);
185243
$position = self::formatBacktrace($backtrace, self::$traceIndex);
186244

187-
self::writef('%s [%s] [%s]%s %s %s' . PHP_EOL, $datetime, $taggedName, $position, $optString, trim($msg), $dataString);
245+
self::writef('%s [%s] [%s]%s %s %s' . PHP_EOL, $datetime, $taggedName, $position, $optString, trim($msg),
246+
$dataString);
188247
}
189248

190249
/**

src/Decorate/AttachApplicationTrait.php

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,7 @@ public function getVerbLevel(): int
106106
}
107107

108108
// return (int)$this->input->getLongOpt('debug', Console::VERB_ERROR);
109-
$envVal = OS::getEnvStrVal(Console::DEBUG_ENV_KEY);
110-
return $envVal !== '' ? (int)$envVal : Console::VERB_ERROR;
109+
return Console::getLevelFromENV();
111110
}
112111

113112
/**
@@ -121,12 +120,7 @@ public function isDebug(int $level = Console::VERB_DEBUG): bool
121120
return $this->app->isDebug();
122121
}
123122

124-
$setVal = Console::VERB_ERROR;
125-
$envVal = OS::getEnvStrVal(Console::DEBUG_ENV_KEY);
126-
if ($envVal !== '') {
127-
$setVal = (int)$envVal;
128-
}
129-
123+
$setVal = Console::getLevelFromENV();
130124
return $level <= $setVal;
131125
}
132126

0 commit comments

Comments
 (0)