PageRenderTime 27ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/sbweb/sbweb_logica/lib/symfony/util/sfContext.class.php

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