Browse Source

Update Cache shell to commands

Convert the cache shell commands to command classes.
Mark Story 7 years ago
parent
commit
c751690b70

+ 61 - 0
src/Command/CacheClearAllCommand.php

@@ -0,0 +1,61 @@
+<?php
+declare(strict_types=1);
+/**
+ * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
+ * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
+ *
+ * Licensed under The MIT License
+ * For full copyright and license information, please see the LICENSE.txt
+ * Redistributions of files must retain the above copyright notice.
+ *
+ * @copyright     Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
+ * @link          https://cakephp.org CakePHP(tm) Project
+ * @since         4.0.0
+ * @license       https://opensource.org/licenses/mit-license.php MIT License
+ */
+namespace Cake\Command;
+
+use Cake\Cache\Cache;
+use Cake\Console\Arguments;
+use Cake\Console\Command;
+use Cake\Console\ConsoleIo;
+use Cake\Console\ConsoleOptionParser;
+
+/**
+ * CacheClearAll command.
+ */
+class CacheClearAllCommand extends Command
+{
+    /**
+     * Hook method for defining this command's option parser.
+     *
+     * @see https://book.cakephp.org/3.0/en/console-and-shells/commands.html#defining-arguments-and-options
+     *
+     * @param \Cake\Console\ConsoleOptionParser $parser The parser to be defined
+     * @return \Cake\Console\ConsoleOptionParser The built parser.
+     */
+    public function buildOptionParser(ConsoleOptionParser $parser): ConsoleOptionParser
+    {
+        $parser = parent::buildOptionParser($parser);
+        $parser->setDescription('Clear all data in all configured cache engines.');
+
+        return $parser;
+    }
+
+    /**
+     * Implement this method with your command's logic.
+     *
+     * @param \Cake\Console\Arguments $args The command arguments.
+     * @param \Cake\Console\ConsoleIo $io The console io
+     * @return null|int The exit code or null for success
+     */
+    public function execute(Arguments $args, ConsoleIo $io): ?int
+    {
+        $engines = Cache::configured();
+        foreach ($engines as $engine) {
+            $this->executeCommand(CacheClearCommand::class, [$engine], $io);
+        }
+
+        return static::CODE_SUCCESS;
+    }
+}

+ 86 - 0
src/Command/CacheClearCommand.php

@@ -0,0 +1,86 @@
+<?php
+declare(strict_types=1);
+/**
+ * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
+ * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
+ *
+ * Licensed under The MIT License
+ * For full copyright and license information, please see the LICENSE.txt
+ * Redistributions of files must retain the above copyright notice.
+ *
+ * @copyright     Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
+ * @link          https://cakephp.org CakePHP(tm) Project
+ * @since         4.0.0
+ * @license       https://opensource.org/licenses/mit-license.php MIT License
+ */
+namespace Cake\Command;
+
+use Cake\Cache\Cache;
+use Cake\Cache\Engine\ApcuEngine;
+use Cake\Cache\Engine\WincacheEngine;
+use Cake\Cache\InvalidArgumentException;
+use Cake\Console\Arguments;
+use Cake\Console\Command;
+use Cake\Console\ConsoleIo;
+use Cake\Console\ConsoleOptionParser;
+
+/**
+ * CacheClear command.
+ */
+class CacheClearCommand extends Command
+{
+    /**
+     * Hook method for defining this command's option parser.
+     *
+     * @see https://book.cakephp.org/3.0/en/console-and-shells/commands.html#defining-arguments-and-options
+     *
+     * @param \Cake\Console\ConsoleOptionParser $parser The parser to be defined
+     * @return \Cake\Console\ConsoleOptionParser The built parser.
+     */
+    public function buildOptionParser(ConsoleOptionParser $parser): ConsoleOptionParser
+    {
+        $parser = parent::buildOptionParser($parser);
+        $parser
+            ->setDescription('Clear all data in a single cache engine')
+            ->addArgument('engine', [
+                'help' => 'The cache engine to clear.' .
+                    'For example, `cake cache clear _cake_model_` will clear the model cache' .
+                    'Use `cake cache list_engines` to list available engines',
+                'required' => true,
+            ]);
+
+        return $parser;
+    }
+
+    /**
+     * Implement this method with your command's logic.
+     *
+     * @param \Cake\Console\Arguments $args The command arguments.
+     * @param \Cake\Console\ConsoleIo $io The console io
+     * @return null|int The exit code or null for success
+     */
+    public function execute(Arguments $args, ConsoleIo $io): ?int
+    {
+        $name = $args->getArgument('engine');
+        try {
+            $io->out("Clearing {$name}");
+
+            $engine = Cache::pool($name);
+            Cache::clear($name);
+            if ($engine instanceof ApcuEngine) {
+                $io->warn("ApcuEngine detected: Cleared {$name} CLI cache successfully " .
+                    "but {$name} web cache must be cleared separately.");
+            } elseif ($engine instanceof WincacheEngine) {
+                $io->warn("WincacheEngine detected: Cleared {$name} CLI cache successfully " .
+                    "but {$name} web cache must be cleared separately.");
+            } else {
+                $io->out("<success>Cleared {$name} cache</success>");
+            }
+        } catch (InvalidArgumentException $e) {
+            $io->error($e->getMessage());
+            $this->abort();
+        }
+
+        return static::CODE_SUCCESS;
+    }
+}

+ 61 - 0
src/Command/CacheListCommand.php

@@ -0,0 +1,61 @@
+<?php
+declare(strict_types=1);
+/**
+ * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
+ * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
+ *
+ * Licensed under The MIT License
+ * For full copyright and license information, please see the LICENSE.txt
+ * Redistributions of files must retain the above copyright notice.
+ *
+ * @copyright     Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
+ * @link          https://cakephp.org CakePHP(tm) Project
+ * @since         4.0.0
+ * @license       https://opensource.org/licenses/mit-license.php MIT License
+ */
+namespace Cake\Command;
+
+use Cake\Cache\Cache;
+use Cake\Console\Arguments;
+use Cake\Console\Command;
+use Cake\Console\ConsoleIo;
+use Cake\Console\ConsoleOptionParser;
+
+/**
+ * CacheList command.
+ */
+class CacheListCommand extends Command
+{
+    /**
+     * Hook method for defining this command's option parser.
+     *
+     * @see https://book.cakephp.org/3.0/en/console-and-shells/commands.html#defining-arguments-and-options
+     *
+     * @param \Cake\Console\ConsoleOptionParser $parser The parser to be defined
+     * @return \Cake\Console\ConsoleOptionParser The built parser.
+     */
+    public function buildOptionParser(ConsoleOptionParser $parser): ConsoleOptionParser
+    {
+        $parser = parent::buildOptionParser($parser);
+        $parser->setDescription('Show a list of configured caches.');
+
+        return $parser;
+    }
+
+    /**
+     * Get the list of cache prefixes
+     *
+     * @param \Cake\Console\Arguments $args The command arguments.
+     * @param \Cake\Console\ConsoleIo $io The console io
+     * @return null|int The exit code or null for success
+     */
+    public function execute(Arguments $args, ConsoleIo $io): ?int
+    {
+        $engines = Cache::configured();
+        foreach ($engines as $engine) {
+            $io->out("- $engine");
+        }
+
+        return static::CODE_SUCCESS;
+    }
+}

+ 39 - 6
src/Console/CommandScanner.php

@@ -15,6 +15,12 @@ declare(strict_types=1);
  */
 namespace Cake\Console;
 
+use Cake\Command\CacheClearAllCommand;
+use Cake\Command\CacheClearCommand;
+use Cake\Command\CacheListCommand;
+use Cake\Command\HelpCommand;
+use Cake\Command\UpgradeCommand;
+use Cake\Command\VersionCommand;
 use Cake\Core\App;
 use Cake\Core\Configure;
 use Cake\Core\Plugin;
@@ -42,12 +48,39 @@ class CommandScanner
             '',
             ['command_list']
         );
-        $coreCommands = $this->scanDir(
-            dirname(__DIR__) . DIRECTORY_SEPARATOR . 'Command' . DIRECTORY_SEPARATOR,
-            'Cake\Command\\',
-            '',
-            ['command_list']
-        );
+
+        $coreCommands = [
+            [
+                'name' => 'cache clear',
+                'fullName' => 'cache clear',
+                'class' => CacheClearCommand::class,
+            ],
+            [
+                'name' => 'cache clear_all',
+                'fullName' => 'cache clear_all',
+                'class' => CacheClearAllCommand::class,
+            ],
+            [
+                'name' => 'cache list',
+                'fullName' => 'cache list',
+                'class' => CacheListCommand::class,
+            ],
+            [
+                'name' => 'help',
+                'fullName' => 'help',
+                'class' => HelpCommand::class,
+            ],
+            [
+                'name' => 'upgrade',
+                'fullName' => 'upgrade',
+                'class' => UpgradeCommand::class,
+            ],
+            [
+                'name' => 'version',
+                'fullName' => 'version',
+                'class' => VersionCommand::class,
+            ],
+        ];
 
         return array_merge($coreShells, $coreCommands);
     }

+ 0 - 119
src/Shell/CacheShell.php

@@ -1,119 +0,0 @@
-<?php
-declare(strict_types=1);
-/**
- * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
- * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
- *
- * Licensed under The MIT License
- * For full copyright and license information, please see the LICENSE.txt
- * Redistributions of files must retain the above copyright notice.
- *
- * @copyright     Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
- * @link          https://cakephp.org CakePHP(tm) Project
- * @since         3.3.0
- * @license       https://opensource.org/licenses/mit-license.php MIT License
- */
-namespace Cake\Shell;
-
-use Cake\Cache\Cache;
-use Cake\Cache\Engine\ApcuEngine;
-use Cake\Cache\Engine\WincacheEngine;
-use Cake\Cache\InvalidArgumentException;
-use Cake\Console\ConsoleOptionParser;
-use Cake\Console\Shell;
-
-/**
- * Cache Shell.
- *
- * Provides a CLI interface to clear caches.
- * This tool can be used in development or by deployment scripts when changes
- * are made that require cached data to be removed.
- */
-class CacheShell extends Shell
-{
-    /**
-     * Get the option parser for this shell.
-     *
-     * @return \Cake\Console\ConsoleOptionParser
-     */
-    public function getOptionParser(): ConsoleOptionParser
-    {
-        $parser = parent::getOptionParser();
-        $parser->addSubcommand('list_prefixes', [
-            'help' => 'Show a list of all defined cache prefixes.',
-        ]);
-        $parser->addSubcommand('clear_all', [
-            'help' => 'Clear all caches.',
-        ]);
-        $parser->addSubcommand('clear', [
-            'help' => 'Clear the cache for a specified prefix.',
-            'parser' => [
-                'description' => [
-                    'Clear the cache for a particular prefix.',
-                    'For example, `cake cache clear _cake_model_` will clear the model cache',
-                    'Use `cake cache list_prefixes` to list available prefixes',
-                ],
-                'arguments' => [
-                    'prefix' => [
-                        'help' => 'The cache prefix to be cleared.',
-                        'required' => true,
-                    ],
-                ],
-            ],
-        ]);
-
-        return $parser;
-    }
-
-    /**
-     * Clear metadata.
-     *
-     * @param string|null $prefix The cache prefix to be cleared.
-     * @throws \Cake\Console\Exception\StopException
-     * @return void
-     */
-    public function clear(?string $prefix = null): void
-    {
-        try {
-            $engine = Cache::pool($prefix);
-            Cache::clear($prefix);
-            if ($engine instanceof ApcuEngine) {
-                $this->warn("ApcuEngine detected: Cleared $prefix CLI cache successfully " .
-                "but $prefix web cache must be cleared separately.");
-            } elseif ($engine instanceof WincacheEngine) {
-                $this->warn("WincacheEngine detected: Cleared $prefix CLI cache successfully " .
-                "but $prefix web cache must be cleared separately.");
-            } else {
-                $this->out("<success>Cleared $prefix cache</success>");
-            }
-        } catch (InvalidArgumentException $e) {
-            $this->abort($e->getMessage());
-        }
-    }
-
-    /**
-     * Clear metadata.
-     *
-     * @return void
-     */
-    public function clearAll(): void
-    {
-        $prefixes = Cache::configured();
-        foreach ($prefixes as $prefix) {
-            $this->clear($prefix);
-        }
-    }
-
-    /**
-     * Show a list of all defined cache prefixes.
-     *
-     * @return void
-     */
-    public function listPrefixes(): void
-    {
-        $prefixes = Cache::configured();
-        foreach ($prefixes as $prefix) {
-            $this->out($prefix);
-        }
-    }
-}

+ 35 - 8
tests/TestCase/Shell/CacheShellTest.php

@@ -13,16 +13,16 @@ declare(strict_types=1);
  * @since         3.3.0
  * @license       https://opensource.org/licenses/mit-license.php MIT License
  */
-namespace Cake\Test\TestCase\Shell;
+namespace Cake\Test\TestCase\Command;
 
 use Cake\Cache\Cache;
 use Cake\Console\Shell;
 use Cake\TestSuite\ConsoleIntegrationTestCase;
 
 /**
- * CacheShell tests.
+ * Cache Commands tests.
  */
-class CacheShellTest extends ConsoleIntegrationTestCase
+class CacheCommandsTest extends ConsoleIntegrationTestCase
 {
     /**
      * setup method
@@ -33,6 +33,8 @@ class CacheShellTest extends ConsoleIntegrationTestCase
     {
         parent::setUp();
         Cache::setConfig('test', ['engine' => 'File', 'path' => CACHE]);
+        $this->setAppNamespace();
+        $this->useCommandRunner();
     }
 
     /**
@@ -47,17 +49,42 @@ class CacheShellTest extends ConsoleIntegrationTestCase
     }
 
     /**
-     * Test that getOptionParser() returns an instance of \Cake\Console\ConsoleOptionParser
+     * Test help output
      *
      * @return void
      */
-    public function testGetOptionParser()
+    public function testClearHelp()
     {
-        $this->exec('cache -h');
+        $this->exec('cache clear -h');
 
         $this->assertExitCode(Shell::CODE_SUCCESS);
-        $this->assertOutputContains('list_prefixes');
-        $this->assertOutputContains('clear_all');
+        $this->assertOutputContains('engine to clear');
+    }
+
+    /**
+     * Test help output
+     *
+     * @return void
+     */
+    public function testClearAllHelp()
+    {
+        $this->exec('cache clear_all -h');
+
+        $this->assertExitCode(Shell::CODE_SUCCESS);
+        $this->assertOutputContains('Clear all');
+    }
+
+    /**
+     * Test help output
+     *
+     * @return void
+     */
+    public function testListHelp()
+    {
+        $this->exec('cache list -h');
+
+        $this->assertExitCode(Shell::CODE_SUCCESS);
+        $this->assertOutputContains('Show a list');
     }
 
     /**