php-cache /
cache-bundle
This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | |||
| 3 | /* |
||
| 4 | * This file is part of php-cache\cache-bundle package. |
||
| 5 | * |
||
| 6 | * (c) 2015 Aaron Scherer <[email protected]>, Tobias Nyholm <[email protected]> |
||
| 7 | * |
||
| 8 | * This source file is subject to the MIT license that is bundled |
||
| 9 | * with this source code in the file LICENSE. |
||
| 10 | */ |
||
| 11 | |||
| 12 | namespace Cache\CacheBundle\Command; |
||
| 13 | |||
| 14 | use Cache\Taggable\TaggablePoolInterface; |
||
| 15 | use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; |
||
| 16 | use Symfony\Component\Console\Input\ArrayInput; |
||
| 17 | use Symfony\Component\Console\Input\InputArgument; |
||
| 18 | use Symfony\Component\Console\Input\InputInterface; |
||
| 19 | use Symfony\Component\Console\Output\OutputInterface; |
||
| 20 | use Symfony\Component\Console\Question\ConfirmationQuestion; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * Class CacheFlushCommand. |
||
| 24 | * |
||
| 25 | * @author Aaron Scherer <[email protected]> |
||
| 26 | */ |
||
| 27 | class CacheFlushCommand extends ContainerAwareCommand |
||
| 28 | { |
||
| 29 | /** |
||
| 30 | * {@inheritdoc} |
||
| 31 | */ |
||
| 32 | protected function configure() |
||
| 33 | { |
||
| 34 | $this->setName('cache:flush'); |
||
| 35 | $this->setDescription('Flushes the given cache'); |
||
| 36 | $this->addArgument('type', InputArgument::OPTIONAL, sprintf('Which type of cache do you want to clear? Valid types are: %s', implode(', ', $this->getValidTypes()))); |
||
| 37 | $this->addArgument('service', InputArgument::OPTIONAL, 'If using type "provider" you must give a service id for the cache you want to clear.'); |
||
| 38 | $this->setHelp(<<<'EOD' |
||
| 39 | |||
| 40 | Types and their description |
||
| 41 | all Clear all types of caches |
||
| 42 | annotation Clear annotation cache |
||
| 43 | doctrine Clear doctrine cache for query, result and metadata |
||
| 44 | privider cache.acme Clear all the cache for the provider with service id "cache.acme" |
||
| 45 | router Clear router cache |
||
| 46 | serializer Clear serializer 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 | validation Clear validation cache |
||
| 50 | |||
| 51 | EOD |
||
| 52 | ); |
||
| 53 | } |
||
| 54 | |||
| 55 | /** |
||
| 56 | * {@inheritdoc} |
||
| 57 | */ |
||
| 58 | protected function execute(InputInterface $input, OutputInterface $output) |
||
| 59 | { |
||
| 60 | if (false === $type = $this->verifyArguments($input, $output)) { |
||
| 61 | return 0; |
||
| 62 | } |
||
| 63 | |||
| 64 | $builtInTypes = ['annotation', 'doctrine', 'serializer', 'session', 'router', 'validation']; |
||
| 65 | if (in_array($type, $builtInTypes)) { |
||
| 66 | return $this->clearCacheForBuiltInType($type) ? 0 : 1; |
||
|
0 ignored issues
–
show
|
|||
| 67 | } |
||
| 68 | |||
| 69 | if ($type === 'symfony') { |
||
| 70 | return $this->clearSymfonyCache($output) ? 0 : 1; |
||
| 71 | } |
||
| 72 | |||
| 73 | if ($type === 'provider') { |
||
| 74 | return $this->clearCacheForProvider($input->getArgument('service')) ? 0 : 1; |
||
| 75 | } |
||
| 76 | |||
| 77 | if ($type === 'all') { |
||
| 78 | $result = true; |
||
| 79 | foreach ($builtInTypes as $builtInType) { |
||
| 80 | $result = $result && $this->clearCacheForBuiltInType($builtInType); |
||
| 81 | } |
||
| 82 | $result = $result && $this->clearSymfonyCache($output); |
||
| 83 | |||
| 84 | return $result ? 0 : 1; |
||
| 85 | } |
||
| 86 | } |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Clear the cache for a type. |
||
| 90 | * |
||
| 91 | * @param string $type |
||
| 92 | * |
||
| 93 | * @return bool |
||
| 94 | */ |
||
| 95 | private function clearCacheForBuiltInType($type) |
||
| 96 | { |
||
| 97 | if (!$this->getContainer()->hasParameter(sprintf('cache.%s', $type))) { |
||
| 98 | return true; |
||
| 99 | } |
||
| 100 | |||
| 101 | $config = $this->getContainer()->getParameter(sprintf('cache.%s', $type)); |
||
| 102 | |||
| 103 | if ($type === 'doctrine') { |
||
| 104 | $result = true; |
||
| 105 | $result = $result && $this->clearTypedCacheFromService($type, $config['metadata']['service_id']); |
||
| 106 | $result = $result && $this->clearTypedCacheFromService($type, $config['result']['service_id']); |
||
| 107 | $result = $result && $this->clearTypedCacheFromService($type, $config['query']['service_id']); |
||
| 108 | |||
| 109 | return $result; |
||
| 110 | } else { |
||
| 111 | return $this->clearTypedCacheFromService($type, $config['service_id']); |
||
| 112 | } |
||
| 113 | } |
||
| 114 | |||
| 115 | /** |
||
| 116 | * @param string $type |
||
| 117 | * @param string $serviceId |
||
| 118 | * |
||
| 119 | * @return bool |
||
| 120 | */ |
||
| 121 | private function clearTypedCacheFromService($type, $serviceId) |
||
| 122 | { |
||
| 123 | /** @type \Psr\Cache\CacheItemPoolInterface $service */ |
||
| 124 | $service = $this->getContainer()->get($serviceId); |
||
| 125 | if ($service instanceof TaggablePoolInterface) { |
||
|
0 ignored issues
–
show
The class
Cache\Taggable\TaggablePoolInterface does not exist. Did you forget a USE statement, or did you not list all dependencies?
This error could be the result of: 1. Missing dependenciesPHP Analyzer uses your Are you sure this class is defined by one of your dependencies, or did you maybe
not list a dependency in either the 2. Missing use statementPHP does not complain about undefined classes in if ($x instanceof DoesNotExist) {
// Do something.
}
If you have not tested against this specific condition, such errors might go unnoticed. Loading history...
|
|||
| 126 | return $service->clearTags([$type]); |
||
| 127 | } else { |
||
| 128 | return $service->clear(); |
||
| 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 | $validTypes = $this->getValidTypes(); |
||
| 142 | if ($type === null) { |
||
| 143 | // ask a question and default $type='all' |
||
| 144 | $helper = $this->getHelper('question'); |
||
| 145 | $question = new ConfirmationQuestion('Do you want to clear all cache? [N] ', false); |
||
| 146 | |||
| 147 | if (!$helper->ask($input, $output, $question)) { |
||
| 148 | return false; |
||
| 149 | } |
||
| 150 | |||
| 151 | $type = 'all'; |
||
| 152 | } |
||
| 153 | |||
| 154 | if (!in_array($type, $validTypes)) { |
||
| 155 | $output->writeln( |
||
| 156 | sprintf( |
||
| 157 | '<error>Type "%s" does not exist. Valid type are: %s.</error>', |
||
| 158 | $type, |
||
| 159 | implode(', ', $validTypes) |
||
| 160 | ) |
||
| 161 | ); |
||
| 162 | |||
| 163 | return false; |
||
| 164 | } |
||
| 165 | |||
| 166 | if ($type === 'provider' && !$input->hasArgument('service')) { |
||
| 167 | $output->writeln( |
||
| 168 | '<error>When using type "provider" you must specify a service id for that provider.</error>' |
||
| 169 | ); |
||
| 170 | $output->writeln('<error>Usage: php app/console cache:flush provider cache.provider.acme</error>'); |
||
| 171 | |||
| 172 | return false; |
||
| 173 | } |
||
| 174 | |||
| 175 | return $type; |
||
| 176 | } |
||
| 177 | |||
| 178 | /** |
||
| 179 | * @param string $serviceId |
||
| 180 | * |
||
| 181 | * @return bool |
||
| 182 | */ |
||
| 183 | protected function clearCacheForProvider($serviceId) |
||
| 184 | { |
||
| 185 | /** @type \Psr\Cache\CacheItemPoolInterface $service */ |
||
| 186 | $service = $this->getContainer()->get($serviceId); |
||
| 187 | |||
| 188 | return $service->clear(); |
||
| 189 | } |
||
| 190 | |||
| 191 | /** |
||
| 192 | * @param OutputInterface $output |
||
| 193 | * |
||
| 194 | * @return bool |
||
| 195 | */ |
||
| 196 | protected function clearSymfonyCache(OutputInterface $output) |
||
| 197 | { |
||
| 198 | $command = $this->getApplication()->find('cache:clear'); |
||
| 199 | $arguments = [ |
||
| 200 | 'command' => 'cache:clear', |
||
| 201 | ]; |
||
| 202 | |||
| 203 | return $command->run(new ArrayInput($arguments), $output) === 0; |
||
| 204 | } |
||
| 205 | |||
| 206 | /** |
||
| 207 | * List of valid cache identifiers. |
||
| 208 | * |
||
| 209 | * @return string[] |
||
| 210 | */ |
||
| 211 | private function getValidTypes() |
||
| 212 | { |
||
| 213 | return ['all', 'annotation', 'session', 'serializer', 'router', 'doctrine', 'symfony', 'validation', 'provider']; |
||
| 214 | } |
||
| 215 | } |
||
| 216 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.