Skip to content

Commit

Permalink
[DX] Make autowired commands with tags (#4385)
Browse files Browse the repository at this point in the history
Co-authored-by: GitHub Action <actions@github.com>
  • Loading branch information
TomasVotruba and actions-user committed Jun 30, 2023
1 parent 8324cf4 commit 5a91bde
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 19 deletions.
11 changes: 3 additions & 8 deletions config/config.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

declare(strict_types=1);
use Symfony\Component\Console\Command\Command;

use Composer\Semver\VersionParser;
use Doctrine\Inflector\Inflector;
Expand Down Expand Up @@ -178,6 +179,8 @@
__DIR__ . '/../src/constants.php',
]);

$services->set(ConsoleApplication::class)
->arg('$commands', tagged_iterator(Command::class));
$services->alias(Application::class, ConsoleApplication::class);

$services->set(EmptyConfigurableRectorCollector::class)
Expand Down Expand Up @@ -246,19 +249,11 @@
if (class_exists(MissingInSetCommand::class)) {
$services->set(MissingInSetCommand::class);
$services->set(OutsideAnySetCommand::class);

$services->get(ConsoleApplication::class)
->call('add', [service(MissingInSetCommand::class)])
->call('add', [service(OutsideAnySetCommand::class)]);
}

if (class_exists(InitRecipeCommand::class)) {
$services->set(InitRecipeCommand::class);
$services->set(GenerateCommand::class);

$services->get(ConsoleApplication::class)
->call('add', [service(InitRecipeCommand::class)])
->call('add', [service(GenerateCommand::class)]);
}

// phpdoc parser
Expand Down
24 changes: 13 additions & 11 deletions src/Console/ConsoleApplication.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,14 @@
use Rector\ChangesReporting\Output\ConsoleOutputFormatter;
use Rector\Core\Application\VersionResolver;
use Rector\Core\Configuration\Option;
use Rector\Core\Console\Command\ListRulesCommand;
use Rector\Core\Console\Command\ProcessCommand;
use Rector\Core\Console\Command\SetupCICommand;
use Rector\Core\Console\Command\WorkerCommand;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputDefinition;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\DependencyInjection\Argument\RewindableGenerator;
use Webmozart\Assert\Assert;

final class ConsoleApplication extends Application
{
Expand All @@ -25,16 +24,19 @@ final class ConsoleApplication extends Application
*/
private const NAME = 'Rector';

public function __construct(
ProcessCommand $processCommand,
WorkerCommand $workerCommand,
SetupCICommand $setupCICommand,
ListRulesCommand $listRulesCommand,
) {
/**
* @param RewindableGenerator<int, Command> $commands
*/
public function __construct(iterable $commands)
{
parent::__construct(self::NAME, VersionResolver::PACKAGE_VERSION);

$this->addCommands([$processCommand, $workerCommand, $setupCICommand, $listRulesCommand]);
$commands = iterator_to_array($commands->getIterator());
Assert::allIsInstanceOf($commands, Command::class);

$this->addCommands($commands);

// run this command, if no command name is provided
$this->setDefaultCommand('process');
}

Expand Down
2 changes: 2 additions & 0 deletions src/Kernel/ContainerBuilderFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Rector\PHPStanStaticTypeMapper\Contract\TypeMapperInterface;
use Rector\StaticTypeMapper\Contract\PhpDocParser\PhpDocTypeMapperInterface;
use Rector\StaticTypeMapper\Contract\PhpParser\PhpParserNodeMapperInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Webmozart\Assert\Assert;
Expand All @@ -39,6 +40,7 @@ final class ContainerBuilderFactory
PhpDocTypeMapperInterface::class,
ClassNameImportSkipVoterInterface::class,
RectorInterface::class,
Command::class,
RectorInterface::class,
OutputFormatterInterface::class,
NonPhpRectorInterface::class,
Expand Down

0 comments on commit 5a91bde

Please sign in to comment.