| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 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 |
|
|---|
| 35 |
$categories = array('required_categories' => array()); |
|---|
| 36 |
$this->initialize($categories); |
|---|
| 37 |
|
|---|
| 38 |
|
|---|
| 39 |
$this->mergeConfig($this->parseYamls($configFiles)); |
|---|
| 40 |
|
|---|
| 41 |
|
|---|
| 42 |
$data = array(); |
|---|
| 43 |
|
|---|
| 44 |
$data[] = "\$context = \$this->getContext();\n"; |
|---|
| 45 |
$data[] = "\$response = \$context->getResponse();\n\n"; |
|---|
| 46 |
|
|---|
| 47 |
|
|---|
| 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 |
|
|---|
| 65 |
$data[] = ($first ? '' : "else\n{")."\n"; |
|---|
| 66 |
$data[] = $this->addTemplate($viewName); |
|---|
| 67 |
$data[] = ($first ? '' : "}")."\n\n"; |
|---|
| 68 |
|
|---|
| 69 |
|
|---|
| 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 |
|
|---|
| 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 |
|
|---|
| 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 |
|
|---|
| 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 |
|
|---|
| 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 |
|
|---|
| 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 |
|
|---|
| 246 |
|
|---|
| 247 |
$stylesheets = $this->mergeConfigValue('stylesheets', $viewName); |
|---|
| 248 |
$tmp = array(); |
|---|
| 249 |
foreach ((array) $stylesheets as $css) |
|---|
| 250 |
{ |
|---|
| 251 |
$position = ''; |
|---|
| 252 |
if (is_array($css)) |
|---|
| 253 |
{ |
|---|
| 254 |
$key = key($css); |
|---|
| 255 |
$options = $css[$key]; |
|---|
| 256 |
if (isset($options['position'])) |
|---|
| 257 |
{ |
|---|
| 258 |
$position = $options['position']; |
|---|
| 259 |
unset($options['position']); |
|---|
| 260 |
} |
|---|
| 261 |
} |
|---|
| 262 |
else |
|---|
| 263 |
{ |
|---|
| 264 |
$key = $css; |
|---|
| 265 |
$options = array(); |
|---|
| 266 |
} |
|---|
| 267 |
|
|---|
| 268 |
$key = $this->replaceConstants($key); |
|---|
| 269 |
|
|---|
| 270 |
if ('-*' == $key) |
|---|
| 271 |
{ |
|---|
| 272 |
$tmp = array(); |
|---|
| 273 |
} |
|---|
| 274 |
else if ('-' == $key[0]) |
|---|
| 275 |
{ |
|---|
| 276 |
unset($tmp[substr($key, 1)]); |
|---|
| 277 |
} |
|---|
| 278 |
else |
|---|
| 279 |
{ |
|---|
| 280 |
$tmp[$key] = sprintf(" \$response->addStylesheet('%s', '%s', %s);", $key, $position, str_replace("\n", '', var_export($options, true))); |
|---|
| 281 |
} |
|---|
| 282 |
} |
|---|
| 283 |
|
|---|
| 284 |
$data = array_merge($data, array_values($tmp)); |
|---|
| 285 |
|
|---|
| 286 |
|
|---|
| 287 |
$javascripts = $this->mergeConfigValue('javascripts', $viewName); |
|---|
| 288 |
$tmp = array(); |
|---|
| 289 |
foreach ((array) $javascripts as $js) |
|---|
| 290 |
{ |
|---|
| 291 |
$position = ''; |
|---|
| 292 |
if (is_array($js)) |
|---|
| 293 |
{ |
|---|
| 294 |
$key = key($js); |
|---|
| 295 |
$options = $js[$key]; |
|---|
| 296 |
if (isset($options['position'])) |
|---|
| 297 |
{ |
|---|
| 298 |
$position = $options['position']; |
|---|
| 299 |
} |
|---|
| 300 |
} |
|---|
| 301 |
else |
|---|
| 302 |
{ |
|---|
| 303 |
$key = $js; |
|---|
| 304 |
} |
|---|
| 305 |
|
|---|
| 306 |
$key = $this->replaceConstants($key); |
|---|
| 307 |
|
|---|
| 308 |
if ('-*' == $key) |
|---|
| 309 |
{ |
|---|
| 310 |
$tmp = array(); |
|---|
| 311 |
} |
|---|
| 312 |
else if ('-' == $key[0]) |
|---|
| 313 |
{ |
|---|
| 314 |
unset($tmp[substr($key, 1)]); |
|---|
| 315 |
} |
|---|
| 316 |
else |
|---|
| 317 |
{ |
|---|
| 318 |
$tmp[$key] = sprintf(" \$response->addJavascript('%s', '%s');", $key, $position); |
|---|
| 319 |
} |
|---|
| 320 |
} |
|---|
| 321 |
|
|---|
| 322 |
$data = array_merge($data, array_values($tmp)); |
|---|
| 323 |
|
|---|
| 324 |
return implode("\n", $data)."\n"; |
|---|
| 325 |
} |
|---|
| 326 |
|
|---|
| 327 |
|
|---|
| 328 |
* Adds an escaping statement to the data. |
|---|
| 329 |
* |
|---|
| 330 |
* @param string The view name |
|---|
| 331 |
* |
|---|
| 332 |
* @return string The PHP statement |
|---|
| 333 |
*/ |
|---|
| 334 |
protected function addEscaping($viewName = '') |
|---|
| 335 |
{ |
|---|
| 336 |
$data = array(); |
|---|
| 337 |
|
|---|
| 338 |
$escaping = $this->getConfigValue('escaping', $viewName); |
|---|
| 339 |
|
|---|
| 340 |
if (isset($escaping['strategy'])) |
|---|
| 341 |
{ |
|---|
| 342 |
$data[] = sprintf(" \$this->setEscaping(%s);", var_export($escaping['strategy'], true)); |
|---|
| 343 |
} |
|---|
| 344 |
|
|---|
| 345 |
if (isset($escaping['method'])) |
|---|
| 346 |
{ |
|---|
| 347 |
$data[] = sprintf(" \$this->setEscapingMethod(%s);", var_export($escaping['method'], true)); |
|---|
| 348 |
} |
|---|
| 349 |
|
|---|
| 350 |
return implode("\n", $data)."\n"; |
|---|
| 351 |
} |
|---|
| 352 |
} |
|---|
| 353 |
|
|---|