| 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 |
|
|---|
| 56 |
function get_component_slot($name, $vars = array()) |
|---|
| 57 |
{ |
|---|
| 58 |
$viewInstance = sfContext::getInstance()->get('view_instance'); |
|---|
| 59 |
|
|---|
| 60 |
if (!$viewInstance->hasComponentSlot($name)) |
|---|
| 61 |
{ |
|---|
| 62 |
|
|---|
| 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 |
|
|---|
| 74 |
|
|---|
| 75 |
|
|---|
| 76 |
|
|---|
| 77 |
|
|---|
| 78 |
function has_component_slot($name) |
|---|
| 79 |
{ |
|---|
| 80 |
$viewInstance = sfContext::getInstance()->get('view_instance'); |
|---|
| 81 |
|
|---|
| 82 |
|
|---|
| 83 |
if (!$viewInstance->hasComponentSlot($name)) |
|---|
| 84 |
{ |
|---|
| 85 |
return false; |
|---|
| 86 |
} |
|---|
| 87 |
|
|---|
| 88 |
|
|---|
| 89 |
if ($viewInstance->getComponentSlot($name)) |
|---|
| 90 |
{ |
|---|
| 91 |
return true; |
|---|
| 92 |
} |
|---|
| 93 |
|
|---|
| 94 |
return false; |
|---|
| 95 |
} |
|---|
| 96 |
|
|---|
| 97 |
|
|---|
| 98 |
|
|---|
| 99 |
|
|---|
| 100 |
|
|---|
| 101 |
|
|---|
| 102 |
|
|---|
| 103 |
|
|---|
| 104 |
|
|---|
| 105 |
|
|---|
| 106 |
|
|---|
| 107 |
|
|---|
| 108 |
|
|---|
| 109 |
|
|---|
| 110 |
|
|---|
| 111 |
|
|---|
| 112 |
|
|---|
| 113 |
function include_component($moduleName, $componentName, $vars = array()) |
|---|
| 114 |
{ |
|---|
| 115 |
echo get_component($moduleName, $componentName, $vars); |
|---|
| 116 |
} |
|---|
| 117 |
|
|---|
| 118 |
|
|---|
| 119 |
|
|---|
| 120 |
|
|---|
| 121 |
|
|---|
| 122 |
|
|---|
| 123 |
|
|---|
| 124 |
|
|---|
| 125 |
|
|---|
| 126 |
|
|---|
| 127 |
|
|---|
| 128 |
|
|---|
| 129 |
|
|---|
| 130 |
|
|---|
| 131 |
|
|---|
| 132 |
|
|---|
| 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 |
|
|---|
| 155 |
$view->getAttributeHolder()->add($allVars); |
|---|
| 156 |
|
|---|
| 157 |
return $view->render(); |
|---|
| 158 |
} |
|---|
| 159 |
} |
|---|
| 160 |
|
|---|
| 161 |
|
|---|
| 162 |
|
|---|
| 163 |
|
|---|
| 164 |
|
|---|
| 165 |
|
|---|
| 166 |
|
|---|
| 167 |
|
|---|
| 168 |
|
|---|
| 169 |
|
|---|
| 170 |
|
|---|
| 171 |
|
|---|
| 172 |
|
|---|
| 173 |
|
|---|
| 174 |
|
|---|
| 175 |
|
|---|
| 176 |
|
|---|
| 177 |
|
|---|
| 178 |
|
|---|
| 179 |
|
|---|
| 180 |
function include_partial($templateName, $vars = array()) |
|---|
| 181 |
{ |
|---|
| 182 |
echo get_partial($templateName, $vars); |
|---|
| 183 |
} |
|---|
| 184 |
|
|---|
| 185 |
|
|---|
| 186 |
|
|---|
| 187 |
|
|---|
| 188 |
|
|---|
| 189 |
|
|---|
| 190 |
|
|---|
| 191 |
|
|---|
| 192 |
|
|---|
| 193 |
|
|---|
| 194 |
|
|---|
| 195 |
|
|---|
| 196 |
|
|---|
| 197 |
|
|---|
| 198 |
|
|---|
| 199 |
|
|---|
| 200 |
function get_partial($templateName, $vars = array()) |
|---|
| 201 |
{ |
|---|
| 202 |
$context = sfContext::getInstance(); |
|---|
| 203 |
|
|---|
| 204 |
|
|---|
| 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 |
|
|---|
| 225 |
|
|---|
| 226 |
|
|---|
| 227 |
|
|---|
| 228 |
|
|---|
| 229 |
|
|---|
| 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 |
|
|---|
| 265 |
|
|---|
| 266 |
|
|---|
| 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 |
|
|---|
| 287 |
|
|---|
| 288 |
|
|---|
| 289 |
|
|---|
| 290 |
|
|---|
| 291 |
|
|---|
| 292 |
|
|---|
| 293 |
function has_slot($name) |
|---|
| 294 |
{ |
|---|
| 295 |
return array_key_exists($name, sfContext::getInstance()->getResponse()->getSlots()); |
|---|
| 296 |
} |
|---|
| 297 |
|
|---|
| 298 |
|
|---|
| 299 |
|
|---|
| 300 |
|
|---|
| 301 |
|
|---|
| 302 |
|
|---|
| 303 |
|
|---|
| 304 |
|
|---|
| 305 |
|
|---|
| 306 |
|
|---|
| 307 |
|
|---|
| 308 |
|
|---|
| 309 |
|
|---|
| 310 |
|
|---|
| 311 |
function include_slot($name, $default = '') |
|---|
| 312 |
{ |
|---|
| 313 |
return ($v = get_slot($name, $default)) ? print $v : false; |
|---|
| 314 |
} |
|---|
| 315 |
|
|---|
| 316 |
|
|---|
| 317 |
|
|---|
| 318 |
|
|---|
| 319 |
|
|---|
| 320 |
|
|---|
| 321 |
|
|---|
| 322 |
|
|---|
| 323 |
|
|---|
| 324 |
|
|---|
| 325 |
|
|---|
| 326 |
|
|---|
| 327 |
|
|---|
| 328 |
|
|---|
| 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 |
|
|---|
| 352 |
throw new sfConfigurationException(sprintf('The component does not exist: "%s", "%s".', $moduleName, $componentName)); |
|---|
| 353 |
} |
|---|
| 354 |
|
|---|
| 355 |
|
|---|
| 356 |
$componentInstance = $controller->getComponent($moduleName, $componentName); |
|---|
| 357 |
|
|---|
| 358 |
|
|---|
| 359 |
require($context->getConfigCache()->checkConfig('modules/'.$moduleName.'/config/module.yml')); |
|---|
| 360 |
|
|---|
| 361 |
|
|---|
| 362 |
$componentInstance->getVarHolder()->add(true === sfConfig::get('sf_escaping_strategy') ? sfOutputEscaper::unescape($vars) : $vars); |
|---|
| 363 |
|
|---|
| 364 |
|
|---|
| 365 |
$componentToRun = 'execute'.ucfirst($componentName); |
|---|
| 366 |
if (!method_exists($componentInstance, $componentToRun)) |
|---|
| 367 |
{ |
|---|
| 368 |
if (!method_exists($componentInstance, 'execute')) |
|---|
| 369 |
{ |
|---|
| 370 |
|
|---|
| 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 |
|
|---|
| 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 |
|
|---|