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

/sbweb/sbweb_logica/lib/symfony/config/sfProjectConfiguration.class.php

http://opac-sbweb.googlecode.com/
PHP | 596 lines | 327 code | 75 blank | 194 comment | 22 complexity | 6905f3d15b372547099e9bb2f649730a 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 13929 2008-12-10 22:06:40Z FabianLange $
  16. */
  17. class sfProjectConfiguration
  18. {
  19. protected
  20. $rootDir = null,
  21. $symfonyLibDir = null,
  22. $plugins = array('sfPropelPlugin'),
  23. $pluginPaths = array(),
  24. $overriddenPluginPaths = array(),
  25. $pluginConfigurations = array(),
  26. $pluginsLoaded = false;
  27. static protected
  28. $active = null;
  29. /**
  30. * Constructor.
  31. *
  32. * @param string $rootDir The project root directory
  33. * @param sfEventDispatcher $dispatcher The event dispatcher
  34. */
  35. public function __construct($rootDir = null, sfEventDispatcher $dispatcher = null)
  36. {
  37. if (is_null(sfProjectConfiguration::$active) || $this instanceof sfApplicationConfiguration)
  38. {
  39. sfProjectConfiguration::$active = $this;
  40. }
  41. $this->rootDir = is_null($rootDir) ? self::guessRootDir() : realpath($rootDir);
  42. $this->symfonyLibDir = realpath(dirname(__FILE__).'/..');
  43. $this->dispatcher = is_null($dispatcher) ? new sfEventDispatcher() : $dispatcher;
  44. ini_set('magic_quotes_runtime', 'off');
  45. ini_set('register_globals', 'off');
  46. sfConfig::set('sf_symfony_lib_dir', $this->symfonyLibDir);
  47. $this->setRootDir($this->rootDir);
  48. $this->setup();
  49. $this->loadPlugins();
  50. }
  51. /**
  52. * Setups the current configuration.
  53. *
  54. * Override this method if you want to customize your project configuration.
  55. */
  56. public function setup()
  57. {
  58. }
  59. /**
  60. * Loads the project's plugin configurations.
  61. */
  62. public function loadPlugins()
  63. {
  64. foreach ($this->getPluginPaths() as $path)
  65. {
  66. if (false === $plugin = array_search($path, $this->overriddenPluginPaths))
  67. {
  68. $plugin = basename($path);
  69. }
  70. $class = $plugin.'Configuration';
  71. if (is_readable($file = sprintf('%s/config/%s.class.php', $path, $class)))
  72. {
  73. require_once $file;
  74. $configuration = new $class($this, $path, $plugin);
  75. }
  76. else
  77. {
  78. $configuration = new sfPluginConfigurationGeneric($this, $path, $plugin);
  79. }
  80. $this->pluginConfigurations[$plugin] = $configuration;
  81. }
  82. $this->pluginsLoaded = true;
  83. }
  84. /**
  85. * Sets the project root directory.
  86. *
  87. * @param string $rootDir The project root directory
  88. */
  89. public function setRootDir($rootDir)
  90. {
  91. $this->rootDir = $rootDir;
  92. sfConfig::add(array(
  93. 'sf_root_dir' => $rootDir,
  94. // global directory structure
  95. 'sf_apps_dir' => $rootDir.DIRECTORY_SEPARATOR.'apps',
  96. 'sf_lib_dir' => $rootDir.DIRECTORY_SEPARATOR.'lib',
  97. 'sf_log_dir' => $rootDir.DIRECTORY_SEPARATOR.'log',
  98. 'sf_data_dir' => $rootDir.DIRECTORY_SEPARATOR.'data',
  99. 'sf_config_dir' => $rootDir.DIRECTORY_SEPARATOR.'config',
  100. 'sf_test_dir' => $rootDir.DIRECTORY_SEPARATOR.'test',
  101. 'sf_doc_dir' => $rootDir.DIRECTORY_SEPARATOR.'doc',
  102. 'sf_plugins_dir' => $rootDir.DIRECTORY_SEPARATOR.'plugins',
  103. ));
  104. $this->setWebDir($rootDir.DIRECTORY_SEPARATOR.'web');
  105. $this->setCacheDir($rootDir.DIRECTORY_SEPARATOR.'cache');
  106. }
  107. /**
  108. * Returns the project root directory.
  109. *
  110. * @return string The project root directory
  111. */
  112. public function getRootDir()
  113. {
  114. return $this->rootDir;
  115. }
  116. /**
  117. * Sets the cache root directory.
  118. *
  119. * @param string $cacheDir The absolute path to the cache dir.
  120. */
  121. public function setCacheDir($cacheDir)
  122. {
  123. sfConfig::set('sf_cache_dir', $cacheDir);
  124. }
  125. /**
  126. * Sets the log directory.
  127. *
  128. * @param string $logDir The absolute path to the log dir.
  129. */
  130. public function setLogDir($logDir)
  131. {
  132. sfConfig::set('sf_log_dir', $logDir);
  133. }
  134. /**
  135. * Sets the web root directory.
  136. *
  137. * @param string $webDir The absolute path to the web dir.
  138. */
  139. public function setWebDir($webDir)
  140. {
  141. sfConfig::add(array(
  142. 'sf_web_dir' => $webDir,
  143. 'sf_upload_dir' => $webDir.DIRECTORY_SEPARATOR.'uploads',
  144. ));
  145. }
  146. /**
  147. * Gets directories where model classes are stored. The order of returned paths is lowest precedence
  148. * to highest precedence.
  149. *
  150. * @return array An array of directories
  151. */
  152. public function getModelDirs()
  153. {
  154. return array_merge(
  155. $this->getPluginSubPaths('/lib/model'), // plugins
  156. array(sfConfig::get('sf_lib_dir').'/model') // project
  157. );
  158. }
  159. /**
  160. * Gets directories where template files are stored for a generator class and a specific theme.
  161. *
  162. * @param string $class The generator class name
  163. * @param string $theme The theme name
  164. *
  165. * @return array An array of directories
  166. */
  167. public function getGeneratorTemplateDirs($class, $theme)
  168. {
  169. return array_merge(
  170. array(sfConfig::get('sf_data_dir').'/generator/'.$class.'/'.$theme.'/template'), // project
  171. $this->getPluginSubPaths('/data/generator/'.$class.'/'.$theme.'/template'), // plugins
  172. array(sfConfig::get('sf_data_dir').'/generator/'.$class.'/default/template'), // project (default theme)
  173. $this->getPluginSubPaths('/data/generator/'.$class.'/default/template') // plugins (default theme)
  174. );
  175. }
  176. /**
  177. * Gets directories where the skeleton is stored for a generator class and a specific theme.
  178. *
  179. * @param string $class The generator class name
  180. * @param string $theme The theme name
  181. *
  182. * @return array An array of directories
  183. */
  184. public function getGeneratorSkeletonDirs($class, $theme)
  185. {
  186. return array_merge(
  187. array(sfConfig::get('sf_data_dir').'/generator/'.$class.'/'.$theme.'/skeleton'), // project
  188. $this->getPluginSubPaths('/data/generator/'.$class.'/'.$theme.'/skeleton'), // plugins
  189. array(sfConfig::get('sf_data_dir').'/generator/'.$class.'/default/skeleton'), // project (default theme)
  190. $this->getPluginSubPaths('/data/generator/'.$class.'/default/skeleton') // plugins (default theme)
  191. );
  192. }
  193. /**
  194. * Gets the template to use for a generator class.
  195. *
  196. * @param string $class The generator class name
  197. * @param string $theme The theme name
  198. * @param string $path The template path
  199. *
  200. * @return string A template path
  201. *
  202. * @throws sfException
  203. */
  204. public function getGeneratorTemplate($class, $theme, $path)
  205. {
  206. $dirs = $this->getGeneratorTemplateDirs($class, $theme);
  207. foreach ($dirs as $dir)
  208. {
  209. if (is_readable($dir.'/'.$path))
  210. {
  211. return $dir.'/'.$path;
  212. }
  213. }
  214. throw new sfException(sprintf('Unable to load "%s" generator template in: %s.', $path, implode(', ', $dirs)));
  215. }
  216. /**
  217. * Gets the configuration file paths for a given relative configuration path.
  218. *
  219. * @param string $configPath The configuration path
  220. *
  221. * @return array An array of paths
  222. */
  223. public function getConfigPaths($configPath)
  224. {
  225. $globalConfigPath = basename(dirname($configPath)).'/'.basename($configPath);
  226. $files = array(
  227. sfConfig::get('sf_symfony_lib_dir').'/config/'.$globalConfigPath, // symfony
  228. );
  229. foreach ($this->getPluginPaths() as $path)
  230. {
  231. if (is_file($file = $path.'/'.$globalConfigPath))
  232. {
  233. $files[] = $file; // plugins
  234. }
  235. }
  236. $files = array_merge($files, array(
  237. sfConfig::get('sf_root_dir').'/'.$globalConfigPath, // project
  238. sfConfig::get('sf_root_dir').'/'.$configPath, // project
  239. ));
  240. foreach ($this->getPluginPaths() as $path)
  241. {
  242. if (is_file($file = $path.'/'.$configPath))
  243. {
  244. $files[] = $file; // plugins
  245. }
  246. }
  247. $configs = array();
  248. foreach (array_unique($files) as $file)
  249. {
  250. if (is_readable($file))
  251. {
  252. $configs[] = $file;
  253. }
  254. }
  255. return $configs;
  256. }
  257. /**
  258. * Sets the enabled plugins.
  259. *
  260. * @param array An array of plugin names
  261. *
  262. * @throws LogicException If plugins have already been loaded
  263. */
  264. public function setPlugins(array $plugins)
  265. {
  266. if ($this->pluginsLoaded)
  267. {
  268. throw new LogicException('Plugins have already been loaded.');
  269. }
  270. $this->plugins = $plugins;
  271. $this->pluginPaths = array();
  272. }
  273. /**
  274. * Enables a plugin or a list of plugins.
  275. *
  276. * @param array|string A plugin name or a plugin list
  277. */
  278. public function enablePlugins($plugins)
  279. {
  280. $this->setPlugins(array_merge($this->plugins, is_array($plugins) ? $plugins : array($plugins)));
  281. }
  282. /**
  283. * Disables a plugin.
  284. *
  285. * @param array|string A plugin name or a plugin list
  286. *
  287. * @throws LogicException If plugins have already been loaded
  288. */
  289. public function disablePlugins($plugins)
  290. {
  291. if ($this->pluginsLoaded)
  292. {
  293. throw new LogicException('Plugins have already been loaded.');
  294. }
  295. if (!is_array($plugins))
  296. {
  297. $plugins = array($plugins);
  298. }
  299. foreach ($plugins as $plugin)
  300. {
  301. if (false !== $pos = array_search($plugin, $this->plugins))
  302. {
  303. unset($this->plugins[$pos]);
  304. }
  305. else
  306. {
  307. throw new InvalidArgumentException(sprintf('The plugin "%s" does not exist.', $plugin));
  308. }
  309. }
  310. $this->pluginPaths = array();
  311. }
  312. /**
  313. * Enabled all installed plugins except the one given as argument.
  314. *
  315. * @param array|string A plugin name or a plugin list
  316. *
  317. * @throws LogicException If plugins have already been loaded
  318. */
  319. public function enableAllPluginsExcept($plugins = array())
  320. {
  321. if ($this->pluginsLoaded)
  322. {
  323. throw new LogicException('Plugins have already been loaded.');
  324. }
  325. $this->plugins = array();
  326. foreach ($this->getAllPluginPaths() as $plugin => $path)
  327. {
  328. $this->plugins[] = $plugin;
  329. }
  330. $this->disablePlugins($plugins);
  331. }
  332. /**
  333. * Gets the list of enabled plugins.
  334. *
  335. * @return array An array of enabled plugins
  336. */
  337. public function getPlugins()
  338. {
  339. return $this->plugins;
  340. }
  341. /**
  342. * Gets the paths plugin sub-directories, minding overloaded plugins.
  343. *
  344. * @param string $subPath The subdirectory to look for
  345. *
  346. * @return array The plugin paths.
  347. */
  348. public function getPluginSubPaths($subPath = '')
  349. {
  350. if (array_key_exists($subPath, $this->pluginPaths))
  351. {
  352. return $this->pluginPaths[$subPath];
  353. }
  354. $this->pluginPaths[$subPath] = array();
  355. $pluginPaths = $this->getPluginPaths();
  356. foreach ($pluginPaths as $pluginPath)
  357. {
  358. if (is_dir($pluginPath.$subPath))
  359. {
  360. $this->pluginPaths[$subPath][] = $pluginPath.$subPath;
  361. }
  362. }
  363. return $this->pluginPaths[$subPath];
  364. }
  365. /**
  366. * Gets the paths to plugins root directories, minding overloaded plugins.
  367. *
  368. * @return array The plugin root paths.
  369. */
  370. public function getPluginPaths()
  371. {
  372. if (array_key_exists('', $this->pluginPaths))
  373. {
  374. return $this->pluginPaths[''];
  375. }
  376. $pluginPaths = $this->getAllPluginPaths();
  377. $this->pluginPaths[''] = array();
  378. foreach ($this->getPlugins() as $plugin)
  379. {
  380. if (isset($pluginPaths[$plugin]))
  381. {
  382. $this->pluginPaths[''][] = $pluginPaths[$plugin];
  383. }
  384. else
  385. {
  386. throw new InvalidArgumentException(sprintf('The plugin "%s" does not exist.', $plugin));
  387. }
  388. }
  389. return $this->pluginPaths[''];
  390. }
  391. /**
  392. * Returns an array of paths for all available plugins.
  393. *
  394. * @return array
  395. */
  396. public function getAllPluginPaths()
  397. {
  398. $pluginPaths = array();
  399. $finder = sfFinder::type('dir')->maxdepth(0)->follow_link()->name('*Plugin');
  400. $dirs = array(
  401. sfConfig::get('sf_symfony_lib_dir').'/plugins',
  402. sfConfig::get('sf_plugins_dir'),
  403. );
  404. foreach ($finder->in($dirs) as $path)
  405. {
  406. $pluginPaths[basename($path)] = $path;
  407. }
  408. foreach ($this->overriddenPluginPaths as $plugin => $path)
  409. {
  410. $pluginPaths[$plugin] = $path;
  411. }
  412. return $pluginPaths;
  413. }
  414. /**
  415. * Manually sets the location of a particular plugin.
  416. *
  417. * This method can be used to ease functional testing of plugins. It is not
  418. * intended to support sharing plugins between projects, as many plugins
  419. * save project specific code (to /lib/form/base, for example).
  420. *
  421. * @param string $plugin
  422. * @param string $path
  423. */
  424. public function setPluginPath($plugin, $path)
  425. {
  426. $this->overriddenPluginPaths[$plugin] = realpath($path);
  427. }
  428. /**
  429. * Returns the configuration for the requested plugin.
  430. *
  431. * @param string $name
  432. *
  433. * @return sfPluginConfiguration
  434. */
  435. public function getPluginConfiguration($name)
  436. {
  437. if (!isset($this->pluginConfigurations[$name]))
  438. {
  439. throw new InvalidArgumentException(sprintf('There is no configuration object for the "%s" object.', $name));
  440. }
  441. return $this->pluginConfigurations[$name];
  442. }
  443. /**
  444. * Returns the event dispatcher.
  445. *
  446. * @return sfEventDispatcher A sfEventDispatcher instance
  447. */
  448. public function getEventDispatcher()
  449. {
  450. return $this->dispatcher;
  451. }
  452. /**
  453. * Returns the symfony lib directory.
  454. *
  455. * @return string The symfony lib directory
  456. */
  457. public function getSymfonyLibDir()
  458. {
  459. return $this->symfonyLibDir;
  460. }
  461. /**
  462. * Returns the active configuration.
  463. *
  464. * @return sfProjectConfiguration The current sfProjectConfiguration instance
  465. */
  466. static public function getActive()
  467. {
  468. if (is_null(sfProjectConfiguration::$active))
  469. {
  470. throw new RuntimeException('There is no active configuration.');
  471. }
  472. return sfProjectConfiguration::$active;
  473. }
  474. static public function guessRootDir()
  475. {
  476. $r = new ReflectionClass('ProjectConfiguration');
  477. return realpath(dirname($r->getFileName()).'/..');
  478. }
  479. /**
  480. * Returns a sfApplicationConfiguration configuration for a given application.
  481. *
  482. * @param string $application An application name
  483. * @param string $environment The environment name
  484. * @param Boolean $debug true to enable debug mode
  485. * @param string $rootDir The project root directory
  486. * @param sfEventDispatcher $dispatcher An event dispatcher
  487. *
  488. * @return sfApplicationConfiguration A sfApplicationConfiguration instance
  489. */
  490. static public function getApplicationConfiguration($application, $environment, $debug, $rootDir = null, sfEventDispatcher $dispatcher = null)
  491. {
  492. $class = $application.'Configuration';
  493. if (is_null($rootDir))
  494. {
  495. $rootDir = self::guessRootDir();
  496. }
  497. if (!file_exists($file = $rootDir.'/apps/'.$application.'/config/'.$class.'.class.php'))
  498. {
  499. throw new InvalidArgumentException(sprintf('The application "%s" does not exist.', $application));
  500. }
  501. require_once $file;
  502. return new $class($environment, $debug, $rootDir, $dispatcher);
  503. }
  504. /**
  505. * Calls methods defined via sfEventDispatcher.
  506. *
  507. * @param string $method The method name
  508. * @param array $arguments The method arguments
  509. *
  510. * @return mixed The returned value of the called method
  511. */
  512. public function __call($method, $arguments)
  513. {
  514. $event = $this->dispatcher->notifyUntil(new sfEvent($this, 'configuration.method_not_found', array('method' => $method, 'arguments' => $arguments)));
  515. if (!$event->isProcessed())
  516. {
  517. throw new sfException(sprintf('Call to undefined method %s::%s.', get_class($this), $method));
  518. }
  519. return $event->getReturnValue();
  520. }
  521. }