|
3 | 3 | /* |
4 | 4 | * This file is part of php-cache\cache-bundle package. |
5 | 5 | * |
6 | | - * (c) 2015-2015 Aaron Scherer <aequasi@gmail.com> |
| 6 | + * (c) 2015-2015 Aaron Scherer <aequasi@gmail.com>, Tobias Nyholm <tobias.nyholm@gmail.com> |
7 | 7 | * |
8 | 8 | * This source file is subject to the MIT license that is bundled |
9 | 9 | * with this source code in the file LICENSE. |
|
13 | 13 |
|
14 | 14 | use Cache\Taggable\TaggablePoolInterface; |
15 | 15 | use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; |
| 16 | +use Symfony\Component\Console\Input\ArrayInput; |
16 | 17 | use Symfony\Component\Console\Input\InputArgument; |
17 | 18 | use Symfony\Component\Console\Input\InputInterface; |
18 | 19 | use Symfony\Component\Console\Output\OutputInterface; |
| 20 | +use Symfony\Component\Console\Question\ConfirmationQuestion; |
19 | 21 |
|
20 | 22 | /** |
21 | 23 | * Class CacheFlushCommand. |
|
24 | 26 | */ |
25 | 27 | class CacheFlushCommand extends ContainerAwareCommand |
26 | 28 | { |
| 29 | + const validTypes = ['all', 'session', 'router', 'doctrine', 'symfony', 'provider']; |
| 30 | + |
27 | 31 | /** |
28 | 32 | * {@inheritdoc} |
29 | 33 | */ |
30 | 34 | protected function configure() |
31 | 35 | { |
32 | 36 | $this->setName('cache:flush'); |
33 | 37 | $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 | + ); |
35 | 52 | } |
36 | 53 |
|
37 | 54 | /** |
38 | 55 | * {@inheritdoc} |
39 | 56 | */ |
40 | 57 | protected function execute(InputInterface $input, OutputInterface $output) |
41 | 58 | { |
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 | + } |
48 | 62 |
|
49 | | - return; |
| 63 | + $builtInTypes = ['session', 'router', 'doctrine']; |
| 64 | + if (in_array($type, $builtInTypes)) { |
| 65 | + return $this->clearCacheForBuiltInType($type) ? 0 : 1; |
50 | 66 | } |
51 | 67 |
|
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 | + } |
55 | 71 |
|
56 | | - return; |
| 72 | + if ($type === 'provider') { |
| 73 | + return $this->clearCacheForProvider($input->getArgument('service')) ? 0 : 1; |
57 | 74 | } |
58 | 75 |
|
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 | + } |
60 | 85 | } |
61 | 86 |
|
62 | 87 | /** |
63 | 88 | * Clear the cache for a type. |
64 | 89 | * |
65 | 90 | * @param string $type |
| 91 | + * |
| 92 | + * @return bool |
66 | 93 | */ |
67 | | - private function clearCacheForType($type) |
| 94 | + private function clearCacheForBuiltInType($type) |
68 | 95 | { |
69 | 96 | if (!$this->getContainer()->hasParameter(sprintf('cache.%s', $type))) { |
70 | | - return; |
| 97 | + return true; |
71 | 98 | } |
72 | 99 |
|
73 | 100 | $config = $this->getContainer()->getParameter(sprintf('cache.%s', $type)); |
74 | 101 |
|
75 | 102 | 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; |
79 | 109 | } else { |
80 | | - $this->doClearCache($type, $config['service_id']); |
| 110 | + return $this->clearTypedCacheFromService($type, $config['service_id']); |
81 | 111 | } |
82 | 112 | } |
83 | 113 |
|
84 | 114 | /** |
85 | 115 | * @param string $type |
86 | 116 | * @param string $serviceId |
| 117 | + * |
| 118 | + * @return bool |
87 | 119 | */ |
88 | | - private function doClearCache($type, $serviceId) |
| 120 | + private function clearTypedCacheFromService($type, $serviceId) |
89 | 121 | { |
90 | 122 | /** @type \Psr\Cache\CacheItemPoolInterface $service */ |
91 | 123 | $service = $this->getContainer()->get($serviceId); |
92 | 124 | if ($service instanceof TaggablePoolInterface) { |
93 | | - $service->clear([$type]); |
| 125 | + return $service->clear([$type]); |
94 | 126 | } 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'; |
96 | 150 | } |
| 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; |
97 | 202 | } |
98 | 203 | } |
0 commit comments