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

Debug toolbar