PageRenderTime 41ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/oiclient/data/symfony/config/sfProjectConfiguration.class.php

http://openirudi.googlecode.com/
PHP | 363 lines | 181 code | 54 blank | 128 comment | 13 complexity | 68918e4141abf6bd8b05f777a1658e3a 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) Fabien Potencier <fabien.potencier@symfony-project.com>
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. /**
  10. * sfProjectConfiguration represents a configuration for a symfony project.
  11. *
  12. * @package symfony
  13. * @subpackage config
  14. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  15. * @version SVN: $Id: sfProjectConfiguration.class.php 11776 2008-09-25 10:11:40Z fabien $
  16. */
  17. class sfProjectConfiguration
  18. {
  19. protected
  20. $rootDir = null,
  21. $symfonyLibDir = null;
  22. static protected
  23. $active = null;
  24. /**
  25. * Constructor.
  26. *
  27. * @param string $rootDir The project root directory
  28. * @param sfEventDispatcher $dispatcher The event dispatcher
  29. */
  30. public function __construct($rootDir = null, sfEventDispatcher $dispatcher = null)
  31. {
  32. if (is_null(sfProjectConfiguration::$active) || $this instanceof sfApplicationConfiguration)
  33. {
  34. sfProjectConfiguration::$active = $this;
  35. }
  36. $this->rootDir = is_null($rootDir) ? self::guessRootDir() : realpath($rootDir);
  37. $this->symfonyLibDir = realpath(dirname(__FILE__).'/..');
  38. // initializes autoloading for symfony core classes
  39. require_once $this->symfonyLibDir.'/autoload/sfCoreAutoload.class.php';
  40. sfCoreAutoload::register();
  41. $this->dispatcher = is_null($dispatcher) ? new sfEventDispatcher() : $dispatcher;
  42. ini_set('magic_quotes_runtime', 'off');
  43. ini_set('register_globals', 'off');
  44. sfConfig::set('sf_symfony_lib_dir', $this->symfonyLibDir);
  45. $this->setRootDir($this->rootDir);
  46. $this->setup();
  47. }
  48. /**
  49. * Setups the current configuration.
  50. *
  51. * Override this method if you want to customize your project configuration.
  52. */
  53. public function setup()
  54. {
  55. }
  56. /**
  57. * Sets the project root directory.
  58. *
  59. * @param string $rootDir The project root directory
  60. */
  61. public function setRootDir($rootDir)
  62. {
  63. $this->rootDir = $rootDir;
  64. sfConfig::add(array(
  65. 'sf_root_dir' => $rootDir,
  66. // global directory structure
  67. 'sf_apps_dir' => $rootDir.DIRECTORY_SEPARATOR.'apps',
  68. 'sf_lib_dir' => $rootDir.DIRECTORY_SEPARATOR.'lib',
  69. 'sf_log_dir' => $rootDir.DIRECTORY_SEPARATOR.'log',
  70. 'sf_data_dir' => $rootDir.DIRECTORY_SEPARATOR.'data',
  71. 'sf_config_dir' => $rootDir.DIRECTORY_SEPARATOR.'config',
  72. 'sf_test_dir' => $rootDir.DIRECTORY_SEPARATOR.'test',
  73. 'sf_doc_dir' => $rootDir.DIRECTORY_SEPARATOR.'doc',
  74. 'sf_plugins_dir' => $rootDir.DIRECTORY_SEPARATOR.'plugins',
  75. ));
  76. $this->setWebDir($rootDir.DIRECTORY_SEPARATOR.'web');
  77. $this->setCacheDir($rootDir.DIRECTORY_SEPARATOR.'cache');
  78. }
  79. /**
  80. * Returns the project root directory.
  81. *
  82. * @return string The project root directory
  83. */
  84. public function getRootDir()
  85. {
  86. return $this->rootDir;
  87. }
  88. /**
  89. * Sets the cache root directory.
  90. *
  91. * @param string $cacheDir The absolute path to the cache dir.
  92. */
  93. public function setCacheDir($cacheDir)
  94. {
  95. sfConfig::set('sf_cache_dir', $cacheDir);
  96. }
  97. /**
  98. * Sets the log directory.
  99. *
  100. * @param string $logDir The absolute path to the log dir.
  101. */
  102. public function setLogDir($logDir)
  103. {
  104. sfConfig::set('sf_log_dir', $logDir);
  105. }
  106. /**
  107. * Sets the web root directory.
  108. *
  109. * @param string $webDir The absolute path to the web dir.
  110. */
  111. public function setWebDir($webDir)
  112. {
  113. sfConfig::add(array(
  114. 'sf_web_dir' => $webDir,
  115. 'sf_upload_dir' => $webDir.DIRECTORY_SEPARATOR.'uploads',
  116. ));
  117. }
  118. /**
  119. * Gets directories where model classes are stored. The order of returned paths is lowest precedence
  120. * to highest precedence.
  121. *
  122. * @return array An array of directories
  123. */
  124. public function getModelDirs()
  125. {
  126. $dirs = array();
  127. if ($pluginDirs = glob(sfConfig::get('sf_plugins_dir').'/*/lib/model')) // plugins
  128. {
  129. $dirs = array_merge($dirs, $pluginDirs);
  130. }
  131. $dirs[] = sfConfig::get('sf_lib_dir').'/model'; // project
  132. return $dirs;
  133. }
  134. /**
  135. * Gets directories where template files are stored for a generator class and a specific theme.
  136. *
  137. * @param string $class The generator class name
  138. * @param string $theme The theme name
  139. *
  140. * @return array An array of directories
  141. */
  142. public function getGeneratorTemplateDirs($class, $theme)
  143. {
  144. $dirs = array(sfConfig::get('sf_data_dir').'/generator/'.$class.'/'.$theme.'/template'); // project
  145. if ($pluginDirs = glob(sfConfig::get('sf_plugins_dir').'/*/data/generator/'.$class.'/'.$theme.'/template'))
  146. {
  147. $dirs = array_merge($dirs, $pluginDirs); // plugin
  148. }
  149. if ($bundledPluginDirs = glob(sfConfig::get('sf_symfony_lib_dir').'/plugins/*/data/generator/'.$class.'/'.$theme.'/template'))
  150. {
  151. $dirs = array_merge($dirs, $bundledPluginDirs); // bundled plugin
  152. }
  153. return $dirs;
  154. }
  155. /**
  156. * Gets directories where the skeleton is stored for a generator class and a specific theme.
  157. *
  158. * @param string $class The generator class name
  159. * @param string $theme The theme name
  160. *
  161. * @return array An array of directories
  162. */
  163. public function getGeneratorSkeletonDirs($class, $theme)
  164. {
  165. $dirs = array(sfConfig::get('sf_data_dir').'/generator/'.$class.'/'.$theme.'/skeleton'); // project
  166. if ($pluginDirs = glob(sfConfig::get('sf_plugins_dir').'/*/data/generator/'.$class.'/'.$theme.'/skeleton'))
  167. {
  168. $dirs = array_merge($dirs, $pluginDirs); // plugin
  169. }
  170. if ($bundledPluginDirs = glob(sfConfig::get('sf_symfony_lib_dir').'/plugins/*/data/generator/'.$class.'/'.$theme.'/skeleton'))
  171. {
  172. $dirs = array_merge($dirs, $bundledPluginDirs); // bundled plugin
  173. }
  174. return $dirs;
  175. }
  176. /**
  177. * Gets the template to use for a generator class.
  178. *
  179. * @param string $class The generator class name
  180. * @param string $theme The theme name
  181. * @param string $path The template path
  182. *
  183. * @return string A template path
  184. *
  185. * @throws sfException
  186. */
  187. public function getGeneratorTemplate($class, $theme, $path)
  188. {
  189. $dirs = $this->getGeneratorTemplateDirs($class, $theme);
  190. foreach ($dirs as $dir)
  191. {
  192. if (is_readable($dir.'/'.$path))
  193. {
  194. return $dir.'/'.$path;
  195. }
  196. }
  197. throw new sfException(sprintf('Unable to load "%s" generator template in: %s.', $path, implode(', ', $dirs)));
  198. }
  199. /**
  200. * Gets the paths to plugins root directories, minding overloaded plugins.
  201. *
  202. * @return array The plugin root paths.
  203. */
  204. public function getPluginPaths()
  205. {
  206. $pluginPaths = array();
  207. $finder = sfFinder::type('dir')->maxdepth(0)->follow_link()->relative();
  208. $bundledPlugins = $finder->discard('.*')->prune('.*')->in(sfConfig::get('sf_symfony_lib_dir').'/plugins');
  209. $projectPlugins = $finder->discard('.*')->prune('.*')->in(sfConfig::get('sf_plugins_dir'));
  210. // bundled plugins
  211. foreach ($bundledPlugins as $plugin)
  212. {
  213. // plugins can override bundle plugins
  214. if (false !== $pos = array_search($plugin, $projectPlugins))
  215. {
  216. $pluginPaths[] = sfConfig::get('sf_plugins_dir').'/'.$plugin;
  217. unset($projectPlugins[$pos]);
  218. }
  219. else
  220. {
  221. $pluginPaths[] = sfConfig::get('sf_symfony_lib_dir').'/plugins/'.$plugin;
  222. }
  223. }
  224. // project plugins
  225. foreach ($projectPlugins as $plugin)
  226. {
  227. $pluginPaths[] = sfConfig::get('sf_plugins_dir').'/'.$plugin;
  228. }
  229. return $pluginPaths;
  230. }
  231. /**
  232. * Returns the event dispatcher.
  233. *
  234. * @return sfEventDispatcher A sfEventDispatcher instance
  235. */
  236. public function getEventDispatcher()
  237. {
  238. return $this->dispatcher;
  239. }
  240. /**
  241. * Returns the symfony lib directory.
  242. *
  243. * @return string The symfony lib directory
  244. */
  245. public function getSymfonyLibDir()
  246. {
  247. return $this->symfonyLibDir;
  248. }
  249. /**
  250. * Returns the active configuration.
  251. *
  252. * @return sfProjectConfiguration The current sfProjectConfiguration instance
  253. */
  254. static public function getActive()
  255. {
  256. if (is_null(sfProjectConfiguration::$active))
  257. {
  258. throw new RuntimeException('There is no active configuration.');
  259. }
  260. return sfProjectConfiguration::$active;
  261. }
  262. static public function guessRootDir()
  263. {
  264. $r = new ReflectionClass('ProjectConfiguration');
  265. return realpath(dirname($r->getFileName()).'/..');
  266. }
  267. /**
  268. * Returns a sfApplicationConfiguration configuration for a given application.
  269. *
  270. * @param string $application An application name
  271. * @param string $environment The environment name
  272. * @param Boolean $debug true to enable debug mode
  273. * @param string $rootDir The project root directory
  274. * @param sfEventDispatcher $dispatcher An event dispatcher
  275. *
  276. * @return sfApplicationConfiguration A sfApplicationConfiguration instance
  277. */
  278. static public function getApplicationConfiguration($application, $environment, $debug, $rootDir = null, sfEventDispatcher $dispatcher = null)
  279. {
  280. $class = $application.'Configuration';
  281. if (is_null($rootDir))
  282. {
  283. $rootDir = self::guessRootDir();
  284. }
  285. if (!file_exists($file = $rootDir.'/apps/'.$application.'/config/'.$class.'.class.php'))
  286. {
  287. throw new InvalidArgumentException(sprintf('The application "%s" does not exist.', $application));
  288. }
  289. require_once $file;
  290. return new $class($environment, $debug, $rootDir, $dispatcher);
  291. }
  292. /**
  293. * Calls methods defined via sfEventDispatcher.
  294. *
  295. * @param string $method The method name
  296. * @param array $arguments The method arguments
  297. *
  298. * @return mixed The returned value of the called method
  299. */
  300. public function __call($method, $arguments)
  301. {
  302. $event = $this->dispatcher->notifyUntil(new sfEvent($this, 'configuration.method_not_found', array('method' => $method, 'arguments' => $arguments)));
  303. if (!$event->isProcessed())
  304. {
  305. throw new sfException(sprintf('Call to undefined method %s::%s.', get_class($this), $method));
  306. }
  307. return $event->getReturnValue();
  308. }
  309. }