PageRenderTime 36ms CodeModel.GetById 7ms RepoModel.GetById 1ms app.codeStats 0ms

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

http://openirudi.googlecode.com/
PHP | 563 lines | 316 code | 80 blank | 167 comment | 35 complexity | 99dc1ac3b546a2229252084a392f46e3 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. * sfConfiguration represents a configuration for a symfony application.
  11. *
  12. * @package symfony
  13. * @subpackage config
  14. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  15. * @version SVN: $Id: sfApplicationConfiguration.class.php 11929 2008-10-03 18:48:51Z fabien $
  16. */
  17. abstract class sfApplicationConfiguration extends ProjectConfiguration
  18. {
  19. static protected
  20. $coreLoaded = false;
  21. protected
  22. $configCache = null,
  23. $application = null,
  24. $environment = null,
  25. $debug = false,
  26. $config = array();
  27. /**
  28. * Constructor.
  29. *
  30. * @param string $environment The environment name
  31. * @param Boolean $debug true to enable debug mode
  32. * @param string $rootDir The project root directory
  33. * @param sfEventDispatcher $dispatcher An event dispatcher
  34. */
  35. public function __construct($environment, $debug, $rootDir = null, sfEventDispatcher $dispatcher = null)
  36. {
  37. $this->environment = $environment;
  38. $this->debug = (boolean) $debug;
  39. $this->application = str_replace('Configuration', '', get_class($this));
  40. parent::__construct($rootDir, $dispatcher);
  41. $this->configure();
  42. $this->initConfiguration();
  43. if (sfConfig::get('sf_check_lock'))
  44. {
  45. $this->checkLock();
  46. }
  47. if (sfConfig::get('sf_check_symfony_version'))
  48. {
  49. $this->checkSymfonyVersion();
  50. }
  51. $this->initialize();
  52. // store current sfConfig values
  53. $this->config = sfConfig::getAll();
  54. }
  55. /**
  56. * Configures the current configuration.
  57. *
  58. * Override this method if you want to customize your application configuration.
  59. */
  60. public function configure()
  61. {
  62. }
  63. /**
  64. * Initialized the current configuration.
  65. *
  66. * Override this method if you want to customize your application initialization.
  67. */
  68. public function initialize()
  69. {
  70. }
  71. public function activate()
  72. {
  73. sfConfig::clear();
  74. sfConfig::add($this->config);
  75. }
  76. /**
  77. * @see sfProjectConfiguration
  78. */
  79. public function initConfiguration()
  80. {
  81. // in debug mode, start global timer
  82. if ($this->isDebug() && !sfConfig::get('sf_timer_start'))
  83. {
  84. sfConfig::set('sf_timer_start', microtime(true));
  85. }
  86. $configCache = $this->getConfigCache();
  87. // required core classes for the framework
  88. if (!sfConfig::get('sf_debug') && !sfConfig::get('sf_test') && !self::$coreLoaded)
  89. {
  90. $configCache->import('config/core_compile.yml', false);
  91. }
  92. sfAutoload::getInstance()->register();
  93. // load base settings
  94. include($configCache->checkConfig('config/settings.yml'));
  95. if ($file = $configCache->checkConfig('config/app.yml', true))
  96. {
  97. include($file);
  98. }
  99. if (false !== sfConfig::get('sf_csrf_secret'))
  100. {
  101. sfForm::enableCSRFProtection(sfConfig::get('sf_csrf_secret'));
  102. }
  103. sfWidget::setCharset(sfConfig::get('sf_charset'));
  104. sfValidatorBase::setCharset(sfConfig::get('sf_charset'));
  105. // force setting default timezone if not set
  106. if ($default_timezone = sfConfig::get('sf_default_timezone'))
  107. {
  108. date_default_timezone_set($default_timezone);
  109. }
  110. else if (sfConfig::get('sf_force_default_timezone', true))
  111. {
  112. date_default_timezone_set(@date_default_timezone_get());
  113. }
  114. // error settings
  115. ini_set('display_errors', $this->isDebug() ? 'on' : 'off');
  116. error_reporting(sfConfig::get('sf_error_reporting'));
  117. // include all config.php from plugins
  118. $this->loadPluginConfig();
  119. // Disabled by default in symfony 1.1 because it causes problems with Doctrine.
  120. // If you want to enable it in your application, just copy the spl_autoload_register() line
  121. // in your configuration class.
  122. if (0 && $this->isDebug())
  123. {
  124. spl_autoload_register(array(sfAutoload::getInstance(), 'autoloadAgain'));
  125. }
  126. // compress output
  127. if (!self::$coreLoaded)
  128. {
  129. ob_start(sfConfig::get('sf_compressed') ? 'ob_gzhandler' : '');
  130. }
  131. self::$coreLoaded = true;
  132. }
  133. /**
  134. * Returns a configuration cache object for the current configuration.
  135. *
  136. * @return sfConfigCache A sfConfigCache instance
  137. */
  138. public function getConfigCache()
  139. {
  140. if (is_null($this->configCache))
  141. {
  142. $this->configCache = new sfConfigCache($this);
  143. }
  144. return $this->configCache;
  145. }
  146. /**
  147. * Check lock files to see if we're not in a cache cleaning process.
  148. *
  149. * @return void
  150. */
  151. public function checkLock()
  152. {
  153. if (
  154. sfToolkit::hasLockFile(sfConfig::get('sf_data_dir').DIRECTORY_SEPARATOR.$this->getApplication().'_'.$this->getEnvironment().'-cli.lck', 5)
  155. ||
  156. sfToolkit::hasLockFile(sfConfig::get('sf_data_dir').DIRECTORY_SEPARATOR.$this->getApplication().'_'.$this->getEnvironment().'.lck')
  157. )
  158. {
  159. // application is not available - we'll find the most specific unavailable page...
  160. $files = array(
  161. sfConfig::get('sf_app_config_dir').'/unavailable.php',
  162. sfConfig::get('sf_config_dir').'/unavailable.php',
  163. sfConfig::get('sf_web_dir').'/errors/unavailable.php',
  164. sfConfig::get('sf_symfony_lib_dir').'/exception/data/unavailable.php',
  165. );
  166. foreach ($files as $file)
  167. {
  168. if (is_readable($file))
  169. {
  170. include $file;
  171. break;
  172. }
  173. }
  174. die(1);
  175. }
  176. }
  177. /**
  178. * Checks symfony version and clears cache if recent update.
  179. *
  180. * @return void
  181. */
  182. public function checkSymfonyVersion()
  183. {
  184. // recent symfony update?
  185. if (SYMFONY_VERSION != @file_get_contents(sfConfig::get('sf_config_cache_dir').'/VERSION'))
  186. {
  187. // clear cache
  188. sfToolkit::clearDirectory(sfConfig::get('sf_config_cache_dir'));
  189. }
  190. }
  191. /**
  192. * Sets the project root directory.
  193. *
  194. * @param string $rootDir The project root directory
  195. */
  196. public function setRootDir($rootDir)
  197. {
  198. parent::setRootDir($rootDir);
  199. sfConfig::add(array(
  200. 'sf_app' => $this->getApplication(),
  201. 'sf_environment' => $this->getEnvironment(),
  202. 'sf_debug' => $this->isDebug(),
  203. ));
  204. $this->setAppDir(sfConfig::get('sf_apps_dir').DIRECTORY_SEPARATOR.$this->getApplication());
  205. }
  206. /**
  207. * Sets the app directory.
  208. *
  209. * @param string $appDir The absolute path to the app dir.
  210. */
  211. public function setAppDir($appDir)
  212. {
  213. sfConfig::add(array(
  214. 'sf_app_dir' => $appDir,
  215. // SF_APP_DIR directory structure
  216. 'sf_app_config_dir' => $appDir.DIRECTORY_SEPARATOR.'config',
  217. 'sf_app_lib_dir' => $appDir.DIRECTORY_SEPARATOR.'lib',
  218. 'sf_app_module_dir' => $appDir.DIRECTORY_SEPARATOR.'modules',
  219. 'sf_app_template_dir' => $appDir.DIRECTORY_SEPARATOR.'templates',
  220. 'sf_app_i18n_dir' => $appDir.DIRECTORY_SEPARATOR.'i18n',
  221. ));
  222. }
  223. /**
  224. * @see sfProjectConfiguration
  225. */
  226. public function setCacheDir($cacheDir)
  227. {
  228. parent::setCacheDir($cacheDir);
  229. sfConfig::add(array(
  230. 'sf_app_base_cache_dir' => $cacheDir.DIRECTORY_SEPARATOR.$this->getApplication(),
  231. 'sf_app_cache_dir' => $appCacheDir = $cacheDir.DIRECTORY_SEPARATOR.$this->getApplication().DIRECTORY_SEPARATOR.$this->getEnvironment(),
  232. // SF_CACHE_DIR directory structure
  233. 'sf_template_cache_dir' => $appCacheDir.DIRECTORY_SEPARATOR.'template',
  234. 'sf_i18n_cache_dir' => $appCacheDir.DIRECTORY_SEPARATOR.'i18n',
  235. 'sf_config_cache_dir' => $appCacheDir.DIRECTORY_SEPARATOR.'config',
  236. 'sf_test_cache_dir' => $appCacheDir.DIRECTORY_SEPARATOR.'test',
  237. 'sf_module_cache_dir' => $appCacheDir.DIRECTORY_SEPARATOR.'modules',
  238. ));
  239. }
  240. /**
  241. * Gets directories where controller classes are stored for a given module.
  242. *
  243. * @param string $moduleName The module name
  244. *
  245. * @return array An array of directories
  246. */
  247. public function getControllerDirs($moduleName)
  248. {
  249. $dirs = array();
  250. foreach (sfConfig::get('sf_module_dirs', array()) as $key => $value)
  251. {
  252. $dirs[$key.'/'.$moduleName.'/actions'] = $value;
  253. }
  254. $dirs[sfConfig::get('sf_app_module_dir').'/'.$moduleName.'/actions'] = false; // application
  255. if ($pluginDirs = glob(sfConfig::get('sf_plugins_dir').'/*/modules/'.$moduleName.'/actions'))
  256. {
  257. $dirs = array_merge($dirs, array_combine($pluginDirs, array_fill(0, count($pluginDirs), true))); // plugins
  258. }
  259. $dirs[sfConfig::get('sf_symfony_lib_dir').'/controller/'.$moduleName.'/actions'] = true; // core modules
  260. return $dirs;
  261. }
  262. /**
  263. * Gets directories where template files are stored for a given module.
  264. *
  265. * @param string $moduleName The module name
  266. *
  267. * @return array An array of directories
  268. */
  269. public function getTemplateDirs($moduleName)
  270. {
  271. $dirs = array();
  272. foreach (sfConfig::get('sf_module_dirs', array()) as $key => $value)
  273. {
  274. $dirs[] = $key.'/'.$moduleName.'/templates';
  275. }
  276. $dirs[] = sfConfig::get('sf_app_module_dir').'/'.$moduleName.'/templates'; // application
  277. if ($pluginDirs = glob(sfConfig::get('sf_plugins_dir').'/*/modules/'.$moduleName.'/templates'))
  278. {
  279. $dirs = array_merge($dirs, $pluginDirs); // plugins
  280. }
  281. $dirs[] = sfConfig::get('sf_symfony_lib_dir').'/controller/'.$moduleName.'/templates'; // core modules
  282. $dirs[] = sfConfig::get('sf_module_cache_dir').'/auto'.ucfirst($moduleName.'/templates'); // generated templates in cache
  283. return $dirs;
  284. }
  285. /**
  286. * Gets the template directory to use for a given module and template file.
  287. *
  288. * @param string $moduleName The module name
  289. * @param string $templateFile The template file
  290. *
  291. * @return string A template directory
  292. */
  293. public function getTemplateDir($moduleName, $templateFile)
  294. {
  295. foreach ($this->getTemplateDirs($moduleName) as $dir)
  296. {
  297. if (is_readable($dir.'/'.$templateFile))
  298. {
  299. return $dir;
  300. }
  301. }
  302. return null;
  303. }
  304. /**
  305. * Gets the template to use for a given module and template file.
  306. *
  307. * @param string $moduleName The module name
  308. * @param string $templateFile The template file
  309. *
  310. * @return string A template path
  311. */
  312. public function getTemplatePath($moduleName, $templateFile)
  313. {
  314. $dir = $this->getTemplateDir($moduleName, $templateFile);
  315. return $dir ? $dir.'/'.$templateFile : null;
  316. }
  317. /**
  318. * Gets the decorator directories.
  319. *
  320. * @return array An array of the decorator directories
  321. */
  322. public function getDecoratorDirs()
  323. {
  324. return array(sfConfig::get('sf_app_template_dir'));
  325. }
  326. /**
  327. * Gets the decorator directory for a given template.
  328. *
  329. * @param string $template The template file
  330. *
  331. * @return string A template directory
  332. */
  333. public function getDecoratorDir($template)
  334. {
  335. foreach ($this->getDecoratorDirs() as $dir)
  336. {
  337. if (is_readable($dir.'/'.$template))
  338. {
  339. return $dir;
  340. }
  341. }
  342. }
  343. /**
  344. * Gets the i18n directories to use globally.
  345. *
  346. * @return array An array of i18n directories
  347. */
  348. public function getI18NGlobalDirs()
  349. {
  350. $dirs = array();
  351. // application
  352. if (is_dir($dir = sfConfig::get('sf_app_dir').'/i18n'))
  353. {
  354. $dirs[] = $dir;
  355. }
  356. // plugins
  357. $pluginDirs = glob(sfConfig::get('sf_plugins_dir').'/*/i18n');
  358. if (isset($pluginDirs[0]))
  359. {
  360. $dirs[] = $pluginDirs[0];
  361. }
  362. return $dirs;
  363. }
  364. /**
  365. * Gets the i18n directories to use for a given module.
  366. *
  367. * @param string $moduleName The module name
  368. *
  369. * @return array An array of i18n directories
  370. */
  371. public function getI18NDirs($moduleName)
  372. {
  373. $dirs = array();
  374. // module
  375. if (is_dir($dir = sfConfig::get('sf_app_module_dir').'/'.$moduleName.'/i18n'))
  376. {
  377. $dirs[] = $dir;
  378. }
  379. // application
  380. if (is_dir($dir = sfConfig::get('sf_app_dir').'/i18n'))
  381. {
  382. $dirs[] = $dir;
  383. }
  384. // module in plugins
  385. $pluginDirs = glob(sfConfig::get('sf_plugins_dir').'/*/modules/'.$moduleName.'/i18n');
  386. if (isset($pluginDirs[0]))
  387. {
  388. $dirs[] = $pluginDirs[0];
  389. }
  390. // plugins
  391. $pluginDirs = glob(sfConfig::get('sf_plugins_dir').'/*/i18n');
  392. if (isset($pluginDirs[0]))
  393. {
  394. $dirs[] = $pluginDirs[0];
  395. }
  396. return $dirs;
  397. }
  398. /**
  399. * Gets the configuration file paths for a given relative configuration path.
  400. *
  401. * @param string $configPath The configuration path
  402. *
  403. * @return array An array of paths
  404. */
  405. public function getConfigPaths($configPath)
  406. {
  407. $globalConfigPath = basename(dirname($configPath)).'/'.basename($configPath);
  408. $files = array(
  409. sfConfig::get('sf_symfony_lib_dir').'/config/'.$globalConfigPath, // symfony
  410. );
  411. if ($bundledPluginDirs = glob(sfConfig::get('sf_symfony_lib_dir').'/plugins/*/'.$globalConfigPath))
  412. {
  413. $files = array_merge($files, $bundledPluginDirs); // bundled plugins
  414. }
  415. if ($pluginDirs = glob(sfConfig::get('sf_plugins_dir').'/*/'.$globalConfigPath))
  416. {
  417. $files = array_merge($files, $pluginDirs); // plugins
  418. }
  419. $files = array_merge($files, array(
  420. sfConfig::get('sf_root_dir').'/'.$globalConfigPath, // project
  421. sfConfig::get('sf_root_dir').'/'.$configPath, // project
  422. sfConfig::get('sf_app_dir').'/'.$globalConfigPath, // application
  423. sfConfig::get('sf_app_cache_dir').'/'.$configPath, // generated modules
  424. ));
  425. if ($pluginDirs = glob(sfConfig::get('sf_plugins_dir').'/*/'.$configPath))
  426. {
  427. $files = array_merge($files, $pluginDirs); // plugins
  428. }
  429. $files[] = sfConfig::get('sf_app_dir').'/'.$configPath; // module
  430. $configs = array();
  431. foreach (array_unique($files) as $file)
  432. {
  433. if (is_readable($file))
  434. {
  435. $configs[] = $file;
  436. }
  437. }
  438. return $configs;
  439. }
  440. /**
  441. * Loads config.php files from plugins
  442. *
  443. * @return void
  444. */
  445. public function loadPluginConfig()
  446. {
  447. foreach ($this->getPluginPaths() as $path)
  448. {
  449. $config = $path.'/config/config.php';
  450. if (is_readable($config))
  451. {
  452. require $config;
  453. }
  454. }
  455. }
  456. /**
  457. * Returns the application name.
  458. *
  459. * @return string The application name
  460. */
  461. public function getApplication()
  462. {
  463. return $this->application;
  464. }
  465. /**
  466. * Returns the environment name.
  467. *
  468. * @return string The environment name
  469. */
  470. public function getEnvironment()
  471. {
  472. return $this->environment;
  473. }
  474. /**
  475. * Returns true if this configuration has debug enabled.
  476. *
  477. * @return Boolean true if the configuration has debug enabled, false otherwise
  478. */
  479. public function isDebug()
  480. {
  481. return $this->debug;
  482. }
  483. }