-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[FrameworkBundle] Allow clearing private cache pools in cache:pool:clear #20810
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
<?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\CachePoolClearCommand; | ||
use Symfony\Component\Console\Tester\CommandTester; | ||
|
||
/** | ||
* @group functional | ||
*/ | ||
class CachePoolClearCommandTest extends WebTestCase | ||
{ | ||
private $application; | ||
|
||
protected function setUp() | ||
{ | ||
static::bootKernel(array('test_case' => 'CachePoolClear', 'root_config' => 'config.yml')); | ||
} | ||
|
||
public function testClearPrivatePool() | ||
{ | ||
$tester = $this->createCommandTester(); | ||
$tester->execute(array('pools' => array('cache.private_pool')), array('decorated' => false)); | ||
|
||
$this->assertSame(0, $tester->getStatusCode(), 'cache:pool:clear exits with 0 in case of success'); | ||
$this->assertContains('Clearing cache pool: cache.private_pool', $tester->getDisplay()); | ||
$this->assertContains('[OK] Cache was successfully cleared.', $tester->getDisplay()); | ||
} | ||
|
||
public function testClearPublicPool() | ||
{ | ||
$tester = $this->createCommandTester(); | ||
$tester->execute(array('pools' => array('cache.public_pool')), array('decorated' => false)); | ||
|
||
$this->assertSame(0, $tester->getStatusCode(), 'cache:pool:clear exits with 0 in case of success'); | ||
$this->assertContains('Clearing cache pool: cache.public_pool', $tester->getDisplay()); | ||
$this->assertContains('[OK] Cache was successfully cleared.', $tester->getDisplay()); | ||
} | ||
|
||
public function testClearPoolWithCustomClearer() | ||
{ | ||
$tester = $this->createCommandTester(); | ||
$tester->execute(array('pools' => array('cache.pool_with_clearer')), array('decorated' => false)); | ||
|
||
$this->assertSame(0, $tester->getStatusCode(), 'cache:pool:clear exits with 0 in case of success'); | ||
$this->assertContains('Clearing cache pool: cache.pool_with_clearer', $tester->getDisplay()); | ||
$this->assertContains('[OK] Cache was successfully cleared.', $tester->getDisplay()); | ||
} | ||
|
||
public function testCallClearer() | ||
{ | ||
$tester = $this->createCommandTester(); | ||
$tester->execute(array('pools' => array('cache.default_clearer')), array('decorated' => false)); | ||
|
||
$this->assertSame(0, $tester->getStatusCode(), 'cache:pool:clear exits with 0 in case of success'); | ||
$this->assertContains('Calling cache clearer: cache.default_clearer', $tester->getDisplay()); | ||
$this->assertContains('[OK] Cache was successfully cleared.', $tester->getDisplay()); | ||
} | ||
|
||
/** | ||
* @expectedException \Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException | ||
* @expectedExceptionMessage You have requested a non-existent service "unknown_pool" | ||
*/ | ||
public function testClearUnexistingPool() | ||
{ | ||
$this->createCommandTester() | ||
->execute(array('pools' => array('unknown_pool')), array('decorated' => false)); | ||
} | ||
|
||
private function createCommandTester() | ||
{ | ||
$command = new CachePoolClearCommand(); | ||
$command->setContainer(static::$kernel->getContainer()); | ||
|
||
return new CommandTester($command); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?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. | ||
*/ | ||
|
||
use Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\TestBundle; | ||
use Symfony\Bundle\FrameworkBundle\FrameworkBundle; | ||
|
||
return array( | ||
new FrameworkBundle(), | ||
new TestBundle(), | ||
); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
imports: | ||
- { resource: ../config/default.yml } | ||
|
||
services: | ||
dummy: | ||
class: Symfony\Bundle\FrameworkBundle\Tests\Fixtures\DeclaredClass | ||
arguments: ['@cache.private_pool'] | ||
custom_clearer: | ||
parent: cache.default_clearer | ||
tags: | ||
- name: kernel.cache_clearer | ||
|
||
framework: | ||
cache: | ||
pools: | ||
cache.private_pool: ~ | ||
cache.public_pool: | ||
public: true | ||
cache.pool_with_clearer: | ||
public: true | ||
clearer: custom_clearer |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,11 +20,32 @@ class Psr6CacheClearer implements CacheClearerInterface | |
{ | ||
private $pools = array(); | ||
|
||
public function __construct(array $pools = array()) | ||
{ | ||
$this->pools = $pools; | ||
} | ||
|
||
public function addPool(CacheItemPoolInterface $pool) | ||
{ | ||
@trigger_error(sprintf('The %s() method is deprecated since version 3.3 and will be removed in 4.0. Pass an array of pools indexed by name to the constructor instead.', __METHOD__), E_USER_DEPRECATED); | ||
|
||
$this->pools[] = $pool; | ||
} | ||
|
||
public function hasPool($name) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This method is useless to me: let clearPool throw, that's way enough. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's convenient to use it in the command since we want to try fetching the pool from the container if the clearer can't find it. I'll first add the global clearer and come back for this point |
||
{ | ||
return isset($this->pools[$name]); | ||
} | ||
|
||
public function clearPool($name) | ||
{ | ||
if (!isset($this->pools[$name])) { | ||
throw new \InvalidArgumentException(sprintf('Cache pool not found: %s.', $name)); | ||
} | ||
|
||
return $this->pools[$name]->clear(); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now I understand why you needed hasPool :)
This can throw in the middle of a clean because of a missing pool.
Then I propose to replace clearPool($pool) by
PsrCacheClearer::getPools()
and handle validation with the resulting array.WDYT?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Either we keep
hasPool()/clearPool()
or we get all the pools and let the caller doing the filtering logic as you propose, it's debatable but I would prefer keep the clearer being a clearer rather than an accessor. As you prefer :)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let's go for hasPool :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done!