sfApplicationConfiguration.class.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  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 14219 2008-12-20 11:04:56Z 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' : null);
  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_i18n_dir')))
  353. {
  354. $dirs[] = $dir;
  355. }
  356. // plugins
  357. if ($pluginDirs = glob(sfConfig::get('sf_plugins_dir').'/*/i18n'))
  358. {
  359. $dirs = array_merge($dirs, $pluginDirs);
  360. }
  361. return $dirs;
  362. }
  363. /**
  364. * Gets the i18n directories to use for a given module.
  365. *
  366. * @param string $moduleName The module name
  367. *
  368. * @return array An array of i18n directories
  369. */
  370. public function getI18NDirs($moduleName)
  371. {
  372. $dirs = array();
  373. // module
  374. if (is_dir($dir = sfConfig::get('sf_app_module_dir').'/'.$moduleName.'/i18n'))
  375. {
  376. $dirs[] = $dir;
  377. }
  378. // application
  379. if (is_dir($dir = sfConfig::get('sf_app_i18n_dir')))
  380. {
  381. $dirs[] = $dir;
  382. }
  383. // modules in plugins
  384. if ($pluginDirs = glob(sfConfig::get('sf_plugins_dir').'/*/modules/'.$moduleName.'/i18n'))
  385. {
  386. $dirs = array_merge($dirs, $pluginDirs);
  387. }
  388. // plugins
  389. if ($pluginDirs = glob(sfConfig::get('sf_plugins_dir').'/*/i18n'))
  390. {
  391. $dirs = array_merge($dirs, $pluginDirs);
  392. }
  393. return $dirs;
  394. }
  395. /**
  396. * Gets the configuration file paths for a given relative configuration path.
  397. *
  398. * @param string $configPath The configuration path
  399. *
  400. * @return array An array of paths
  401. */
  402. public function getConfigPaths($configPath)
  403. {
  404. $globalConfigPath = basename(dirname($configPath)).'/'.basename($configPath);
  405. $files = array(
  406. sfConfig::get('sf_symfony_lib_dir').'/config/'.$globalConfigPath, // symfony
  407. );
  408. if ($bundledPluginDirs = glob(sfConfig::get('sf_symfony_lib_dir').'/plugins/*/'.$globalConfigPath))
  409. {
  410. $files = array_merge($files, $bundledPluginDirs); // bundled plugins
  411. }
  412. if ($pluginDirs = glob(sfConfig::get('sf_plugins_dir').'/*/'.$globalConfigPath))
  413. {
  414. $files = array_merge($files, $pluginDirs); // plugins
  415. }
  416. $files = array_merge($files, array(
  417. sfConfig::get('sf_root_dir').'/'.$globalConfigPath, // project
  418. sfConfig::get('sf_root_dir').'/'.$configPath, // project
  419. sfConfig::get('sf_app_dir').'/'.$globalConfigPath, // application
  420. sfConfig::get('sf_app_cache_dir').'/'.$configPath, // generated modules
  421. ));
  422. if ($pluginDirs = glob(sfConfig::get('sf_plugins_dir').'/*/'.$configPath))
  423. {
  424. $files = array_merge($files, $pluginDirs); // plugins
  425. }
  426. $files[] = sfConfig::get('sf_app_dir').'/'.$configPath; // module
  427. $configs = array();
  428. foreach (array_unique($files) as $file)
  429. {
  430. if (is_readable($file))
  431. {
  432. $configs[] = $file;
  433. }
  434. }
  435. return $configs;
  436. }
  437. /**
  438. * Loads config.php files from plugins
  439. *
  440. * @return void
  441. */
  442. public function loadPluginConfig()
  443. {
  444. foreach ($this->getPluginPaths() as $path)
  445. {
  446. $config = $path.'/config/config.php';
  447. if (is_readable($config))
  448. {
  449. require $config;
  450. }
  451. }
  452. }
  453. /**
  454. * Returns the application name.
  455. *
  456. * @return string The application name
  457. */
  458. public function getApplication()
  459. {
  460. return $this->application;
  461. }
  462. /**
  463. * Returns the environment name.
  464. *
  465. * @return string The environment name
  466. */
  467. public function getEnvironment()
  468. {
  469. return $this->environment;
  470. }
  471. /**
  472. * Returns true if this configuration has debug enabled.
  473. *
  474. * @return Boolean true if the configuration has debug enabled, false otherwise
  475. */
  476. public function isDebug()
  477. {
  478. return $this->debug;
  479. }
  480. }