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

[FEATURE] Use CommandLoader API of symfony/console #676

Merged
merged 1 commit into from
Feb 17, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 6 additions & 4 deletions Classes/Core/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,17 +165,19 @@ public function handle(InputInterface $input): int
{
$this->initialize();

$commandRegistry = new CommandCollection(
$commandCollection = new CommandCollection(
$this->runLevel,
GeneralUtility::makeInstance(PackageManager::class)
);

$application = new Application($this->runLevel, Bootstrap::usesComposerClassLoading());
$application->addCommandsIfAvailable($commandRegistry);
$application->setCommandLoader($commandCollection);

$commandIdentifier = $input->getFirstArgument() ?: '';
$this->runLevel->runSequenceForCommand($commandIdentifier);
$application->addCommandsIfAvailable($commandRegistry->addCommandControllerCommands(GeneralUtility::makeInstance(ObjectManager::class)->get(CommandManager::class)));
if ($this->runLevel->isCommandAvailable($commandIdentifier)) {
$this->runLevel->runSequenceForCommand($commandIdentifier);
$commandCollection->addCommandControllerCommands(GeneralUtility::makeInstance(ObjectManager::class)->get(CommandManager::class));
}

return $application->run($input);
}
Expand Down
73 changes: 48 additions & 25 deletions Classes/Mvc/Cli/CommandCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
use Helhum\Typo3Console\Core\Booting\RunLevel;
use Helhum\Typo3Console\Mvc\Cli\Symfony\Command\CommandControllerCommand;
use Symfony\Component\Console\Command\Command as BaseCommand;
use Symfony\Component\Console\CommandLoader\CommandLoaderInterface;
use Symfony\Component\Console\Exception\CommandNotFoundException;
use Symfony\Component\Console\Exception\RuntimeException;
use TYPO3\CMS\Core\Console\CommandNameAlreadyInUseException;
use TYPO3\CMS\Core\Package\PackageManager;
Expand All @@ -29,7 +31,7 @@
* This implementation pulls in the commands from various places,
* mainly reading configuration files from TYPO3 extensions and composer packages
*/
class CommandCollection implements \IteratorAggregate
class CommandCollection implements CommandLoaderInterface
{
/**
* @var BaseCommand[]
Expand Down Expand Up @@ -61,33 +63,56 @@ class CommandCollection implements \IteratorAggregate
*/
private $packageManager;

public function __construct(RunLevel $runLevel, PackageManager $packageManager = null)
public function __construct(RunLevel $runLevel, PackageManager $packageManager)
{
$this->runLevel = $runLevel;
$this->packageManager = $packageManager;
$this->populateCommands();
}

/**
* @return \Generator
* @param string $name
* @throws \Symfony\Component\Console\Exception\CommandNotFoundException
* @return BaseCommand
*/
public function getIterator(): \Generator
public function get($name): BaseCommand
{
$this->populateCommands();
foreach ($this->commands as $commandName => $command) {
yield $commandName => $command;
if (!isset($this->commands[$name]) || !$this->runLevel->isCommandAvailable($name)) {
throw new CommandNotFoundException(sprintf('A command with name "%s" could not be found.', $name), 1518812618);
}
$command = $this->commands[$name]['closure']();
if (isset($commandDefinition['alias'])) {
$command->setAliases([$this->commands[$name]['alias']]);
}
return $command;
}

/**
* @param string $name
* @return bool
*/
public function has($name): bool
{
return isset($this->commands[$name]) && $this->runLevel->isCommandAvailable($name);
}

/**
* @return string[]
*/
public function getNames(): array
{
return array_keys($this->commands);
}

/**
* Add command controller commands
*
* @throws \TYPO3\CMS\Core\Console\CommandNameAlreadyInUseException
* @return BaseCommand[]
*/
public function addCommandControllerCommands(CommandManager $commandManager): array
public function addCommandControllerCommands(CommandManager $commandManager)
{
$this->populateCommands();
return $this->populateCommandControllerCommands($commandManager);
$this->populateCommandControllerCommands($commandManager);
}

private function populateCommands()
Expand Down Expand Up @@ -127,11 +152,9 @@ private function initializeConfiguration()

/**
* @param CommandManager $commandManager
* @return array
*/
private function populateCommandControllerCommands(CommandManager $commandManager): array
private function populateCommandControllerCommands(CommandManager $commandManager)
{
$registeredCommands = [];
$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['extbase']['commandControllers'] = $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['extbase']['commandControllers'] ?? [];
$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['extbase']['commandControllers'] = array_merge($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['extbase']['commandControllers'], $this->commandControllerClasses);
foreach ($commandManager->getAvailableCommands() as $commandDefinition) {
Expand All @@ -142,13 +165,12 @@ private function populateCommandControllerCommands(CommandManager $commandManage
$fullCommandIdentifier
);
if ($commandName) {
$commandControllerCommand = GeneralUtility::makeInstance(CommandControllerCommand::class, $commandName, $commandDefinition);
$this->add($commandControllerCommand, $fullCommandIdentifier);
$registeredCommands[] = $commandControllerCommand;
$closure = function () use ($commandName, $commandDefinition) {
return GeneralUtility::makeInstance(CommandControllerCommand::class, $commandName, $commandDefinition);
};
$this->add($closure, $commandName, $fullCommandIdentifier);
}
}

return $registeredCommands;
}

/**
Expand All @@ -169,22 +191,23 @@ private function populateNativeCommands()
$fullCommandIdentifier
);
if ($commandName) {
/** @var BaseCommand $command */
$command = GeneralUtility::makeInstance($commandConfig['class'], $commandName);
$closure = function () use ($commandConfig, $commandName) {
/** @var BaseCommand $command */
return GeneralUtility::makeInstance($commandConfig['class'], $commandName);
};
// No aliases for native commands (they can define their own aliases)
$this->add($command, $commandName);
$this->add($closure, $commandName, $commandName);
}
}
}
}

private function add(BaseCommand $command, $fullCommandIdentifier)
private function add(\Closure $closure, $commandName, $fullCommandIdentifier)
{
$commandName = $command->getName();
$this->commands[$commandName] = $command;
$this->commands[$commandName]['closure'] = $closure;
if ($commandName !== $fullCommandIdentifier) {
// @deprecated in 5.0 will be removed in 6.0
$this->commands[$commandName]->setAliases([$fullCommandIdentifier]);
$this->commands[$commandName]['alias'] = $fullCommandIdentifier;
}
}

Expand Down
8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@
"typo3/cms-saltedpasswords": "^8.7.10 || ~9.1.0",

"doctrine/annotations": "^1.4",
"symfony/console": "^3.3.6 || ^4.0",
"symfony/process": "^3.3.6 || ^4.0"
"symfony/console": "^3.4.4 || ^4.0",
"symfony/process": "^3.4.4 || ^4.0"
},
"require-dev": {
"typo3/cms-reports": "^8.7.10 || ~9.1.0 || 9.2.*@dev",
"typo3/cms-filemetadata": "^8.7.10 || ~9.1.0 || 9.2.*@dev",

"typo3-console/php-server-command": "^0.1.1",
"typo3-console/php-server-command": "^0.2",
"typo3-console/create-reference-command": "^2.2",
"symfony/filesystem": "^3.2",
"nimut/testing-framework": "3.*@dev"
Expand Down Expand Up @@ -89,7 +89,7 @@
"extension-create-libs": [
"mkdir -p Libraries/temp",
"[ -f $HOME/.composer/vendor/bin/phar-composer ] || composer global require clue/phar-composer",
"if [ ! -f Libraries/symfony-process.phar ]; then cd Libraries/temp && composer require symfony/console=^3.3.6 symfony/process=^3.3.6 && composer config classmap-authoritative true && composer config prepend-autoloader true && composer dump-autoload -o; fi",
"if [ ! -f Libraries/symfony-process.phar ]; then cd Libraries/temp && composer require symfony/console=^3.4.4 symfony/process=^3.4.4 && composer config classmap-authoritative true && composer config prepend-autoloader true && composer dump-autoload -o; fi",
"[ -f Libraries/symfony-process.phar ] || $HOME/.composer/vendor/bin/phar-composer build Libraries/temp/ Libraries/symfony-process.phar",
"chmod -x Libraries/*.phar",
"rm -rf Libraries/temp"
Expand Down