From 1e568bafd4a3cdb84fe8c4b25d3aeb1500e8b50b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Pineau?= Date: Mon, 25 Nov 2024 09:54:12 +0100 Subject: [PATCH] Add a way to disable stubs generation And refactor the way we deal with env var. They are now all gatherd in the Factory, and put in the DIC as parameter --- .github/actions/cache/action.yaml | 2 +- CHANGELOG.md | 4 +++- src/Console/Application.php | 4 +++- src/Console/ApplicationFactory.php | 13 +++++++++++-- src/Console/Output/SectionOutput.php | 10 +++++++--- src/ContextRegistry.php | 11 +++++++---- src/Import/Remote/Composer.php | 5 ++++- src/Listener/GenerateStubsListener.php | 6 ++++++ tests/Slow/CompileCommandTest.php | 2 +- 9 files changed, 43 insertions(+), 14 deletions(-) diff --git a/.github/actions/cache/action.yaml b/.github/actions/cache/action.yaml index 3648d0fc..397fd554 100644 --- a/.github/actions/cache/action.yaml +++ b/.github/actions/cache/action.yaml @@ -28,7 +28,7 @@ runs: echo cache_key_darwin_amd64=$cache_key_darwin_amd64 >> $GITHUB_ENV # And should be the same command as the one in CompileCommandTest - cache_dirname_test=$(tests/bin/compile-get-cache-key phar-location-is-not-used-in-cache-key --os linux --php-extensions mbstring,phar,posix,tokenizer) + cache_dirname_test=$(tests/bin/compile-get-cache-key phar-location-is-not-used-in-cache-key --os linux --php-extensions filter,mbstring,phar,posix,tokenizer) cache_key_test=$(basename $cache_dirname_test) echo cache_dirname_test=$cache_dirname_test >> $GITHUB_ENV echo cache_key_test=$cache_key_test >> $GITHUB_ENV diff --git a/CHANGELOG.md b/CHANGELOG.md index 65d810e1..ba57402f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,9 @@ ### Fixes -Add more missing vendor classes into stubs +* Add more missing vendor classes into stubs +* Add support for disabling stubs generation (with `CASTOR_GENERATE_STUBS=0` + environment variable) ## 0.21.0 (2024-11-19) diff --git a/src/Console/Application.php b/src/Console/Application.php index 836510d1..33013fa6 100644 --- a/src/Console/Application.php +++ b/src/Console/Application.php @@ -33,6 +33,8 @@ public function __construct( private readonly SymfonyStyle $io, #[Autowire(lazy: true)] private readonly ProcessRunner $processRunner, + #[Autowire('%test%')] + public readonly bool $test, ) { parent::__construct(static::NAME, static::VERSION); } @@ -156,7 +158,7 @@ private function enhanceException(\Throwable $exception): void private function getLogo(): string { - if (!($_SERVER['CASTOR_TEST'] ?? false)) { + if (!$this->test) { $now = new \DateTime(); $year = date('Y'); diff --git a/src/Console/ApplicationFactory.php b/src/Console/ApplicationFactory.php index d2c478af..74dd7789 100644 --- a/src/Console/ApplicationFactory.php +++ b/src/Console/ApplicationFactory.php @@ -66,11 +66,20 @@ public static function create(): SymfonyApplication $container = self::buildContainer($repacked); $container->getParameterBag()->add([ 'root_dir' => $rootDir, - 'cache_dir' => $_SERVER['CASTOR_CACHE_DIR'] ?? PlatformHelper::getDefaultCacheDirectory(), + '.default_cache_dir' => PlatformHelper::getDefaultCacheDirectory(), 'event_dispatcher.event_aliases' => ConsoleEvents::ALIASES, 'repacked' => $repacked, + // UPGRADE: once on Symfony 7.3, remove this and use the new autowiring feature + // see https://github.com/symfony/symfony/pull/58986 + 'true' => true, + 'cache_dir' => '%env(default:.default_cache_dir:CASTOR_CACHE_DIR)%', + 'composer_no_remote' => '%env(bool:default::CASTOR_NO_REMOTE)%', + 'context' => '%env(default::CASTOR_CONTEXT)%', + 'generate_stubs' => '%env(bool:default:true:CASTOR_GENERATE_STUBS)%', + 'test' => '%env(bool:default::CASTOR_TEST)%', + 'use_output_section' => '%env(bool:default::CASTOR_USE_SECTION)%', ]); - $container->compile(); + $container->compile(true); $container->set(ContainerInterface::class, $container); $container->set(ErrorHandler::class, $errorHandler); diff --git a/src/Console/Output/SectionOutput.php b/src/Console/Output/SectionOutput.php index 41b0ba15..f4415eaa 100644 --- a/src/Console/Output/SectionOutput.php +++ b/src/Console/Output/SectionOutput.php @@ -5,6 +5,7 @@ use Symfony\Component\Console\Output\ConsoleOutput; use Symfony\Component\Console\Output\ConsoleSectionOutput; use Symfony\Component\Console\Output\OutputInterface; +use Symfony\Component\DependencyInjection\Attribute\Autowire; use Symfony\Component\Process\Process; /** @internal */ @@ -17,13 +18,16 @@ class SectionOutput /** @var \SplObjectStorage */ private \SplObjectStorage $sections; - public function __construct(OutputInterface $output) - { + public function __construct( + OutputInterface $output, + #[Autowire('%use_output_section%')] + bool $useOutputSection, + ) { $this->consoleOutput = $output; $this->mainOutput = null; $this->sections = new \SplObjectStorage(); - if ($output instanceof ConsoleOutput && 'true' === getenv('CASTOR_USE_SECTION') && stream_isatty(\STDOUT)) { + if ($output instanceof ConsoleOutput && $useOutputSection && stream_isatty(\STDOUT)) { $this->mainOutput = $output; $this->consoleOutput = $output->section(); } diff --git a/src/ContextRegistry.php b/src/ContextRegistry.php index 012b4043..967fc002 100644 --- a/src/ContextRegistry.php +++ b/src/ContextRegistry.php @@ -6,6 +6,7 @@ use Castor\Event\ContextCreatedEvent; use Castor\Exception\FunctionConfigurationException; use Castor\Helper\PathHelper; +use Symfony\Component\DependencyInjection\Attribute\Autowire; use Symfony\Component\EventDispatcher\EventDispatcherInterface; /** @internal */ @@ -13,14 +14,16 @@ class ContextRegistry { /** @var array */ private array $descriptors = []; - private ?string $defaultName = null; /** @var array */ private array $contexts = []; private Context $currentContext; - public function __construct(private EventDispatcherInterface $eventDispatcher) - { + public function __construct( + private EventDispatcherInterface $eventDispatcher, + #[Autowire('%context%')] + private ?string $defaultName = null, + ) { } public function addDescriptor(ContextDescriptor $descriptor): void @@ -35,7 +38,7 @@ public function addDescriptor(ContextDescriptor $descriptor): void $this->descriptors[$name] = $descriptor; if ($descriptor->contextAttribute->default) { - if ($this->defaultName) { + if ($this->defaultName && ($this->descriptors[$this->defaultName] ?? false)) { $alreadyDefined = $this->descriptors[$this->defaultName]->function; throw new FunctionConfigurationException(\sprintf('You cannot set multiple "default: true" context. There is one already defined in "%s:%d".', PathHelper::makeRelative((string) $alreadyDefined->getFileName()), $alreadyDefined->getStartLine()), $descriptor->function); diff --git a/src/Import/Remote/Composer.php b/src/Import/Remote/Composer.php index 2295b9ad..d2df2c0b 100644 --- a/src/Import/Remote/Composer.php +++ b/src/Import/Remote/Composer.php @@ -16,6 +16,7 @@ use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\Output; use Symfony\Component\Console\Output\OutputInterface; +use Symfony\Component\DependencyInjection\Attribute\Autowire; use Symfony\Component\Filesystem\Filesystem; /** @internal */ @@ -28,13 +29,15 @@ public function __construct( private readonly InputInterface $input, private readonly OutputInterface $output, private readonly Filesystem $filesystem, + #[Autowire('%composer_no_remote%')] + private readonly bool $disableRemote, private readonly LoggerInterface $logger = new NullLogger(), ) { } public function isRemoteAllowed(): bool { - if ($_SERVER['CASTOR_NO_REMOTE'] ?? false) { + if ($this->disableRemote) { return false; } diff --git a/src/Listener/GenerateStubsListener.php b/src/Listener/GenerateStubsListener.php index 435ea557..696c6104 100644 --- a/src/Listener/GenerateStubsListener.php +++ b/src/Listener/GenerateStubsListener.php @@ -14,6 +14,8 @@ public function __construct( private readonly StubsGenerator $stubsGenerator, #[Autowire('%repacked%')] private readonly bool $repacked, + #[Autowire('%generate_stubs%')] + private readonly bool $generateStubs, ) { } @@ -26,6 +28,10 @@ public function generateStubs(ConsoleCommandEvent $event): void return; } + if (!$this->generateStubs) { + return; + } + $command = $event->getCommand(); if (!$command) { return; diff --git a/tests/Slow/CompileCommandTest.php b/tests/Slow/CompileCommandTest.php index ea2d8dad..a4278354 100644 --- a/tests/Slow/CompileCommandTest.php +++ b/tests/Slow/CompileCommandTest.php @@ -37,7 +37,7 @@ public function test() 'compile', $castorAppDirPath . '/my-app.linux.phar', '--os', 'linux', '--binary-path', $binary, - '--php-extensions', 'mbstring,phar,posix,tokenizer', + '--php-extensions', 'filter,mbstring,phar,posix,tokenizer', '--php-ini-file', 'php.ini', '-vvv', ],