| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 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 |
|
|---|
| 55 |
if (file_exists($target)) |
|---|
| 56 |
{ |
|---|
| 57 |
$this->getFilesystem()->remove($target); |
|---|
| 58 |
} |
|---|
| 59 |
|
|---|
| 60 |
|
|---|
| 61 |
$this->setConfiguration($this->createConfiguration($this->configuration->getApplication(), $this->configuration->getEnvironment())); |
|---|
| 62 |
|
|---|
| 63 |
|
|---|
| 64 |
sfContext::createInstance($this->configuration); |
|---|
| 65 |
|
|---|
| 66 |
|
|---|
| 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 |
|
|---|
| 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 |
|
|---|
| 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 |
|
|---|
| 171 |
$dirs = array(sfConfig::get('sf_app_module_dir')); |
|---|
| 172 |
|
|---|
| 173 |
|
|---|
| 174 |
foreach ($this->configuration->getPluginSubPaths(DIRECTORY_SEPARATOR.'modules') as $path) |
|---|
| 175 |
{ |
|---|
| 176 |
|
|---|
| 177 |
if (preg_match("#plugins".preg_quote(DIRECTORY_SEPARATOR)."([^".preg_quote(DIRECTORY_SEPARATOR)."]+)".preg_quote(DIRECTORY_SEPARATOR)."modules#", $path, $matches)) |
|---|
| 178 |
{ |
|---|
| 179 |
|
|---|
| 180 |
if (in_array($matches[1], sfConfig::get('sf_enabled_modules'))) |
|---|
| 181 |
{ |
|---|
| 182 |
$dirs[] = $path; |
|---|
| 183 |
} |
|---|
| 184 |
} |
|---|
| 185 |
} |
|---|
| 186 |
|
|---|
| 187 |
|
|---|
| 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 |
|
|---|