Skip to content

Commit b4285c5

Browse files
Add clear cache command (#544)
1 parent 74cb0e5 commit b4285c5

11 files changed

+282
-7
lines changed

CHANGELOG.md

+7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
Changelog
22
=========
33

4+
2.9.0
5+
-----
6+
7+
### Added
8+
9+
* New Feature: Command `fos:httpcache:clear` to clear the whole http cache.
10+
411
2.8.0
512
-----
613

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
],
2323
"require": {
2424
"php": "^7.2",
25-
"friendsofsymfony/http-cache": "^2.5.2",
25+
"friendsofsymfony/http-cache": "^2.6",
2626
"symfony/framework-bundle": "^3.4.26 || ^4.2.7 || ^5.0",
2727
"symfony/http-foundation": "^3.4.26 || ^4.2.7 || ^5.0",
2828
"symfony/http-kernel": "^3.4.26 || ^4.2.7 || ^5.0"

src/Command/ClearCommand.php

+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the FOSHttpCacheBundle package.
5+
*
6+
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace FOS\HttpCacheBundle\Command;
13+
14+
use FOS\HttpCache\CacheInvalidator;
15+
use FOS\HttpCacheBundle\CacheManager;
16+
use Symfony\Component\Console\Input\InputInterface;
17+
use Symfony\Component\Console\Output\OutputInterface;
18+
19+
/**
20+
* A command to clear the whole cache from the command line.
21+
*
22+
* @author Alexander Schranz <[email protected]>
23+
*/
24+
class ClearCommand extends BaseInvalidateCommand
25+
{
26+
use PathSanityCheck;
27+
28+
protected static $defaultName = 'fos:httpcache:clear';
29+
30+
/**
31+
* If no cache manager is specified explicitly, fos_http_cache.cache_manager
32+
* is automatically loaded.
33+
*
34+
* @param CacheManager|null $cacheManager The cache manager to talk to
35+
*/
36+
public function __construct(CacheManager $cacheManager = null)
37+
{
38+
parent::__construct($cacheManager);
39+
}
40+
41+
/**
42+
* {@inheritdoc}
43+
*/
44+
protected function configure()
45+
{
46+
$this
47+
->setDescription('Clear the HTTP cache.')
48+
->setHelp(
49+
<<<'EOF'
50+
The <info>%command.name%</info> command clears the whole cache or, if that is not supported, invalidates all cache entries in the configured caching proxies.
51+
52+
Example:
53+
54+
<info>php %command.full_name%</info>
55+
EOF
56+
)
57+
;
58+
}
59+
60+
/**
61+
* {@inheritdoc}
62+
*/
63+
protected function execute(InputInterface $input, OutputInterface $output): int
64+
{
65+
$cacheManager = $this->getCacheManager();
66+
67+
if ($cacheManager->supports(CacheInvalidator::CLEAR)) {
68+
$this->getCacheManager()->clearCache();
69+
70+
return 0;
71+
}
72+
73+
if ($cacheManager->supports(CacheInvalidator::INVALIDATE)) {
74+
$this->getCacheManager()->invalidateRegex('.*');
75+
76+
return 0;
77+
}
78+
79+
$output->writeln(
80+
'<error>The configured HTTP cache does not support "clear" or "invalidate".</error>'
81+
);
82+
83+
return 1;
84+
}
85+
}

src/Command/InvalidatePathCommand.php

-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ public function __construct(CacheManager $cacheManager = null)
4848
protected function configure()
4949
{
5050
$this
51-
->setName(static::$defaultName) // BC with 2.8
5251
->setDescription('Invalidate cached paths on all configured caching proxies')
5352
->addArgument(
5453
'paths',

src/Command/InvalidateRegexCommand.php

-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ public function __construct(CacheManager $cacheManager = null, $commandName = 'f
4747
protected function configure()
4848
{
4949
$this
50-
->setName(static::$defaultName) // BC with 2.8
5150
->setDescription('Invalidate everything matching a regular expression on all configured caching proxies')
5251
->addArgument(
5352
'regex',

src/Command/InvalidateTagCommand.php

-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ public function __construct(CacheManager $cacheManager = null, $commandName = 'f
4646
protected function configure()
4747
{
4848
$this
49-
->setName(static::$defaultName) // BC with 2.8
5049
->setDescription('Invalidate cached content matching the specified tags on all configured caching proxies')
5150
->addArgument(
5251
'tags',

src/Command/RefreshPathCommand.php

-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ public function __construct(CacheManager $cacheManager = null)
4848
protected function configure()
4949
{
5050
$this
51-
->setName(static::$defaultName) // BC with 2.8
5251
->setDescription('Refresh paths on all configured caching proxies')
5352
->addArgument(
5453
'paths',

src/Resources/config/cache_manager_commands.xml

+5
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@
2020
<argument type="service" id="fos_http_cache.cache_manager" />
2121
<tag name="console.command"/>
2222
</service>
23+
24+
<service id="fos_http_cache.command.clear" class="FOS\HttpCacheBundle\Command\ClearCommand">
25+
<argument type="service" id="fos_http_cache.cache_manager" />
26+
<tag name="console.command"/>
27+
</service>
2328
</services>
2429

2530
</container>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the FOSHttpCacheBundle package.
5+
*
6+
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace FOS\HttpCacheBundle\Tests\Functional\Command;
13+
14+
use FOS\HttpCache\CacheInvalidator;
15+
use FOS\HttpCacheBundle\CacheManager;
16+
17+
class ClearCommandTest extends CommandTestCase
18+
{
19+
public function testExecuteClearVerbose()
20+
{
21+
$client = self::createClient();
22+
23+
$mock = \Mockery::mock(CacheManager::class);
24+
$mock->shouldReceive('supports')
25+
->with(CacheInvalidator::CLEAR)
26+
->andReturnTrue();
27+
28+
$mock->shouldReceive('clearCache')
29+
->once()
30+
;
31+
$mock->shouldReceive('flush')
32+
->once()
33+
->andReturn(1)
34+
;
35+
$client->getContainer()->set('fos_http_cache.cache_manager', $mock);
36+
37+
$output = $this->runCommand($client, 'fos:httpcache:clear');
38+
39+
$this->assertEquals("Sent 1 invalidation request(s)\n", $output);
40+
}
41+
42+
public function testExecuteBanVerbose()
43+
{
44+
$client = self::createClient();
45+
46+
$mock = \Mockery::mock(CacheManager::class);
47+
$mock->shouldReceive('supports')
48+
->with(CacheInvalidator::CLEAR)
49+
->andReturnFalse();
50+
$mock->shouldReceive('supports')
51+
->with(CacheInvalidator::INVALIDATE)
52+
->andReturnTrue();
53+
54+
$mock->shouldReceive('invalidateRegex')
55+
->with('.*')
56+
->once()
57+
;
58+
$mock->shouldReceive('flush')
59+
->once()
60+
->andReturn(1)
61+
;
62+
$client->getContainer()->set('fos_http_cache.cache_manager', $mock);
63+
64+
$output = $this->runCommand($client, 'fos:httpcache:clear');
65+
66+
$this->assertEquals("Sent 1 invalidation request(s)\n", $output);
67+
}
68+
69+
public function testExecuteErrorVerbose()
70+
{
71+
$client = self::createClient();
72+
73+
$mock = \Mockery::mock(CacheManager::class);
74+
$mock->shouldReceive('supports')
75+
->with(CacheInvalidator::CLEAR)
76+
->andReturnFalse();
77+
$mock->shouldReceive('supports')
78+
->with(CacheInvalidator::INVALIDATE)
79+
->andReturnFalse();
80+
81+
$mock->shouldReceive('flush')
82+
->once()
83+
->andReturn(0)
84+
;
85+
$client->getContainer()->set('fos_http_cache.cache_manager', $mock);
86+
87+
$output = $this->runCommand($client, 'fos:httpcache:clear', 1);
88+
89+
$this->assertStringContainsString('The configured HTTP cache does not support "clear" or "invalidate".', $output);
90+
}
91+
}
+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the FOSHttpCacheBundle package.
5+
*
6+
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace FOS\HttpCacheBundle\Tests\Unit\Command;
13+
14+
use FOS\HttpCache\CacheInvalidator;
15+
use FOS\HttpCacheBundle\CacheManager;
16+
use FOS\HttpCacheBundle\Command\ClearCommand;
17+
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
18+
use PHPUnit\Framework\TestCase;
19+
use Symfony\Component\Console\Application;
20+
use Symfony\Component\Console\Tester\CommandTester;
21+
22+
class ClearCommandTest extends TestCase
23+
{
24+
use MockeryPHPUnitIntegration;
25+
26+
public function testExecuteClear()
27+
{
28+
$invalidator = \Mockery::mock(CacheManager::class)
29+
->shouldReceive('supports')->once()->with(CacheInvalidator::CLEAR)->andReturnTrue()
30+
->shouldReceive('clearCache')->once()
31+
->getMock()
32+
;
33+
34+
$application = new Application();
35+
$application->add(new ClearCommand($invalidator));
36+
37+
$command = $application->find('fos:httpcache:clear');
38+
$commandTester = new CommandTester($command);
39+
$commandTester->execute([
40+
'command' => $command->getName(),
41+
]);
42+
43+
// the only output should be generated by the listener in verbose mode
44+
$this->assertEquals('', $commandTester->getDisplay());
45+
}
46+
47+
public function testExecuteInvalidate()
48+
{
49+
$invalidator = \Mockery::mock(CacheManager::class)
50+
->shouldReceive('supports')->once()->with(CacheInvalidator::CLEAR)->andReturnFalse()
51+
->shouldReceive('supports')->once()->with(CacheInvalidator::INVALIDATE)->andReturnTrue()
52+
->shouldReceive('invalidateRegex')->once()->with('.*')
53+
->getMock()
54+
;
55+
56+
$application = new Application();
57+
$application->add(new ClearCommand($invalidator));
58+
59+
$command = $application->find('fos:httpcache:clear');
60+
$commandTester = new CommandTester($command);
61+
$commandTester->execute([
62+
'command' => $command->getName(),
63+
]);
64+
65+
// the only output should be generated by the listener in verbose mode
66+
$this->assertEquals('', $commandTester->getDisplay());
67+
}
68+
69+
public function testExecuteNotSupported()
70+
{
71+
$invalidator = \Mockery::mock(CacheManager::class)
72+
->shouldReceive('supports')->once()->with(CacheInvalidator::CLEAR)->andReturnFalse()
73+
->shouldReceive('supports')->once()->with(CacheInvalidator::INVALIDATE)->andReturnfalse()
74+
->getMock()
75+
;
76+
77+
$application = new Application();
78+
$application->add(new ClearCommand($invalidator));
79+
80+
$command = $application->find('fos:httpcache:clear');
81+
$commandTester = new CommandTester($command);
82+
$commandTester->execute([
83+
'command' => $command->getName(),
84+
]);
85+
86+
$this->assertStringContainsString(
87+
'The configured HTTP cache does not support "clear" or "invalidate".',
88+
$commandTester->getDisplay()
89+
);
90+
}
91+
}

tests/Unit/DependencyInjection/Compiler/HashGeneratorPassTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,9 @@ public function testConfigNoContext()
5151
$this->userContextListenerPass->process($container);
5252
if ($container->hasDefinition('service_container')) {
5353
// symfony 3.3+
54-
$this->assertCount(22, $container->getDefinitions());
54+
$this->assertCount(23, $container->getDefinitions());
5555
} else {
56-
$this->assertCount(21, $container->getDefinitions());
56+
$this->assertCount(24, $container->getDefinitions());
5757
}
5858
}
5959

0 commit comments

Comments
 (0)