sfYamlConfigHandler.class.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. /*
  3. * This file is part of the symfony package.
  4. * (c) 2004-2006 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. * sfYamlConfigHandler is a base class for YAML (.yml) configuration handlers. This class
  11. * provides a central location for parsing YAML files.
  12. *
  13. * @package symfony
  14. * @subpackage config
  15. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  16. * @version SVN: $Id: sfYamlConfigHandler.class.php 9085 2008-05-20 01:53:23Z Carl.Vondrick $
  17. */
  18. abstract class sfYamlConfigHandler extends sfConfigHandler
  19. {
  20. protected
  21. $yamlConfig = null;
  22. /**
  23. * Parses an array of YAMLs files and merges them in one configuration array.
  24. *
  25. * @param array $configFiles An array of configuration file paths
  26. *
  27. * @return array A merged configuration array
  28. */
  29. static public function parseYamls($configFiles)
  30. {
  31. $config = array();
  32. foreach ($configFiles as $configFile)
  33. {
  34. $config = sfToolkit::arrayDeepMerge($config, self::parseYaml($configFile));
  35. }
  36. return $config;
  37. }
  38. /**
  39. * Parses a YAML (.yml) configuration file.
  40. *
  41. * @param string $configFile An absolute filesystem path to a configuration file
  42. *
  43. * @return string A parsed .yml configuration
  44. *
  45. * @throws sfConfigurationException If a requested configuration file does not exist or is not readable
  46. * @throws sfParseException If a requested configuration file is improperly formatted
  47. */
  48. static public function parseYaml($configFile)
  49. {
  50. if (!is_readable($configFile))
  51. {
  52. // can't read the configuration
  53. throw new sfConfigurationException(sprintf('Configuration file "%s" does not exist or is not readable.', $configFile));
  54. }
  55. // parse our config
  56. $config = sfYaml::load($configFile);
  57. if ($config === false)
  58. {
  59. // configuration couldn't be parsed
  60. throw new sfParseException(sprintf('Configuration file "%s" could not be parsed', $configFile));
  61. }
  62. return is_null($config) ? array() : $config;
  63. }
  64. /**
  65. * Merges configuration values for a given key and category.
  66. *
  67. * @param string $keyName The key name
  68. * @param string $category The category name
  69. *
  70. * @return string The value associated with this key name and category
  71. */
  72. protected function mergeConfigValue($keyName, $category)
  73. {
  74. $values = array();
  75. if (isset($this->yamlConfig['all'][$keyName]) && is_array($this->yamlConfig['all'][$keyName]))
  76. {
  77. $values = $this->yamlConfig['all'][$keyName];
  78. }
  79. if ($category && isset($this->yamlConfig[$category][$keyName]) && is_array($this->yamlConfig[$category][$keyName]))
  80. {
  81. $values = array_merge($values, $this->yamlConfig[$category][$keyName]);
  82. }
  83. return $values;
  84. }
  85. /**
  86. * Gets a configuration value for a given key and category.
  87. *
  88. * @param string $keyName The key name
  89. * @param string $category The category name
  90. * @param string $defaultValue The default value
  91. *
  92. * @return string The value associated with this key name and category
  93. */
  94. protected function getConfigValue($keyName, $category, $defaultValue = null)
  95. {
  96. if (isset($this->yamlConfig[$category][$keyName]))
  97. {
  98. return $this->yamlConfig[$category][$keyName];
  99. }
  100. else if (isset($this->yamlConfig['all'][$keyName]))
  101. {
  102. return $this->yamlConfig['all'][$keyName];
  103. }
  104. return $defaultValue;
  105. }
  106. static public function flattenConfiguration($config)
  107. {
  108. $config['all'] = sfToolkit::arrayDeepMerge(
  109. isset($config['default']) && is_array($config['default']) ? $config['default'] : array(),
  110. isset($config['all']) && is_array($config['all']) ? $config['all'] : array()
  111. );
  112. unset($config['default']);
  113. return $config;
  114. }
  115. /**
  116. * Merges default, all and current environment configurations.
  117. *
  118. * @param array $config The main configuratino array
  119. *
  120. * @return array The merged configuration
  121. */
  122. static public function flattenConfigurationWithEnvironment($config)
  123. {
  124. return sfToolkit::arrayDeepMerge(
  125. isset($config['default']) && is_array($config['default']) ? $config['default'] : array(),
  126. isset($config['all']) && is_array($config['all']) ? $config['all'] : array(),
  127. isset($config[sfConfig::get('sf_environment')]) && is_array($config[sfConfig::get('sf_environment')]) ? $config[sfConfig::get('sf_environment')] : array()
  128. );
  129. }
  130. }