Skip to content

Commit 249a441

Browse files
Add invalidate domain command
1 parent 396eea2 commit 249a441

File tree

4 files changed

+197
-0
lines changed

4 files changed

+197
-0
lines changed
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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\HttpCacheBundle\CacheManager;
15+
use Symfony\Component\Console\Input\InputArgument;
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 David Buchmann <[email protected]>
23+
*/
24+
class InvalidateDomainCommand extends BaseInvalidateCommand
25+
{
26+
use PathSanityCheck;
27+
28+
protected static $defaultName = 'fos:httpcache:invalidate:domain';
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+
if (2 <= func_num_args()) {
39+
@trigger_error('Passing a command name in the constructor is deprecated and will be removed in version 3', E_USER_DEPRECATED);
40+
static::$defaultName = func_get_arg(1);
41+
}
42+
parent::__construct($cacheManager);
43+
}
44+
45+
/**
46+
* {@inheritdoc}
47+
*/
48+
protected function configure()
49+
{
50+
$this
51+
->setName(static::$defaultName) // BC with 2.8
52+
->setDescription('Invalidate a domain on all configured caching proxies')
53+
->addArgument(
54+
'domains',
55+
InputArgument::IS_ARRAY | InputArgument::REQUIRED,
56+
'URL domains you want to invalidate, you can specify any number of domains'
57+
)
58+
->setHelp(<<<'EOF'
59+
The <info>%command.name%</info> command invalidates a list of paths on the configured caching proxies.
60+
61+
Example:
62+
63+
<info>php %command.full_name% /some/path /other/path</info>
64+
EOF
65+
)
66+
;
67+
}
68+
69+
/**
70+
* {@inheritdoc}
71+
*/
72+
protected function execute(InputInterface $input, OutputInterface $output): int
73+
{
74+
$domains = $input->getArgument('domains');
75+
76+
foreach ($domains as $domain) {
77+
if ($this->looksLikeRegularExpression($domain)) {
78+
$output->writeln(sprintf('Domain %s looks like a regular expression. Invalidation requests operate on exact domains. Use regex invalidation for regular expressions.', $domain));
79+
}
80+
81+
$this->getCacheManager()->invalidateDomain($domain);
82+
}
83+
84+
return 0;
85+
}
86+
}

src/Resources/config/cache_manager_commands.xml

Lines changed: 5 additions & 0 deletions
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.invalidate_domain" class="FOS\HttpCacheBundle\Command\InvalidateDomainCommand">
25+
<argument type="service" id="fos_http_cache.cache_manager" />
26+
<tag name="console.command"/>
27+
</service>
2328
</services>
2429

2530
</container>
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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\HttpCacheBundle\CacheManager;
15+
16+
class InvalidateDomainCommandTest extends CommandTestCase
17+
{
18+
public function testExecuteVerbose()
19+
{
20+
$client = self::createClient();
21+
22+
$mock = \Mockery::mock(CacheManager::class);
23+
$mock->shouldReceive('supports')
24+
->zeroOrMoreTimes()
25+
->andReturnTrue()
26+
;
27+
$mock->shouldReceive('invalidateDomain')
28+
->once()
29+
->with('localhost')
30+
;
31+
$mock->shouldReceive('invalidateDomain')
32+
->once()
33+
->with('example.localhost')
34+
;
35+
$mock->shouldReceive('flush')
36+
->once()
37+
->andReturn(2)
38+
;
39+
$client->getContainer()->set('fos_http_cache.cache_manager', $mock);
40+
41+
$output = $this->runCommand($client, 'fos:httpcache:invalidate:domain localhost example.localhost');
42+
43+
$this->assertEquals("Sent 2 invalidation request(s)\n", $output);
44+
}
45+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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\HttpCacheBundle\CacheManager;
15+
use FOS\HttpCacheBundle\Command\InvalidateDomainCommand;
16+
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
17+
use PHPUnit\Framework\TestCase;
18+
use Symfony\Component\Console\Application;
19+
use Symfony\Component\Console\Tester\CommandTester;
20+
21+
class InvalidateDomainCommandTest extends TestCase
22+
{
23+
use MockeryPHPUnitIntegration;
24+
25+
/**
26+
* @expectedException \RuntimeException
27+
*/
28+
public function testExecuteMissingParameters()
29+
{
30+
$invalidator = \Mockery::mock(CacheManager::class);
31+
32+
$application = new Application();
33+
$application->add(new InvalidateDomainCommand($invalidator));
34+
35+
$command = $application->find('fos:httpcache:invalidate:domain');
36+
$commandTester = new CommandTester($command);
37+
$commandTester->execute(['command' => $command->getName()]);
38+
}
39+
40+
public function testExecuteParameter()
41+
{
42+
$invalidator = \Mockery::mock(CacheManager::class)
43+
->shouldReceive('invalidateDomain')->once()->with('localhost')
44+
->shouldReceive('invalidateDomain')->once()->with('example.localhost')
45+
->getMock()
46+
;
47+
48+
$application = new Application();
49+
$application->add(new InvalidateDomainCommand($invalidator));
50+
51+
$command = $application->find('fos:httpcache:invalidate:domain');
52+
$commandTester = new CommandTester($command);
53+
$commandTester->execute([
54+
'command' => $command->getName(),
55+
'domains' => ['localhost', 'example.localhost'],
56+
]);
57+
58+
// the only output should be generated by the listener in verbose mode
59+
$this->assertEquals('', $commandTester->getDisplay());
60+
}
61+
}

0 commit comments

Comments
 (0)