Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

load commnads automatically #70

Merged
merged 1 commit into from
Jan 13, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 31 additions & 27 deletions src/DependencyInjection/ContainerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,12 @@
use Illuminate\Container\Container;
use PhpParser\Parser;
use PhpParser\ParserFactory;
use Rector\SwissKnife\Behastan\Command\BehastanCommand;
use Rector\SwissKnife\Command\CheckCommentedCodeCommand;
use Rector\SwissKnife\Command\CheckConflictsCommand;
use Rector\SwissKnife\Command\DumpEditorconfigCommand;
use Rector\SwissKnife\Command\FinalizeClassesCommand;
use Rector\SwissKnife\Command\FindMultiClassesCommand;
use Rector\SwissKnife\Command\NamespaceToPSR4Command;
use Rector\SwissKnife\Command\PrettyJsonCommand;
use Rector\SwissKnife\Command\PrivatizeConstantsCommand;
use Rector\SwissKnife\Command\SearchRegexCommand;
use Rector\SwissKnife\Testing\Command\DetectUnitTestsCommand;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\ConsoleOutput;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\Finder\Finder;
use Webmozart\Assert\Assert;

final class ContainerFactory
{
Expand All @@ -36,23 +27,13 @@ public function create(): Container
$container->singleton(Application::class, function (Container $container): Application {
$application = new Application('Rector Swiss Knife');

// @todo add automated way to load these, so we don't forget any
$commands = [
$container->make(PrettyJsonCommand::class),
$container->make(CheckCommentedCodeCommand::class),
$container->make(CheckConflictsCommand::class),
$container->make(DetectUnitTestsCommand::class),
$container->make(FindMultiClassesCommand::class),
$container->make(NamespaceToPSR4Command::class),
$container->make(DumpEditorconfigCommand::class),
$container->make(FinalizeClassesCommand::class),
$container->make(PrivatizeConstantsCommand::class),
$commandClasses = $this->findCommandClasses();

$container->make(BehastanCommand::class),
$container->make(SearchRegexCommand::class),
];

$application->addCommands($commands);
// register commands
foreach ($commandClasses as $commandClass) {
$command = $container->make($commandClass);
$application->add($command);
}

// remove basic command to make output clear
$this->hideDefaultCommands($application);
Expand Down Expand Up @@ -83,4 +64,27 @@ public function hideDefaultCommands(Application $application): void
$application->get('help')
->setHidden(true);
}

/**
* @return string[]
*/
private function findCommandClasses(): array
{
$commandFinder = Finder::create()
->files()
->name('*Command.php')
->in(__DIR__ . '/../Command');

$commandClasses = [];
foreach ($commandFinder as $commandFile) {
$commandClass = 'Rector\\SwissKnife\\Command\\' . $commandFile->getBasename('.php');

// make sure it exists
Assert::classExists($commandClass);

$commandClasses[] = $commandClass;
}

return $commandClasses;
}
}
Loading