| 26 | | $this->addArguments(array( |
|---|
| 27 | | new sfCommandArgument('application', sfCommandArgument::OPTIONAL, 'The application name'), |
|---|
| 28 | | new sfCommandArgument('type', sfCommandArgument::OPTIONAL, 'The cache type to clear'), |
|---|
| | 29 | $this->addOptions(array( |
|---|
| | 30 | new sfCommandOption('app', null, sfCommandOption::PARAMETER_OPTIONAL, 'The application name', null), |
|---|
| | 31 | new sfCommandOption('env', null, sfCommandOption::PARAMETER_OPTIONAL, 'The environment', null), |
|---|
| | 32 | new sfCommandOption('type', null, sfCommandOption::PARAMETER_OPTIONAL, 'The type', 'all'), |
|---|
| 39 | | It removes all the files found in the [sf_cache_dir|COMMENT] directory |
|---|
| 40 | | ([cache/|COMMENT] by default). It does not remove directories. |
|---|
| 41 | | |
|---|
| 42 | | If it's called with an application name, it only clears the cache |
|---|
| 43 | | for the given application. |
|---|
| 44 | | |
|---|
| 45 | | For example, to clear the frontend application cache: |
|---|
| 46 | | |
|---|
| 47 | | [./symfony cache:clear frontend|INFO] |
|---|
| 48 | | |
|---|
| 49 | | If it's called with an application name and a type, it will only |
|---|
| 50 | | clears the cache for the given application and type. |
|---|
| 51 | | The symfony built-in types are: [config|COMMENT], [i18n|COMMENT] and [template|COMMENT]. |
|---|
| 52 | | |
|---|
| 53 | | So, to clear the frontend application template cache: |
|---|
| 54 | | |
|---|
| 55 | | [./symfony cache:clear frontend template|INFO] |
|---|
| | 43 | By default, it removes the cache for all available types, all applications, |
|---|
| | 44 | and all environments. |
|---|
| | 45 | |
|---|
| | 46 | You can restrict by type, application, or environment: |
|---|
| | 47 | |
|---|
| | 48 | For example, to clear the [frontend|COMMENT] application cache: |
|---|
| | 49 | |
|---|
| | 50 | [./symfony cache:clear --app=frontend|INFO] |
|---|
| | 51 | |
|---|
| | 52 | To clear the cache for the [prod|COMMENT] environment for the [frontend|COMMENT] application: |
|---|
| | 53 | |
|---|
| | 54 | [./symfony cache:clear --app=frontend --env=prod|INFO] |
|---|
| | 55 | |
|---|
| | 56 | To clear the cache for all [prod|COMMENT] environments: |
|---|
| | 57 | |
|---|
| | 58 | [./symfony cache:clear --env=prod|INFO] |
|---|
| | 59 | |
|---|
| | 60 | To clear the [config|COMMENT] cache for all [prod|COMMENT] environments: |
|---|
| | 61 | |
|---|
| | 62 | [./symfony cache:clear --type=config --env=prod|INFO] |
|---|
| | 63 | |
|---|
| | 64 | The built-in types are: [config|COMMENT], [i18n|COMMENT], [routing|COMMENT], and [template|COMMENT]. |
|---|
| 65 | | $cacheDir = sfConfig::get('sf_cache_dir'); |
|---|
| 66 | | if (!$cacheDir || !is_dir($cacheDir)) |
|---|
| 67 | | { |
|---|
| 68 | | throw new sfException(sprintf('Cache directory "%s" does not exist.', $cacheDir)); |
|---|
| 69 | | } |
|---|
| 70 | | |
|---|
| 71 | | // app |
|---|
| 72 | | $mainApp = ''; |
|---|
| 73 | | if (isset($arguments['application'])) |
|---|
| 74 | | { |
|---|
| 75 | | $mainApp = $arguments['application']; |
|---|
| 76 | | } |
|---|
| 77 | | |
|---|
| 78 | | // type (template, i18n or config) |
|---|
| 79 | | $mainType = ''; |
|---|
| 80 | | if (isset($arguments['type'])) |
|---|
| 81 | | { |
|---|
| 82 | | $mainType = $arguments['type']; |
|---|
| 83 | | } |
|---|
| 84 | | |
|---|
| 85 | | // declare type that must be cleaned safely (with a lock file during cleaning) |
|---|
| 86 | | $safeTypes = array('config', 'i18n'); |
|---|
| 87 | | |
|---|
| 88 | | // finder to remove all files in a cache directory |
|---|
| 89 | | $finder = sfFinder::type('file')->ignore_version_control()->discard('.sf'); |
|---|
| | 74 | if (!sfConfig::get('sf_cache_dir') || !is_dir(sfConfig::get('sf_cache_dir'))) |
|---|
| | 75 | { |
|---|
| | 76 | throw new sfException(sprintf('Cache directory "%s" does not exist.', sfConfig::get('sf_cache_dir'))); |
|---|
| | 77 | } |
|---|
| 155 | | /** |
|---|
| 156 | | * Removes a directory safely. |
|---|
| 157 | | * |
|---|
| 158 | | * @param object $finder |
|---|
| 159 | | * @param string $subDir |
|---|
| 160 | | * @param string $lockName |
|---|
| 161 | | */ |
|---|
| 162 | | protected function safeCacheRemove($finder, $subDir, $lockName) |
|---|
| | 131 | protected function getClearCacheMethod($type) |
|---|
| | 132 | { |
|---|
| | 133 | return sprintf('clear%sCache', ucfirst($type)); |
|---|
| | 134 | } |
|---|
| | 135 | |
|---|
| | 136 | protected function clearAllCache(sfApplicationConfiguration $appConfiguration, $env) |
|---|
| | 137 | { |
|---|
| | 138 | $this->clearI18NCache($appConfiguration, $env); |
|---|
| | 139 | $this->clearRoutingCache($appConfiguration, $env); |
|---|
| | 140 | $this->clearTemplateCache($appConfiguration, $env); |
|---|
| | 141 | $this->clearConfigCache($appConfiguration, $env); |
|---|
| | 142 | } |
|---|
| | 143 | |
|---|
| | 144 | protected function clearConfigCache(sfApplicationConfiguration $appConfiguration, $env) |
|---|
| | 145 | { |
|---|
| | 146 | $subDir = sfConfig::get('sf_cache_dir').'/'.$appConfiguration->getApplication().'/'.$env.'/config'; |
|---|
| | 147 | if (is_dir($subDir)) |
|---|
| | 148 | { |
|---|
| | 149 | // remove cache files |
|---|
| | 150 | $this->getFilesystem()->remove(sfFinder::type('file')->ignore_version_control()->discard('.sf')->in($subDir)); |
|---|
| | 151 | } |
|---|
| | 152 | } |
|---|
| | 153 | |
|---|
| | 154 | protected function clearI18NCache(sfApplicationConfiguration $appConfiguration, $env) |
|---|
| | 155 | { |
|---|
| | 156 | $config = $this->getFactoriesConfiguration($appConfiguration); |
|---|
| | 157 | |
|---|
| | 158 | $this->cleanCacheFromFactoryConfig($config['i18n']['param']['cache']['class'], $config['i18n']['param']['cache']['param']); |
|---|
| | 159 | } |
|---|
| | 160 | |
|---|
| | 161 | protected function clearRoutingCache(sfApplicationConfiguration $appConfiguration, $env) |
|---|
| | 162 | { |
|---|
| | 163 | $config = $this->getFactoriesConfiguration($appConfiguration); |
|---|
| | 164 | |
|---|
| | 165 | $this->cleanCacheFromFactoryConfig($config['routing']['param']['cache']['class'], $config['routing']['param']['cache']['param']); |
|---|
| | 166 | } |
|---|
| | 167 | |
|---|
| | 168 | protected function clearTemplateCache(sfApplicationConfiguration $appConfiguration, $env) |
|---|
| | 169 | { |
|---|
| | 170 | $config = $this->getFactoriesConfiguration($appConfiguration); |
|---|
| | 171 | |
|---|
| | 172 | $this->cleanCacheFromFactoryConfig($config['view_cache']['class'], $config['view_cache']['param']); |
|---|
| | 173 | } |
|---|
| | 174 | |
|---|
| | 175 | public function getFactoriesConfiguration(sfApplicationConfiguration $appConfiguration) |
|---|
| | 176 | { |
|---|
| | 177 | if (is_null($this->config)) |
|---|
| | 178 | { |
|---|
| | 179 | $this->config = sfFactoryConfigHandler::getConfiguration($appConfiguration->getConfigPaths('config/factories.yml')); |
|---|
| | 180 | } |
|---|
| | 181 | |
|---|
| | 182 | return $this->config; |
|---|
| | 183 | } |
|---|
| | 184 | |
|---|
| | 185 | public function cleanCacheFromFactoryConfig($class, $parameters = array()) |
|---|
| | 186 | { |
|---|
| | 187 | $cache = new $class($parameters); |
|---|
| | 188 | $cache->clean(); |
|---|
| | 189 | } |
|---|
| | 190 | |
|---|
| | 191 | protected function lock($app, $env) |
|---|