| 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 |
* sfYamlConfigHandler is a base class for YAML (.yml) configuration handlers. This class
|
|
|
13 |
* provides a central location for parsing YAML files.
|
|
|
14 |
*
|
|
|
15 |
* @package symfony
|
|
|
16 |
* @subpackage config
|
|
|
17 |
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
|
|
|
18 |
* @version SVN: $Id: sfYamlConfigHandler.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
|
|
|
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 $configFiles An array of configuration file paths
|
|
|
29 |
*
|
|
|
30 |
* @return array A merged configuration array
|
|
|
31 |
*/
|
|
|
32 |
static public function parseYamls($configFiles)
|
|
|
33 |
{
|
|
|
34 |
$config = array();
|
|
|
35 |
foreach ($configFiles as $configFile)
|
|
|
36 |
{
|
|
|
37 |
// the first level is an environment and its value must be an array
|
|
|
38 |
$values = array();
|
|
|
39 |
foreach (self::parseYaml($configFile) as $env => $value)
|
|
|
40 |
{
|
|
|
41 |
if (null !== $value)
|
|
|
42 |
{
|
|
|
43 |
$values[$env] = $value;
|
|
|
44 |
}
|
|
|
45 |
}
|
|
|
46 |
|
|
|
47 |
$config = sfToolkit::arrayDeepMerge($config, $values);
|
|
|
48 |
}
|
|
|
49 |
|
|
|
50 |
return $config;
|
|
|
51 |
}
|
|
|
52 |
|
|
|
53 |
/**
|
|
|
54 |
* Parses a YAML (.yml) configuration file.
|
|
|
55 |
*
|
|
|
56 |
* @param string $configFile An absolute filesystem path to a configuration file
|
|
|
57 |
*
|
|
|
58 |
* @return string A parsed .yml configuration
|
|
|
59 |
*
|
|
|
60 |
* @throws sfConfigurationException If a requested configuration file does not exist or is not readable
|
|
|
61 |
* @throws sfParseException If a requested configuration file is improperly formatted
|
|
|
62 |
*/
|
|
|
63 |
static public function parseYaml($configFile)
|
|
|
64 |
{
|
|
|
65 |
if (!is_readable($configFile))
|
|
|
66 |
{
|
|
|
67 |
// can't read the configuration
|
|
|
68 |
throw new sfConfigurationException(sprintf('Configuration file "%s" does not exist or is not readable.', $configFile));
|
|
|
69 |
}
|
|
|
70 |
|
|
|
71 |
// parse our config
|
|
|
72 |
$config = sfYaml::load($configFile);
|
|
|
73 |
|
|
|
74 |
if ($config === false)
|
|
|
75 |
{
|
|
|
76 |
// configuration couldn't be parsed
|
|
|
77 |
throw new sfParseException(sprintf('Configuration file "%s" could not be parsed', $configFile));
|
|
|
78 |
}
|
|
|
79 |
|
|
|
80 |
return null === $config ? array() : $config;
|
|
|
81 |
}
|
|
|
82 |
|
|
|
83 |
/**
|
|
|
84 |
* Merges configuration values for a given key and category.
|
|
|
85 |
*
|
|
|
86 |
* @param string $keyName The key name
|
|
|
87 |
* @param string $category The category name
|
|
|
88 |
*
|
|
|
89 |
* @return string The value associated with this key name and category
|
|
|
90 |
*/
|
|
|
91 |
protected function mergeConfigValue($keyName, $category)
|
|
|
92 |
{
|
|
|
93 |
$values = array();
|
|
|
94 |
|
|
|
95 |
if (isset($this->yamlConfig['all'][$keyName]) && is_array($this->yamlConfig['all'][$keyName]))
|
|
|
96 |
{
|
|
|
97 |
$values = $this->yamlConfig['all'][$keyName];
|
|
|
98 |
}
|
|
|
99 |
|
|
|
100 |
if ($category && isset($this->yamlConfig[$category][$keyName]) && is_array($this->yamlConfig[$category][$keyName]))
|
|
|
101 |
{
|
|
|
102 |
$values = array_merge($values, $this->yamlConfig[$category][$keyName]);
|
|
|
103 |
}
|
|
|
104 |
|
|
|
105 |
return $values;
|
|
|
106 |
}
|
|
|
107 |
|
|
|
108 |
/**
|
|
|
109 |
* Gets a configuration value for a given key and category.
|
|
|
110 |
*
|
|
|
111 |
* @param string $keyName The key name
|
|
|
112 |
* @param string $category The category name
|
|
|
113 |
* @param string $defaultValue The default value
|
|
|
114 |
*
|
|
|
115 |
* @return string The value associated with this key name and category
|
|
|
116 |
*/
|
|
|
117 |
protected function getConfigValue($keyName, $category, $defaultValue = null)
|
|
|
118 |
{
|
|
|
119 |
if (isset($this->yamlConfig[$category][$keyName]))
|
|
|
120 |
{
|
|
|
121 |
return $this->yamlConfig[$category][$keyName];
|
|
|
122 |
}
|
|
|
123 |
else if (isset($this->yamlConfig['all'][$keyName]))
|
|
|
124 |
{
|
|
|
125 |
return $this->yamlConfig['all'][$keyName];
|
|
|
126 |
}
|
|
|
127 |
|
|
|
128 |
return $defaultValue;
|
|
|
129 |
}
|
|
|
130 |
|
|
|
131 |
static public function flattenConfiguration($config)
|
|
|
132 |
{
|
|
|
133 |
$config['all'] = sfToolkit::arrayDeepMerge(
|
|
|
134 |
isset($config['default']) && is_array($config['default']) ? $config['default'] : array(),
|
|
|
135 |
isset($config['all']) && is_array($config['all']) ? $config['all'] : array()
|
|
|
136 |
);
|
|
|
137 |
|
|
|
138 |
unset($config['default']);
|
|
|
139 |
|
|
|
140 |
return $config;
|
|
|
141 |
}
|
|
|
142 |
|
|
|
143 |
/**
|
|
|
144 |
* Merges default, all and current environment configurations.
|
|
|
145 |
*
|
|
|
146 |
* @param array $config The main configuratino array
|
|
|
147 |
*
|
|
|
148 |
* @return array The merged configuration
|
|
|
149 |
*/
|
|
|
150 |
static public function flattenConfigurationWithEnvironment($config)
|
|
|
151 |
{
|
|
|
152 |
return sfToolkit::arrayDeepMerge(
|
|
|
153 |
isset($config['default']) && is_array($config['default']) ? $config['default'] : array(),
|
|
|
154 |
isset($config['all']) && is_array($config['all']) ? $config['all'] : array(),
|
|
|
155 |
isset($config[sfConfig::get('sf_environment')]) && is_array($config[sfConfig::get('sf_environment')]) ? $config[sfConfig::get('sf_environment')] : array()
|
|
|
156 |
);
|
|
|
157 |
}
|
|
|
158 |
}
|