| 1 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
/*
|
|
|
4 |
* This file is part of the symfony package.
|
|
|
5 |
* (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
|
|
|
6 |
*
|
|
|
7 |
* For the full copyright and license information, please view the LICENSE
|
|
|
8 |
* file that was distributed with this source code.
|
|
|
9 |
*/
|
|
|
10 |
|
|
|
11 |
/**
|
|
|
12 |
*
|
|
|
13 |
* @package symfony
|
|
|
14 |
* @subpackage config
|
|
|
15 |
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
|
|
|
16 |
* @version SVN: $Id: sfDefineEnvironmentConfigHandler.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
|
|
|
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 |
// get our prefix
|
|
|
33 |
$prefix = strtolower($this->getParameterHolder()->get('prefix', ''));
|
|
|
34 |
|
|
|
35 |
// add module prefix if needed
|
|
|
36 |
if ($this->getParameterHolder()->get('module', false))
|
|
|
37 |
{
|
|
|
38 |
$wildcardValues = $this->getParameterHolder()->get('wildcardValues');
|
|
|
39 |
// either the module name is in wildcard values, or it needs to be inserted on runtime
|
|
|
40 |
$moduleName = $wildcardValues ? strtolower($wildcardValues[0]) : "'.strtolower(\$moduleName).'";
|
|
|
41 |
$prefix .= $moduleName."_";
|
|
|
42 |
}
|
|
|
43 |
|
|
|
44 |
// parse the yaml
|
|
|
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 |
// compile data
|
|
|
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 |
// loop through all key/value pairs
|
|
|
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 |
// categories starting without a period will be prepended to the key
|
|
|
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 |
}
|