Development

/branches/1.1/lib/helper/PartialHelper.php

You must first sign up to be able to contribute.

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

Revision 10227, 10.0 kB (checked in by fabien, 6 years ago)

fixed cache on components

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