feature #31021 [Cache] Added command for list all available cache pools (Nyholm)

This PR was squashed before being merged into the 4.3-dev branch (closes #31021).

Discussion
----------

[Cache] Added command for list all available cache pools

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | https://github.com/symfony/symfony-docs/issues/9782
| License       | MIT
| Doc PR        |

Commits
-------

5c210e6fd5 [Cache] Added command for list all available cache pools
This commit is contained in:
Nicolas Grekas 2019-04-10 13:38:41 +02:00
commit b09dfd9d8e
5 changed files with 125 additions and 0 deletions

View File

@ -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
-----

View File

@ -0,0 +1,62 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* 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 <tobias.nyholm@gmail.com>
*/
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 <info>%command.name%</info> 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));
}
}

View File

@ -48,6 +48,11 @@
<tag name="console.command" command="cache:pool:delete" />
</service>
<service id="console.command.cache_pool_list" class="Symfony\Bundle\FrameworkBundle\Command\CachePoolListCommand">
<argument /> <!-- Pool names -->
<tag name="console.command" command="cache:pool:list" />
</service>
<service id="console.command.cache_warmup" class="Symfony\Bundle\FrameworkBundle\Command\CacheWarmupCommand">
<argument type="service" id="cache_warmer" />
<tag name="console.command" command="cache:warmup" />

View File

@ -0,0 +1,53 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* 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'));
}
}

View File

@ -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)