PageRenderTime 51ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/task/cache/sfCacheClearTask.class.php

https://github.com/bheneka/gitta
PHP | 257 lines | 176 code | 49 blank | 32 comment | 25 complexity | f7907a739738f7ff8b7e53a8dc3c160b MD5 | raw file
  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$
  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');
  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 = 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 = 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 (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. if (isset($config['i18n']['param']['cache']))
  128. {
  129. $this->cleanCacheFromFactoryConfig($config['i18n']['param']['cache']);
  130. }
  131. }
  132. protected function clearRoutingCache(sfApplicationConfiguration $appConfiguration)
  133. {
  134. $config = $this->getFactoriesConfiguration($appConfiguration);
  135. if (isset($config['routing']['param']['cache']))
  136. {
  137. $this->cleanCacheFromFactoryConfig($config['routing']['param']['cache']);
  138. }
  139. }
  140. protected function clearTemplateCache(sfApplicationConfiguration $appConfiguration)
  141. {
  142. $config = $this->getFactoriesConfiguration($appConfiguration);
  143. if (isset($config['view_cache']))
  144. {
  145. $this->cleanCacheFromFactoryConfig($config['view_cache']);
  146. }
  147. }
  148. protected function clearModuleCache(sfApplicationConfiguration $appConfiguration)
  149. {
  150. $subDir = sfConfig::get('sf_cache_dir').'/'.$appConfiguration->getApplication().'/'.$appConfiguration->getEnvironment().'/modules';
  151. if (is_dir($subDir))
  152. {
  153. // remove cache files
  154. $this->getFilesystem()->remove(sfFinder::type('file')->discard('.sf')->in($subDir));
  155. }
  156. }
  157. public function getFactoriesConfiguration(sfApplicationConfiguration $appConfiguration)
  158. {
  159. $app = $appConfiguration->getApplication();
  160. $env = $appConfiguration->getEnvironment();
  161. if (!isset($this->config[$app]))
  162. {
  163. $this->config[$app] = array();
  164. }
  165. if (!isset($this->config[$app][$env]))
  166. {
  167. $this->config[$app][$env] = sfFactoryConfigHandler::getConfiguration($appConfiguration->getConfigPaths('config/factories.yml'));
  168. }
  169. return $this->config[$app][$env] ;
  170. }
  171. public function cleanCacheFromFactoryConfig($class, $parameters = array())
  172. {
  173. if ($class)
  174. {
  175. // the standard array with ['class'] and ['param'] can be passed as well
  176. if (is_array($class))
  177. {
  178. if (!isset($class['class']))
  179. {
  180. return;
  181. }
  182. if (isset($class['param']))
  183. {
  184. $parameters = $class['param'];
  185. }
  186. $class = $class['class'];
  187. }
  188. $cache = new $class($parameters);
  189. $cache->clean();
  190. }
  191. }
  192. protected function lock($app, $env)
  193. {
  194. // create a lock file
  195. $this->getFilesystem()->touch($this->getLockFile($app, $env));
  196. // change mode so the web user can remove it if we die
  197. $this->getFilesystem()->chmod($this->getLockFile($app, $env), 0777);
  198. }
  199. protected function unlock($app, $env)
  200. {
  201. // release lock
  202. $this->getFilesystem()->remove($this->getLockFile($app, $env));
  203. }
  204. protected function getLockFile($app, $env)
  205. {
  206. return sfConfig::get('sf_data_dir').'/'.$app.'_'.$env.'-cli.lck';
  207. }
  208. }