Skip to content

Commit d33a91d

Browse files
author
ityaozm@gmail.com
committed
refactor(app): Improve OutputStyle instantiation in AppServiceProvider
- Replace the anonymous function with a named function for clarity - Ensure `$_SERVER['argv']` is set to prevent missing indexes - Automatically configure IO options for ConsoleOutput - Enhance readability and maintainability of the code by introducing comments
1 parent 4665aea commit d33a91d

2 files changed

Lines changed: 25 additions & 2 deletions

File tree

app/Providers/AppServiceProvider.php

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,42 @@
1515

1616
use Illuminate\Console\OutputStyle;
1717
use Illuminate\Support\ServiceProvider;
18+
use Symfony\Component\Console\Application;
1819
use Symfony\Component\Console\Input\ArgvInput;
20+
use Symfony\Component\Console\Input\InputInterface;
1921
use Symfony\Component\Console\Output\ConsoleOutput;
22+
use Symfony\Component\Console\Output\OutputInterface;
2023

24+
/**
25+
* @method void configureIO(InputInterface $input, OutputInterface $output)
26+
*/
2127
final class AppServiceProvider extends ServiceProvider
2228
{
2329
/**
2430
* @noinspection PhpMissingParentCallCommonInspection
31+
* @noinspection GlobalVariableUsageInspection
2532
*/
2633
public function register(): void
2734
{
35+
/**
36+
* @see \Rector\Console\Style\SymfonyStyleFactory
37+
*/
2838
$this->app->singletonIf(
2939
OutputStyle::class,
30-
static fn (): OutputStyle => new OutputStyle(new ArgvInput, new ConsoleOutput)
40+
static function (): OutputStyle {
41+
// to prevent missing argv indexes
42+
if (!isset($_SERVER['argv'])) {
43+
$_SERVER['argv'] = [];
44+
}
45+
46+
$argvInput = new ArgvInput;
47+
$consoleOutput = new ConsoleOutput;
48+
49+
// to configure all -v, -vv, -vvv options without memory-lock to Application run() arguments
50+
(fn () => $this->configureIO($argvInput, $consoleOutput))->call(new Application);
51+
52+
return new OutputStyle($argvInput, $consoleOutput);
53+
}
3154
);
3255
}
3356

bootstrap/app.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,6 @@
6363
})
6464
->create()
6565
->tap(static function (Application $app): void {
66-
$app->call(DefineTraceIdListener::class);
6766
// $app->afterLoadingEnvironment((new DefineTraceIdListener)(...));
67+
$app->call(DefineTraceIdListener::class);
6868
});

0 commit comments

Comments
 (0)