| [ Index ] |
|
Code source de Symfony 1.0.0 |
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 * sfViewConfigHandler allows you to configure views. 13 * 14 * @package symfony 15 * @subpackage config 16 * @author Fabien Potencier <fabien.potencier@symfony-project.com> 17 * @version SVN: $Id: sfViewConfigHandler.class.php 3289 2007-01-15 21:28:51Z fabien $ 18 */ 19 class sfViewConfigHandler extends sfYamlConfigHandler 20 { 21 /** 22 * Executes this configuration handler. 23 * 24 * @param array An array of absolute filesystem path to a configuration file 25 * 26 * @return string Data to be written to a cache file 27 * 28 * @throws <b>sfConfigurationException</b> If a requested configuration file does not exist or is not readable 29 * @throws <b>sfParseException</b> If a requested configuration file is improperly formatted 30 * @throws <b>sfInitializationException</b> If a view.yml key check fails 31 */ 32 public function execute($configFiles) 33 { 34 // set our required categories list and initialize our handler 35 $categories = array('required_categories' => array()); 36 $this->initialize($categories); 37 38 // parse the yaml 39 $this->mergeConfig($this->parseYamls($configFiles)); 40 41 // init our data array 42 $data = array(); 43 44 $data[] = "\$context = \$this->getContext();\n"; 45 $data[] = "\$response = \$context->getResponse();\n\n"; 46 47 // first pass: iterate through all view names to determine the real view name 48 $first = true; 49 foreach ($this->yamlConfig as $viewName => $values) 50 { 51 if ($viewName == 'all') 52 { 53 continue; 54 } 55 56 $data[] = ($first ? '' : 'else ')."if (\$this->actionName.\$this->viewName == '$viewName')\n". 57 "{\n"; 58 $data[] = $this->addTemplate($viewName); 59 $data[] = "}\n"; 60 61 $first = false; 62 } 63 64 // general view configuration 65 $data[] = ($first ? '' : "else\n{")."\n"; 66 $data[] = $this->addTemplate($viewName); 67 $data[] = ($first ? '' : "}")."\n\n"; 68 69 // second pass: iterate through all real view names 70 $first = true; 71 foreach ($this->yamlConfig as $viewName => $values) 72 { 73 if ($viewName == 'all') 74 { 75 continue; 76 } 77 78 $data[] = ($first ? '' : 'else ')."if (\$templateName.\$this->viewName == '$viewName')\n". 79 "{\n"; 80 81 $data[] = $this->addLayout($viewName); 82 $data[] = $this->addComponentSlots($viewName); 83 $data[] = $this->addHtmlHead($viewName); 84 $data[] = $this->addEscaping($viewName); 85 86 $data[] = $this->addHtmlAsset($viewName); 87 88 $data[] = "}\n"; 89 90 $first = false; 91 } 92 93 // general view configuration 94 $data[] = ($first ? '' : "else\n{")."\n"; 95 96 $data[] = $this->addLayout(); 97 $data[] = $this->addComponentSlots(); 98 $data[] = $this->addHtmlHead(); 99 $data[] = $this->addEscaping(); 100 101 $data[] = $this->addHtmlAsset(); 102 $data[] = ($first ? '' : "}")."\n"; 103 104 // compile data 105 $retval = sprintf("<?php\n". 106 "// auto-generated by sfViewConfigHandler\n". 107 "// date: %s\n%s\n", 108 date('Y/m/d H:i:s'), implode('', $data)); 109 110 return $retval; 111 } 112 113 /** 114 * Merges assets and environement configuration. 115 * 116 * @param array A configuration array 117 */ 118 protected function mergeConfig($myConfig) 119 { 120 // merge javascripts and stylesheets 121 $myConfig['all']['stylesheets'] = array_merge(isset($myConfig['default']['stylesheets']) && is_array($myConfig['default']['stylesheets']) ? $myConfig['default']['stylesheets'] : array(), isset($myConfig['all']['stylesheets']) && is_array($myConfig['all']['stylesheets']) ? $myConfig['all']['stylesheets'] : array()); 122 unset($myConfig['default']['stylesheets']); 123 124 $myConfig['all']['javascripts'] = array_merge(isset($myConfig['default']['javascripts']) && is_array($myConfig['default']['javascripts']) ? $myConfig['default']['javascripts'] : array(), isset($myConfig['all']['javascripts']) && is_array($myConfig['all']['javascripts']) ? $myConfig['all']['javascripts'] : array()); 125 unset($myConfig['default']['javascripts']); 126 127 // merge default and all 128 $myConfig['all'] = sfToolkit::arrayDeepMerge( 129 isset($myConfig['default']) && is_array($myConfig['default']) ? $myConfig['default'] : array(), 130 isset($myConfig['all']) && is_array($myConfig['all']) ? $myConfig['all'] : array() 131 ); 132 133 unset($myConfig['default']); 134 135 $this->yamlConfig = $myConfig; 136 } 137 138 /** 139 * Adds a component slot statement to the data. 140 * 141 * @param string The view name 142 * 143 * @return string The PHP statement 144 */ 145 protected function addComponentSlots($viewName = '') 146 { 147 $data = ''; 148 149 $components = $this->mergeConfigValue('components', $viewName); 150 foreach ($components as $name => $component) 151 { 152 if (!is_array($component) || count($component) < 1) 153 { 154 $component = array(null, null); 155 } 156 157 $data .= " \$this->setComponentSlot('$name', '{$component[0]}', '{$component[1]}');\n"; 158 $data .= " if (sfConfig::get('sf_logging_enabled')) \$context->getLogger()->info('{sfViewConfig} set component \"$name\" ({$component[0]}/{$component[1]})');\n"; 159 } 160 161 return $data; 162 } 163 164 /** 165 * Adds a template setting statement to the data. 166 * 167 * @param string The view name 168 * 169 * @return string The PHP statement 170 */ 171 protected function addTemplate($viewName = '') 172 { 173 $data = ''; 174 175 $templateName = $this->getConfigValue('template', $viewName); 176 $defaultTemplateName = $templateName ? "'$templateName'" : '$this->actionName'; 177 178 $data .= " \$templateName = \$response->getParameter(\$this->moduleName.'_'.\$this->actionName.'_template', $defaultTemplateName, 'symfony/action/view');\n"; 179 $data .= " \$this->setTemplate(\$templateName.\$this->viewName.\$this->getExtension());\n"; 180 181 return $data; 182 } 183 184 /** 185 * Adds a layour statement statement to the data. 186 * 187 * @param string The view name 188 * 189 * @return string The PHP statement 190 */ 191 protected function addLayout($viewName = '') 192 { 193 $data = ''; 194 195 if ($this->getConfigValue('has_layout', $viewName) && false !== $layout = $this->getConfigValue('layout', $viewName)) 196 { 197 $data = " \$this->setDecoratorTemplate('$layout'.\$this->getExtension());\n"; 198 } 199 200 // For XMLHttpRequest, we want no layout by default 201 // So, we check if the user requested has_layout: true or if he gave a layout: name for this particular action 202 $localLayout = isset($this->yamlConfig[$viewName]['layout']) || isset($this->yamlConfig[$viewName]['has_layout']); 203 if (!$localLayout && $data) 204 { 205 $data = " if (!\$context->getRequest()->isXmlHttpRequest())\n {\n $data }\n"; 206 } 207 208 return $data; 209 } 210 211 /** 212 * Adds http metas and metas statements to the data. 213 * 214 * @param string The view name 215 * 216 * @return string The PHP statement 217 */ 218 protected function addHtmlHead($viewName = '') 219 { 220 $data = array(); 221 222 foreach ($this->mergeConfigValue('http_metas', $viewName) as $httpequiv => $content) 223 { 224 $data[] = sprintf(" \$response->addHttpMeta('%s', '%s', false);", $httpequiv, str_replace('\'', '\\\'', $content)); 225 } 226 227 foreach ($this->mergeConfigValue('metas', $viewName) as $name => $content) 228 { 229 $data[] = sprintf(" \$response->addMeta('%s', '%s', false, false);", $name, str_replace('\'', '\\\'', preg_replace('/&(?=\w+;)/', '&', htmlentities($content, ENT_QUOTES, sfConfig::get('sf_charset'))))); 230 } 231 232 return implode("\n", $data)."\n"; 233 } 234 235 /** 236 * Adds stylesheets and javascripts statements to the data. 237 * 238 * @param string The view name 239 * 240 * @return string The PHP statement 241 */ 242 protected function addHtmlAsset($viewName = '') 243 { 244 $data = array(); 245 $omit = array(); 246 $delete = array(); 247 $delete_all = false; 248 249 // Merge the current view's stylesheets with the app's default stylesheets 250 $stylesheets = $this->mergeConfigValue('stylesheets', $viewName); 251 $tmp = array(); 252 foreach ((array) $stylesheets as $css) 253 { 254 $position = ''; 255 if (is_array($css)) 256 { 257 $key = key($css); 258 $options = $css[$key]; 259 if (isset($options['position'])) 260 { 261 $position = $options['position']; 262 unset($options['position']); 263 } 264 } 265 else 266 { 267 $key = $css; 268 $options = array(); 269 } 270 271 $key = $this->replaceConstants($key); 272 273 if ('-*' == $key) 274 { 275 $tmp = array(); 276 } 277 else if ('-' == $key[0]) 278 { 279 unset($tmp[substr($key, 1)]); 280 } 281 else 282 { 283 $tmp[$key] = sprintf(" \$response->addStylesheet('%s', '%s', %s);", $key, $position, str_replace("\n", '', var_export($options, true))); 284 } 285 } 286 287 $data = array_merge($data, array_values($tmp)); 288 289 $omit = array(); 290 $delete_all = false; 291 292 // Populate $javascripts with the values from ONLY the current view 293 $javascripts = $this->mergeConfigValue('javascripts', $viewName); 294 $tmp = array(); 295 foreach ((array) $javascripts as $js) 296 { 297 $js = $this->replaceConstants($js); 298 299 if ('-*' == $js) 300 { 301 $tmp = array(); 302 } 303 else if ('-' == $js[0]) 304 { 305 unset($tmp[substr($js, 1)]); 306 } 307 else 308 { 309 $tmp[$js] = sprintf(" \$response->addJavascript('%s');", $js); 310 } 311 } 312 313 $data = array_merge($data, array_values($tmp)); 314 315 return implode("\n", $data)."\n"; 316 } 317 318 /** 319 * Adds an escaping statement to the data. 320 * 321 * @param string The view name 322 * 323 * @return string The PHP statement 324 */ 325 protected function addEscaping($viewName = '') 326 { 327 $data = array(); 328 329 $escaping = $this->getConfigValue('escaping', $viewName); 330 331 if (isset($escaping['strategy'])) 332 { 333 $data[] = sprintf(" \$this->setEscaping(%s);", var_export($escaping['strategy'], true)); 334 } 335 336 if (isset($escaping['method'])) 337 { 338 $data[] = sprintf(" \$this->setEscapingMethod(%s);", var_export($escaping['method'], true)); 339 } 340 341 return implode("\n", $data)."\n"; 342 } 343 }
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
| Généré le : Fri Mar 16 22:42:14 2007 | par Balluche grâce à PHPXref 0.7 |