Development

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

You must first sign up to be able to contribute.

root/branches/1.1/lib/task/cache/sfCacheClearTask.class.php

Revision 16656, 7.3 kB (checked in by Kris.Wallsmith, 5 years ago)

[1.1, 1.2, 1.3] fixed invalid assumption in cache:clear task (closes #6181)

  • Property svn:mime-type set to text/x-php
  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
Line 
1 <?php
2
3 /*
4  * This file is part of the symfony package.
5  * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
6  *
7  * For the full copyright and license information, please view the LICENSE
8  * file that was distributed with this source code.
9  */
10
11 /**
12  * Clears the symfony cache.
13  *
14  * @package    symfony
15  * @subpackage task
16  * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
17  * @version    SVN: $Id$
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     // finder to find directories (1 level) in a directory
81     $dirFinder = sfFinder::type('dir')->discard('.sf')->maxdepth(0)->relative();
82
83     // iterate through applications
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       // iterate through environments
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           // default cleaning process
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     // clear global cache
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       // remove cache files
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     $this->cleanCacheFromFactoryConfig($config['i18n']['param']['cache']['class'], $config['i18n']['param']['cache']['param']);
162   }
163
164   protected function clearRoutingCache(sfApplicationConfiguration $appConfiguration)
165   {
166     $config = $this->getFactoriesConfiguration($appConfiguration);
167
168     $this->cleanCacheFromFactoryConfig($config['routing']['param']['cache']['class'], $config['routing']['param']['cache']['param']);
169   }
170
171   protected function clearTemplateCache(sfApplicationConfiguration $appConfiguration)
172   {
173     $config = $this->getFactoriesConfiguration($appConfiguration);
174
175     $this->cleanCacheFromFactoryConfig($config['view_cache']['class'], $config['view_cache']['param']);
176   }
177
178   protected function clearModuleCache(sfApplicationConfiguration $appConfiguration)
179   {
180     $subDir = sfConfig::get('sf_cache_dir').'/'.$appConfiguration->getApplication().'/'.$appConfiguration->getEnvironment().'/modules';
181
182     if (is_dir($subDir))
183     {
184       // remove cache files
185       $this->getFilesystem()->remove(sfFinder::type('file')->discard('.sf')->in($subDir));
186     }
187   }
188
189   public function getFactoriesConfiguration(sfApplicationConfiguration $appConfiguration)
190   {
191     $app = $appConfiguration->getApplication();
192     $env = $appConfiguration->getEnvironment();
193
194     if (!isset($this->config[$app]))
195     {
196       $this->config[$app] = array();
197     }
198
199     if (!isset($this->config[$app][$env]))
200     {
201       $this->config[$app][$env] = sfFactoryConfigHandler::getConfiguration($appConfiguration->getConfigPaths('config/factories.yml'));
202     }
203
204     return $this->config[$app][$env] ;
205   }
206
207   public function cleanCacheFromFactoryConfig($class, $parameters = array())
208   {
209     if ($class)
210     {
211       $cache = new $class($parameters);
212       $cache->clean();
213     }
214   }
215
216   protected function lock($app, $env)
217   {
218     // create a lock file
219     $this->getFilesystem()->touch($this->getLockFile($app, $env));
220
221     // change mode so the web user can remove it if we die
222     $this->getFilesystem()->chmod($this->getLockFile($app, $env), 0777);
223   }
224
225   protected function unlock($app, $env)
226   {
227     // release lock
228     $this->getFilesystem()->remove($this->getLockFile($app, $env));
229   }
230
231   protected function getLockFile($app, $env)
232   {
233     return sfConfig::get('sf_data_dir').'/'.$app.'_'.$env.'-cli.lck';
234   }
235 }
236
Note: See TracBrowser for help on using the browser.