Browse Source

Replace Help/Version shells with Command equivalents.

Convert the Help/VersionShells into Commands. I've removed the old
classes, which might be a backwards incompatible change depending on
what you consider the 'API' of the console libraries is. I'm OK with
reverting the deletions if folks don't feel comfortable with that part
of the change.
Mark Story 8 years ago
parent
commit
ac2e3bf6ba

+ 38 - 44
src/Shell/HelpShell.php

@@ -9,16 +9,18 @@
  *
  * @copyright     Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  * @link          https://cakephp.org CakePHP(tm) Project
- * @since         3.5.0
+ * @since         3.6.0
  * @license       https://opensource.org/licenses/mit-license.php MIT License
  */
+namespace Cake\Command;
 
-namespace Cake\Shell;
-
+use Cake\Console\Arguments;
+use Cake\Console\Command;
 use Cake\Console\CommandCollection;
 use Cake\Console\CommandCollectionAwareInterface;
+use Cake\Console\ConsoleIo;
+use Cake\Console\ConsoleOptionParser;
 use Cake\Console\ConsoleOutput;
-use Cake\Console\Shell;
 use Cake\Shell\Task\CommandTask;
 use Cake\Utility\Inflector;
 use SimpleXmlElement;
@@ -26,7 +28,7 @@ use SimpleXmlElement;
 /**
  * Print out command list
  */
-class HelpShell extends Shell implements CommandCollectionAwareInterface
+class HelpCommand extends Command implements CommandCollectionAwareInterface
 {
     /**
      * The command collection to get help on.
@@ -36,18 +38,6 @@ class HelpShell extends Shell implements CommandCollectionAwareInterface
     protected $commands;
 
     /**
-     * startup
-     *
-     * @return void
-     */
-    public function startup()
-    {
-        if (!$this->param('xml')) {
-            parent::startup();
-        }
-    }
-
-    /**
      * {@inheritDoc}
      */
     public function setCommandCollection(CommandCollection $commands)
@@ -58,34 +48,36 @@ class HelpShell extends Shell implements CommandCollectionAwareInterface
     /**
      * Main function Prints out the list of shells.
      *
-     * @return void
+     * @param \Cake\Console\Arguments $args The command arguments.
+     * @param \Cake\Console\ConsoleIo $io The console io
+     * @return int|null
      */
-    public function main()
+    public function execute(Arguments $args, ConsoleIo $io)
     {
-        if (!$this->param('xml')) {
-            $this->out('<info>Current Paths:</info>', 2);
-            $this->out('* app:  ' . APP_DIR);
-            $this->out('* root: ' . rtrim(ROOT, DIRECTORY_SEPARATOR));
-            $this->out('* core: ' . rtrim(CORE_PATH, DIRECTORY_SEPARATOR));
-            $this->out('');
-
-            $this->out('<info>Available Commands:</info>', 2);
+        if (!$args->getOption('xml')) {
+            $io->out('<info>Current Paths:</info>', 2);
+            $io->out('* app:  ' . APP_DIR);
+            $io->out('* root: ' . rtrim(ROOT, DIRECTORY_SEPARATOR));
+            $io->out('* core: ' . rtrim(CORE_PATH, DIRECTORY_SEPARATOR));
+            $io->out('');
+
+            $io->out('<info>Available Commands:</info>', 2);
         }
 
         if (!$this->commands) {
-            $this->commands = new CommandCollection($this->getCommands());
+            $this->commands = new CommandCollection($this->getCommands($io));
         }
 
         $commands = $this->commands->getIterator();
         $commands->ksort();
         $commands = new CommandCollection((array)$commands);
 
-        if ($this->param('xml')) {
-            $this->asXml($commands);
+        if ($args->getOption('xml')) {
+            $this->asXml($io, $commands);
 
             return;
         }
-        $this->asText($commands);
+        $this->asText($io, $commands);
     }
 
     /**
@@ -94,11 +86,12 @@ class HelpShell extends Shell implements CommandCollectionAwareInterface
      * Provides backwards compatibility when an application doesn't use
      * CommandRunner.
      *
+     * @param \Cake\Console\ConsoleIo $io The console io
      * @return array
      */
-    protected function getCommands()
+    protected function getCommands($io)
     {
-        $task = new CommandTask($this->getIo());
+        $task = new CommandTask($io);
         $nested = $task->getShellList();
         $out = [];
         foreach ($nested as $section => $commands) {
@@ -117,27 +110,29 @@ class HelpShell extends Shell implements CommandCollectionAwareInterface
     /**
      * Output text.
      *
+     * @param \Cake\Console\ConsoleIo $io The console io
      * @param \Cake\Console\CommandCollection $commands The command collection to output.
      * @return void
      */
-    protected function asText($commands)
+    protected function asText($io, $commands)
     {
         foreach ($commands as $name => $class) {
-            $this->out('- ' . $name);
+            $io->out('- ' . $name);
         }
-        $this->out('');
+        $io->out('');
 
-        $this->out('To run a command, type <info>`cake shell_name [args|options]`</info>');
-        $this->out('To get help on a specific command, type <info>`cake shell_name --help`</info>', 2);
+        $io->out('To run a command, type <info>`cake shell_name [args|options]`</info>');
+        $io->out('To get help on a specific command, type <info>`cake shell_name --help`</info>', 2);
     }
 
     /**
      * Output as XML
      *
+     * @param \Cake\Console\ConsoleIo $io The console io
      * @param \Cake\Console\CommandCollection $commands The command collection to output
      * @return void
      */
-    protected function asXml($commands)
+    protected function asXml($io, $commands)
     {
         $shells = new SimpleXmlElement('<shells></shells>');
         foreach ($commands as $name => $class) {
@@ -147,19 +142,18 @@ class HelpShell extends Shell implements CommandCollectionAwareInterface
             $shell->addAttribute('provider', $class);
             $shell->addAttribute('help', $name . ' -h');
         }
-        $this->_io->setOutputAs(ConsoleOutput::RAW);
-        $this->out($shells->saveXML());
+        $io->setOutputAs(ConsoleOutput::RAW);
+        $io->out($shells->saveXML());
     }
 
     /**
      * Gets the option parser instance and configures it.
      *
+     * @param \Cake\Console\ConsoleOptionParser $parser The parser to build
      * @return \Cake\Console\ConsoleOptionParser
      */
-    public function getOptionParser()
+    protected function buildOptionParser(ConsoleOptionParser $parser)
     {
-        $parser = parent::getOptionParser();
-
         $parser->setDescription(
             'Get the list of available shells for this application.'
         )->addOption('xml', [

+ 11 - 8
src/Shell/VersionShell.php

@@ -9,27 +9,30 @@
  *
  * @copyright     Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  * @link          https://cakephp.org CakePHP(tm) Project
- * @since         3.5.0
+ * @since         3.6.0
  * @license       https://opensource.org/licenses/mit-license.php MIT License
  */
+namespace Cake\Command;
 
-namespace Cake\Shell;
-
-use Cake\Console\Shell;
+use Cake\Console\Arguments;
+use Cake\Console\ConsoleIo;
+use Cake\Console\Command;
 use Cake\Core\Configure;
 
 /**
  * Print out the version of CakePHP in use.
  */
-class VersionShell extends Shell
+class VersionCommand extends Command
 {
     /**
      * Print out the version of CakePHP in use.
      *
-     * @return void
+     * @param \Cake\Console\Arguments $args The command arguments.
+     * @param \Cake\Console\ConsoleIo $io The console io
+     * @return int|null
      */
-    public function main()
+    public function execute(Arguments $args, ConsoleIo $io)
     {
-        $this->out(Configure::version());
+        $io->out(Configure::version());
     }
 }

+ 4 - 4
src/Console/CommandRunner.php

@@ -14,6 +14,8 @@
  */
 namespace Cake\Console;
 
+use Cake\Command\HelpCommand;
+use Cake\Command\VersionCommand;
 use Cake\Console\CommandCollection;
 use Cake\Console\CommandCollectionAwareInterface;
 use Cake\Console\ConsoleIo;
@@ -21,8 +23,6 @@ use Cake\Console\Exception\StopException;
 use Cake\Console\Shell;
 use Cake\Core\ConsoleApplicationInterface;
 use Cake\Event\EventManagerTrait;
-use Cake\Shell\HelpShell;
-use Cake\Shell\VersionShell;
 use Cake\Utility\Inflector;
 use RuntimeException;
 
@@ -114,8 +114,8 @@ class CommandRunner
         $this->app->bootstrap();
 
         $commands = new CommandCollection([
-            'version' => VersionShell::class,
-            'help' => HelpShell::class,
+            'version' => VersionCommand::class,
+            'help' => HelpCommand::class,
         ]);
         $commands = $this->app->console($commands);
         if (!($commands instanceof CommandCollection)) {

+ 1 - 1
tests/TestCase/Shell/HelpShellTest.php

@@ -12,7 +12,7 @@
  * @since         3.5.0
  * @license       https://opensource.org/licenses/mit-license.php MIT License
  */
-namespace Cake\Test\TestCase\Shell;
+namespace Cake\Test\TestCase\Command;
 
 use Cake\Console\Shell;
 use Cake\Core\Plugin;