|
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 | + * |
| 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'; |
96 | 151 | }
|
| 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; |
97 | 203 | }
|
98 | 204 | }
|
0 commit comments