Development

/branches/1.3/lib/task/project/sfProjectOptimizeTask.class.php

You must first sign up to be able to contribute.

root/branches/1.3/lib/task/project/sfProjectOptimizeTask.class.php

Revision 29415, 5.1 kB (checked in by fabien, 4 years ago)

[1.3, 1.4] fixed sfProjectOptimizeTask on Windows (closes #8640)

  • 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  * Optimizes a project for better performance.
13  *
14  * @package    symfony
15  * @subpackage task
16  * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
17  * @version    SVN: $Id$
18  */
19 class sfProjectOptimizeTask extends sfBaseTask
20 {
21   /**
22    * @see sfTask
23    */
24   protected function configure()
25   {
26     $this->addArguments(array(
27       new sfCommandArgument('application', sfCommandArgument::REQUIRED, 'The application name'),
28       new sfCommandArgument('env', sfCommandArgument::OPTIONAL, 'The environment name', 'prod'),
29     ));
30
31     $this->namespace = 'project';
32     $this->name = 'optimize';
33     $this->briefDescription = 'Optimizes a project for better performance';
34
35     $this->detailedDescription = <<<EOF
36 The [project:optimize|INFO] optimizes a project for better performance:
37
38   [./symfony project:optimize frontend prod|INFO]
39
40 This task should only be used on a production server. Don't forget to re-run
41 the task each time the project changes.
42 EOF;
43   }
44
45   /**
46    * @see sfTask
47    */
48   protected function execute($arguments = array(), $options = array())
49   {
50     $data = array();
51     $modules = $this->findModules();
52     $target = sfConfig::get('sf_cache_dir').'/'.$arguments['application'].'/'.$arguments['env'].'/config/configuration.php';
53
54     // remove existing optimization file
55     if (file_exists($target))
56     {
57       $this->getFilesystem()->remove($target);
58     }
59
60     // recreate configuration without the cache
61     $this->setConfiguration($this->createConfiguration($this->configuration->getApplication(), $this->configuration->getEnvironment()));
62
63     // initialize the context
64     sfContext::createInstance($this->configuration);
65
66     // force cache generation for generated modules
67     foreach ($modules as $module)
68     {
69       $this->configuration->getConfigCache()->import('modules/'.$module.'/config/generator.yml', false, true);
70     }
71
72     $templates = $this->findTemplates($modules);
73
74     $data['getTemplateDir'] = $this->optimizeGetTemplateDir($modules, $templates);
75     $data['getControllerDirs'] = $this->optimizeGetControllerDirs($modules);
76     $data['getPluginPaths'] = $this->configuration->getPluginPaths();
77     $data['loadHelpers'] = $this->optimizeLoadHelpers($modules);
78
79     if (!file_exists($directory = dirname($target)))
80     {
81       $this->getFilesystem()->mkdirs($directory);
82     }
83
84     $this->logSection('file+', $target);
85     file_put_contents($target, '<?php return '.var_export($data, true).';');
86   }
87
88   protected function optimizeGetControllerDirs($modules)
89   {
90     $data = array();
91     foreach ($modules as $module)
92     {
93       $data[$module] = $this->configuration->getControllerDirs($module);
94     }
95
96     return $data;
97   }
98
99   protected function optimizeGetTemplateDir($modules, $templates)
100   {
101     $data = array();
102     foreach ($modules as $module)
103     {
104       $data[$module] = array();
105       foreach ($templates[$module] as $template)
106       {
107         if (null !== $dir = $this->configuration->getTemplateDir($module, $template))
108         {
109           $data[$module][$template] = $dir;
110         }
111       }
112     }
113
114     return $data;
115   }
116
117   protected function optimizeLoadHelpers($modules)
118   {
119     $data = array();
120
121     $finder = sfFinder::type('file')->name('*Helper.php');
122
123     // module helpers
124     foreach ($modules as $module)
125     {
126       $helpers = array();
127
128       $dirs = $this->configuration->getHelperDirs($module);
129       foreach ($finder->in($dirs[0]) as $file)
130       {
131         $helpers[basename($file, 'Helper.php')] = $file;
132       }
133
134       if (count($helpers))
135       {
136         $data[$module] = $helpers;
137       }
138     }
139
140     // all other helpers
141     foreach ($this->configuration->getHelperDirs() as $dir)
142     {
143       foreach ($finder->in($dir) as $file)
144       {
145         $helper = basename($file, 'Helper.php');
146         if (!isset($data[''][$helper]))
147         {
148           $data[''][$helper] = $file;
149         }
150       }
151     }
152
153     return $data;
154   }
155
156   protected function findTemplates($modules)
157   {
158     $files = array();
159
160     foreach ($modules as $module)
161     {
162       $files[$module] = sfFinder::type('file')->follow_link()->relative()->in($this->configuration->getTemplateDirs($module));
163     }
164
165     return $files;
166   }
167
168   protected function findModules()
169   {
170     // application
171     $dirs = array(sfConfig::get('sf_app_module_dir'));
172
173     // plugins
174     foreach ($this->configuration->getPluginSubPaths(DIRECTORY_SEPARATOR.'modules') as $path)
175     {
176       // parse out the plugin name
177       if (preg_match("#plugins".preg_quote(DIRECTORY_SEPARATOR)."([^".preg_quote(DIRECTORY_SEPARATOR)."]+)".preg_quote(DIRECTORY_SEPARATOR)."modules#", $path, $matches))
178       {
179         // plugin module enabled?
180         if (in_array($matches[1], sfConfig::get('sf_enabled_modules')))
181         {
182           $dirs[] = $path;
183         }
184       }
185     }
186
187     // core modules
188     $dirs[] = sfConfig::get('sf_symfony_lib_dir').'/controller';
189
190     return array_unique(sfFinder::type('dir')->maxdepth(0)->follow_link()->relative()->in($dirs));
191   }
192 }
193
Note: See TracBrowser for help on using the browser.