sfContext.class.php 13 KB

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