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