| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
class sfCacheClearTask extends sfBaseTask |
|---|
| 20 |
{ |
|---|
| 21 |
protected |
|---|
| 22 |
$config = null; |
|---|
| 23 |
|
|---|
| 24 |
|
|---|
| 25 |
* @see sfTask |
|---|
| 26 |
*/ |
|---|
| 27 |
protected function configure() |
|---|
| 28 |
{ |
|---|
| 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'), |
|---|
| 33 |
)); |
|---|
| 34 |
|
|---|
| 35 |
$this->aliases = array('cc', 'clear-cache'); |
|---|
| 36 |
$this->namespace = 'cache'; |
|---|
| 37 |
$this->name = 'clear'; |
|---|
| 38 |
$this->briefDescription = 'Clears the cache'; |
|---|
| 39 |
|
|---|
| 40 |
$this->detailedDescription = <<<EOF |
|---|
| 41 |
The [cache:clear|INFO] task clears the symfony cache. |
|---|
| 42 |
|
|---|
| 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], [module|COMMENT] |
|---|
| 65 |
and [template|COMMENT]. |
|---|
| 66 |
|
|---|
| 67 |
EOF; |
|---|
| 68 |
} |
|---|
| 69 |
|
|---|
| 70 |
|
|---|
| 71 |
* @see sfTask |
|---|
| 72 |
*/ |
|---|
| 73 |
protected function execute($arguments = array(), $options = array()) |
|---|
| 74 |
{ |
|---|
| 75 |
if (!sfConfig::get('sf_cache_dir') || !is_dir(sfConfig::get('sf_cache_dir'))) |
|---|
| 76 |
{ |
|---|
| 77 |
throw new sfException(sprintf('Cache directory "%s" does not exist.', sfConfig::get('sf_cache_dir'))); |
|---|
| 78 |
} |
|---|
| 79 |
|
|---|
| 80 |
|
|---|
| 81 |
$dirFinder = sfFinder::type('dir')->discard('.sf')->maxdepth(0)->relative(); |
|---|
| 82 |
|
|---|
| 83 |
|
|---|
| 84 |
$apps = is_null($options['app']) ? $dirFinder->in(sfConfig::get('sf_apps_dir')) : array($options['app']); |
|---|
| 85 |
foreach ($apps as $app) |
|---|
| 86 |
{ |
|---|
| 87 |
$this->checkAppExists($app); |
|---|
| 88 |
|
|---|
| 89 |
if (!is_dir(sfConfig::get('sf_cache_dir').'/'.$app)) |
|---|
| 90 |
{ |
|---|
| 91 |
continue; |
|---|
| 92 |
} |
|---|
| 93 |
|
|---|
| 94 |
|
|---|
| 95 |
$envs = is_null($options['env']) ? $dirFinder->in(sfConfig::get('sf_cache_dir').'/'.$app) : array($options['env']); |
|---|
| 96 |
foreach ($envs as $env) |
|---|
| 97 |
{ |
|---|
| 98 |
if (!is_dir(sfConfig::get('sf_cache_dir').'/'.$app.'/'.$env)) |
|---|
| 99 |
{ |
|---|
| 100 |
continue; |
|---|
| 101 |
} |
|---|
| 102 |
|
|---|
| 103 |
$this->logSection('cache', sprintf('Clearing cache type "%s" for "%s" app and "%s" env', $options['type'], $app, $env)); |
|---|
| 104 |
|
|---|
| 105 |
$appConfiguration = ProjectConfiguration::getApplicationConfiguration($app, $env, true); |
|---|
| 106 |
|
|---|
| 107 |
$this->lock($app, $env); |
|---|
| 108 |
|
|---|
| 109 |
$event = $appConfiguration->getEventDispatcher()->notifyUntil(new sfEvent($this, 'task.cache.clear', array('app' => $appConfiguration, 'env' => $env, 'type' => $options['type']))); |
|---|
| 110 |
if (!$event->isProcessed()) |
|---|
| 111 |
{ |
|---|
| 112 |
|
|---|
| 113 |
$method = $this->getClearCacheMethod($options['type']); |
|---|
| 114 |
if (!method_exists($this, $method)) |
|---|
| 115 |
{ |
|---|
| 116 |
throw new InvalidArgumentException(sprintf('Do not know how to remove cache for type "%s".', $options['type'])); |
|---|
| 117 |
} |
|---|
| 118 |
$this->$method($appConfiguration); |
|---|
| 119 |
} |
|---|
| 120 |
|
|---|
| 121 |
$this->unlock($app, $env); |
|---|
| 122 |
} |
|---|
| 123 |
} |
|---|
| 124 |
|
|---|
| 125 |
|
|---|
| 126 |
if (is_null($options['app']) && 'all' == $options['type']) |
|---|
| 127 |
{ |
|---|
| 128 |
$this->getFilesystem()->remove(sfFinder::type('file')->discard('.sf')->in(sfConfig::get('sf_cache_dir'))); |
|---|
| 129 |
} |
|---|
| 130 |
} |
|---|
| 131 |
|
|---|
| 132 |
protected function getClearCacheMethod($type) |
|---|
| 133 |
{ |
|---|
| 134 |
return sprintf('clear%sCache', ucfirst($type)); |
|---|
| 135 |
} |
|---|
| 136 |
|
|---|
| 137 |
protected function clearAllCache(sfApplicationConfiguration $appConfiguration) |
|---|
| 138 |
{ |
|---|
| 139 |
$this->clearI18NCache($appConfiguration); |
|---|
| 140 |
$this->clearRoutingCache($appConfiguration); |
|---|
| 141 |
$this->clearTemplateCache($appConfiguration); |
|---|
| 142 |
$this->clearModuleCache($appConfiguration); |
|---|
| 143 |
$this->clearConfigCache($appConfiguration); |
|---|
| 144 |
} |
|---|
| 145 |
|
|---|
| 146 |
protected function clearConfigCache(sfApplicationConfiguration $appConfiguration) |
|---|
| 147 |
{ |
|---|
| 148 |
$subDir = sfConfig::get('sf_cache_dir').'/'.$appConfiguration->getApplication().'/'.$appConfiguration->getEnvironment().'/config'; |
|---|
| 149 |
|
|---|
| 150 |
if (is_dir($subDir)) |
|---|
| 151 |
{ |
|---|
| 152 |
|
|---|
| 153 |
$this->getFilesystem()->remove(sfFinder::type('file')->discard('.sf')->in($subDir)); |
|---|
| 154 |
} |
|---|
| 155 |
} |
|---|
| 156 |
|
|---|
| 157 |
protected function clearI18NCache(sfApplicationConfiguration $appConfiguration) |
|---|
| 158 |
{ |
|---|
| 159 |
$config = $this->getFactoriesConfiguration($appConfiguration); |
|---|
| 160 |
|
|---|
| 161 |
if (isset($config['i18n']['param']['cache'])) |
|---|
| 162 |
{ |
|---|
| 163 |
$this->cleanCacheFromFactoryConfig($config['i18n']['param']['cache']); |
|---|
| 164 |
} |
|---|
| 165 |
} |
|---|
| 166 |
|
|---|
| 167 |
protected function clearRoutingCache(sfApplicationConfiguration $appConfiguration) |
|---|
| 168 |
{ |
|---|
| 169 |
$config = $this->getFactoriesConfiguration($appConfiguration); |
|---|
| 170 |
|
|---|
| 171 |
if (isset($config['routing']['param']['cache'])) |
|---|
| 172 |
{ |
|---|
| 173 |
$this->cleanCacheFromFactoryConfig($config['routing']['param']['cache']); |
|---|
| 174 |
} |
|---|
| 175 |
} |
|---|
| 176 |
|
|---|
| 177 |
protected function clearTemplateCache(sfApplicationConfiguration $appConfiguration) |
|---|
| 178 |
{ |
|---|
| 179 |
$config = $this->getFactoriesConfiguration($appConfiguration); |
|---|
| 180 |
|
|---|
| 181 |
if (isset($config['view_cache'])) |
|---|
| 182 |
{ |
|---|
| 183 |
$this->cleanCacheFromFactoryConfig($config['view_cache']); |
|---|
| 184 |
} |
|---|
| 185 |
} |
|---|
| 186 |
|
|---|
| 187 |
protected function clearModuleCache(sfApplicationConfiguration $appConfiguration) |
|---|
| 188 |
{ |
|---|
| 189 |
$subDir = sfConfig::get('sf_cache_dir').'/'.$appConfiguration->getApplication().'/'.$appConfiguration->getEnvironment().'/modules'; |
|---|
| 190 |
|
|---|
| 191 |
if (is_dir($subDir)) |
|---|
| 192 |
{ |
|---|
| 193 |
|
|---|
| 194 |
$this->getFilesystem()->remove(sfFinder::type('file')->discard('.sf')->in($subDir)); |
|---|
| 195 |
} |
|---|
| 196 |
} |
|---|
| 197 |
|
|---|
| 198 |
public function getFactoriesConfiguration(sfApplicationConfiguration $appConfiguration) |
|---|
| 199 |
{ |
|---|
| 200 |
$app = $appConfiguration->getApplication(); |
|---|
| 201 |
$env = $appConfiguration->getEnvironment(); |
|---|
| 202 |
|
|---|
| 203 |
if (!isset($this->config[$app])) |
|---|
| 204 |
{ |
|---|
| 205 |
$this->config[$app] = array(); |
|---|
| 206 |
} |
|---|
| 207 |
|
|---|
| 208 |
if (!isset($this->config[$app][$env])) |
|---|
| 209 |
{ |
|---|
| 210 |
$this->config[$app][$env] = sfFactoryConfigHandler::getConfiguration($appConfiguration->getConfigPaths('config/factories.yml')); |
|---|
| 211 |
} |
|---|
| 212 |
|
|---|
| 213 |
return $this->config[$app][$env] ; |
|---|
| 214 |
} |
|---|
| 215 |
|
|---|
| 216 |
public function cleanCacheFromFactoryConfig($class, $parameters = array()) |
|---|
| 217 |
{ |
|---|
| 218 |
if ($class) |
|---|
| 219 |
{ |
|---|
| 220 |
|
|---|
| 221 |
if (is_array($class)) |
|---|
| 222 |
{ |
|---|
| 223 |
if (!isset($class['class'])) |
|---|
| 224 |
{ |
|---|
| 225 |
return; |
|---|
| 226 |
} |
|---|
| 227 |
if (isset($class['param'])) |
|---|
| 228 |
{ |
|---|
| 229 |
$parameters = $class['param']; |
|---|
| 230 |
} |
|---|
| 231 |
$class = $class['class']; |
|---|
| 232 |
} |
|---|
| 233 |
$cache = new $class($parameters); |
|---|
| 234 |
$cache->clean(); |
|---|
| 235 |
} |
|---|
| 236 |
} |
|---|
| 237 |
|
|---|
| 238 |
protected function lock($app, $env) |
|---|
| 239 |
{ |
|---|
| 240 |
|
|---|
| 241 |
$this->getFilesystem()->touch($this->getLockFile($app, $env)); |
|---|
| 242 |
|
|---|
| 243 |
|
|---|
| 244 |
$this->getFilesystem()->chmod($this->getLockFile($app, $env), 0777); |
|---|
| 245 |
} |
|---|
| 246 |
|
|---|
| 247 |
protected function unlock($app, $env) |
|---|
| 248 |
{ |
|---|
| 249 |
|
|---|
| 250 |
$this->getFilesystem()->remove($this->getLockFile($app, $env)); |
|---|
| 251 |
} |
|---|
| 252 |
|
|---|
| 253 |
protected function getLockFile($app, $env) |
|---|
| 254 |
{ |
|---|
| 255 |
return sfConfig::get('sf_data_dir').'/'.$app.'_'.$env.'-cli.lck'; |
|---|
| 256 |
} |
|---|
| 257 |
} |
|---|
| 258 |
|
|---|