sfProjectConfiguration.class.php 10 KB

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