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] Unify command registration #685

Merged
merged 2 commits into from
Mar 10, 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
15 changes: 10 additions & 5 deletions Classes/Composer/InstallerScript/PopulateCommandConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/

use Composer\Script\Event as ScriptEvent;
use Helhum\Typo3Console\Mvc\Cli\CommandConfiguration;
use TYPO3\CMS\Composer\Plugin\Core\InstallerScript;

/**
Expand Down Expand Up @@ -50,6 +51,7 @@ public function run(ScriptEvent $event): bool
}
$commandConfiguration = array_merge($commandConfiguration, $this->getConfigFromPackage($installPath, $packageName));
}

$success = file_put_contents(
__DIR__ . '/../../../Configuration/Console/ComposerPackagesCommands.php',
'<?php' . chr(10)
Expand All @@ -65,7 +67,7 @@ public function run(ScriptEvent $event): bool
* @param \Composer\Composer $composer
* @return array
*/
private function extractPackageMapFromComposer(\Composer\Composer $composer)
private function extractPackageMapFromComposer(\Composer\Composer $composer): array
{
$mainPackage = $composer->getPackage();
$autoLoadGenerator = $composer->getAutoloadGenerator();
Expand All @@ -74,10 +76,12 @@ private function extractPackageMapFromComposer(\Composer\Composer $composer)
}

/**
* @param $installPath
* @return mixed
* @param string $installPath
* @param string $packageName
* @throws \Symfony\Component\Console\Exception\RuntimeException
* @return array
*/
private function getConfigFromPackage(string $installPath, string $packageName)
private function getConfigFromPackage(string $installPath, string $packageName): array
{
$commandConfiguration = [];
if (file_exists($commandConfigurationFile = $installPath . '/Configuration/Console/Commands.php')) {
Expand All @@ -89,6 +93,7 @@ private function getConfigFromPackage(string $installPath, string $packageName)
if (empty($commandConfiguration)) {
return [];
}
return [$packageName => $commandConfiguration];
CommandConfiguration::ensureValidCommandRegistration($commandConfiguration, $packageName);
return [$packageName => CommandConfiguration::unifyCommandConfiguration($commandConfiguration, $packageName)];
}
}
33 changes: 11 additions & 22 deletions Classes/Core/Booting/RunLevel.php
Original file line number Diff line number Diff line change
Expand Up @@ -292,13 +292,13 @@ private function addStep(Sequence $sequence, string $stepIdentifier)
* @return string
* @internal
*/
private function getRunLevelForCommand(string $commandIdentifier): string
public function getRunLevelForCommand(string $commandIdentifier): string
{
if (in_array($commandIdentifier, ['', 'help', 'list'], true)) {
if (in_array($commandIdentifier, ['help', 'list'], true)) {
return $this->getMaximumAvailableRunLevel();
}
$options = $this->getOptionsForCommand($commandIdentifier);
return isset($options['runLevel']) ? $options['runLevel'] : self::LEVEL_FULL;
return $options['runLevel'] ?? self::LEVEL_FULL;
}

/**
Expand Down Expand Up @@ -337,30 +337,19 @@ private function removeStepsForCommand(Sequence $sequence, string $commandIdenti
*/
private function getOptionsForCommand(string $commandIdentifier)
{
$commandIdentifierParts = explode(':', $commandIdentifier);
if (count($commandIdentifierParts) < 2 || count($commandIdentifierParts) > 3) {
return null;
$commandIdentifierPrefix = $commandIdentifier;
$position = strrpos($commandIdentifier, ':');
if ($position !== false) {
$commandIdentifierPrefix = substr($commandIdentifier, 0, $position);
}

if (isset($this->commandOptions[$commandIdentifier])) {
return $this->commandOptions[$commandIdentifier];
}

if (count($commandIdentifierParts) === 3) {
$currentCommandControllerName = $commandIdentifierParts[1];
$currentCommandName = $commandIdentifierParts[2];
} else {
$currentCommandControllerName = $commandIdentifierParts[0];
$currentCommandName = $commandIdentifierParts[1];
}

foreach ($this->commandOptions as $fullControllerIdentifier => $commandRegistry) {
list(, $controllerName, $commandName) = explode(':', $fullControllerIdentifier);
if ($controllerName === $currentCommandControllerName && $commandName === $currentCommandName) {
return $this->commandOptions[$fullControllerIdentifier];
}
if ($controllerName === $currentCommandControllerName && $commandName === '*') {
return $this->commandOptions[$fullControllerIdentifier];
}
$lookupKey = $commandIdentifierPrefix . ':*';
if (isset($this->commandOptions[$lookupKey])) {
return $this->commandOptions[$lookupKey];
}

return null;
Expand Down
24 changes: 13 additions & 11 deletions Classes/Core/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@
use Doctrine\Common\Annotations\AnnotationRegistry;
use Helhum\Typo3Console\Core\Booting\RunLevel;
use Helhum\Typo3Console\Core\Booting\Scripts;
use Helhum\Typo3Console\Exception;
use Helhum\Typo3Console\Mvc\Cli\CommandCollection;
use Helhum\Typo3Console\Mvc\Cli\CommandConfiguration;
use Helhum\Typo3Console\Mvc\Cli\Symfony\Application;
use Symfony\Component\Console\Exception\InvalidArgumentException;
use Symfony\Component\Console\Input\InputInterface;
use TYPO3\CMS\Core\Core\Bootstrap;
use TYPO3\CMS\Core\Package\PackageManager;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Mvc\Cli\CommandManager;
use TYPO3\CMS\Extbase\Object\ObjectManager;

/**
* @internal
Expand Down Expand Up @@ -138,8 +139,8 @@ class_alias($compatibilityClassName, $className);
* without actually executing a command (e.g. during composer install)
*
* @param string $runLevel
* @throws \Helhum\Typo3Console\Exception
* @throws \Symfony\Component\Console\Exception\InvalidArgumentException
* @throws Exception
* @throws InvalidArgumentException
*/
public function initialize(string $runLevel = null)
{
Expand All @@ -157,8 +158,8 @@ public function initialize(string $runLevel = null)
* Handle the given command input and return the exit code of the called command
*
* @param InputInterface $input
* @throws \Helhum\Typo3Console\Exception
* @throws \Symfony\Component\Console\Exception\InvalidArgumentException
* @throws Exception
* @throws InvalidArgumentException
* @return int
*/
public function handle(InputInterface $input): int
Expand All @@ -167,16 +168,17 @@ public function handle(InputInterface $input): int

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

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

$commandIdentifier = $input->getFirstArgument() ?: '';
if ($this->runLevel->isCommandAvailable($commandIdentifier)) {
$this->runLevel->runSequenceForCommand($commandIdentifier);
$commandCollection->addCommandControllerCommands(GeneralUtility::makeInstance(ObjectManager::class)->get(CommandManager::class));
// Try to resolve short command names and aliases
$commandName = $commandCollection->find($input->getFirstArgument() ?: 'list');
if ($this->runLevel->isCommandAvailable($commandName)) {
$this->runLevel->runSequenceForCommand($commandName);
$commandCollection->addCommandControllerCommands($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['extbase']['commandControllers'] ?? []);
}

return $application->run($input);
Expand Down