PageRenderTime 43ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/dmCorePlugin/lib/context/dmContext.php

https://github.com/xdade/diem
PHP | 356 lines | 203 code | 66 blank | 87 comment | 13 complexity | dd0c7c9fcd955882ece789338a1ffadb MD5 | raw file
  1. <?php
  2. class dmContext extends sfContext
  3. {
  4. protected
  5. $serviceContainer,
  6. $page,
  7. $helper;
  8. /**
  9. * Creates a new context instance.
  10. *
  11. * @param sfApplicationConfiguration $configuration An sfApplicationConfiguration instance
  12. * @param string $name A name for this context (application name by default)
  13. * @param string $class The context class to use (dmContext by default)
  14. *
  15. * @return dmContext A dmContext instance
  16. */
  17. public static function createInstance(sfApplicationConfiguration $configuration, $name = null, $class = __CLASS__)
  18. {
  19. return parent::createInstance($configuration, $name, $class);
  20. }
  21. /**
  22. * @param string $name
  23. * @param string $class
  24. * @return dmContext
  25. */
  26. public static function getInstance($name = null, $class = __CLASS__)
  27. {
  28. return parent::getInstance($name, $class);
  29. }
  30. /**
  31. * Initializes the current dmContext instance.
  32. *
  33. * @param dmApplicationConfiguration $configuration An dmApplicationConfiguration instance
  34. */
  35. public function initialize(sfApplicationConfiguration $configuration)
  36. {
  37. $this->checkProjectIsSetup();
  38. parent::initialize($configuration);
  39. sfConfig::set('dm_debug', $this->getRequest()->getParameter('dm_debug', false));
  40. // load the service container instance
  41. $this->loadServiceContainer();
  42. // configure the service container with its required dependencies
  43. $this->configureServiceContainer($this->serviceContainer);
  44. if (method_exists($this->configuration, 'configureServiceContainer'))
  45. {
  46. $this->configuration->configureServiceContainer($this->serviceContainer);
  47. }
  48. // connect the service container and its services to the event dispatcher
  49. $this->serviceContainer->connect();
  50. /*
  51. * dmForm requires service container...
  52. */
  53. dmForm::setServiceContainer($this->serviceContainer);
  54. /*
  55. * some classes needs the event dispatcher to communicate
  56. * and the service container...
  57. */
  58. dmDoctrineQuery::setModuleManager($this->getModuleManager());
  59. dmDoctrineTable::setServiceContainer($this->serviceContainer);
  60. $this->helper = $this->serviceContainer->getService('helper');
  61. // notify that context is ready
  62. $this->dispatcher->notify(new sfEvent($this, 'dm.context.loaded'));
  63. }
  64. /**
  65. * Gets an object from the current context.
  66. *
  67. * @param string $name The name of the object to retrieve
  68. *
  69. * @return object The object associated with the given name
  70. *
  71. * @throws Exception if object does not exist in the current context
  72. */
  73. public function get($name, $class = null)
  74. {
  75. if (isset($this->factories[$name]))
  76. {
  77. return $this->factories[$name];
  78. }
  79. if($this->serviceContainer->hasService($name))
  80. {
  81. return $this->serviceContainer->getService($name, $class);
  82. }
  83. throw new sfException(sprintf('The "%s" object does not exist in the current context.', $name));
  84. }
  85. /**
  86. * Loads the symfony factories.
  87. */
  88. public function loadFactories()
  89. {
  90. $this->reloadModuleManager();
  91. parent::loadFactories();
  92. $this->factories['response']->setIsHtmlForHuman(
  93. !dmConfig::isCli()
  94. && !$this->factories['request']->isXmlHttpRequest()
  95. && !$this->factories['request']->isFlashRequest()
  96. && $this->factories['response']->isHtml()
  97. );
  98. }
  99. public function reloadModuleManager()
  100. {
  101. // create a new module_manager
  102. $this->factories['module_manager'] = include($this->getConfigCache()->checkConfig('config/dm/modules.yml'));
  103. dmModule::setManager($this->factories['module_manager']);
  104. }
  105. /**
  106. * Loads the diem services
  107. */
  108. protected function loadServiceContainer()
  109. {
  110. require_once(sfConfig::get('dm_core_dir').'/lib/vendor/sfService/sfServiceContainerInterface.php');
  111. require_once(sfConfig::get('dm_core_dir').'/lib/vendor/sfService/sfServiceContainer.php');
  112. $name = 'dm'.dmString::camelize(sfConfig::get('sf_app')).'ServiceContainer';
  113. $file = dmOs::join(sfConfig::get('dm_cache_dir'), $name.'.php');
  114. if (!file_exists($file))
  115. {
  116. $this->dumpServiceContainer($name, $file);
  117. }
  118. require_once($file);
  119. $this->serviceContainer = new $name;
  120. $this->dispatcher->connect('dm.config.updated', array($this, 'listenToConfigUpdatedEvent'));
  121. }
  122. public function listenToConfigUpdatedEvent(sfEvent $e)
  123. {
  124. $this->getFilesystem()->unlink(
  125. sfFinder::type('file')->name('dm*ServiceContainer.php')->in(sfConfig::get('dm_cache_dir'))
  126. );
  127. }
  128. public function configureServiceContainer(dmBaseServiceContainer $serviceContainer)
  129. {
  130. $serviceContainer->configure(array(
  131. 'context' => $this,
  132. 'doctrine_manager' => Doctrine_Manager::getInstance()
  133. ));
  134. }
  135. protected function dumpServiceContainer($name, $file)
  136. {
  137. foreach(array(sfConfig::get('dm_cache_dir'), dirname($file)) as $dir)
  138. {
  139. if (!is_dir($dir))
  140. {
  141. $oldUmask = umask(0);
  142. mkdir($dir, 0777);
  143. umask($oldUmask);
  144. }
  145. }
  146. $this->loadServiceContainerExtraStuff();
  147. $sc = new sfServiceContainerBuilder;
  148. $configPaths = $this->configuration->getConfigPaths('config/dm/services.yml');
  149. $loader = new sfServiceContainerLoaderFileYaml($sc);
  150. $loader->load($configPaths);
  151. $loader = new dmServiceContainerLoaderConfiguration($sc, $this->dispatcher);
  152. $loader->load(dmConfig::getAll());
  153. /*
  154. * Allow listeners of dm.service_container.pre_dump event
  155. * to modify the loader
  156. */
  157. $sc = $this->dispatcher->filter(
  158. new sfEvent($this, 'dm.service_container.pre_dump'),
  159. $sc
  160. )->getReturnValue();
  161. $dumper = new sfServiceContainerDumperPhp($sc);
  162. $baseClass = sfConfig::get('dm_service_container_base_class', 'dm'.ucfirst(sfConfig::get('dm_context_type')).'BaseServiceContainer');
  163. file_put_contents($file, $dumper->dump(array('class' => $name, 'base_class' => $baseClass)));
  164. $oldUmask = umask(0);
  165. @chmod($file, 0777);
  166. umask($oldUmask);
  167. if(!file_exists($file))
  168. {
  169. throw new dmException('Can not write the generated service container to '.$file);
  170. }
  171. unset($dumper, $loader, $sc);
  172. }
  173. /**
  174. * Load the required classes to load a service container from yml configuration
  175. */
  176. public function loadServiceContainerExtraStuff()
  177. {
  178. $loaderFilePrefix = dmOs::join(sfConfig::get('dm_core_dir'), 'lib/vendor/sfService/sfService');
  179. foreach(array('Definition', 'Reference', 'ContainerBuilder', 'ContainerLoaderInterface', 'ContainerLoader', 'ContainerLoaderFile', 'ContainerLoaderFileYaml', 'ContainerDumperInterface', 'ContainerDumper', 'ContainerDumperPhp') as $requiredClassSuffix)
  180. {
  181. require_once($loaderFilePrefix.$requiredClassSuffix.'.php');
  182. }
  183. }
  184. /**
  185. * @return sfServiceContainer
  186. */
  187. public function getServiceContainer()
  188. {
  189. return $this->serviceContainer;
  190. }
  191. /**
  192. * @return dmCacheManager
  193. */
  194. public function getCacheManager()
  195. {
  196. return $this->serviceContainer->getService('cache_manager');
  197. }
  198. /**
  199. * @return dmFilesystem
  200. */
  201. public function getFilesystem()
  202. {
  203. return $this->serviceContainer->getService('filesystem');
  204. }
  205. /**
  206. * @return dmHelper
  207. */
  208. public function getHelper()
  209. {
  210. return $this->helper;
  211. }
  212. /**
  213. * @return dmModuleManager
  214. */
  215. public function getModuleManager()
  216. {
  217. return $this->factories['module_manager'];
  218. }
  219. /**
  220. * Retrieves the mailer.
  221. *
  222. * @return sfMailer The current sfMailer implementation instance.
  223. */
  224. public function getMailer()
  225. {
  226. if (!isset($this->factories['mailer']))
  227. {
  228. Swift::registerAutoload();
  229. sfMailer::initialize();
  230. $this->factories['mailer'] = new $this->mailerConfiguration['class']($this->dispatcher, $this->mailerConfiguration);
  231. }
  232. return $this->factories['mailer'];
  233. }
  234. /**
  235. * Dispatches the current request.
  236. */
  237. public function dispatch()
  238. {
  239. $this->getController()->dispatch();
  240. $this->dispatcher->notify(new sfEvent($this, 'dm.context.end'));
  241. }
  242. public function shutdown()
  243. {
  244. $this->dispatcher->notify(new sfEvent($this, 'sf.context.end.before'));
  245. parent::shutdown();
  246. $this->dispatcher->notify(new sfEvent($this, 'sf.context.end.after'));
  247. }
  248. public function isModuleAction($module, $action)
  249. {
  250. return $this->getModuleName() === $module && $this->getActionName() === $action;
  251. }
  252. /**
  253. * @return DmPage the current page object
  254. */
  255. public function getPage()
  256. {
  257. return $this->page;
  258. }
  259. public function setPage(DmPage $page)
  260. {
  261. $this->page = $page;
  262. $this->dispatcher->notify(new sfEvent($this, 'dm.context.change_page', array('page' => $page)));
  263. }
  264. /**
  265. * Listens to the template.filter_parameters event.
  266. *
  267. * @param sfEvent $event An sfEvent instance
  268. * @param array $parameters An array of template parameters to filter
  269. *
  270. * @return array The filtered parameters array
  271. */
  272. public function filterTemplateParameters(sfEvent $event, $parameters)
  273. {
  274. $parameters = parent::filterTemplateParameters($event, $parameters);
  275. $parameters['dm_page'] = $this->getPage();
  276. return $parameters;
  277. }
  278. protected function checkProjectIsSetup()
  279. {
  280. if (file_exists(sfConfig::get('dm_data_dir').'/lock'))
  281. {
  282. if (!dmConfig::isCli())
  283. {
  284. die('Please setup this project with the dm:setup task : "php symfony dm:setup"');
  285. }
  286. return false;
  287. }
  288. return true;
  289. }
  290. }