| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
abstract class sfYamlConfigHandler extends sfConfigHandler |
|---|
| 21 |
{ |
|---|
| 22 |
protected |
|---|
| 23 |
$yamlConfig = null; |
|---|
| 24 |
|
|---|
| 25 |
|
|---|
| 26 |
* Parses an array of YAMLs files and merges them in one configuration array. |
|---|
| 27 |
* |
|---|
| 28 |
* @param array An array of configuration file paths |
|---|
| 29 |
* |
|---|
| 30 |
* @return array A merged configuration array |
|---|
| 31 |
*/ |
|---|
| 32 |
protected function parseYamls($configFiles) |
|---|
| 33 |
{ |
|---|
| 34 |
$config = array(); |
|---|
| 35 |
foreach ($configFiles as $configFile) |
|---|
| 36 |
{ |
|---|
| 37 |
$config = sfToolkit::arrayDeepMerge($config, $this->parseYaml($configFile)); |
|---|
| 38 |
} |
|---|
| 39 |
|
|---|
| 40 |
return $config; |
|---|
| 41 |
} |
|---|
| 42 |
|
|---|
| 43 |
|
|---|
| 44 |
* Parses a YAML (.yml) configuration file. |
|---|
| 45 |
* |
|---|
| 46 |
* @param string An absolute filesystem path to a configuration file |
|---|
| 47 |
* |
|---|
| 48 |
* @return string A parsed .yml configuration |
|---|
| 49 |
* |
|---|
| 50 |
* @throws sfConfigurationException If a requested configuration file does not exist or is not readable |
|---|
| 51 |
* @throws sfParseException If a requested configuration file is improperly formatted |
|---|
| 52 |
*/ |
|---|
| 53 |
protected function parseYaml($configFile) |
|---|
| 54 |
{ |
|---|
| 55 |
if (!is_readable($configFile)) |
|---|
| 56 |
{ |
|---|
| 57 |
|
|---|
| 58 |
$error = sprintf('Configuration file "%s" does not exist or is not readable', $configFile); |
|---|
| 59 |
|
|---|
| 60 |
throw new sfConfigurationException($error); |
|---|
| 61 |
} |
|---|
| 62 |
|
|---|
| 63 |
|
|---|
| 64 |
$config = sfYaml::load($configFile); |
|---|
| 65 |
|
|---|
| 66 |
if ($config === false || $config === null) |
|---|
| 67 |
{ |
|---|
| 68 |
|
|---|
| 69 |
$error = sprintf('Configuration file "%s" could not be parsed', $configFile); |
|---|
| 70 |
throw new sfParseException($error); |
|---|
| 71 |
} |
|---|
| 72 |
|
|---|
| 73 |
|
|---|
| 74 |
$categories = $this->getParameterHolder()->get('required_categories', array()); |
|---|
| 75 |
foreach ($categories as $category) |
|---|
| 76 |
{ |
|---|
| 77 |
if (!isset($config[$category])) |
|---|
| 78 |
{ |
|---|
| 79 |
$error = sprintf('Configuration file "%s" is missing "%s" category', $configFile, $category); |
|---|
| 80 |
throw new sfParseException($error); |
|---|
| 81 |
} |
|---|
| 82 |
} |
|---|
| 83 |
|
|---|
| 84 |
return $config; |
|---|
| 85 |
} |
|---|
| 86 |
|
|---|
| 87 |
|
|---|
| 88 |
* Merges configuration values for a given key and category. |
|---|
| 89 |
* |
|---|
| 90 |
* @param string The key name |
|---|
| 91 |
* @param string The category name |
|---|
| 92 |
* |
|---|
| 93 |
* @return string The value associated with this key name and category |
|---|
| 94 |
*/ |
|---|
| 95 |
protected function mergeConfigValue($keyName, $category) |
|---|
| 96 |
{ |
|---|
| 97 |
$values = array(); |
|---|
| 98 |
|
|---|
| 99 |
if (isset($this->yamlConfig['all'][$keyName]) && is_array($this->yamlConfig['all'][$keyName])) |
|---|
| 100 |
{ |
|---|
| 101 |
$values = $this->yamlConfig['all'][$keyName]; |
|---|
| 102 |
} |
|---|
| 103 |
|
|---|
| 104 |
if ($category && isset($this->yamlConfig[$category][$keyName]) && is_array($this->yamlConfig[$category][$keyName])) |
|---|
| 105 |
{ |
|---|
| 106 |
$values = array_merge($values, $this->yamlConfig[$category][$keyName]); |
|---|
| 107 |
} |
|---|
| 108 |
|
|---|
| 109 |
return $values; |
|---|
| 110 |
} |
|---|
| 111 |
|
|---|
| 112 |
|
|---|
| 113 |
* Gets a configuration value for a given key and category. |
|---|
| 114 |
* |
|---|
| 115 |
* @param string The key name |
|---|
| 116 |
* @param string The category name |
|---|
| 117 |
* @param string The default value |
|---|
| 118 |
* |
|---|
| 119 |
* @return string The value associated with this key name and category |
|---|
| 120 |
*/ |
|---|
| 121 |
protected function getConfigValue($keyName, $category, $defaultValue = null) |
|---|
| 122 |
{ |
|---|
| 123 |
if (isset($this->yamlConfig[$category][$keyName])) |
|---|
| 124 |
{ |
|---|
| 125 |
return $this->yamlConfig[$category][$keyName]; |
|---|
| 126 |
} |
|---|
| 127 |
else if (isset($this->yamlConfig['all'][$keyName])) |
|---|
| 128 |
{ |
|---|
| 129 |
return $this->yamlConfig['all'][$keyName]; |
|---|
| 130 |
} |
|---|
| 131 |
|
|---|
| 132 |
return $defaultValue; |
|---|
| 133 |
} |
|---|
| 134 |
} |
|---|
| 135 |
|
|---|