sfCompileConfigHandler.class.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. /*
  3. * This file is part of the symfony package.
  4. * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
  5. * (c) 2004-2006 Sean Kerr <sean@code-box.org>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. /**
  11. * sfCompileConfigHandler gathers multiple files and puts them into a single file.
  12. * Upon creation of the new file, all comments and blank lines are removed.
  13. *
  14. * @package symfony
  15. * @subpackage config
  16. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  17. * @author Sean Kerr <sean@code-box.org>
  18. * @version SVN: $Id: sfCompileConfigHandler.class.php 17858 2009-05-01 21:22:50Z FabianLange $
  19. */
  20. class sfCompileConfigHandler extends sfYamlConfigHandler
  21. {
  22. /**
  23. * Executes this configuration handler.
  24. *
  25. * @param array $configFiles An array of absolute filesystem path to a configuration file
  26. *
  27. * @return string Data to be written to a cache file
  28. *
  29. * @throws sfConfigurationException If a requested configuration file does not exist or is not readable
  30. * @throws sfParseException If a requested configuration file is improperly formatted
  31. */
  32. public function execute($configFiles)
  33. {
  34. // parse the yaml
  35. $config = self::getConfiguration($configFiles);
  36. // init our data
  37. $data = '';
  38. // let's do our fancy work
  39. foreach ($config as $file)
  40. {
  41. if (!is_readable($file))
  42. {
  43. // file doesn't exist
  44. throw new sfParseException(sprintf('Configuration file "%s" specifies nonexistent or unreadable file "%s".', $configFiles[0], $file));
  45. }
  46. $contents = file_get_contents($file);
  47. // strip comments (not in debug mode)
  48. if (!sfConfig::get('sf_debug'))
  49. {
  50. $contents = sfToolkit::stripComments($contents);
  51. }
  52. // insert configuration files
  53. /* $contents = preg_replace_callback(array('#(require|include)(_once)?\((sfContext::getInstance\(\)\->getConfigCache\(\)|\$configCache)->checkConfig\(\'config/([^\']+)\'\)\);#m',
  54. '#()()(sfContext::getInstance\(\)\->getConfigCache\(\)|\$configCache)->import\(\'config/([^\']+)\'(, false)?\);#m'),
  55. array($this, 'insertConfigFileCallback'), $contents);
  56. */
  57. // strip php tags
  58. $contents = sfToolkit::pregtr($contents, array('/^\s*<\?(php)?/m' => '',
  59. '/^\s*\?>/m' => ''));
  60. // replace windows and mac format with unix format
  61. $contents = str_replace("\r", "\n", $contents);
  62. // replace multiple new lines with a single newline
  63. $contents = preg_replace(array('/\s+$/Sm', '/\n+/S'), "\n", $contents);
  64. // append file data
  65. $data .= "\n".$contents;
  66. }
  67. // compile data
  68. $retval = sprintf("<?php\n".
  69. "// auto-generated by sfCompileConfigHandler\n".
  70. "// date: %s\n%s\n",
  71. date('Y/m/d H:i:s'), $data);
  72. // save current symfony release
  73. file_put_contents(sfConfig::get('sf_config_cache_dir').'/VERSION', SYMFONY_VERSION);
  74. return $retval;
  75. }
  76. /**
  77. * Callback for configuration file insertion in the cache.
  78. *
  79. */
  80. protected function insertConfigFileCallback($matches)
  81. {
  82. $configFile = 'config/'.$matches[4];
  83. $configCache = sfContext::getInstance()->getConfigCache();
  84. $configCache->checkConfig($configFile);
  85. $config = "// '$configFile' config file\n".file_get_contents($configCache->getCacheName($configFile));
  86. return $config;
  87. }
  88. /**
  89. * @see sfConfigHandler
  90. */
  91. static public function getConfiguration(array $configFiles)
  92. {
  93. $config = array();
  94. foreach ($configFiles as $configFile)
  95. {
  96. $config = array_merge($config, self::parseYaml($configFile));
  97. }
  98. return self::replacePath(self::replaceConstants($config));
  99. }
  100. }