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

Debug toolbar