| 1 |
<?php |
|---|
| 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 |
function include_component_slot($name, $vars = array()) |
|---|
| 37 |
{ |
|---|
| 38 |
echo get_component_slot($name, $vars); |
|---|
| 39 |
} |
|---|
| 40 |
|
|---|
| 41 |
|
|---|
| 42 |
|
|---|
| 43 |
|
|---|
| 44 |
|
|---|
| 45 |
|
|---|
| 46 |
|
|---|
| 47 |
|
|---|
| 48 |
|
|---|
| 49 |
|
|---|
| 50 |
|
|---|
| 51 |
|
|---|
| 52 |
|
|---|
| 53 |
|
|---|
| 54 |
|
|---|
| 55 |
function get_component_slot($name, $vars = array()) |
|---|
| 56 |
{ |
|---|
| 57 |
$context = sfContext::getInstance(); |
|---|
| 58 |
|
|---|
| 59 |
$actionStackEntry = $context->getController()->getActionStack()->getLastEntry(); |
|---|
| 60 |
$viewInstance = $actionStackEntry->getViewInstance(); |
|---|
| 61 |
|
|---|
| 62 |
if (!$viewInstance->hasComponentSlot($name)) |
|---|
| 63 |
{ |
|---|
| 64 |
|
|---|
| 65 |
$error = 'The component slot "%s" is not set'; |
|---|
| 66 |
$error = sprintf($error, $name); |
|---|
| 67 |
|
|---|
| 68 |
throw new sfConfigurationException($error); |
|---|
| 69 |
} |
|---|
| 70 |
|
|---|
| 71 |
if ($componentSlot = $viewInstance->getComponentSlot($name)) |
|---|
| 72 |
{ |
|---|
| 73 |
return get_component($componentSlot[0], $componentSlot[1], $vars); |
|---|
| 74 |
} |
|---|
| 75 |
} |
|---|
| 76 |
|
|---|
| 77 |
|
|---|
| 78 |
|
|---|
| 79 |
|
|---|
| 80 |
|
|---|
| 81 |
|
|---|
| 82 |
|
|---|
| 83 |
|
|---|
| 84 |
|
|---|
| 85 |
|
|---|
| 86 |
|
|---|
| 87 |
|
|---|
| 88 |
|
|---|
| 89 |
|
|---|
| 90 |
|
|---|
| 91 |
|
|---|
| 92 |
|
|---|
| 93 |
function include_component($moduleName, $componentName, $vars = array()) |
|---|
| 94 |
{ |
|---|
| 95 |
echo get_component($moduleName, $componentName, $vars); |
|---|
| 96 |
} |
|---|
| 97 |
|
|---|
| 98 |
|
|---|
| 99 |
|
|---|
| 100 |
|
|---|
| 101 |
|
|---|
| 102 |
|
|---|
| 103 |
|
|---|
| 104 |
|
|---|
| 105 |
|
|---|
| 106 |
|
|---|
| 107 |
|
|---|
| 108 |
|
|---|
| 109 |
|
|---|
| 110 |
|
|---|
| 111 |
|
|---|
| 112 |
|
|---|
| 113 |
function get_component($moduleName, $componentName, $vars = array()) |
|---|
| 114 |
{ |
|---|
| 115 |
$context = sfContext::getInstance(); |
|---|
| 116 |
$actionName = '_'.$componentName; |
|---|
| 117 |
|
|---|
| 118 |
|
|---|
| 119 |
if ($cacheManager = $context->getViewCacheManager()) |
|---|
| 120 |
{ |
|---|
| 121 |
$cacheManager->registerConfiguration($moduleName); |
|---|
| 122 |
$uri = '@sf_cache_partial?module='.$moduleName.'&action='.$actionName.'&sf_cache_key='.(isset($vars['sf_cache_key']) ? $vars['sf_cache_key'] : md5(serialize($vars))); |
|---|
| 123 |
if ($retval = _get_cache($cacheManager, $uri)) |
|---|
| 124 |
{ |
|---|
| 125 |
return $retval; |
|---|
| 126 |
} |
|---|
| 127 |
} |
|---|
| 128 |
|
|---|
| 129 |
$controller = $context->getController(); |
|---|
| 130 |
|
|---|
| 131 |
if (!$controller->componentExists($moduleName, $componentName)) |
|---|
| 132 |
{ |
|---|
| 133 |
|
|---|
| 134 |
$error = 'The component does not exist: "%s", "%s"'; |
|---|
| 135 |
$error = sprintf($error, $moduleName, $componentName); |
|---|
| 136 |
|
|---|
| 137 |
throw new sfConfigurationException($error); |
|---|
| 138 |
} |
|---|
| 139 |
|
|---|
| 140 |
|
|---|
| 141 |
$componentInstance = $controller->getComponent($moduleName, $componentName); |
|---|
| 142 |
|
|---|
| 143 |
|
|---|
| 144 |
if (!$componentInstance->initialize($context)) |
|---|
| 145 |
{ |
|---|
| 146 |
|
|---|
| 147 |
$error = 'Component initialization failed for module "%s", component "%s"'; |
|---|
| 148 |
$error = sprintf($error, $moduleName, $componentName); |
|---|
| 149 |
|
|---|
| 150 |
throw new sfInitializationException($error); |
|---|
| 151 |
} |
|---|
| 152 |
|
|---|
| 153 |
|
|---|
| 154 |
require(sfConfigCache::getInstance()->checkConfig(sfConfig::get('sf_app_module_dir_name').'/'.$moduleName.'/'.sfConfig::get('sf_app_module_config_dir_name').'/module.yml')); |
|---|
| 155 |
|
|---|
| 156 |
$componentInstance->getVarHolder()->add($vars); |
|---|
| 157 |
|
|---|
| 158 |
|
|---|
| 159 |
$componentToRun = 'execute'.ucfirst($componentName); |
|---|
| 160 |
if (!method_exists($componentInstance, $componentToRun)) |
|---|
| 161 |
{ |
|---|
| 162 |
if (!method_exists($componentInstance, 'execute')) |
|---|
| 163 |
{ |
|---|
| 164 |
|
|---|
| 165 |
$error = 'sfComponent initialization failed for module "%s", component "%s"'; |
|---|
| 166 |
$error = sprintf($error, $moduleName, $componentName); |
|---|
| 167 |
throw new sfInitializationException($error); |
|---|
| 168 |
} |
|---|
| 169 |
|
|---|
| 170 |
$componentToRun = 'execute'; |
|---|
| 171 |
} |
|---|
| 172 |
|
|---|
| 173 |
if (sfConfig::get('sf_logging_enabled')) |
|---|
| 174 |
{ |
|---|
| 175 |
$context->getLogger()->info('{PartialHelper} call "'.$moduleName.'->'.$componentToRun.'()'.'"'); |
|---|
| 176 |
} |
|---|
| 177 |
|
|---|
| 178 |
|
|---|
| 179 |
if (sfConfig::get('sf_debug') && sfConfig::get('sf_logging_enabled')) |
|---|
| 180 |
{ |
|---|
| 181 |
$timer = sfTimerManager::getTimer(sprintf('Component "%s/%s"', $moduleName, $componentName)); |
|---|
| 182 |
} |
|---|
| 183 |
|
|---|
| 184 |
$retval = $componentInstance->$componentToRun(); |
|---|
| 185 |
|
|---|
| 186 |
if (sfConfig::get('sf_debug') && sfConfig::get('sf_logging_enabled')) |
|---|
| 187 |
{ |
|---|
| 188 |
$timer->addTime(); |
|---|
| 189 |
} |
|---|
| 190 |
|
|---|
| 191 |
if ($retval != sfView::NONE) |
|---|
| 192 |
{ |
|---|
| 193 |
|
|---|
| 194 |
$view = new sfPartialView(); |
|---|
| 195 |
$view->initialize($context, $moduleName, $actionName, ''); |
|---|
| 196 |
|
|---|
| 197 |
$retval = $view->render($componentInstance->getVarHolder()->getAll()); |
|---|
| 198 |
|
|---|
| 199 |
if ($cacheManager) |
|---|
| 200 |
{ |
|---|
| 201 |
$retval = _set_cache($cacheManager, $uri, $retval); |
|---|
| 202 |
} |
|---|
| 203 |
|
|---|
| 204 |
return $retval; |
|---|
| 205 |
} |
|---|
| 206 |
} |
|---|
| 207 |
|
|---|
| 208 |
|
|---|
| 209 |
|
|---|
| 210 |
|
|---|
| 211 |
|
|---|
| 212 |
|
|---|
| 213 |
|
|---|
| 214 |
|
|---|
| 215 |
|
|---|
| 216 |
|
|---|
| 217 |
|
|---|
| 218 |
|
|---|
| 219 |
|
|---|
| 220 |
|
|---|
| 221 |
|
|---|
| 222 |
|
|---|
| 223 |
|
|---|
| 224 |
|
|---|
| 225 |
|
|---|
| 226 |
|
|---|
| 227 |
function include_partial($templateName, $vars = array()) |
|---|
| 228 |
{ |
|---|
| 229 |
echo get_partial($templateName, $vars); |
|---|
| 230 |
} |
|---|
| 231 |
|
|---|
| 232 |
|
|---|
| 233 |
|
|---|
| 234 |
|
|---|
| 235 |
|
|---|
| 236 |
|
|---|
| 237 |
|
|---|
| 238 |
|
|---|
| 239 |
|
|---|
| 240 |
|
|---|
| 241 |
|
|---|
| 242 |
|
|---|
| 243 |
|
|---|
| 244 |
|
|---|
| 245 |
|
|---|
| 246 |
function get_partial($templateName, $vars = array()) |
|---|
| 247 |
{ |
|---|
| 248 |
$context = sfContext::getInstance(); |
|---|
| 249 |
|
|---|
| 250 |
|
|---|
| 251 |
if (false !== $sep = strpos($templateName, '/')) |
|---|
| 252 |
{ |
|---|
| 253 |
$moduleName = substr($templateName, 0, $sep); |
|---|
| 254 |
$templateName = substr($templateName, $sep + 1); |
|---|
| 255 |
} |
|---|
| 256 |
else |
|---|
| 257 |
{ |
|---|
| 258 |
$moduleName = $context->getActionStack()->getLastEntry()->getModuleName(); |
|---|
| 259 |
} |
|---|
| 260 |
$actionName = '_'.$templateName; |
|---|
| 261 |
|
|---|
| 262 |
if ($cacheManager = $context->getViewCacheManager()) |
|---|
| 263 |
{ |
|---|
| 264 |
$cacheManager->registerConfiguration($moduleName); |
|---|
| 265 |
$uri = '@sf_cache_partial?module='.$moduleName.'&action='.$actionName.'&sf_cache_key='.(isset($vars['sf_cache_key']) ? $vars['sf_cache_key'] : md5(serialize($vars))); |
|---|
| 266 |
if ($retval = _get_cache($cacheManager, $uri)) |
|---|
| 267 |
{ |
|---|
| 268 |
return $retval; |
|---|
| 269 |
} |
|---|
| 270 |
} |
|---|
| 271 |
|
|---|
| 272 |
$view = new sfPartialView(); |
|---|
| 273 |
$view->initialize($context, $moduleName, $actionName, ''); |
|---|
| 274 |
$retval = $view->render($vars); |
|---|
| 275 |
|
|---|
| 276 |
if ($cacheManager) |
|---|
| 277 |
{ |
|---|
| 278 |
$retval = _set_cache($cacheManager, $uri, $retval); |
|---|
| 279 |
} |
|---|
| 280 |
|
|---|
| 281 |
return $retval; |
|---|
| 282 |
} |
|---|
| 283 |
|
|---|
| 284 |
function _get_cache($cacheManager, $uri) |
|---|
| 285 |
{ |
|---|
| 286 |
$retval = $cacheManager->get($uri); |
|---|
| 287 |
|
|---|
| 288 |
if (sfConfig::get('sf_web_debug')) |
|---|
| 289 |
{ |
|---|
| 290 |
$retval = sfWebDebug::getInstance()->decorateContentWithDebug($uri, $retval, false); |
|---|
| 291 |
} |
|---|
| 292 |
|
|---|
| 293 |
return $retval; |
|---|
| 294 |
} |
|---|
| 295 |
|
|---|
| 296 |
function _set_cache($cacheManager, $uri, $retval) |
|---|
| 297 |
{ |
|---|
| 298 |
$saved = $cacheManager->set($retval, $uri); |
|---|
| 299 |
|
|---|
| 300 |
if ($saved && sfConfig::get('sf_web_debug')) |
|---|
| 301 |
{ |
|---|
| 302 |
$retval = sfWebDebug::getInstance()->decorateContentWithDebug($uri, $retval, true); |
|---|
| 303 |
} |
|---|
| 304 |
|
|---|
| 305 |
return $retval; |
|---|
| 306 |
} |
|---|
| 307 |
|
|---|
| 308 |
|
|---|
| 309 |
|
|---|
| 310 |
|
|---|
| 311 |
|
|---|
| 312 |
|
|---|
| 313 |
|
|---|
| 314 |
|
|---|
| 315 |
function slot($name) |
|---|
| 316 |
{ |
|---|
| 317 |
$context = sfContext::getInstance(); |
|---|
| 318 |
$response = $context->getResponse(); |
|---|
| 319 |
|
|---|
| 320 |
$slots = $response->getParameter('slots', array(), 'symfony/view/sfView/slot'); |
|---|
| 321 |
$slot_names = $response->getParameter('slot_names', array(), 'symfony/view/sfView/slot'); |
|---|
| 322 |
if (in_array($name, $slot_names)) |
|---|
| 323 |
{ |
|---|
| 324 |
throw new sfCacheException(sprintf('A slot named "%s" is already started.', $name)); |
|---|
| 325 |
} |
|---|
| 326 |
|
|---|
| 327 |
$slot_names[] = $name; |
|---|
| 328 |
$slots[$name] = ''; |
|---|
| 329 |
|
|---|
| 330 |
$response->setParameter('slots', $slots, 'symfony/view/sfView/slot'); |
|---|
| 331 |
$response->setParameter('slot_names', $slot_names, 'symfony/view/sfView/slot'); |
|---|
| 332 |
|
|---|
| 333 |
if (sfConfig::get('sf_logging_enabled')) |
|---|
| 334 |
{ |
|---|
| 335 |
$context->getLogger()->info(sprintf('{PartialHelper} set slot "%s"', $name)); |
|---|
| 336 |
} |
|---|
| 337 |
|
|---|
| 338 |
ob_start(); |
|---|
| 339 |
ob_implicit_flush(0); |
|---|
| 340 |
} |
|---|
| 341 |
|
|---|
| 342 |
|
|---|
| 343 |
|
|---|
| 344 |
|
|---|
| 345 |
|
|---|
| 346 |
|
|---|
| 347 |
|
|---|
| 348 |
function end_slot() |
|---|
| 349 |
{ |
|---|
| 350 |
$content = ob_get_clean(); |
|---|
| 351 |
|
|---|
| 352 |
$response = sfContext::getInstance()->getResponse(); |
|---|
| 353 |
$slots = $response->getParameter('slots', array(), 'symfony/view/sfView/slot'); |
|---|
| 354 |
$slot_names = $response->getParameter('slot_names', array(), 'symfony/view/sfView/slot'); |
|---|
| 355 |
if (!$slot_names) |
|---|
| 356 |
{ |
|---|
| 357 |
throw new sfCacheException('No slot started.'); |
|---|
| 358 |
} |
|---|
| 359 |
|
|---|
| 360 |
$name = array_pop($slot_names); |
|---|
| 361 |
$slots[$name] = $content; |
|---|
| 362 |
|
|---|
| 363 |
$response->setParameter('slots', $slots, 'symfony/view/sfView/slot'); |
|---|
| 364 |
$response->setParameter('slot_names', $slot_names, 'symfony/view/sfView/slot'); |
|---|
| 365 |
} |
|---|
| 366 |
|
|---|
| 367 |
|
|---|
| 368 |
|
|---|
| 369 |
|
|---|
| 370 |
|
|---|
| 371 |
|
|---|
| 372 |
|
|---|
| 373 |
|
|---|
| 374 |
function has_slot($name) |
|---|
| 375 |
{ |
|---|
| 376 |
$response = sfContext::getInstance()->getResponse(); |
|---|
| 377 |
$slots = $response->getParameter('slots', array(), 'symfony/view/sfView/slot'); |
|---|
| 378 |
|
|---|
| 379 |
return array_key_exists($name, $slots); |
|---|
| 380 |
} |
|---|
| 381 |
|
|---|
| 382 |
|
|---|
| 383 |
|
|---|
| 384 |
|
|---|
| 385 |
|
|---|
| 386 |
|
|---|
| 387 |
|
|---|
| 388 |
|
|---|
| 389 |
|
|---|
| 390 |
|
|---|
| 391 |
|
|---|
| 392 |
|
|---|
| 393 |
|
|---|
| 394 |
function include_slot($name) |
|---|
| 395 |
{ |
|---|
| 396 |
$context = sfContext::getInstance(); |
|---|
| 397 |
$slots = $context->getResponse()->getParameter('slots', array(), 'symfony/view/sfView/slot'); |
|---|
| 398 |
|
|---|
| 399 |
if (sfConfig::get('sf_logging_enabled')) |
|---|
| 400 |
{ |
|---|
| 401 |
$context->getLogger()->info(sprintf('{PartialHelper} get slot "%s"', $name)); |
|---|
| 402 |
} |
|---|
| 403 |
|
|---|
| 404 |
if (isset($slots[$name])) |
|---|
| 405 |
{ |
|---|
| 406 |
echo $slots[$name]; |
|---|
| 407 |
|
|---|
| 408 |
return true; |
|---|
| 409 |
} |
|---|
| 410 |
else |
|---|
| 411 |
{ |
|---|
| 412 |
return false; |
|---|
| 413 |
} |
|---|
| 414 |
} |
|---|
| 415 |
|
|---|
| 416 |
|
|---|
| 417 |
|
|---|
| 418 |
|
|---|
| 419 |
|
|---|
| 420 |
|
|---|
| 421 |
|
|---|
| 422 |
|
|---|
| 423 |
|
|---|
| 424 |
|
|---|
| 425 |
|
|---|
| 426 |
|
|---|
| 427 |
|
|---|
| 428 |
function get_slot($name) |
|---|
| 429 |
{ |
|---|
| 430 |
$context = sfContext::getInstance(); |
|---|
| 431 |
$slots = $context->getResponse()->getParameter('slots', array(), 'symfony/view/sfView/slot'); |
|---|
| 432 |
|
|---|
| 433 |
if (sfConfig::get('sf_logging_enabled')) |
|---|
| 434 |
{ |
|---|
| 435 |
$context->getLogger()->info(sprintf('{PartialHelper} get slot "%s"', $name)); |
|---|
| 436 |
} |
|---|
| 437 |
|
|---|
| 438 |
return isset($slots[$name]) ? $slots[$name] : ''; |
|---|
| 439 |
} |
|---|
| 440 |
|
|---|