Skip to content

Commit

Permalink
Add a way to disable stubs generation
Browse files Browse the repository at this point in the history
And refactor the way we deal with env var. They are now all gatherd in
the Factory, and put in the DIC as parameter
  • Loading branch information
lyrixx committed Nov 25, 2024
1 parent 342f4d8 commit 1e568ba
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 14 deletions.
2 changes: 1 addition & 1 deletion .github/actions/cache/action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
4 changes: 3 additions & 1 deletion src/Console/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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');

Expand Down
13 changes: 11 additions & 2 deletions src/Console/ApplicationFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
10 changes: 7 additions & 3 deletions src/Console/Output/SectionOutput.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand All @@ -17,13 +18,16 @@ class SectionOutput
/** @var \SplObjectStorage<Process, SectionDetails> */
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();
}
Expand Down
11 changes: 7 additions & 4 deletions src/ContextRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,24 @@
use Castor\Event\ContextCreatedEvent;
use Castor\Exception\FunctionConfigurationException;
use Castor\Helper\PathHelper;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;

/** @internal */
class ContextRegistry
{
/** @var array<string, ContextDescriptor> */
private array $descriptors = [];
private ?string $defaultName = null;
/** @var array<string, Context> */
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
Expand All @@ -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);
Expand Down
5 changes: 4 additions & 1 deletion src/Import/Remote/Composer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand All @@ -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;
}

Expand Down
6 changes: 6 additions & 0 deletions src/Listener/GenerateStubsListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ public function __construct(
private readonly StubsGenerator $stubsGenerator,
#[Autowire('%repacked%')]
private readonly bool $repacked,
#[Autowire('%generate_stubs%')]
private readonly bool $generateStubs,
) {
}

Expand All @@ -26,6 +28,10 @@ public function generateStubs(ConsoleCommandEvent $event): void
return;
}

if (!$this->generateStubs) {
return;
}

$command = $event->getCommand();
if (!$command) {
return;
Expand Down
2 changes: 1 addition & 1 deletion tests/Slow/CompileCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
],
Expand Down

0 comments on commit 1e568ba

Please sign in to comment.