From 3db01f464b7902b38964ae344bca3274d2c2458e Mon Sep 17 00:00:00 2001
From: Helmut Hummel <typo3@helhum.io>
Date: Thu, 20 Feb 2020 17:44:22 +0100
Subject: [PATCH] [BUGFIX] Fix cli command aliasing

Instead of overriding the command name, an aliases
are now set for a command.

Command names and aliases are now configured during
container compilation pass instead of runtime.

Resolves: #90450
Releases: master
Change-Id: I82dd96717e30d38387273fa2a10828906c5c46bc
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/63331
Tested-by: Simon Gilli <typo3@gilbertsoft.org>
Tested-by: TYPO3com <noreply@typo3.com>
Tested-by: Susanne Moog <look@susi.dev>
Reviewed-by: Simon Gilli <typo3@gilbertsoft.org>
Reviewed-by: Susanne Moog <look@susi.dev>
---
 .../core/Classes/Console/CommandRegistry.php  | 22 ++++++-------------
 .../ConsoleCommandPass.php                    | 17 ++++++++++----
 .../install/Classes/ServiceProvider.php       |  6 ++---
 3 files changed, 23 insertions(+), 22 deletions(-)

diff --git a/typo3/sysext/core/Classes/Console/CommandRegistry.php b/typo3/sysext/core/Classes/Console/CommandRegistry.php
index 969a1be4f09e..a82a32acb787 100644
--- a/typo3/sysext/core/Classes/Console/CommandRegistry.php
+++ b/typo3/sysext/core/Classes/Console/CommandRegistry.php
@@ -112,7 +112,7 @@ class CommandRegistry implements CommandLoaderInterface, \IteratorAggregate, Sin
         $this->populateCommandsFromPackages();
         foreach ($this->commands as $commandName => $command) {
             if (is_string($command)) {
-                $command = $this->getInstance($command, $commandName);
+                $command = $this->getInstance($command);
             }
             yield $commandName => $command;
         }
@@ -129,7 +129,7 @@ class CommandRegistry implements CommandLoaderInterface, \IteratorAggregate, Sin
         foreach ($this->commands as $commandName => $command) {
             if ($this->commandConfigurations[$commandName]['schedulable'] ?? true) {
                 if (is_string($command)) {
-                    $command = $this->getInstance($command, $commandName);
+                    $command = $this->getInstance($command);
                 }
                 yield $commandName => $command;
             }
@@ -171,7 +171,7 @@ class CommandRegistry implements CommandLoaderInterface, \IteratorAggregate, Sin
 
         $command = $this->commands[$identifier] ?? null;
         if (is_string($command)) {
-            $command = $this->getInstance($command, $identifier);
+            $command = $this->getInstance($command);
         }
 
         return $command;
@@ -218,7 +218,7 @@ class CommandRegistry implements CommandLoaderInterface, \IteratorAggregate, Sin
                         if (array_key_exists($commandName, $this->lazyCommandConfigurations)) {
                             // Lazy (DI managed) commands override classic commands from Configuration/Commands.php
                             // Skip this case to allow extensions to provide commands via DI config and to allow
-                            // TYPO3 v9 backwards compatibile confguration via Configuration/Commands.php.
+                            // TYPO3 v9 backwards compatible configuration via Configuration/Commands.php.
                             // Note: Also the deprecation error is skipped on-demand as the extension has been
                             // adapted and the configuration will be ignored as of TYPO3 v11.
                             continue;
@@ -242,26 +242,18 @@ class CommandRegistry implements CommandLoaderInterface, \IteratorAggregate, Sin
         }
     }
 
-    protected function getInstance(string $class, string $commandName): Command
+    protected function getInstance(string $class): Command
     {
-        $command = $this->container->get($class);
-
-        if ($command instanceof Command) {
-            $command->setName($commandName);
-            return $command;
-        }
-
-        throw new \InvalidArgumentException('Registered console command class ' . get_class($command) . ' does not inherit from ' . Command::class, 1567966448);
+        return $this->container->get($class);
     }
 
     /**
      * @internal
      */
-    public function addLazyCommand(string $commandName, string $serviceName, bool $alias = false, bool $schedulable = true): void
+    public function addLazyCommand(string $commandName, string $serviceName, bool $schedulable = true): void
     {
         $this->lazyCommandConfigurations[$commandName] = [
             'class' => $serviceName,
-            'alias' => $alias,
             'schedulable' => $schedulable,
         ];
     }
diff --git a/typo3/sysext/core/Classes/DependencyInjection/ConsoleCommandPass.php b/typo3/sysext/core/Classes/DependencyInjection/ConsoleCommandPass.php
index f35cc5cd9d4c..75351ddbe75f 100644
--- a/typo3/sysext/core/Classes/DependencyInjection/ConsoleCommandPass.php
+++ b/typo3/sysext/core/Classes/DependencyInjection/ConsoleCommandPass.php
@@ -47,20 +47,29 @@ final class ConsoleCommandPass implements CompilerPassInterface
             return;
         }
 
-        $unorderedEventListeners = [];
         foreach ($container->findTaggedServiceIds($this->tagName) as $serviceName => $tags) {
-            $container->findDefinition($serviceName)->setPublic(true);
+            $commandServiceDefinition = $container->findDefinition($serviceName)->setPublic(true);
+            $commandName = null;
+            $aliases = [];
             foreach ($tags as $attributes) {
                 if (!isset($attributes['command'])) {
                     continue;
                 }
-
                 $commandRegistryDefinition->addMethodCall('addLazyCommand', [
                     $attributes['command'],
                     $serviceName,
-                    (bool)($attributes['alias'] ?? false),
                     (bool)($attributes['schedulable'] ?? true)
                 ]);
+                $isAlias = isset($commandName) || ($attributes['alias'] ?? false);
+                if (!$isAlias) {
+                    $commandName = $attributes['command'];
+                } else {
+                    $aliases[] = $attributes['command'];
+                }
+            }
+            $commandServiceDefinition->addMethodCall('setName', [$commandName]);
+            if ($aliases) {
+                $commandServiceDefinition->addMethodCall('setAliases', [$aliases]);
             }
         }
     }
diff --git a/typo3/sysext/install/Classes/ServiceProvider.php b/typo3/sysext/install/Classes/ServiceProvider.php
index a1874db01e6f..5de3b358796e 100644
--- a/typo3/sysext/install/Classes/ServiceProvider.php
+++ b/typo3/sysext/install/Classes/ServiceProvider.php
@@ -115,17 +115,17 @@ class ServiceProvider extends AbstractServiceProvider
 
     public static function getLanguagePackCommand(ContainerInterface $container): Command\LanguagePackCommand
     {
-        return new Command\LanguagePackCommand;
+        return new Command\LanguagePackCommand('language:update');
     }
 
     public static function getUpgradeWizardRunCommand(ContainerInterface $container): Command\UpgradeWizardRunCommand
     {
-        return new Command\UpgradeWizardRunCommand;
+        return new Command\UpgradeWizardRunCommand('upgrade:run');
     }
 
     public static function getUpgradeWizardListCommand(ContainerInterface $container): Command\UpgradeWizardListCommand
     {
-        return new Command\UpgradeWizardListCommand;
+        return new Command\UpgradeWizardListCommand('upgrade:list');
     }
 
     public static function configureCommands(ContainerInterface $container, CommandRegistry $commandRegistry): CommandRegistry
-- 
GitLab