Skip to content

Commit 8befd0a

Browse files
committed
Merge pull request #30 from Nyholm/symfony
Improved the cache:flush
2 parents 8ca3c0d + 50a6df9 commit 8befd0a

18 files changed

+161
-48
lines changed

.travis.yml

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,37 @@
11
language: php
2+
sudo: false
3+
24
php:
3-
- "7.0"
4-
- "5.6"
5-
- "5.5"
5+
- 7.0
6+
- 5.6
7+
- 5.5
8+
- hhvm
69

710
env:
11+
global:
12+
- COMPOSER_COMMAND="composer install --prefer-dist"
13+
matrix:
814
- SYMFONY_VERSION=2.6.*
915
- SYMFONY_VERSION=2.7.*
1016
- SYMFONY_VERSION=2.8.*
1117
- SYMFONY_VERSION=3.0.*
1218

1319
matrix:
1420
fast_finish: true
15-
16-
sudo: false
21+
include:
22+
- php: 5.5
23+
env: COMPOSER_COMMAND="composer update --prefer-lowest --prefer-stable" && SYMFONY_VERSION=2.7.*
1724

1825
cache:
1926
directories:
2027
- $HOME/.composer/cache
2128

22-
before_script:
29+
install_script:
2330
- composer self-update
31+
32+
install:
2433
- composer require symfony/symfony:${SYMFONY_VERSION} --no-update
25-
- composer install --dev --prefer-dist --no-interaction
34+
- travis_retry ${COMPOSER_COMMAND} --no-interaction
2635

2736
script:
2837
- php vendor/bin/phpunit

composer.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,12 @@
2626
"require": {
2727
"php": "^5.5|^7",
2828
"symfony/framework-bundle": "^2.6|^3.0",
29-
"cache/taggable-cache": "^0.1",
30-
"psr/cache-implementation": "~1.0"
29+
"cache/taggable-cache": "^0.2"
3130
},
3231
"require-dev": {
3332
"phpunit/phpunit": "^5.1|^4.0",
3433
"cache/psr-6-doctrine-bridge": "^2.0",
35-
"cache/doctrine-adapter": "^0.1"
34+
"cache/doctrine-adapter": "^0.3"
3635
},
3736
"suggest": {
3837
"cache/doctrine-adapter-bundle": "A bundle to register PSR-6 compliant services based on Doctrine cache",

src/Cache/FixedTaggingCachePool.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
/*
44
* This file is part of php-cache\cache-bundle package.
55
*
6-
* (c) 2015-2015 Aaron Scherer <aequasi@gmail.com>
6+
* (c) 2015-2015 Aaron Scherer <aequasi@gmail.com>, Tobias Nyholm <tobias.nyholm@gmail.com>
77
*
88
* This source file is subject to the MIT license that is bundled
99
* with this source code in the file LICENSE.

src/Cache/LoggingCachePool.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
/*
44
* This file is part of php-cache\cache-bundle package.
55
*
6-
* (c) 2015-2015 Aaron Scherer <aequasi@gmail.com>
6+
* (c) 2015-2015 Aaron Scherer <aequasi@gmail.com>, Tobias Nyholm <tobias.nyholm@gmail.com>
77
*
88
* This source file is subject to the MIT license that is bundled
99
* with this source code in the file LICENSE.

src/CacheBundle.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
/*
44
* This file is part of php-cache\cache-bundle package.
55
*
6-
* (c) 2015-2015 Aaron Scherer <aequasi@gmail.com>
6+
* (c) 2015-2015 Aaron Scherer <aequasi@gmail.com>, Tobias Nyholm <tobias.nyholm@gmail.com>
77
*
88
* This source file is subject to the MIT license that is bundled
99
* with this source code in the file LICENSE.

src/Command/CacheFlushCommand.php

Lines changed: 128 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
/*
44
* This file is part of php-cache\cache-bundle package.
55
*
6-
* (c) 2015-2015 Aaron Scherer <aequasi@gmail.com>
6+
* (c) 2015-2015 Aaron Scherer <aequasi@gmail.com>, Tobias Nyholm <tobias.nyholm@gmail.com>
77
*
88
* This source file is subject to the MIT license that is bundled
99
* with this source code in the file LICENSE.
@@ -13,9 +13,11 @@
1313

1414
use Cache\Taggable\TaggablePoolInterface;
1515
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
16+
use Symfony\Component\Console\Input\ArrayInput;
1617
use Symfony\Component\Console\Input\InputArgument;
1718
use Symfony\Component\Console\Input\InputInterface;
1819
use Symfony\Component\Console\Output\OutputInterface;
20+
use Symfony\Component\Console\Question\ConfirmationQuestion;
1921

2022
/**
2123
* Class CacheFlushCommand.
@@ -24,75 +26,178 @@
2426
*/
2527
class CacheFlushCommand extends ContainerAwareCommand
2628
{
29+
const validTypes = ['all', 'session', 'router', 'doctrine', 'symfony', 'provider'];
30+
2731
/**
2832
* {@inheritdoc}
2933
*/
3034
protected function configure()
3135
{
3236
$this->setName('cache:flush');
3337
$this->setDescription('Flushes the given cache');
34-
$this->addArgument('type', InputArgument::REQUIRED, 'Which type of cache do you want to clear?');
38+
$this->addArgument('type', InputArgument::OPTIONAL, sprintf('Which type of cache do you want to clear? Valid types are: %s', implode(', ', self::validTypes)));
39+
$this->addArgument('service', InputArgument::OPTIONAL, 'If using type "provider" you must give a service id for the cache you want to clear.');
40+
$this->setHelp(<<<EOD
41+
42+
Types and their description
43+
all Clear all types of caches
44+
doctrine Clear doctrine cache for query, result and metadata
45+
privider cache.acme Clear all the cache for the provider with service id "cache.acme"
46+
router Clear router cache
47+
session Clear session cache. All your logged in users will be logged out.
48+
symfony Clear Symfony cache. This is the same as cache:clear
49+
50+
EOD
51+
);
3552
}
3653

3754
/**
3855
* {@inheritdoc}
3956
*/
4057
protected function execute(InputInterface $input, OutputInterface $output)
4158
{
42-
$validTypes = ['session', 'router', 'doctrine'];
43-
$type = $input->getArgument('type');
44-
if ($type === 'all') {
45-
foreach ($validTypes as $type) {
46-
$this->clearCacheForType($type);
47-
}
59+
if (false === $type = $this->verifyArguments($input, $output)) {
60+
return 0;
61+
}
4862

49-
return;
63+
$builtInTypes = ['session', 'router', 'doctrine'];
64+
if (in_array($type, $builtInTypes)) {
65+
return $this->clearCacheForBuiltInType($type) ? 0 : 1;
5066
}
5167

52-
// If not "all", verify that $type is valid
53-
if (!in_array($type, $validTypes)) {
54-
$output->writeln(sprintf('Type "%s" does not exist. Valid type are: %s', $type, implode(',', $validTypes)));
68+
if ($type === 'symfony') {
69+
return $this->clearSymfonyCache($output) ? 0 : 1;
70+
}
5571

56-
return;
72+
if ($type === 'provider') {
73+
return $this->clearCacheForProvider($input->getArgument('service')) ? 0 : 1;
5774
}
5875

59-
$this->clearCacheForType($type);
76+
if ($type === 'all') {
77+
$result = true;
78+
foreach ($builtInTypes as $builtInType) {
79+
$result = $result && $this->clearCacheForBuiltInType($builtInType);
80+
}
81+
$result = $result && $this->clearSymfonyCache($output);
82+
83+
return $result ? 0 : 1;
84+
}
6085
}
6186

6287
/**
6388
* Clear the cache for a type.
6489
*
6590
* @param string $type
91+
*
92+
* @return bool
6693
*/
67-
private function clearCacheForType($type)
94+
private function clearCacheForBuiltInType($type)
6895
{
6996
if (!$this->getContainer()->hasParameter(sprintf('cache.%s', $type))) {
70-
return;
97+
return true;
7198
}
7299

73100
$config = $this->getContainer()->getParameter(sprintf('cache.%s', $type));
74101

75102
if ($type === 'doctrine') {
76-
$this->doClearCache($type, $config['metadata']['service_id']);
77-
$this->doClearCache($type, $config['result']['service_id']);
78-
$this->doClearCache($type, $config['query']['service_id']);
103+
$result = true;
104+
$result = $result && $this->clearTypedCacheFromService($type, $config['metadata']['service_id']);
105+
$result = $result && $this->clearTypedCacheFromService($type, $config['result']['service_id']);
106+
$result = $result && $this->clearTypedCacheFromService($type, $config['query']['service_id']);
107+
108+
return $result;
79109
} else {
80-
$this->doClearCache($type, $config['service_id']);
110+
return $this->clearTypedCacheFromService($type, $config['service_id']);
81111
}
82112
}
83113

84114
/**
85115
* @param string $type
86116
* @param string $serviceId
117+
*
118+
* @return bool
87119
*/
88-
private function doClearCache($type, $serviceId)
120+
private function clearTypedCacheFromService($type, $serviceId)
89121
{
90122
/** @type \Psr\Cache\CacheItemPoolInterface $service */
91123
$service = $this->getContainer()->get($serviceId);
92124
if ($service instanceof TaggablePoolInterface) {
93-
$service->clear([$type]);
125+
return $service->clear([$type]);
94126
} else {
95-
$service->clear();
127+
return $service->clear();
128+
}
129+
}
130+
131+
/**
132+
* @param InputInterface $input
133+
* @param OutputInterface $output
134+
*
135+
* @return string|bool type or false if invalid arguements
136+
*/
137+
protected function verifyArguments(InputInterface $input, OutputInterface $output)
138+
{
139+
$type = $input->getArgument('type');
140+
if ($type === null) {
141+
// ask a question and default $type='all'
142+
$helper = $this->getHelper('question');
143+
$question = new ConfirmationQuestion('Do you want to clear all cache? [N] ', false);
144+
145+
if (!$helper->ask($input, $output, $question)) {
146+
return false;
147+
}
148+
149+
$type = 'all';
96150
}
151+
152+
if (!in_array($type, self::validTypes)) {
153+
$output->writeln(
154+
sprintf(
155+
'<error>Type "%s" does not exist. Valid type are: %s.</error>',
156+
$type,
157+
implode(', ', self::validTypes)
158+
)
159+
);
160+
161+
return false;
162+
}
163+
164+
if ($type === 'provider' && !$input->hasArgument('service')) {
165+
$output->writeln(
166+
'<error>When using type "provider" you must specify a service id for that provider.</error>'
167+
);
168+
$output->writeln('<error>Usage: php app/console cache:flush provider cache.provider.acme</error>');
169+
170+
return false;
171+
}
172+
173+
return $type;
174+
}
175+
176+
/**
177+
* @param string $serviceId
178+
*
179+
* @return bool
180+
*/
181+
protected function clearCacheForProvider($serviceId)
182+
{
183+
/** @type \Psr\Cache\CacheItemPoolInterface $service */
184+
$service = $this->getContainer()->get($serviceId);
185+
186+
return $service->clear();
187+
}
188+
189+
/**
190+
* @param OutputInterface $output
191+
*
192+
* @return bool
193+
*/
194+
protected function clearSymfonyCache(OutputInterface $output)
195+
{
196+
$command = $this->getApplication()->find('cache:clear');
197+
$arguments = [
198+
'command' => 'cache:clear',
199+
];
200+
201+
return $command->run(new ArrayInput($arguments), $output) === 0;
97202
}
98203
}

src/DataCollector/CacheDataCollector.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
/*
44
* This file is part of php-cache\cache-bundle package.
55
*
6-
* (c) 2015-2015 Aaron Scherer <aequasi@gmail.com>
6+
* (c) 2015-2015 Aaron Scherer <aequasi@gmail.com>, Tobias Nyholm <tobias.nyholm@gmail.com>
77
*
88
* This source file is subject to the MIT license that is bundled
99
* with this source code in the file LICENSE.

src/DependencyInjection/CacheExtension.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
/*
44
* This file is part of php-cache\cache-bundle package.
55
*
6-
* (c) 2015-2015 Aaron Scherer <aequasi@gmail.com>
6+
* (c) 2015-2015 Aaron Scherer <aequasi@gmail.com>, Tobias Nyholm <tobias.nyholm@gmail.com>
77
*
88
* This source file is subject to the MIT license that is bundled
99
* with this source code in the file LICENSE.

src/DependencyInjection/Compiler/BaseCompilerPass.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
/*
44
* This file is part of php-cache\cache-bundle package.
55
*
6-
* (c) 2015-2015 Aaron Scherer <aequasi@gmail.com>
6+
* (c) 2015-2015 Aaron Scherer <aequasi@gmail.com>, Tobias Nyholm <tobias.nyholm@gmail.com>
77
*
88
* This source file is subject to the MIT license that is bundled
99
* with this source code in the file LICENSE.

src/DependencyInjection/Compiler/DataCollectorCompilerPass.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
/*
44
* This file is part of php-cache\cache-bundle package.
55
*
6-
* (c) 2015-2015 Aaron Scherer <aequasi@gmail.com>
6+
* (c) 2015-2015 Aaron Scherer <aequasi@gmail.com>, Tobias Nyholm <tobias.nyholm@gmail.com>
77
*
88
* This source file is subject to the MIT license that is bundled
99
* with this source code in the file LICENSE.

0 commit comments

Comments
 (0)