Skip to content

Commit e71e044

Browse files
committed
refactor(ComposerScripts): optimize makeSymfonyStyle method
- Cache the SymfonyStyle instance to avoid unnecessary object creation. - Ensure proper handling of argv parameters to prevent missing indexes. - Configure verbosity based on debug options for better output control. Signed-off-by: guanguans <[email protected]>
1 parent 6972229 commit e71e044

File tree

1 file changed

+37
-6
lines changed

1 file changed

+37
-6
lines changed

src/Support/ComposerScripts.php

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,18 @@
2020
use Illuminate\Support\Stringable;
2121
use Rector\Config\RectorConfig;
2222
use Rector\DependencyInjection\LazyContainerFactory;
23+
use Symfony\Component\Console\Application;
2324
use Symfony\Component\Console\Input\ArgvInput;
25+
use Symfony\Component\Console\Input\InputInterface;
2426
use Symfony\Component\Console\Output\ConsoleOutput;
27+
use Symfony\Component\Console\Output\OutputInterface;
2528
use Symfony\Component\Console\Style\SymfonyStyle;
2629
use Symfony\Component\Yaml\Yaml;
2730

2831
/**
2932
* @internal
33+
*
34+
* @method void configureIO(InputInterface $input, OutputInterface $output)
3035
*/
3136
final class ComposerScripts
3237
{
@@ -38,19 +43,18 @@ final class ComposerScripts
3843
public static function checkSoarBinary(): int
3944
{
4045
// self::requireAutoload($event);
41-
$symfonyStyle = self::makeSymfonyStyle();
4246

4347
foreach ((array) glob(__DIR__.'/../../bin/soar.*-*') as $file) {
4448
if (!is_executable($file)) {
45-
$symfonyStyle->error("The file [$file] is not executable.");
49+
self::makeSymfonyStyle()->error("The file [$file] is not executable.");
4650

4751
exit(1);
4852
}
4953

50-
$symfonyStyle->comment("<info>OK</info> $file");
54+
self::makeSymfonyStyle()->comment("<info>OK</info> $file");
5155
}
5256

53-
$symfonyStyle->success('No errors');
57+
self::makeSymfonyStyle()->success('No errors');
5458

5559
return 0;
5660
}
@@ -236,12 +240,39 @@ static function (Collection $options, Collection $option): Collection {
236240
*/
237241
public static function makeRectorConfig(): RectorConfig
238242
{
239-
return (new LazyContainerFactory)->create();
243+
static $rectorConfig;
244+
245+
return $rectorConfig ??= (new LazyContainerFactory)->create();
240246
}
241247

248+
/**
249+
* @see \Rector\Console\Style\SymfonyStyleFactory
250+
*/
242251
private static function makeSymfonyStyle(): SymfonyStyle
243252
{
244-
return new SymfonyStyle(new ArgvInput, new ConsoleOutput);
253+
static $symfonyStyle;
254+
255+
if ($symfonyStyle instanceof SymfonyStyle) {
256+
return $symfonyStyle;
257+
}
258+
259+
// to prevent missing argv indexes
260+
if (!isset($_SERVER['argv'])) {
261+
$_SERVER['argv'] = [];
262+
}
263+
264+
$argvInput = new ArgvInput;
265+
$consoleOutput = new ConsoleOutput;
266+
267+
// to configure all -v, -vv, -vvv options without memory-lock to Application run() arguments
268+
(fn () => $this->configureIO($argvInput, $consoleOutput))->call(new Application);
269+
270+
// --debug is called
271+
if ($argvInput->hasParameterOption(['--debug', '--xdebug'])) {
272+
$consoleOutput->setVerbosity(OutputInterface::VERBOSITY_DEBUG);
273+
}
274+
275+
return $symfonyStyle = new SymfonyStyle($argvInput, $consoleOutput);
245276
}
246277

247278
private static function requireAutoload(Event $event): void

0 commit comments

Comments
 (0)