From 5c210e6fd5544bc018225abd6be7c942c6d6b213 Mon Sep 17 00:00:00 2001 From: Tobias Nyholm Date: Mon, 8 Apr 2019 20:16:33 +0200 Subject: [PATCH] [Cache] Added command for list all available cache pools --- .../Bundle/FrameworkBundle/CHANGELOG.md | 1 + .../Command/CachePoolListCommand.php | 62 +++++++++++++++++++ .../Resources/config/console.xml | 5 ++ .../Functional/CachePoolListCommandTest.php | 53 ++++++++++++++++ .../DependencyInjection/CachePoolPass.php | 4 ++ 5 files changed, 125 insertions(+) create mode 100644 src/Symfony/Bundle/FrameworkBundle/Command/CachePoolListCommand.php create mode 100644 src/Symfony/Bundle/FrameworkBundle/Tests/Functional/CachePoolListCommandTest.php diff --git a/src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md b/src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md index 79aded380f..f1fbb2a895 100644 --- a/src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md +++ b/src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md @@ -31,6 +31,7 @@ CHANGELOG * Added the `messenger:setup-transports` command to setup messenger transports * Added a `InMemoryTransport` to Messenger. Use it with a DSN starting with `in-memory://`. * Added `framework.property_access.throw_exception_on_invalid_property_path` config option. + * Added `cache:pool:list` command to list all available cache pools. 4.2.0 ----- diff --git a/src/Symfony/Bundle/FrameworkBundle/Command/CachePoolListCommand.php b/src/Symfony/Bundle/FrameworkBundle/Command/CachePoolListCommand.php new file mode 100644 index 0000000000..4f399ab615 --- /dev/null +++ b/src/Symfony/Bundle/FrameworkBundle/Command/CachePoolListCommand.php @@ -0,0 +1,62 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Bundle\FrameworkBundle\Command; + +use Symfony\Component\Console\Command\Command; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Output\OutputInterface; +use Symfony\Component\Console\Style\SymfonyStyle; + +/** + * List available cache pools. + * + * @author Tobias Nyholm + */ +final class CachePoolListCommand extends Command +{ + protected static $defaultName = 'cache:pool:list'; + + private $poolNames; + + public function __construct(array $poolNames) + { + parent::__construct(); + + $this->poolNames = $poolNames; + } + + /** + * {@inheritdoc} + */ + protected function configure() + { + $this + ->setDescription('List available cache pools') + ->setHelp(<<<'EOF' +The %command.name% command lists all available cache pools. +EOF + ) + ; + } + + /** + * {@inheritdoc} + */ + protected function execute(InputInterface $input, OutputInterface $output) + { + $io = new SymfonyStyle($input, $output); + + $io->table(['Pool name'], array_map(function ($pool) { + return [$pool]; + }, $this->poolNames)); + } +} diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/config/console.xml b/src/Symfony/Bundle/FrameworkBundle/Resources/config/console.xml index 7b79664c1f..1c74220bf8 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/config/console.xml +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/config/console.xml @@ -48,6 +48,11 @@ + + + + + diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/CachePoolListCommandTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/CachePoolListCommandTest.php new file mode 100644 index 0000000000..15e7994e46 --- /dev/null +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/CachePoolListCommandTest.php @@ -0,0 +1,53 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Bundle\FrameworkBundle\Tests\Functional; + +use Symfony\Bundle\FrameworkBundle\Command\CachePoolListCommand; +use Symfony\Bundle\FrameworkBundle\Console\Application; +use Symfony\Component\Console\Tester\CommandTester; + +/** + * @group functional + */ +class CachePoolListCommandTest extends WebTestCase +{ + protected function setUp() + { + static::bootKernel(['test_case' => 'CachePools', 'root_config' => 'config.yml']); + } + + public function testListPools() + { + $tester = $this->createCommandTester(['cache.app', 'cache.system']); + $tester->execute([]); + + $this->assertSame(0, $tester->getStatusCode(), 'cache:pool:list exits with 0 in case of success'); + $this->assertContains('cache.app', $tester->getDisplay()); + $this->assertContains('cache.system', $tester->getDisplay()); + } + + public function testEmptyList() + { + $tester = $this->createCommandTester([]); + $tester->execute([]); + + $this->assertSame(0, $tester->getStatusCode(), 'cache:pool:list exits with 0 in case of success'); + } + + private function createCommandTester(array $poolNames) + { + $application = new Application(static::$kernel); + $application->add(new CachePoolListCommand($poolNames)); + + return new CommandTester($application->find('cache:pool:list')); + } +} diff --git a/src/Symfony/Component/Cache/DependencyInjection/CachePoolPass.php b/src/Symfony/Component/Cache/DependencyInjection/CachePoolPass.php index b1af39755e..1c69e10c94 100644 --- a/src/Symfony/Component/Cache/DependencyInjection/CachePoolPass.php +++ b/src/Symfony/Component/Cache/DependencyInjection/CachePoolPass.php @@ -136,6 +136,10 @@ class CachePoolPass implements CompilerPassInterface $clearer->addTag($this->cacheSystemClearerTag); } } + + if ($container->hasDefinition('console.command.cache_pool_list')) { + $container->getDefinition('console.command.cache_pool_list')->replaceArgument(0, array_keys($pools)); + } } private function getNamespace($seed, $id)