1. sfCacheClearTask.class.php
  2. /** * Clears the symfony cache. * * @package symfony * @subpackage task * @author Fabien Potencier * @version SVN: $Id: sfCacheClearTask.class.php 23922 2009-11-14 14:58:38Z fabien $ */
  3. class sfCacheClearTask extends sfBaseTask
  4. {
  5. protected
  6. $config = null;
  7. /**
  8. * @see sfTask
  9. */
  10. protected function configure()
  11. {
  12. $this->addOptions(array(
  13. new sfCommandOption('app', null, sfCommandOption::PARAMETER_OPTIONAL, 'The application name', null),
  14. new sfCommandOption('env', null, sfCommandOption::PARAMETER_OPTIONAL, 'The environment', null),
  15. new sfCommandOption('type', null, sfCommandOption::PARAMETER_OPTIONAL, 'The type', 'all'),
  16. ));
  17. $this->aliases = array('cc');
  18. $this->namespace = 'cache';
  19. $this->name = 'clear';
  20. $this->briefDescription = 'Clears the cache';
  21. $this->detailedDescription = <<<EOF
  22. The [cache:clear|INFO] task clears the symfony cache.
  23. By default, it removes the cache for all available types, all applications,
  24. and all environments.
  25. You can restrict by type, application, or environment:
  26. For example, to clear the [frontend|COMMENT] application cache:
  27. [./symfony cache:clear --app=frontend|INFO]
  28. To clear the cache for the [prod|COMMENT] environment for the [frontend|COMMENT] application:
  29. [./symfony cache:clear --app=frontend --env=prod|INFO]
  30. To clear the cache for all [prod|COMMENT] environments:
  31. [./symfony cache:clear --env=prod|INFO]
  32. To clear the [config|COMMENT] cache for all [prod|COMMENT] environments:
  33. [./symfony cache:clear --type=config --env=prod|INFO]
  34. The built-in types are: [config|COMMENT], [i18n|COMMENT], [routing|COMMENT], [module|COMMENT]
  35. and [template|COMMENT].
  36. EOF;
  37. }
  38. /**
  39. * @see sfTask
  40. */
  41. protected function execute($arguments = array(), $options = array())
  42. {
  43. if (!sfConfig::get('sf_cache_dir') || !is_dir(sfConfig::get('sf_cache_dir')))
  44. {
  45. throw new sfException(sprintf('Cache directory "%s" does not exist.', sfConfig::get('sf_cache_dir')));
  46. }
  47. // finder to find directories (1 level) in a directory
  48. $dirFinder = sfFinder::type('dir')->discard('.sf')->maxdepth(0)->relative();
  49. // iterate through applications
  50. $apps = null === $options['app'] ? $dirFinder->in(sfConfig::get('sf_apps_dir')) : array($options['app']);
  51. foreach ($apps as $app)
  52. {
  53. $this->checkAppExists($app);
  54. if (!is_dir(sfConfig::get('sf_cache_dir').'/'.$app))
  55. {
  56. continue;
  57. }
  58. // iterate through environments
  59. $envs = null === $options['env'] ? $dirFinder->in(sfConfig::get('sf_cache_dir').'/'.$app) : array($options['env']);
  60. foreach ($envs as $env)
  61. {
  62. if (!is_dir(sfConfig::get('sf_cache_dir').'/'.$app.'/'.$env))
  63. {
  64. continue;
  65. }
  66. $this->logSection('cache', sprintf('Clearing cache type "%s" for "%s" app and "%s" env', $options['type'], $app, $env));
  67. $appConfiguration = ProjectConfiguration::getApplicationConfiguration($app, $env, true);
  68. $this->lock($app, $env);
  69. $event = $appConfiguration->getEventDispatcher()->notifyUntil(new sfEvent($this, 'task.cache.clear', array('app' => $appConfiguration, 'env' => $env, 'type' => $options['type'])));
  70. if (!$event->isProcessed())
  71. {
  72. // default cleaning process
  73. $method = $this->getClearCacheMethod($options['type']);
  74. if (!method_exists($this, $method))
  75. {
  76. throw new InvalidArgumentException(sprintf('Do not know how to remove cache for type "%s".', $options['type']));
  77. }
  78. $this->$method($appConfiguration);
  79. }
  80. $this->unlock($app, $env);
  81. }
  82. }
  83. // clear global cache
  84. if (null === $options['app'] && 'all' == $options['type'])
  85. {
  86. $this->getFilesystem()->remove(sfFinder::type('file')->discard('.sf')->in(sfConfig::get('sf_cache_dir')));
  87. }
  88. }
  89. protected function getClearCacheMethod($type)
  90. {
  91. return sprintf('clear%sCache', ucfirst($type));
  92. }
  93. protected function clearAllCache(sfApplicationConfiguration $appConfiguration)
  94. {
  95. $this->clearI18NCache($appConfiguration);
  96. $this->clearRoutingCache($appConfiguration);
  97. $this->clearTemplateCache($appConfiguration);
  98. $this->clearModuleCache($appConfiguration);
  99. $this->clearConfigCache($appConfiguration);
  100. }
  101. protected function clearConfigCache(sfApplicationConfiguration $appConfiguration)
  102. {
  103. $subDir = sfConfig::get('sf_cache_dir').'/'.$appConfiguration->getApplication().'/'.$appConfiguration->getEnvironment().'/config';
  104. if (is_dir($subDir))
  105. {
  106. // remove cache files
  107. $this->getFilesystem()->remove(sfFinder::type('file')->discard('.sf')->in($subDir));
  108. }
  109. }
  110. protected function clearI18NCache(sfApplicationConfiguration $appConfiguration)
  111. {
  112. $config = $this->getFactoriesConfiguration($appConfiguration);
  113. if (isset($config['i18n']['param']['cache']))
  114. {
  115. $this->cleanCacheFromFactoryConfig($config['i18n']['param']['cache']);
  116. }
  117. }
  118. protected function clearRoutingCache(sfApplicationConfiguration $appConfiguration)
  119. {
  120. $config = $this->getFactoriesConfiguration($appConfiguration);
  121. if (isset($config['routing']['param']['cache']))
  122. {
  123. $this->cleanCacheFromFactoryConfig($config['routing']['param']['cache']);
  124. }
  125. }
  126. protected function clearTemplateCache(sfApplicationConfiguration $appConfiguration)
  127. {
  128. $config = $this->getFactoriesConfiguration($appConfiguration);
  129. if (isset($config['view_cache']))
  130. {
  131. $this->cleanCacheFromFactoryConfig($config['view_cache']);
  132. }
  133. }
  134. protected function clearModuleCache(sfApplicationConfiguration $appConfiguration)
  135. {
  136. $subDir = sfConfig::get('sf_cache_dir').'/'.$appConfiguration->getApplication().'/'.$appConfiguration->getEnvironment().'/modules';
  137. if (is_dir($subDir))
  138. {
  139. // remove cache files
  140. $this->getFilesystem()->remove(sfFinder::type('file')->discard('.sf')->in($subDir));
  141. }
  142. }
  143. public function getFactoriesConfiguration(sfApplicationConfiguration $appConfiguration)
  144. {
  145. $app = $appConfiguration->getApplication();
  146. $env = $appConfiguration->getEnvironment();
  147. if (!isset($this->config[$app]))
  148. {
  149. $this->config[$app] = array();
  150. }
  151. if (!isset($this->config[$app][$env]))
  152. {
  153. $this->config[$app][$env] = sfFactoryConfigHandler::getConfiguration($appConfiguration->getConfigPaths('config/factories.yml'));
  154. }
  155. return $this->config[$app][$env] ;
  156. }
  157. public function cleanCacheFromFactoryConfig($class, $parameters = array())
  158. {
  159. if ($class)
  160. {
  161. // the standard array with ['class'] and ['param'] can be passed as well
  162. if (is_array($class))
  163. {
  164. if (!isset($class['class']))
  165. {
  166. return;
  167. }
  168. if (isset($class['param']))
  169. {
  170. $parameters = $class['param'];
  171. }
  172. $class = $class['class'];
  173. }
  174. $cache = new $class($parameters);
  175. $cache->clean();
  176. }
  177. }
  178. protected function lock($app, $env)
  179. {
  180. // create a lock file
  181. $this->getFilesystem()->touch($this->getLockFile($app, $env));
  182. // change mode so the web user can remove it if we die
  183. $this->getFilesystem()->chmod($this->getLockFile($app, $env), 0777);
  184. }
  185. protected function unlock($app, $env)
  186. {
  187. // release lock
  188. $this->getFilesystem()->remove($this->getLockFile($app, $env));
  189. }
  190. protected function getLockFile($app, $env)
  191. {
  192. return sfConfig::get('sf_data_dir').'/'.$app.'_'.$env.'-cli.lck';
  193. }
  194. }

Debug toolbar