Development

/branches/1.0/lib/config/sfViewConfigHandler.class.php

You must first sign up to be able to contribute.

root/branches/1.0/lib/config/sfViewConfigHandler.class.php

Revision 17714, 10.2 kB (checked in by FabianLange, 5 years ago)

[1.0, 1.1, 1.2, 1.3] code-cleanup. Being DRY (r17713) helps to eliminate waste

  • Property svn:mime-type set to text/x-php
  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Rev Date
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  * 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$
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('/&amp;(?=\w+;)/', '&', htmlspecialchars($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     // Merge the current view's stylesheets with the app's default stylesheets
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     // Populate $javascripts with the values from ONLY the current view
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           unset($options['position']);
300         }
301       }
302       else
303       {
304         $key = $js;
305       }
306
307       $key = $this->replaceConstants($key);
308
309       if ('-*' == $key)
310       {
311         $tmp = array();
312       }
313       else if ('-' == $key[0])
314       {
315         unset($tmp[substr($key, 1)]);
316       }
317       else
318       {
319         $tmp[$key] = sprintf("  \$response->addJavascript('%s', '%s');", $key, $position);
320       }
321     }
322
323     $data = array_merge($data, array_values($tmp));
324
325     return implode("\n", $data)."\n";
326   }
327
328   /**
329    * Adds an escaping statement to the data.
330    *
331    * @param string The view name
332    *
333    * @return string The PHP statement
334    */
335   protected function addEscaping($viewName = '')
336   {
337     $data = array();
338
339     $escaping = $this->getConfigValue('escaping', $viewName);
340
341     if (isset($escaping['strategy']))
342     {
343       $data[] = sprintf("  \$this->setEscaping(%s);", var_export($escaping['strategy'], true));
344     }
345
346     if (isset($escaping['method']))
347     {
348       $data[] = sprintf("  \$this->setEscapingMethod(%s);", var_export($escaping['method'], true));
349     }
350
351     return implode("\n", $data)."\n";
352   }
353 }
354
Note: See TracBrowser for help on using the browser.