Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 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: sfCacheClearTask.class.php 23922 2009-11-14 14:58:38Z fabien $
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');
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 = 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 = 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 (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
    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
      // remove cache files
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
      // the standard array with ['class'] and ['param'] can be passed as well
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
    // create a lock file
241
    $this->getFilesystem()->touch($this->getLockFile($app, $env));
242
 
243
    // change mode so the web user can remove it if we die
244
    $this->getFilesystem()->chmod($this->getLockFile($app, $env), 0777);
245
  }
246
 
247
  protected function unlock($app, $env)
248
  {
249
    // release lock
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
}