Skip to content

Commit fc6f5d3

Browse files
committed
Improved the command to include symfony
1 parent 8ca3c0d commit fc6f5d3

File tree

1 file changed

+128
-22
lines changed

1 file changed

+128
-22
lines changed

src/Command/CacheFlushCommand.php

Lines changed: 128 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -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,179 @@
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+
*
133+
* @param InputInterface $input
134+
* @param OutputInterface $output
135+
*
136+
* @return string|bool type or false if invalid arguements
137+
*/
138+
protected function verifyArguments(InputInterface $input, OutputInterface $output)
139+
{
140+
$type = $input->getArgument('type');
141+
if ($type === null) {
142+
// ask a question and default $type='all'
143+
$helper = $this->getHelper('question');
144+
$question = new ConfirmationQuestion('Do you want to clear all cache? [N] ', false);
145+
146+
if (!$helper->ask($input, $output, $question)) {
147+
return false;
148+
}
149+
150+
$type = 'all';
96151
}
152+
153+
if (!in_array($type, self::validTypes)) {
154+
$output->writeln(
155+
sprintf(
156+
'<error>Type "%s" does not exist. Valid type are: %s.</error>',
157+
$type,
158+
implode(', ', self::validTypes)
159+
)
160+
);
161+
162+
return false;
163+
}
164+
165+
if ($type === 'provider' && !$input->hasArgument('service')) {
166+
$output->writeln(
167+
'<error>When using type "provider" you must specify a service id for that provider.</error>'
168+
);
169+
$output->writeln('<error>Usage: php app/console cache:flush provider cache.provider.acme</error>');
170+
171+
return false;
172+
}
173+
174+
return $type;
175+
}
176+
177+
/**
178+
* @param string $serviceId
179+
*
180+
* @return bool
181+
*/
182+
protected function clearCacheForProvider($serviceId)
183+
{
184+
/** @type \Psr\Cache\CacheItemPoolInterface $service */
185+
$service = $this->getContainer()->get($serviceId);
186+
187+
return $service->clear();
188+
}
189+
190+
/**
191+
* @param OutputInterface $output
192+
*
193+
* @return bool
194+
*/
195+
protected function clearSymfonyCache(OutputInterface $output)
196+
{
197+
$command = $this->getApplication()->find('cache:clear');
198+
$arguments = array(
199+
'command' => 'cache:clear',
200+
);
201+
202+
return $command->run(new ArrayInput($arguments), $output) === 0;
97203
}
98204
}

0 commit comments

Comments
 (0)