Development

/branches/1.4/lib/helper/PartialHelper.php

You must first sign up to be able to contribute.

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

Revision 33122, 10.8 kB (checked in by fabien, 3 years ago)

[1.4] fixed include|get_component when sfPartialView class is customized (closes #9932)

  • 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   require($context->getConfigCache()->checkConfig('modules/'.$moduleName.'/config/module.yml'));
140
141   $class = sfConfig::get('mod_'.strtolower($moduleName).'_partial_view_class', 'sf').'PartialView';
142   $view = new $class($context, $moduleName, $actionName, '');
143   $view->setPartialVars(true === sfConfig::get('sf_escaping_strategy') ? sfOutputEscaper::unescape($vars) : $vars);
144
145   if ($retval = $view->getCache())
146   {
147     return $retval;
148   }
149
150   $allVars = _call_component($moduleName, $componentName, $vars);
151
152   if (null !== $allVars)
153   {
154     // render
155     $view->getAttributeHolder()->add($allVars);
156
157     return $view->render();
158   }
159 }
160
161 /**
162  * Evaluates and echoes a partial.
163  * The partial name is composed as follows: 'mymodule/mypartial'.
164  * The partial file name is _mypartial.php and is looked for in modules/mymodule/templates/.
165  * If the partial name doesn't include a module name,
166  * then the partial file is searched for in the caller's template/ directory.
167  * If the module name is 'global', then the partial file is looked for in myapp/templates/.
168  * For a variable to be accessible to the partial, it has to be passed in the second argument.
169  *
170  * <b>Example:</b>
171  * <code>
172  *  include_partial('mypartial', array('myvar' => 12345));
173  * </code>
174  *
175  * @param  string $templateName  partial name
176  * @param  array  $vars          variables to be made accessible to the partial
177  *
178  * @see    get_partial, include_component
179  */
180 function include_partial($templateName, $vars = array())
181 {
182   echo get_partial($templateName, $vars);
183 }
184
185 /**
186  * Evaluates and returns a partial.
187  * The syntax is similar to the one of include_partial
188  *
189  * <b>Example:</b>
190  * <code>
191  *  echo get_partial('mypartial', array('myvar' => 12345));
192  * </code>
193  *
194  * @param  string $templateName  partial name
195  * @param  array  $vars          variables to be made accessible to the partial
196  *
197  * @return string result of the partial execution
198  * @see    include_partial
199  */
200 function get_partial($templateName, $vars = array())
201 {
202   $context = sfContext::getInstance();
203
204   // partial is in another module?
205   if (false !== $sep = strpos($templateName, '/'))
206   {
207     $moduleName   = substr($templateName, 0, $sep);
208     $templateName = substr($templateName, $sep + 1);
209   }
210   else
211   {
212     $moduleName = $context->getActionStack()->getLastEntry()->getModuleName();
213   }
214   $actionName = '_'.$templateName;
215
216   $class = sfConfig::get('mod_'.strtolower($moduleName).'_partial_view_class', 'sf').'PartialView';
217   $view = new $class($context, $moduleName, $actionName, '');
218   $view->setPartialVars(true === sfConfig::get('sf_escaping_strategy') ? sfOutputEscaper::unescape($vars) : $vars);
219
220   return $view->render();
221 }
222
223 /**
224  * Begins the capturing of the slot.
225  *
226  * @param  string $name   slot name
227  * @param  string $value  The slot content
228  *
229  * @see    end_slot
230  */
231 function slot($name, $value = null)
232 {
233   $context = sfContext::getInstance();
234   $response = $context->getResponse();
235
236   $slot_names = sfConfig::get('symfony.view.slot_names', array());
237   if (in_array($name, $slot_names))
238   {
239     throw new sfCacheException(sprintf('A slot named "%s" is already started.', $name));
240   }
241
242   if (sfConfig::get('sf_logging_enabled'))
243   {
244     $context->getEventDispatcher()->notify(new sfEvent(null, 'application.log', array(sprintf('Set slot "%s"', $name))));
245   }
246
247   if (null !== $value)
248   {
249     $response->setSlot($name, $value);
250
251     return;
252   }
253
254   $slot_names[] = $name;
255
256   $response->setSlot($name, '');
257   sfConfig::set('symfony.view.slot_names', $slot_names);
258
259   ob_start();
260   ob_implicit_flush(0);
261 }
262
263 /**
264  * Stops the content capture and save the content in the slot.
265  *
266  * @see    slot
267  */
268 function end_slot()
269 {
270   $content = ob_get_clean();
271
272   $response = sfContext::getInstance()->getResponse();
273   $slot_names = sfConfig::get('symfony.view.slot_names', array());
274   if (!$slot_names)
275   {
276     throw new sfCacheException('No slot started.');
277   }
278
279   $name = array_pop($slot_names);
280
281   $response->setSlot($name, $content);
282   sfConfig::set('symfony.view.slot_names', $slot_names);
283 }
284
285 /**
286  * Returns true if the slot exists.
287  *
288  * @param  string $name  slot name
289  *
290  * @return bool true, if the slot exists
291  * @see    get_slot, include_slot
292  */
293 function has_slot($name)
294 {
295   return array_key_exists($name, sfContext::getInstance()->getResponse()->getSlots());
296 }
297
298 /**
299  * Evaluates and echoes a slot.
300  *
301  * <b>Example:</b>
302  * <code>
303  *  include_slot('navigation');
304  * </code>
305  *
306  * @param  string $name     slot name
307  * @param  string $default  default content to return if slot is unexistent
308  *
309  * @see    has_slot, get_slot
310  */
311 function include_slot($name, $default = '')
312 {
313   return ($v = get_slot($name, $default)) ? print $v : false;
314 }
315
316 /**
317  * Evaluates and returns a slot.
318  *
319  * <b>Example:</b>
320  * <code>
321  *  echo get_slot('navigation');
322  * </code>
323  *
324  * @param  string $name     slot name
325  * @param  string $default  default content to return if slot is unexistent
326  *
327  * @return string content of the slot
328  * @see    has_slot, include_slot
329  */
330 function get_slot($name, $default = '')
331 {
332   $context = sfContext::getInstance();
333   $slots = $context->getResponse()->getSlots();
334
335   if (sfConfig::get('sf_logging_enabled'))
336   {
337     $context->getEventDispatcher()->notify(new sfEvent(null, 'application.log', array(sprintf('Get slot "%s"', $name))));
338   }
339
340   return isset($slots[$name]) ? $slots[$name] : $default;
341 }
342
343 function _call_component($moduleName, $componentName, $vars)
344 {
345   $context = sfContext::getInstance();
346
347   $controller = $context->getController();
348
349   if (!$controller->componentExists($moduleName, $componentName))
350   {
351     // cannot find component
352     throw new sfConfigurationException(sprintf('The component does not exist: "%s", "%s".', $moduleName, $componentName));
353   }
354
355   // create an instance of the action
356   $componentInstance = $controller->getComponent($moduleName, $componentName);
357
358   // load component's module config file
359   require($context->getConfigCache()->checkConfig('modules/'.$moduleName.'/config/module.yml'));
360
361   // pass unescaped vars to the component if escaping_strategy is set to true
362   $componentInstance->getVarHolder()->add(true === sfConfig::get('sf_escaping_strategy') ? sfOutputEscaper::unescape($vars) : $vars);
363
364   // dispatch component
365   $componentToRun = 'execute'.ucfirst($componentName);
366   if (!method_exists($componentInstance, $componentToRun))
367   {
368     if (!method_exists($componentInstance, 'execute'))
369     {
370       // component not found
371       throw new sfInitializationException(sprintf('sfComponent initialization failed for module "%s", component "%s".', $moduleName, $componentName));
372     }
373
374     $componentToRun = 'execute';
375   }
376
377   if (sfConfig::get('sf_logging_enabled'))
378   {
379     $context->getEventDispatcher()->notify(new sfEvent(null, 'application.log', array(sprintf('Call "%s->%s()'.'"', $moduleName, $componentToRun))));
380   }
381
382   // run component
383   if (sfConfig::get('sf_debug') && sfConfig::get('sf_logging_enabled'))
384   {
385     $timer = sfTimerManager::getTimer(sprintf('Component "%s/%s"', $moduleName, $componentName));
386   }
387
388   $retval = $componentInstance->$componentToRun($context->getRequest());
389
390   if (sfConfig::get('sf_debug') && sfConfig::get('sf_logging_enabled'))
391   {
392     $timer->addTime();
393   }
394
395   return sfView::NONE == $retval ? null : $componentInstance->getVarHolder()->getAll();
396 }
397
Note: See TracBrowser for help on using the browser.