Skip to content

Commit eb27165

Browse files
Adjust from invalidateDomain to clearCache and invalidate
1 parent 6d86331 commit eb27165

File tree

9 files changed

+282
-194
lines changed

9 files changed

+282
-194
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
"require-dev": {
3131
"php-http/guzzle6-adapter": "^1.0 || ^2.0",
3232
"php-http/message": "^1.0 || ^2.0",
33-
"mockery/mockery": "^1.0 || ^2.0",
33+
"mockery/mockery": "^1.0",
3434
"monolog/monolog": "*",
3535
"sensio/framework-extra-bundle": "^3.0 || ^4.0 || ^5.0",
3636
"symfony/browser-kit": "^3.4.4 || ^4.1.12 || ^5.0",

phpunit.xml.dist

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,6 @@
3232
<env name="KERNEL_DIR" value="./tests/Functional/Fixtures/app" />
3333
<env name="KERNEL_CLASS" value="AppKernel" />
3434
<env name="SYMFONY_DEPRECATIONS_HELPER" value="weak" />
35+
<server name="SYMFONY_PHPUNIT_VERSION" value="6.5" />
3536
</php>
3637
</phpunit>

src/Command/ClearCommand.php

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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 trigger cache invalidation by path 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+
->setName(static::$defaultName) // BC with 2.8
48+
->setDescription('Invalidate the whole http cache.')
49+
->setHelp(
50+
<<<'EOF'
51+
The <info>%command.name%</info> command invalidates the whole cache in the configured caching proxies.
52+
53+
Example:
54+
55+
<info>php %command.full_name%</info>
56+
EOF
57+
)
58+
;
59+
}
60+
61+
/**
62+
* {@inheritdoc}
63+
*/
64+
protected function execute(InputInterface $input, OutputInterface $output): int
65+
{
66+
$cacheManager = $this->getCacheManager();
67+
68+
if ($cacheManager->supports(CacheInvalidator::CLEAR)) {
69+
$this->getCacheManager()->clearCache();
70+
} elseif ($cacheManager->supports(CacheInvalidator::INVALIDATE)) {
71+
$this->getCacheManager()->invalidateRegex('.*');
72+
} else {
73+
$output->writeln(
74+
'<error>The configured http cache does not support "clear" or "invalidate".</error>'
75+
);
76+
77+
return 1;
78+
}
79+
80+
return 0;
81+
}
82+
}

src/Command/InvalidateDomainCommand.php

Lines changed: 0 additions & 86 deletions
This file was deleted.

src/Resources/config/cache_manager_commands.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
<tag name="console.command"/>
2222
</service>
2323

24-
<service id="fos_http_cache.command.invalidate_domain" class="FOS\HttpCacheBundle\Command\InvalidateDomainCommand">
24+
<service id="fos_http_cache.command.clear" class="FOS\HttpCacheBundle\Command\ClearCommand">
2525
<argument type="service" id="fos_http_cache.cache_manager" />
2626
<tag name="console.command"/>
2727
</service>
Lines changed: 91 additions & 0 deletions
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+
}

tests/Functional/Command/InvalidateDomainCommandTest.php

Lines changed: 0 additions & 45 deletions
This file was deleted.

0 commit comments

Comments
 (0)