PageRenderTime 27ms CodeModel.GetById 36ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/config/sfApplicationConfiguration.class.php

https://github.com/bheneka/gitta
PHP | 700 lines | 414 code | 93 blank | 193 comment | 43 complexity | b73f9ab4b4b1de5bae9f74c9ff702510 MD5 | raw file
  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$
  16. */
  17. abstract class sfApplicationConfiguration extends ProjectConfiguration
  18. {
  19. static protected
  20. $coreLoaded = false,
  21. $loadedHelpers = array();
  22. protected
  23. $configCache = null,
  24. $application = null,
  25. $environment = null,
  26. $debug = false,
  27. $config = array(),
  28. $cache = null;
  29. /**
  30. * Constructor.
  31. *
  32. * @param string $environment The environment name
  33. * @param Boolean $debug true to enable debug mode
  34. * @param string $rootDir The project root directory
  35. * @param sfEventDispatcher $dispatcher An event dispatcher
  36. */
  37. public function __construct($environment, $debug, $rootDir = null, sfEventDispatcher $dispatcher = null)
  38. {
  39. $this->environment = $environment;
  40. $this->debug = (boolean) $debug;
  41. $this->application = str_replace('Configuration', '', get_class($this));
  42. parent::__construct($rootDir, $dispatcher);
  43. $this->configure();
  44. $this->initConfiguration();
  45. if (sfConfig::get('sf_check_lock'))
  46. {
  47. $this->checkLock();
  48. }
  49. if (file_exists($file = sfConfig::get('sf_app_cache_dir').'/config/configuration.php'))
  50. {
  51. $this->cache = require $file;
  52. }
  53. $this->initialize();
  54. // store current sfConfig values
  55. $this->config = sfConfig::getAll();
  56. }
  57. /**
  58. * Configures the current configuration.
  59. *
  60. * Override this method if you want to customize your application configuration.
  61. */
  62. public function configure()
  63. {
  64. }
  65. /**
  66. * Initialized the current configuration.
  67. *
  68. * Override this method if you want to customize your application initialization.
  69. */
  70. public function initialize()
  71. {
  72. }
  73. public function activate()
  74. {
  75. sfConfig::clear();
  76. sfConfig::add($this->config);
  77. }
  78. /**
  79. * Various initializations.
  80. */
  81. public function initConfiguration()
  82. {
  83. $configCache = $this->getConfigCache();
  84. // in debug mode, start global timer
  85. if ($this->isDebug() && !sfWebDebugPanelTimer::isStarted())
  86. {
  87. sfWebDebugPanelTimer::startTime();
  88. }
  89. // required core classes for the framework
  90. if (!$this->isDebug() && !sfConfig::get('sf_test') && !self::$coreLoaded)
  91. {
  92. $configCache->import('config/core_compile.yml', false);
  93. }
  94. // autoloader(s)
  95. $this->dispatcher->connect('autoload.filter_config', array($this, 'filterAutoloadConfig'));
  96. sfAutoload::getInstance()->register();
  97. if ($this->isDebug())
  98. {
  99. sfAutoloadAgain::getInstance()->register();
  100. }
  101. // load base settings
  102. include($configCache->checkConfig('config/settings.yml'));
  103. if ($file = $configCache->checkConfig('config/app.yml', true))
  104. {
  105. include($file);
  106. }
  107. if (false !== sfConfig::get('sf_csrf_secret'))
  108. {
  109. sfForm::enableCSRFProtection(sfConfig::get('sf_csrf_secret'));
  110. }
  111. sfWidget::setCharset(sfConfig::get('sf_charset'));
  112. sfValidatorBase::setCharset(sfConfig::get('sf_charset'));
  113. // force setting default timezone if not set
  114. if ($default_timezone = sfConfig::get('sf_default_timezone'))
  115. {
  116. date_default_timezone_set($default_timezone);
  117. }
  118. else if (sfConfig::get('sf_force_default_timezone', true))
  119. {
  120. date_default_timezone_set(@date_default_timezone_get());
  121. }
  122. // error settings
  123. ini_set('display_errors', $this->isDebug() ? 'on' : 'off');
  124. error_reporting(sfConfig::get('sf_error_reporting'));
  125. // initialize plugin configuration objects
  126. $this->initializePlugins();
  127. // compress output
  128. if (!self::$coreLoaded)
  129. {
  130. ob_start(sfConfig::get('sf_compressed') ? 'ob_gzhandler' : '');
  131. }
  132. self::$coreLoaded = true;
  133. }
  134. /**
  135. * Initializes plugin configuration objects.
  136. */
  137. protected function initializePlugins()
  138. {
  139. foreach ($this->pluginConfigurations as $name => $configuration)
  140. {
  141. if (
  142. false === $configuration->initialize()
  143. &&
  144. is_readable($config = $configuration->getRootDir().'/config/config.php')
  145. )
  146. {
  147. require $config;
  148. }
  149. }
  150. }
  151. /**
  152. * Adds enabled plugins to autoload config.
  153. *
  154. * @param sfEvent $event
  155. * @param array $config
  156. *
  157. * @return array
  158. */
  159. public function filterAutoloadConfig(sfEvent $event, array $config)
  160. {
  161. foreach ($this->pluginConfigurations as $name => $configuration)
  162. {
  163. $config = $configuration->filterAutoloadConfig($event, $config);
  164. }
  165. return $config;
  166. }
  167. /**
  168. * Returns a configuration cache object for the current configuration.
  169. *
  170. * @return sfConfigCache A sfConfigCache instance
  171. */
  172. public function getConfigCache()
  173. {
  174. if (null === $this->configCache)
  175. {
  176. $this->configCache = new sfConfigCache($this);
  177. }
  178. return $this->configCache;
  179. }
  180. /**
  181. * Check lock files to see if we're not in a cache cleaning process.
  182. *
  183. * @return void
  184. */
  185. public function checkLock()
  186. {
  187. if (
  188. $this->hasLockFile(sfConfig::get('sf_data_dir').DIRECTORY_SEPARATOR.$this->getApplication().'_'.$this->getEnvironment().'-cli.lck', 5)
  189. ||
  190. $this->hasLockFile(sfConfig::get('sf_data_dir').DIRECTORY_SEPARATOR.$this->getApplication().'_'.$this->getEnvironment().'.lck')
  191. )
  192. {
  193. // application is not available - we'll find the most specific unavailable page...
  194. $files = array(
  195. sfConfig::get('sf_app_config_dir').'/unavailable.php',
  196. sfConfig::get('sf_config_dir').'/unavailable.php',
  197. sfConfig::get('sf_web_dir').'/errors/unavailable.php',
  198. $this->getSymfonyLibDir().'/exception/data/unavailable.php',
  199. );
  200. foreach ($files as $file)
  201. {
  202. if (is_readable($file))
  203. {
  204. header("HTTP/1.1 503 Service Temporarily Unavailable");
  205. header("Status: 503 Service Temporarily Unavailable");
  206. include $file;
  207. break;
  208. }
  209. }
  210. die(1);
  211. }
  212. }
  213. /**
  214. * Determines if a lock file is present.
  215. *
  216. * @param string $lockFile Name of the lock file.
  217. * @param integer $maxLockFileLifeTime A max amount of life time for the lock file.
  218. *
  219. * @return bool true, if the lock file is present, otherwise false.
  220. */
  221. protected function hasLockFile($lockFile, $maxLockFileLifeTime = 0)
  222. {
  223. $isLocked = false;
  224. if (is_readable($lockFile) && ($last_access = fileatime($lockFile)))
  225. {
  226. $now = time();
  227. $timeDiff = $now - $last_access;
  228. if (!$maxLockFileLifeTime || $timeDiff < $maxLockFileLifeTime)
  229. {
  230. $isLocked = true;
  231. }
  232. else
  233. {
  234. $isLocked = @unlink($lockFile) ? false : true;
  235. }
  236. }
  237. return $isLocked;
  238. }
  239. /**
  240. * Sets the project root directory.
  241. *
  242. * @param string $rootDir The project root directory
  243. */
  244. public function setRootDir($rootDir)
  245. {
  246. parent::setRootDir($rootDir);
  247. sfConfig::add(array(
  248. 'sf_app' => $this->getApplication(),
  249. 'sf_environment' => $this->getEnvironment(),
  250. 'sf_debug' => $this->isDebug(),
  251. ));
  252. $this->setAppDir(sfConfig::get('sf_apps_dir').DIRECTORY_SEPARATOR.$this->getApplication());
  253. }
  254. /**
  255. * Sets the app directory.
  256. *
  257. * @param string $appDir The absolute path to the app dir.
  258. */
  259. public function setAppDir($appDir)
  260. {
  261. sfConfig::add(array(
  262. 'sf_app_dir' => $appDir,
  263. // SF_APP_DIR directory structure
  264. 'sf_app_config_dir' => $appDir.DIRECTORY_SEPARATOR.'config',
  265. 'sf_app_lib_dir' => $appDir.DIRECTORY_SEPARATOR.'lib',
  266. 'sf_app_module_dir' => $appDir.DIRECTORY_SEPARATOR.'modules',
  267. 'sf_app_template_dir' => $appDir.DIRECTORY_SEPARATOR.'templates',
  268. 'sf_app_i18n_dir' => $appDir.DIRECTORY_SEPARATOR.'i18n',
  269. ));
  270. }
  271. /**
  272. * @see sfProjectConfiguration
  273. */
  274. public function setCacheDir($cacheDir)
  275. {
  276. parent::setCacheDir($cacheDir);
  277. sfConfig::add(array(
  278. 'sf_app_base_cache_dir' => $cacheDir.DIRECTORY_SEPARATOR.$this->getApplication(),
  279. 'sf_app_cache_dir' => $appCacheDir = $cacheDir.DIRECTORY_SEPARATOR.$this->getApplication().DIRECTORY_SEPARATOR.$this->getEnvironment(),
  280. // SF_CACHE_DIR directory structure
  281. 'sf_template_cache_dir' => $appCacheDir.DIRECTORY_SEPARATOR.'template',
  282. 'sf_i18n_cache_dir' => $appCacheDir.DIRECTORY_SEPARATOR.'i18n',
  283. 'sf_config_cache_dir' => $appCacheDir.DIRECTORY_SEPARATOR.'config',
  284. 'sf_test_cache_dir' => $appCacheDir.DIRECTORY_SEPARATOR.'test',
  285. 'sf_module_cache_dir' => $appCacheDir.DIRECTORY_SEPARATOR.'modules',
  286. ));
  287. }
  288. /**
  289. * Gets directories where controller classes are stored for a given module.
  290. *
  291. * @param string $moduleName The module name
  292. *
  293. * @return array An array of directories
  294. */
  295. public function getControllerDirs($moduleName)
  296. {
  297. if (!isset($this->cache['getControllerDirs'][$moduleName]))
  298. {
  299. $dirs = array();
  300. $dirs[sfConfig::get('sf_app_module_dir').'/'.$moduleName.'/actions'] = false; // application
  301. foreach ($this->getPluginPaths() as $path)
  302. {
  303. if (is_dir($dir = $path.'/modules/'.$moduleName.'/actions'))
  304. {
  305. $dirs[$dir] = true; // plugins
  306. }
  307. }
  308. if (is_dir($dir = $this->getSymfonyLibDir().'/controller/'.$moduleName.'/actions'))
  309. {
  310. $dirs[$dir] = true; // core modules
  311. }
  312. $this->cache['getControllerDirs'][$moduleName] = $dirs;
  313. }
  314. return $this->cache['getControllerDirs'][$moduleName];
  315. }
  316. /**
  317. * Gets directories where lib files are stored for a given module.
  318. *
  319. * @param string $moduleName The module name
  320. *
  321. * @return array An array of directories
  322. */
  323. public function getLibDirs($moduleName)
  324. {
  325. $dirs = array();
  326. $dirs[] = sfConfig::get('sf_app_module_dir').'/'.$moduleName.'/lib'; // application
  327. $dirs = array_merge($dirs, $this->getPluginSubPaths('/modules/'.$moduleName.'/lib')); // plugins
  328. $dirs[] = $this->getSymfonyLibDir().'/controller/'.$moduleName.'/lib'; // core modules
  329. $dirs[] = sfConfig::get('sf_module_cache_dir').'/auto'.ucfirst($moduleName.'/lib'); // generated templates in cache
  330. return $dirs;
  331. }
  332. /**
  333. * Gets directories where template files are stored for a given module.
  334. *
  335. * @param string $moduleName The module name
  336. *
  337. * @return array An array of directories
  338. */
  339. public function getTemplateDirs($moduleName)
  340. {
  341. $dirs = array();
  342. $dirs[] = sfConfig::get('sf_app_module_dir').'/'.$moduleName.'/templates'; // application
  343. $dirs = array_merge($dirs, $this->getPluginSubPaths('/modules/'.$moduleName.'/templates')); // plugins
  344. $dirs[] = $this->getSymfonyLibDir().'/controller/'.$moduleName.'/templates'; // core modules
  345. $dirs[] = sfConfig::get('sf_module_cache_dir').'/auto'.ucfirst($moduleName.'/templates'); // generated templates in cache
  346. return $dirs;
  347. }
  348. /**
  349. * Gets the helper directories for a given module name.
  350. *
  351. * @param string $moduleName The module name
  352. *
  353. * @return array An array of directories
  354. */
  355. public function getHelperDirs($moduleName = '')
  356. {
  357. $dirs = array();
  358. if ($moduleName)
  359. {
  360. $dirs[] = sfConfig::get('sf_app_module_dir').'/'.$moduleName.'/lib/helper'; // module
  361. $dirs = array_merge($dirs, $this->getPluginSubPaths('/modules/'.$moduleName.'/lib/helper'));
  362. }
  363. return array_merge(
  364. $dirs,
  365. array(
  366. sfConfig::get('sf_app_lib_dir').'/helper', // application
  367. sfConfig::get('sf_lib_dir').'/helper', // project
  368. ),
  369. $this->getPluginSubPaths('/lib/helper'), // plugins
  370. array($this->getSymfonyLibDir().'/helper') // symfony
  371. );
  372. }
  373. /**
  374. * Gets the template directory to use for a given module and template file.
  375. *
  376. * @param string $moduleName The module name
  377. * @param string $templateFile The template file
  378. *
  379. * @return string A template directory
  380. */
  381. public function getTemplateDir($moduleName, $templateFile)
  382. {
  383. if (!isset($this->cache['getTemplateDir'][$moduleName][$templateFile]))
  384. {
  385. $this->cache['getTemplateDir'][$moduleName][$templateFile] = null;
  386. foreach ($this->getTemplateDirs($moduleName) as $dir)
  387. {
  388. if (is_readable($dir.'/'.$templateFile))
  389. {
  390. $this->cache['getTemplateDir'][$moduleName][$templateFile] = $dir;
  391. break;
  392. }
  393. }
  394. }
  395. return $this->cache['getTemplateDir'][$moduleName][$templateFile];
  396. }
  397. /**
  398. * Gets the template to use for a given module and template file.
  399. *
  400. * @param string $moduleName The module name
  401. * @param string $templateFile The template file
  402. *
  403. * @return string A template path
  404. */
  405. public function getTemplatePath($moduleName, $templateFile)
  406. {
  407. $dir = $this->getTemplateDir($moduleName, $templateFile);
  408. return $dir ? $dir.'/'.$templateFile : null;
  409. }
  410. /**
  411. * @see sfProjectConfiguration
  412. */
  413. public function getPluginPaths()
  414. {
  415. if (!isset($this->cache['getPluginPaths']))
  416. {
  417. $this->cache['getPluginPaths'] = parent::getPluginPaths();
  418. }
  419. return $this->cache['getPluginPaths'];
  420. }
  421. /**
  422. * Gets the decorator directories.
  423. *
  424. * @return array An array of the decorator directories
  425. */
  426. public function getDecoratorDirs()
  427. {
  428. return array(sfConfig::get('sf_app_template_dir'));
  429. }
  430. /**
  431. * Gets the decorator directory for a given template.
  432. *
  433. * @param string $template The template file
  434. *
  435. * @return string A template directory
  436. */
  437. public function getDecoratorDir($template)
  438. {
  439. foreach ($this->getDecoratorDirs() as $dir)
  440. {
  441. if (is_readable($dir.'/'.$template))
  442. {
  443. return $dir;
  444. }
  445. }
  446. }
  447. /**
  448. * Gets the i18n directories to use globally.
  449. *
  450. * @return array An array of i18n directories
  451. */
  452. public function getI18NGlobalDirs()
  453. {
  454. $dirs = array();
  455. // application
  456. if (is_dir($dir = sfConfig::get('sf_app_i18n_dir')))
  457. {
  458. $dirs[] = $dir;
  459. }
  460. // plugins
  461. return array_merge($dirs, $this->getPluginSubPaths('/i18n'));
  462. }
  463. /**
  464. * Gets the i18n directories to use for a given module.
  465. *
  466. * @param string $moduleName The module name
  467. *
  468. * @return array An array of i18n directories
  469. */
  470. public function getI18NDirs($moduleName)
  471. {
  472. $dirs = array();
  473. // module
  474. if (is_dir($dir = sfConfig::get('sf_app_module_dir').'/'.$moduleName.'/i18n'))
  475. {
  476. $dirs[] = $dir;
  477. }
  478. // application
  479. if (is_dir($dir = sfConfig::get('sf_app_i18n_dir')))
  480. {
  481. $dirs[] = $dir;
  482. }
  483. return array_merge(
  484. $dirs,
  485. $this->getPluginSubPaths('/modules/'.$moduleName.'/i18n'), // module in plugins
  486. $this->getPluginSubPaths('/i18n') // plugins
  487. );
  488. }
  489. /**
  490. * Gets the configuration file paths for a given relative configuration path.
  491. *
  492. * @param string $configPath The configuration path
  493. *
  494. * @return array An array of paths
  495. */
  496. public function getConfigPaths($configPath)
  497. {
  498. $globalConfigPath = basename(dirname($configPath)).'/'.basename($configPath);
  499. $files = array(
  500. $this->getSymfonyLibDir().'/config/'.$globalConfigPath, // symfony
  501. );
  502. foreach ($this->getPluginPaths() as $path)
  503. {
  504. if (is_file($file = $path.'/'.$globalConfigPath))
  505. {
  506. $files[] = $file; // plugins
  507. }
  508. }
  509. $files = array_merge($files, array(
  510. $this->getRootDir().'/'.$globalConfigPath, // project
  511. $this->getRootDir().'/'.$configPath, // project
  512. sfConfig::get('sf_app_dir').'/'.$globalConfigPath, // application
  513. sfConfig::get('sf_app_cache_dir').'/'.$configPath, // generated modules
  514. ));
  515. foreach ($this->getPluginPaths() as $path)
  516. {
  517. if (is_file($file = $path.'/'.$configPath))
  518. {
  519. $files[] = $file; // plugins
  520. }
  521. }
  522. $files[] = sfConfig::get('sf_app_dir').'/'.$configPath; // module
  523. $configs = array();
  524. foreach (array_unique($files) as $file)
  525. {
  526. if (is_readable($file))
  527. {
  528. $configs[] = $file;
  529. }
  530. }
  531. return $configs;
  532. }
  533. /**
  534. * Loads helpers.
  535. *
  536. * @param array $helpers An array of helpers to load
  537. * @param string $moduleName A module name (optional)
  538. */
  539. public function loadHelpers($helpers, $moduleName = '')
  540. {
  541. foreach ((array) $helpers as $helperName)
  542. {
  543. if (isset(self::$loadedHelpers[$helperName]))
  544. {
  545. continue;
  546. }
  547. if (isset($this->cache['loadedHelpers'][$moduleName][$helperName]))
  548. {
  549. include_once $this->cache['loadedHelpers'][$moduleName][$helperName];
  550. }
  551. else if (isset($this->cache['loadedHelpers'][''][$helperName]))
  552. {
  553. include_once $this->cache['loadedHelpers'][''][$helperName];
  554. }
  555. else
  556. {
  557. $fileName = $helperName.'Helper.php';
  558. if (!isset($dirs))
  559. {
  560. $dirs = $this->getHelperDirs($moduleName);
  561. }
  562. foreach ($dirs as $dir)
  563. {
  564. $included = false;
  565. if (is_readable($dir.'/'.$fileName))
  566. {
  567. include_once $dir.'/'.$fileName;
  568. $included = true;
  569. break;
  570. }
  571. }
  572. if (!$included)
  573. {
  574. throw new InvalidArgumentException(sprintf('Unable to load "%sHelper.php" helper in: %s.', $helperName, implode(', ', array_map(array('sfDebug', 'shortenFilePath'), $dirs))));
  575. }
  576. }
  577. self::$loadedHelpers[$helperName] = true;
  578. }
  579. }
  580. /**
  581. * Returns the application name.
  582. *
  583. * @return string The application name
  584. */
  585. public function getApplication()
  586. {
  587. return $this->application;
  588. }
  589. /**
  590. * Returns the environment name.
  591. *
  592. * @return string The environment name
  593. */
  594. public function getEnvironment()
  595. {
  596. return $this->environment;
  597. }
  598. /**
  599. * Returns true if this configuration has debug enabled.
  600. *
  601. * @return Boolean true if the configuration has debug enabled, false otherwise
  602. */
  603. public function isDebug()
  604. {
  605. return $this->debug;
  606. }
  607. }