Development

/branches/1.3/lib/helper/PartialHelper.php

You must first sign up to be able to contribute.

root/branches/1.3/lib/helper/PartialHelper.php

Revision 27755, 10.7 kB (checked in by Kris.Wallsmith, 4 years ago)

[1.3, 1.4] fixed double escaping of partial vars (closes #7825, refs #1638)

  • Property svn:mime-type set to text/x-php
  • 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  * PartialHelper.
13  *
14  * @package    symfony
15  * @subpackage helper
16  * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
17  * @version    SVN: $Id$
18  */
19
20 /**
21  * Evaluates and echoes a component slot.
22  * The component name is deduced from the definition of the view.yml
23  * For a variable to be accessible to the component and its partial,
24  * it has to be passed in the second argument.
25  *
26  * <b>Example:</b>
27  * <code>
28  *  include_component_slot('sidebar', array('myvar' => 12345));
29  * </code>
30  *
31  * @param  string slot name
32  * @param  array variables to be made accessible to the component
33  *
34  * @see    get_component_slot, include_partial, include_component
35  */
36 function include_component_slot($name, $vars = array())
37 {
38   echo get_component_slot($name, $vars);
39 }
40
41 /**
42  * Evaluates and returns a component slot.
43  * The syntax is similar to the one of include_component_slot.
44  *
45  * <b>Example:</b>
46  * <code>
47  *  echo get_component_slot('sidebar', array('myvar' => 12345));
48  * </code>
49  *
50  * @param  string $name  slot name
51  * @param  array  $vars  variables to be made accessible to the component
52  *
53  * @return string result of the component execution
54  * @see    get_component_slot, include_partial, include_component
55  */
56 function get_component_slot($name, $vars = array())
57 {
58   $viewInstance = sfContext::getInstance()->get('view_instance');
59
60   if (!$viewInstance->hasComponentSlot($name))
61   {
62     // cannot find component slot
63     throw new sfConfigurationException(sprintf('The component slot "%s" is not set.', $name));
64   }
65
66   if ($componentSlot = $viewInstance->getComponentSlot($name))
67   {
68     return get_component($componentSlot[0], $componentSlot[1], $vars);
69   }
70 }
71
72 /**
73  * Returns true if component slot exists.
74  *
75  * @param  string slot name
76  * @return bool true if component slot exists, false otherwise
77  */
78 function has_component_slot($name)
79 {
80   $viewInstance = sfContext::getInstance()->get('view_instance');
81
82   // check to see if one is defined
83   if (!$viewInstance->hasComponentSlot($name))
84   {
85     return false;
86   }
87  
88   // check to see if component slot is empty (null)
89   if ($viewInstance->getComponentSlot($name))
90   {
91     return true;
92   }
93
94   return false;
95 }
96
97 /**
98  * Evaluates and echoes a component.
99  * For a variable to be accessible to the component and its partial,
100  * it has to be passed in the third argument.
101  *
102  * <b>Example:</b>
103  * <code>
104  *  include_component('mymodule', 'mypartial', array('myvar' => 12345));
105  * </code>
106  *
107  * @param  string $moduleName     module name
108  * @param  string $componentName  component name
109  * @param  array  $vars           variables to be made accessible to the component
110  *
111  * @see    get_component, include_partial, include_component_slot
112  */
113 function include_component($moduleName, $componentName, $vars = array())
114 {
115   echo get_component($moduleName, $componentName, $vars);
116 }
117
118 /**
119  * Evaluates and returns a component.
120  * The syntax is similar to the one of include_component.
121  *
122  * <b>Example:</b>
123  * <code>
124  *  echo get_component('mymodule', 'mypartial', array('myvar' => 12345));
125  * </code>
126  *
127  * @param  string $moduleName     module name
128  * @param  string $componentName  component name
129  * @param  array  $vars           variables to be made accessible to the component
130  *
131  * @return string result of the component execution
132  * @see    include_component
133  */
134 function get_component($moduleName, $componentName, $vars = array())
135 {
136   $context = sfContext::getInstance();
137   $actionName = '_'.$componentName;
138
139   $class = sfConfig::get('mod_'.strtolower($moduleName).'_partial_view_class', 'sf').'PartialView';
140   $view = new $class($context, $moduleName, $actionName, '');
141   $view->setPartialVars(true === sfConfig::get('sf_escaping_strategy') ? sfOutputEscaper::unescape($vars) : $vars);
142
143   if ($retval = $view->getCache())
144   {
145     return $retval;
146   }
147
148   $allVars = _call_component($moduleName, $componentName, $vars);
149
150   if (null !== $allVars)
151   {
152     // render
153     $view->getAttributeHolder()->add($allVars);
154
155     return $view->render();
156   }
157 }
158
159 /**
160  * Evaluates and echoes a partial.
161  * The partial name is composed as follows: 'mymodule/mypartial'.
162  * The partial file name is _mypartial.php and is looked for in modules/mymodule/templates/.
163  * If the partial name doesn't include a module name,
164  * then the partial file is searched for in the caller's template/ directory.
165  * If the module name is 'global', then the partial file is looked for in myapp/templates/.
166  * For a variable to be accessible to the partial, it has to be passed in the second argument.
167  *
168  * <b>Example:</b>
169  * <code>
170  *  include_partial('mypartial', array('myvar' => 12345));
171  * </code>
172  *
173  * @param  string $templateName  partial name
174  * @param  array  $vars          variables to be made accessible to the partial
175  *
176  * @see    get_partial, include_component
177  */
178 function include_partial($templateName, $vars = array())
179 {
180   echo get_partial($templateName, $vars);
181 }
182
183 /**
184  * Evaluates and returns a partial.
185  * The syntax is similar to the one of include_partial
186  *
187  * <b>Example:</b>
188  * <code>
189  *  echo get_partial('mypartial', array('myvar' => 12345));
190  * </code>
191  *
192  * @param  string $templateName  partial name
193  * @param  array  $vars          variables to be made accessible to the partial
194  *
195  * @return string result of the partial execution
196  * @see    include_partial
197  */
198 function get_partial($templateName, $vars = array())
199 {
200   $context = sfContext::getInstance();
201
202   // partial is in another module?
203   if (false !== $sep = strpos($templateName, '/'))
204   {
205     $moduleName   = substr($templateName, 0, $sep);
206     $templateName = substr($templateName, $sep + 1);
207   }
208   else
209   {
210     $moduleName = $context->getActionStack()->getLastEntry()->getModuleName();
211   }
212   $actionName = '_'.$templateName;
213
214   $class = sfConfig::get('mod_'.strtolower($moduleName).'_partial_view_class', 'sf').'PartialView';
215   $view = new $class($context, $moduleName, $actionName, '');
216   $view->setPartialVars(true === sfConfig::get('sf_escaping_strategy') ? sfOutputEscaper::unescape($vars) : $vars);
217
218   return $view->render();
219 }
220
221 /**
222  * Begins the capturing of the slot.
223  *
224  * @param  string $name   slot name
225  * @param  string $value  The slot content
226  *
227  * @see    end_slot
228  */
229 function slot($name, $value = null)
230 {
231   $context = sfContext::getInstance();
232   $response = $context->getResponse();
233
234   $slot_names = sfConfig::get('symfony.view.slot_names', array());
235   if (in_array($name, $slot_names))
236   {
237     throw new sfCacheException(sprintf('A slot named "%s" is already started.', $name));
238   }
239
240   if (sfConfig::get('sf_logging_enabled'))
241   {
242     $context->getEventDispatcher()->notify(new sfEvent(null, 'application.log', array(sprintf('Set slot "%s"', $name))));
243   }
244
245   if (null !== $value)
246   {
247     $response->setSlot($name, $value);
248
249     return;
250   }
251
252   $slot_names[] = $name;
253
254   $response->setSlot($name, '');
255   sfConfig::set('symfony.view.slot_names', $slot_names);
256
257   ob_start();
258   ob_implicit_flush(0);
259 }
260
261 /**
262  * Stops the content capture and save the content in the slot.
263  *
264  * @see    slot
265  */
266 function end_slot()
267 {
268   $content = ob_get_clean();
269
270   $response = sfContext::getInstance()->getResponse();
271   $slot_names = sfConfig::get('symfony.view.slot_names', array());
272   if (!$slot_names)
273   {
274     throw new sfCacheException('No slot started.');
275   }
276
277   $name = array_pop($slot_names);
278
279   $response->setSlot($name, $content);
280   sfConfig::set('symfony.view.slot_names', $slot_names);
281 }
282
283 /**
284  * Returns true if the slot exists.
285  *
286  * @param  string $name  slot name
287  *
288  * @return bool true, if the slot exists
289  * @see    get_slot, include_slot
290  */
291 function has_slot($name)
292 {
293   return array_key_exists($name, sfContext::getInstance()->getResponse()->getSlots());
294 }
295
296 /**
297  * Evaluates and echoes a slot.
298  *
299  * <b>Example:</b>
300  * <code>
301  *  include_slot('navigation');
302  * </code>
303  *
304  * @param  string $name     slot name
305  * @param  string $default  default content to return if slot is unexistent
306  *
307  * @see    has_slot, get_slot
308  */
309 function include_slot($name, $default = '')
310 {
311   return ($v = get_slot($name, $default)) ? print $v : false;
312 }
313
314 /**
315  * Evaluates and returns a slot.
316  *
317  * <b>Example:</b>
318  * <code>
319  *  echo get_slot('navigation');
320  * </code>
321  *
322  * @param  string $name     slot name
323  * @param  string $default  default content to return if slot is unexistent
324  *
325  * @return string content of the slot
326  * @see    has_slot, include_slot
327  */
328 function get_slot($name, $default = '')
329 {
330   $context = sfContext::getInstance();
331   $slots = $context->getResponse()->getSlots();
332
333   if (sfConfig::get('sf_logging_enabled'))
334   {
335     $context->getEventDispatcher()->notify(new sfEvent(null, 'application.log', array(sprintf('Get slot "%s"', $name))));
336   }
337
338   return isset($slots[$name]) ? $slots[$name] : $default;
339 }
340
341 function _call_component($moduleName, $componentName, $vars)
342 {
343   $context = sfContext::getInstance();
344
345   $controller = $context->getController();
346
347   if (!$controller->componentExists($moduleName, $componentName))
348   {
349     // cannot find component
350     throw new sfConfigurationException(sprintf('The component does not exist: "%s", "%s".', $moduleName, $componentName));
351   }
352
353   // create an instance of the action
354   $componentInstance = $controller->getComponent($moduleName, $componentName);
355
356   // load component's module config file
357   require($context->getConfigCache()->checkConfig('modules/'.$moduleName.'/config/module.yml'));
358
359   // pass unescaped vars to the component if escaping_strategy is set to true
360   $componentInstance->getVarHolder()->add(true === sfConfig::get('sf_escaping_strategy') ? sfOutputEscaper::unescape($vars) : $vars);
361
362   // dispatch component
363   $componentToRun = 'execute'.ucfirst($componentName);
364   if (!method_exists($componentInstance, $componentToRun))
365   {
366     if (!method_exists($componentInstance, 'execute'))
367     {
368       // component not found
369       throw new sfInitializationException(sprintf('sfComponent initialization failed for module "%s", component "%s".', $moduleName, $componentName));
370     }
371
372     $componentToRun = 'execute';
373   }
374
375   if (sfConfig::get('sf_logging_enabled'))
376   {
377     $context->getEventDispatcher()->notify(new sfEvent(null, 'application.log', array(sprintf('Call "%s->%s()'.'"', $moduleName, $componentToRun))));
378   }
379
380   // run component
381   if (sfConfig::get('sf_debug') && sfConfig::get('sf_logging_enabled'))
382   {
383     $timer = sfTimerManager::getTimer(sprintf('Component "%s/%s"', $moduleName, $componentName));
384   }
385
386   $retval = $componentInstance->$componentToRun($context->getRequest());
387
388   if (sfConfig::get('sf_debug') && sfConfig::get('sf_logging_enabled'))
389   {
390     $timer->addTime();
391   }
392
393   return sfView::NONE == $retval ? null : $componentInstance->getVarHolder()->getAll();
394 }
395
Note: See TracBrowser for help on using the browser.