Skip to content

Commit

Permalink
ConsoleApplicationResolver made lazy
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Oct 5, 2022
1 parent 016e441 commit e5459eb
Showing 1 changed file with 20 additions and 12 deletions.
32 changes: 20 additions & 12 deletions src/Symfony/ConsoleApplicationResolver.php
Expand Up @@ -16,39 +16,47 @@
final class ConsoleApplicationResolver
{

/** @var string|null */
private $consoleApplicationLoader;

/** @var Application|null */
private $consoleApplication;

public function __construct(Configuration $configuration)
{
$consoleApplicationLoader = $configuration->getConsoleApplicationLoader();
if ($consoleApplicationLoader === null) {
return;
}
$this->consoleApplication = $this->loadConsoleApplication($consoleApplicationLoader);
$this->consoleApplicationLoader = $configuration->getConsoleApplicationLoader();
}

/**
* @return Application|null
* @phpcsSuppress SlevomatCodingStandard.TypeHints.ReturnTypeHint.MissingNativeTypeHint
*/
private function loadConsoleApplication(string $consoleApplicationLoader)
private function getConsoleApplication()
{
if (!file_exists($consoleApplicationLoader)
|| !is_readable($consoleApplicationLoader)
if ($this->consoleApplicationLoader === null) {
return null;
}

if ($this->consoleApplication !== null) {
return $this->consoleApplication;
}

if (!file_exists($this->consoleApplicationLoader)
|| !is_readable($this->consoleApplicationLoader)
) {
throw new ShouldNotHappenException(sprintf('Cannot load console application. Check the parameters.symfony.consoleApplicationLoader setting in PHPStan\'s config. The offending value is "%s".', $consoleApplicationLoader));
throw new ShouldNotHappenException(sprintf('Cannot load console application. Check the parameters.symfony.consoleApplicationLoader setting in PHPStan\'s config. The offending value is "%s".', $this->consoleApplicationLoader));
}

return require $consoleApplicationLoader;
return $this->consoleApplication = require $this->consoleApplicationLoader;
}

/**
* @return Command[]
*/
public function findCommands(ClassReflection $classReflection): array
{
if ($this->consoleApplication === null) {
$consoleApplication = $this->getConsoleApplication();
if ($consoleApplication === null) {
return [];
}

Expand All @@ -58,7 +66,7 @@ public function findCommands(ClassReflection $classReflection): array
}

$commands = [];
foreach ($this->consoleApplication->all() as $name => $command) {
foreach ($consoleApplication->all() as $name => $command) {
$commandClass = new ObjectType(get_class($command));
$isLazyCommand = (new ObjectType('Symfony\Component\Console\Command\LazyCommand'))->isSuperTypeOf($commandClass)->yes();

Expand Down

0 comments on commit e5459eb

Please sign in to comment.