sfCacheClearTask.class.php 7.3 KB

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