| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
class sfDefineEnvironmentConfigHandler extends sfYamlConfigHandler |
|---|
| 19 |
{ |
|---|
| 20 |
|
|---|
| 21 |
* Executes this configuration handler. |
|---|
| 22 |
* |
|---|
| 23 |
* @param string $configFiles An absolute filesystem path to a configuration file |
|---|
| 24 |
* |
|---|
| 25 |
* @return string Data to be written to a cache file |
|---|
| 26 |
* |
|---|
| 27 |
* @throws sfConfigurationException If a requested configuration file does not exist or is not readable |
|---|
| 28 |
* @throws sfParseException If a requested configuration file is improperly formatted |
|---|
| 29 |
*/ |
|---|
| 30 |
public function execute($configFiles) |
|---|
| 31 |
{ |
|---|
| 32 |
|
|---|
| 33 |
$prefix = strtolower($this->getParameterHolder()->get('prefix', '')); |
|---|
| 34 |
|
|---|
| 35 |
|
|---|
| 36 |
if ($this->getParameterHolder()->get('module', false)) |
|---|
| 37 |
{ |
|---|
| 38 |
$wildcardValues = $this->getParameterHolder()->get('wildcardValues'); |
|---|
| 39 |
|
|---|
| 40 |
$moduleName = $wildcardValues ? strtolower($wildcardValues[0]) : "'.strtolower(\$moduleName).'"; |
|---|
| 41 |
$prefix .= $moduleName."_"; |
|---|
| 42 |
} |
|---|
| 43 |
|
|---|
| 44 |
|
|---|
| 45 |
$config = self::getConfiguration($configFiles); |
|---|
| 46 |
|
|---|
| 47 |
$values = array(); |
|---|
| 48 |
foreach ($config as $category => $keys) |
|---|
| 49 |
{ |
|---|
| 50 |
$values = array_merge($values, $this->getValues($prefix, $category, $keys)); |
|---|
| 51 |
} |
|---|
| 52 |
|
|---|
| 53 |
$data = ''; |
|---|
| 54 |
foreach ($values as $key => $value) |
|---|
| 55 |
{ |
|---|
| 56 |
$data .= sprintf(" '%s' => %s,\n", $key, var_export($value, true)); |
|---|
| 57 |
} |
|---|
| 58 |
|
|---|
| 59 |
|
|---|
| 60 |
$retval = ''; |
|---|
| 61 |
if ($values) |
|---|
| 62 |
{ |
|---|
| 63 |
$retval = "<?php\n". |
|---|
| 64 |
"// auto-generated by sfDefineEnvironmentConfigHandler\n". |
|---|
| 65 |
"// date: %s\nsfConfig::add(array(\n%s));\n"; |
|---|
| 66 |
$retval = sprintf($retval, date('Y/m/d H:i:s'), $data); |
|---|
| 67 |
} |
|---|
| 68 |
|
|---|
| 69 |
return $retval; |
|---|
| 70 |
} |
|---|
| 71 |
|
|---|
| 72 |
|
|---|
| 73 |
* Gets values from the configuration array. |
|---|
| 74 |
* |
|---|
| 75 |
* @param string $prefix The prefix name |
|---|
| 76 |
* @param string $category The category name |
|---|
| 77 |
* @param mixed $keys The key/value array |
|---|
| 78 |
* |
|---|
| 79 |
* @return array The new key/value array |
|---|
| 80 |
*/ |
|---|
| 81 |
protected function getValues($prefix, $category, $keys) |
|---|
| 82 |
{ |
|---|
| 83 |
if (!is_array($keys)) |
|---|
| 84 |
{ |
|---|
| 85 |
list($key, $value) = $this->fixCategoryValue($prefix.strtolower($category), '', $keys); |
|---|
| 86 |
|
|---|
| 87 |
return array($key => $value); |
|---|
| 88 |
} |
|---|
| 89 |
|
|---|
| 90 |
$values = array(); |
|---|
| 91 |
|
|---|
| 92 |
$category = $this->fixCategoryName($category, $prefix); |
|---|
| 93 |
|
|---|
| 94 |
|
|---|
| 95 |
foreach ($keys as $key => $value) |
|---|
| 96 |
{ |
|---|
| 97 |
list($key, $value) = $this->fixCategoryValue($category, $key, $value); |
|---|
| 98 |
$values[$key] = $value; |
|---|
| 99 |
} |
|---|
| 100 |
|
|---|
| 101 |
return $values; |
|---|
| 102 |
} |
|---|
| 103 |
|
|---|
| 104 |
|
|---|
| 105 |
* Fixes the category name and replaces constants in the value. |
|---|
| 106 |
* |
|---|
| 107 |
* @param string $category The category name |
|---|
| 108 |
* @param string $key The key name |
|---|
| 109 |
* @param string $value The value |
|---|
| 110 |
* |
|---|
| 111 |
* @return string Return the new key and value |
|---|
| 112 |
*/ |
|---|
| 113 |
protected function fixCategoryValue($category, $key, $value) |
|---|
| 114 |
{ |
|---|
| 115 |
return array($category.$key, $value); |
|---|
| 116 |
} |
|---|
| 117 |
|
|---|
| 118 |
|
|---|
| 119 |
* Fixes the category name. |
|---|
| 120 |
* |
|---|
| 121 |
* @param string $category The category name |
|---|
| 122 |
* @param string $prefix The prefix |
|---|
| 123 |
* |
|---|
| 124 |
* @return string The fixed category name |
|---|
| 125 |
*/ |
|---|
| 126 |
protected function fixCategoryName($category, $prefix) |
|---|
| 127 |
{ |
|---|
| 128 |
|
|---|
| 129 |
if ($category[0] != '.') |
|---|
| 130 |
{ |
|---|
| 131 |
$category = $prefix.$category.'_'; |
|---|
| 132 |
} |
|---|
| 133 |
else |
|---|
| 134 |
{ |
|---|
| 135 |
$category = $prefix; |
|---|
| 136 |
} |
|---|
| 137 |
|
|---|
| 138 |
return $category; |
|---|
| 139 |
} |
|---|
| 140 |
|
|---|
| 141 |
|
|---|
| 142 |
* @see sfConfigHandler |
|---|
| 143 |
*/ |
|---|
| 144 |
static public function getConfiguration(array $configFiles) |
|---|
| 145 |
{ |
|---|
| 146 |
return self::replaceConstants(self::flattenConfigurationWithEnvironment(self::parseYamls($configFiles))); |
|---|
| 147 |
} |
|---|
| 148 |
} |
|---|
| 149 |
|
|---|