| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 |
class sfContext |
|---|
| 24 |
{ |
|---|
| 25 |
protected |
|---|
| 26 |
$dispatcher = null, |
|---|
| 27 |
$configuration = null, |
|---|
| 28 |
$factories = array(); |
|---|
| 29 |
|
|---|
| 30 |
protected static |
|---|
| 31 |
$instances = array(), |
|---|
| 32 |
$current = 'default'; |
|---|
| 33 |
|
|---|
| 34 |
|
|---|
| 35 |
* Creates a new context instance. |
|---|
| 36 |
* |
|---|
| 37 |
* @param sfApplicationConfiguration $configuration An sfApplicationConfiguration instance |
|---|
| 38 |
* @param string $name A name for this context (application name by default) |
|---|
| 39 |
* @param string $class The context class to use (sfContext by default) |
|---|
| 40 |
* |
|---|
| 41 |
* @return sfContext An sfContext instance |
|---|
| 42 |
*/ |
|---|
| 43 |
static public function createInstance(sfApplicationConfiguration $configuration, $name = null, $class = __CLASS__) |
|---|
| 44 |
{ |
|---|
| 45 |
if (is_null($name)) |
|---|
| 46 |
{ |
|---|
| 47 |
$name = $configuration->getApplication(); |
|---|
| 48 |
} |
|---|
| 49 |
|
|---|
| 50 |
self::$current = $name; |
|---|
| 51 |
|
|---|
| 52 |
self::$instances[$name] = new $class(); |
|---|
| 53 |
|
|---|
| 54 |
if (!self::$instances[$name] instanceof sfContext) |
|---|
| 55 |
{ |
|---|
| 56 |
throw new sfFactoryException(sprintf('Class "%s" is not of the type sfContext.', $class)); |
|---|
| 57 |
} |
|---|
| 58 |
|
|---|
| 59 |
self::$instances[$name]->initialize($configuration); |
|---|
| 60 |
|
|---|
| 61 |
return self::$instances[$name]; |
|---|
| 62 |
} |
|---|
| 63 |
|
|---|
| 64 |
|
|---|
| 65 |
* Initializes the current sfContext instance. |
|---|
| 66 |
* |
|---|
| 67 |
* @param sfApplicationConfiguration $configuration An sfApplicationConfiguration instance |
|---|
| 68 |
*/ |
|---|
| 69 |
public function initialize(sfApplicationConfiguration $configuration) |
|---|
| 70 |
{ |
|---|
| 71 |
$this->configuration = $configuration; |
|---|
| 72 |
$this->dispatcher = $configuration->getEventDispatcher(); |
|---|
| 73 |
|
|---|
| 74 |
try |
|---|
| 75 |
{ |
|---|
| 76 |
$this->loadFactories(); |
|---|
| 77 |
} |
|---|
| 78 |
catch (sfException $e) |
|---|
| 79 |
{ |
|---|
| 80 |
$e->printStackTrace(); |
|---|
| 81 |
} |
|---|
| 82 |
catch (Exception $e) |
|---|
| 83 |
{ |
|---|
| 84 |
sfException::createFromException($e)->printStackTrace(); |
|---|
| 85 |
} |
|---|
| 86 |
|
|---|
| 87 |
$this->dispatcher->connect('template.filter_parameters', array($this, 'filterTemplateParameters')); |
|---|
| 88 |
|
|---|
| 89 |
|
|---|
| 90 |
register_shutdown_function(array($this, 'shutdown')); |
|---|
| 91 |
} |
|---|
| 92 |
|
|---|
| 93 |
|
|---|
| 94 |
* Retrieves the singleton instance of this class. |
|---|
| 95 |
* |
|---|
| 96 |
* @param string $name The name of the sfContext to retrieve. |
|---|
| 97 |
* @param string $class The context class to use (sfContext by default) |
|---|
| 98 |
* |
|---|
| 99 |
* @return sfContext An sfContext implementation instance. |
|---|
| 100 |
*/ |
|---|
| 101 |
static public function getInstance($name = null, $class = __CLASS__) |
|---|
| 102 |
{ |
|---|
| 103 |
if (is_null($name)) |
|---|
| 104 |
{ |
|---|
| 105 |
$name = self::$current; |
|---|
| 106 |
} |
|---|
| 107 |
|
|---|
| 108 |
if (!isset(self::$instances[$name])) |
|---|
| 109 |
{ |
|---|
| 110 |
throw new sfException(sprintf('The "%s" context does not exist.', $name)); |
|---|
| 111 |
} |
|---|
| 112 |
|
|---|
| 113 |
return self::$instances[$name]; |
|---|
| 114 |
} |
|---|
| 115 |
|
|---|
| 116 |
|
|---|
| 117 |
* Checks to see if there has been a context created |
|---|
| 118 |
* |
|---|
| 119 |
* @param string $name The name of the sfContext to check for |
|---|
| 120 |
* |
|---|
| 121 |
* @return bool true is instanced, otherwise false |
|---|
| 122 |
*/ |
|---|
| 123 |
|
|---|
| 124 |
public static function hasInstance($name = null) |
|---|
| 125 |
{ |
|---|
| 126 |
if (is_null($name)) |
|---|
| 127 |
{ |
|---|
| 128 |
$name = self::$current; |
|---|
| 129 |
} |
|---|
| 130 |
|
|---|
| 131 |
return isset(self::$instances[$name]); |
|---|
| 132 |
} |
|---|
| 133 |
|
|---|
| 134 |
|
|---|
| 135 |
* Loads the symfony factories. |
|---|
| 136 |
*/ |
|---|
| 137 |
public function loadFactories() |
|---|
| 138 |
{ |
|---|
| 139 |
if (sfConfig::get('sf_use_database')) |
|---|
| 140 |
{ |
|---|
| 141 |
|
|---|
| 142 |
$this->factories['databaseManager'] = new sfDatabaseManager($this->configuration, array('auto_shutdown' => false)); |
|---|
| 143 |
} |
|---|
| 144 |
|
|---|
| 145 |
|
|---|
| 146 |
$this->factories['actionStack'] = new sfActionStack(); |
|---|
| 147 |
|
|---|
| 148 |
|
|---|
| 149 |
require($this->configuration->getConfigCache()->checkConfig('config/factories.yml')); |
|---|
| 150 |
|
|---|
| 151 |
$this->dispatcher->notify(new sfEvent($this, 'context.load_factories')); |
|---|
| 152 |
} |
|---|
| 153 |
|
|---|
| 154 |
|
|---|
| 155 |
* Dispatches the current request. |
|---|
| 156 |
*/ |
|---|
| 157 |
public function dispatch() |
|---|
| 158 |
{ |
|---|
| 159 |
$this->getController()->dispatch(); |
|---|
| 160 |
} |
|---|
| 161 |
|
|---|
| 162 |
|
|---|
| 163 |
* Sets the current context to something else |
|---|
| 164 |
* |
|---|
| 165 |
* @param string $name The name of the context to switch to |
|---|
| 166 |
* |
|---|
| 167 |
*/ |
|---|
| 168 |
public static function switchTo($name) |
|---|
| 169 |
{ |
|---|
| 170 |
if (!isset(self::$instances[$name])) |
|---|
| 171 |
{ |
|---|
| 172 |
$currentConfiguration = sfContext::getInstance()->getConfiguration(); |
|---|
| 173 |
sfContext::createInstance(ProjectConfiguration::getApplicationConfiguration($name, $currentConfiguration->getEnvironment(), $currentConfiguration->isDebug())); |
|---|
| 174 |
} |
|---|
| 175 |
|
|---|
| 176 |
self::$current = $name; |
|---|
| 177 |
|
|---|
| 178 |
sfContext::getInstance()->getConfiguration()->activate(); |
|---|
| 179 |
} |
|---|
| 180 |
|
|---|
| 181 |
|
|---|
| 182 |
* Returns the configuration instance. |
|---|
| 183 |
* |
|---|
| 184 |
* @return sfApplicationConfiguration The current application configuration instance |
|---|
| 185 |
*/ |
|---|
| 186 |
public function getConfiguration() |
|---|
| 187 |
{ |
|---|
| 188 |
return $this->configuration; |
|---|
| 189 |
} |
|---|
| 190 |
|
|---|
| 191 |
|
|---|
| 192 |
* Retrieves the current event dispatcher. |
|---|
| 193 |
* |
|---|
| 194 |
* @return sfEventDispatcher An sfEventDispatcher instance |
|---|
| 195 |
*/ |
|---|
| 196 |
public function getEventDispatcher() |
|---|
| 197 |
{ |
|---|
| 198 |
return $this->dispatcher; |
|---|
| 199 |
} |
|---|
| 200 |
|
|---|
| 201 |
|
|---|
| 202 |
* Retrieve the action name for this context. |
|---|
| 203 |
* |
|---|
| 204 |
* @return string The currently executing action name, if one is set, |
|---|
| 205 |
* otherwise null. |
|---|
| 206 |
*/ |
|---|
| 207 |
public function getActionName() |
|---|
| 208 |
{ |
|---|
| 209 |
|
|---|
| 210 |
if ($this->factories['actionStack'] && $lastEntry = $this->factories['actionStack']->getLastEntry()) |
|---|
| 211 |
{ |
|---|
| 212 |
return $lastEntry->getActionName(); |
|---|
| 213 |
} |
|---|
| 214 |
} |
|---|
| 215 |
|
|---|
| 216 |
|
|---|
| 217 |
|
|---|
| 218 |
* Retrieve the ActionStack. |
|---|
| 219 |
* |
|---|
| 220 |
* @return sfActionStack the sfActionStack instance |
|---|
| 221 |
*/ |
|---|
| 222 |
public function getActionStack() |
|---|
| 223 |
{ |
|---|
| 224 |
return $this->factories['actionStack']; |
|---|
| 225 |
} |
|---|
| 226 |
|
|---|
| 227 |
|
|---|
| 228 |
* Retrieve the controller. |
|---|
| 229 |
* |
|---|
| 230 |
* @return sfController The current sfController implementation instance. |
|---|
| 231 |
*/ |
|---|
| 232 |
public function getController() |
|---|
| 233 |
{ |
|---|
| 234 |
return isset($this->factories['controller']) ? $this->factories['controller'] : null; |
|---|
| 235 |
} |
|---|
| 236 |
|
|---|
| 237 |
|
|---|
| 238 |
* Retrieve the logger. |
|---|
| 239 |
* |
|---|
| 240 |
* @return sfLogger The current sfLogger implementation instance. |
|---|
| 241 |
*/ |
|---|
| 242 |
public function getLogger() |
|---|
| 243 |
{ |
|---|
| 244 |
if (!isset($this->factories['logger'])) |
|---|
| 245 |
{ |
|---|
| 246 |
$this->factories['logger'] = new sfNoLogger($this->dispatcher); |
|---|
| 247 |
} |
|---|
| 248 |
|
|---|
| 249 |
return $this->factories['logger']; |
|---|
| 250 |
} |
|---|
| 251 |
|
|---|
| 252 |
|
|---|
| 253 |
* Retrieve a database connection from the database manager. |
|---|
| 254 |
* |
|---|
| 255 |
* This is a shortcut to manually getting a connection from an existing |
|---|
| 256 |
* database implementation instance. |
|---|
| 257 |
* |
|---|
| 258 |
* If the [sf_use_database] setting is off, this will return null. |
|---|
| 259 |
* |
|---|
| 260 |
* @param name $name A database name. |
|---|
| 261 |
* |
|---|
| 262 |
* @return mixed A database instance. |
|---|
| 263 |
* |
|---|
| 264 |
* @throws sfDatabaseException if the requested database name does not exist. |
|---|
| 265 |
*/ |
|---|
| 266 |
public function getDatabaseConnection($name = 'default') |
|---|
| 267 |
{ |
|---|
| 268 |
if (!is_null($this->factories['databaseManager'])) |
|---|
| 269 |
{ |
|---|
| 270 |
return $this->factories['databaseManager']->getDatabase($name)->getConnection(); |
|---|
| 271 |
} |
|---|
| 272 |
|
|---|
| 273 |
return null; |
|---|
| 274 |
} |
|---|
| 275 |
|
|---|
| 276 |
public function retrieveObjects($class, $peerMethod) |
|---|
| 277 |
{ |
|---|
| 278 |
$retrievingClass = 'sf'.ucfirst(sfConfig::get('sf_orm', 'propel')).'DataRetriever'; |
|---|
| 279 |
|
|---|
| 280 |
return call_user_func(array($retrievingClass, 'retrieveObjects'), $class, $peerMethod); |
|---|
| 281 |
} |
|---|
| 282 |
|
|---|
| 283 |
|
|---|
| 284 |
* Retrieve the database manager. |
|---|
| 285 |
* |
|---|
| 286 |
* @return sfDatabaseManager The current sfDatabaseManager instance. |
|---|
| 287 |
*/ |
|---|
| 288 |
public function getDatabaseManager() |
|---|
| 289 |
{ |
|---|
| 290 |
return isset($this->factories['databaseManager']) ? $this->factories['databaseManager'] : null; |
|---|
| 291 |
} |
|---|
| 292 |
|
|---|
| 293 |
|
|---|
| 294 |
* Retrieve the module directory for this context. |
|---|
| 295 |
* |
|---|
| 296 |
* @return string An absolute filesystem path to the directory of the |
|---|
| 297 |
* currently executing module, if one is set, otherwise null. |
|---|
| 298 |
*/ |
|---|
| 299 |
public function getModuleDirectory() |
|---|
| 300 |
{ |
|---|
| 301 |
|
|---|
| 302 |
if (isset($this->factories['actionStack']) && $lastEntry = $this->factories['actionStack']->getLastEntry()) |
|---|
| 303 |
{ |
|---|
| 304 |
return sfConfig::get('sf_app_module_dir').'/'.$lastEntry->getModuleName(); |
|---|
| 305 |
} |
|---|
| 306 |
} |
|---|
| 307 |
|
|---|
| 308 |
|
|---|
| 309 |
* Retrieve the module name for this context. |
|---|
| 310 |
* |
|---|
| 311 |
* @return string The currently executing module name, if one is set, |
|---|
| 312 |
* otherwise null. |
|---|
| 313 |
*/ |
|---|
| 314 |
public function getModuleName() |
|---|
| 315 |
{ |
|---|
| 316 |
|
|---|
| 317 |
if (isset($this->factories['actionStack']) && $lastEntry = $this->factories['actionStack']->getLastEntry()) |
|---|
| 318 |
{ |
|---|
| 319 |
return $lastEntry->getModuleName(); |
|---|
| 320 |
} |
|---|
| 321 |
} |
|---|
| 322 |
|
|---|
| 323 |
|
|---|
| 324 |
* Retrieve the request. |
|---|
| 325 |
* |
|---|
| 326 |
* @return sfRequest The current sfRequest implementation instance. |
|---|
| 327 |
*/ |
|---|
| 328 |
public function getRequest() |
|---|
| 329 |
{ |
|---|
| 330 |
return isset($this->factories['request']) ? $this->factories['request'] : null; |
|---|
| 331 |
} |
|---|
| 332 |
|
|---|
| 333 |
|
|---|
| 334 |
* Retrieve the response. |
|---|
| 335 |
* |
|---|
| 336 |
* @return sfResponse The current sfResponse implementation instance. |
|---|
| 337 |
*/ |
|---|
| 338 |
public function getResponse() |
|---|
| 339 |
{ |
|---|
| 340 |
return isset($this->factories['response']) ? $this->factories['response'] : null; |
|---|
| 341 |
} |
|---|
| 342 |
|
|---|
| 343 |
|
|---|
| 344 |
* Set the response object. |
|---|
| 345 |
* |
|---|
| 346 |
* @param sfResponse $response An sfResponse instance. |
|---|
| 347 |
* |
|---|
| 348 |
* @return void |
|---|
| 349 |
*/ |
|---|
| 350 |
public function setResponse($response) |
|---|
| 351 |
{ |
|---|
| 352 |
$this->factories['response'] = $response; |
|---|
| 353 |
} |
|---|
| 354 |
|
|---|
| 355 |
|
|---|
| 356 |
* Retrieve the storage. |
|---|
| 357 |
* |
|---|
| 358 |
* @return sfStorage The current sfStorage implementation instance. |
|---|
| 359 |
*/ |
|---|
| 360 |
public function getStorage() |
|---|
| 361 |
{ |
|---|
| 362 |
return isset($this->factories['storage']) ? $this->factories['storage'] : null; |
|---|
| 363 |
} |
|---|
| 364 |
|
|---|
| 365 |
|
|---|
| 366 |
* Retrieve the view cache manager |
|---|
| 367 |
* |
|---|
| 368 |
* @return sfViewCacheManager The current sfViewCacheManager implementation instance. |
|---|
| 369 |
*/ |
|---|
| 370 |
public function getViewCacheManager() |
|---|
| 371 |
{ |
|---|
| 372 |
return isset($this->factories['viewCacheManager']) ? $this->factories['viewCacheManager'] : null; |
|---|
| 373 |
} |
|---|
| 374 |
|
|---|
| 375 |
|
|---|
| 376 |
* Retrieve the i18n instance |
|---|
| 377 |
* |
|---|
| 378 |
* @return sfI18N The current sfI18N implementation instance. |
|---|
| 379 |
*/ |
|---|
| 380 |
public function getI18N() |
|---|
| 381 |
{ |
|---|
| 382 |
if (!sfConfig::get('sf_i18n')) |
|---|
| 383 |
{ |
|---|
| 384 |
throw new sfConfigurationException('You must enable i18n support in your settings.yml configuration file.'); |
|---|
| 385 |
} |
|---|
| 386 |
|
|---|
| 387 |
return $this->factories['i18n']; |
|---|
| 388 |
} |
|---|
| 389 |
|
|---|
| 390 |
|
|---|
| 391 |
* Retrieve the routing instance. |
|---|
| 392 |
* |
|---|
| 393 |
* @return sfRouting The current sfRouting implementation instance. |
|---|
| 394 |
*/ |
|---|
| 395 |
public function getRouting() |
|---|
| 396 |
{ |
|---|
| 397 |
return isset($this->factories['routing']) ? $this->factories['routing'] : null; |
|---|
| 398 |
} |
|---|
| 399 |
|
|---|
| 400 |
|
|---|
| 401 |
* Retrieve the user. |
|---|
| 402 |
* |
|---|
| 403 |
* @return sfUser The current sfUser implementation instance. |
|---|
| 404 |
*/ |
|---|
| 405 |
public function getUser() |
|---|
| 406 |
{ |
|---|
| 407 |
return isset($this->factories['user']) ? $this->factories['user'] : null; |
|---|
| 408 |
} |
|---|
| 409 |
|
|---|
| 410 |
|
|---|
| 411 |
* Returns the configuration cache. |
|---|
| 412 |
* |
|---|
| 413 |
* @return sfConfigCache A sfConfigCache instance |
|---|
| 414 |
*/ |
|---|
| 415 |
public function getConfigCache() |
|---|
| 416 |
{ |
|---|
| 417 |
return $this->configuration->getConfigCache(); |
|---|
| 418 |
} |
|---|
| 419 |
|
|---|
| 420 |
|
|---|
| 421 |
* Gets an object from the current context. |
|---|
| 422 |
* |
|---|
| 423 |
* @param string $name The name of the object to retrieve |
|---|
| 424 |
* |
|---|
| 425 |
* @return object The object associated with the given name |
|---|
| 426 |
*/ |
|---|
| 427 |
public function get($name) |
|---|
| 428 |
{ |
|---|
| 429 |
if (!$this->has($name)) |
|---|
| 430 |
{ |
|---|
| 431 |
throw new sfException(sprintf('The "%s" object does not exist in the current context.', $name)); |
|---|
| 432 |
} |
|---|
| 433 |
|
|---|
| 434 |
return $this->factories[$name]; |
|---|
| 435 |
} |
|---|
| 436 |
|
|---|
| 437 |
|
|---|
| 438 |
* Puts an object in the current context. |
|---|
| 439 |
* |
|---|
| 440 |
* @param string $name The name of the object to store |
|---|
| 441 |
* @param object $object The object to store |
|---|
| 442 |
*/ |
|---|
| 443 |
public function set($name, $object) |
|---|
| 444 |
{ |
|---|
| 445 |
$this->factories[$name] = $object; |
|---|
| 446 |
} |
|---|
| 447 |
|
|---|
| 448 |
|
|---|
| 449 |
* Returns true if an object is currently stored in the current context with the given name, false otherwise. |
|---|
| 450 |
* |
|---|
| 451 |
* @param string $name The object name |
|---|
| 452 |
* |
|---|
| 453 |
* @return bool true if the object is not null, false otherwise |
|---|
| 454 |
*/ |
|---|
| 455 |
public function has($name) |
|---|
| 456 |
{ |
|---|
| 457 |
return isset($this->factories[$name]); |
|---|
| 458 |
} |
|---|
| 459 |
|
|---|
| 460 |
|
|---|
| 461 |
* Listens to the template.filter_parameters event. |
|---|
| 462 |
* |
|---|
| 463 |
* @param sfEvent $event An sfEvent instance |
|---|
| 464 |
* @param array $parameters An array of template parameters to filter |
|---|
| 465 |
* |
|---|
| 466 |
* @return array The filtered parameters array |
|---|
| 467 |
*/ |
|---|
| 468 |
public function filterTemplateParameters(sfEvent $event, $parameters) |
|---|
| 469 |
{ |
|---|
| 470 |
$parameters['sf_context'] = $this; |
|---|
| 471 |
$parameters['sf_request'] = $this->factories['request']; |
|---|
| 472 |
$parameters['sf_params'] = $this->factories['request']->getParameterHolder(); |
|---|
| 473 |
$parameters['sf_response'] = $this->factories['response']; |
|---|
| 474 |
$parameters['sf_user'] = $this->factories['user']; |
|---|
| 475 |
|
|---|
| 476 |
return $parameters; |
|---|
| 477 |
} |
|---|
| 478 |
|
|---|
| 479 |
|
|---|
| 480 |
* Execute the shutdown procedure. |
|---|
| 481 |
* |
|---|
| 482 |
* @return void |
|---|
| 483 |
*/ |
|---|
| 484 |
public function shutdown() |
|---|
| 485 |
{ |
|---|
| 486 |
|
|---|
| 487 |
if($this->has('user')) |
|---|
| 488 |
{ |
|---|
| 489 |
$this->getUser()->shutdown(); |
|---|
| 490 |
$this->getStorage()->shutdown(); |
|---|
| 491 |
} |
|---|
| 492 |
|
|---|
| 493 |
if ($this->has('routing')) |
|---|
| 494 |
{ |
|---|
| 495 |
$this->getRouting()->shutdown(); |
|---|
| 496 |
} |
|---|
| 497 |
|
|---|
| 498 |
if (sfConfig::get('sf_use_database')) |
|---|
| 499 |
{ |
|---|
| 500 |
$this->getDatabaseManager()->shutdown(); |
|---|
| 501 |
} |
|---|
| 502 |
|
|---|
| 503 |
if (sfConfig::get('sf_logging_enabled')) |
|---|
| 504 |
{ |
|---|
| 505 |
$this->getLogger()->shutdown(); |
|---|
| 506 |
} |
|---|
| 507 |
} |
|---|
| 508 |
} |
|---|
| 509 |
|
|---|