|
1 <?php |
|
2 |
|
3 /* |
|
4 * This file is part of the Symfony package. |
|
5 * |
|
6 * (c) Fabien Potencier <fabien@symfony.com> |
|
7 * |
|
8 * For the full copyright and license information, please view the LICENSE |
|
9 * file that was distributed with this source code. |
|
10 */ |
|
11 |
|
12 namespace Symfony\Component\Config\Definition; |
|
13 |
|
14 /** |
|
15 * This class is the entry point for config normalization/merging/finalization. |
|
16 * |
|
17 * @author Johannes M. Schmitt <schmittjoh@gmail.com> |
|
18 */ |
|
19 class Processor |
|
20 { |
|
21 /** |
|
22 * Processes an array of configurations. |
|
23 * |
|
24 * @param NodeInterface $configTree The node tree describing the configuration |
|
25 * @param array $configs An array of configuration items to process |
|
26 * |
|
27 * @return array The processed configuration |
|
28 */ |
|
29 public function process(NodeInterface $configTree, array $configs) |
|
30 { |
|
31 $configs = self::normalizeKeys($configs); |
|
32 |
|
33 $currentConfig = array(); |
|
34 foreach ($configs as $config) { |
|
35 $config = $configTree->normalize($config); |
|
36 $currentConfig = $configTree->merge($currentConfig, $config); |
|
37 } |
|
38 |
|
39 return $configTree->finalize($currentConfig); |
|
40 } |
|
41 |
|
42 /** |
|
43 * Processes an array of configurations. |
|
44 * |
|
45 * @param ConfigurationInterface $configuration The configuration class |
|
46 * @param array $configs An array of configuration items to process |
|
47 * |
|
48 * @return array The processed configuration |
|
49 */ |
|
50 public function processConfiguration(ConfigurationInterface $configuration, array $configs) |
|
51 { |
|
52 return $this->process($configuration->getConfigTreeBuilder()->buildTree(), $configs); |
|
53 } |
|
54 |
|
55 /** |
|
56 * This method normalizes keys between the different configuration formats |
|
57 * |
|
58 * Namely, you mostly have foo_bar in YAML while you have foo-bar in XML. |
|
59 * After running this method, all keys are normalized to foo_bar. |
|
60 * |
|
61 * If you have a mixed key like foo-bar_moo, it will not be altered. |
|
62 * The key will also not be altered if the target key already exists. |
|
63 * |
|
64 * @param array $config |
|
65 * |
|
66 * @return array the config with normalized keys |
|
67 */ |
|
68 static public function normalizeKeys(array $config) |
|
69 { |
|
70 foreach ($config as $key => $value) { |
|
71 if (is_array($value)) { |
|
72 $config[$key] = self::normalizeKeys($value); |
|
73 } |
|
74 |
|
75 if (false !== strpos($key, '-') && false === strpos($key, '_') && !array_key_exists($normalizedKey = str_replace('-', '_', $key), $config)) { |
|
76 $config[$normalizedKey] = $config[$key]; |
|
77 unset($config[$key]); |
|
78 } |
|
79 } |
|
80 |
|
81 return $config; |
|
82 } |
|
83 |
|
84 /** |
|
85 * Normalizes a configuration entry. |
|
86 * |
|
87 * This method returns a normalize configuration array for a given key |
|
88 * to remove the differences due to the original format (YAML and XML mainly). |
|
89 * |
|
90 * Here is an example. |
|
91 * |
|
92 * The configuration is XML: |
|
93 * |
|
94 * <twig:extension id="twig.extension.foo" /> |
|
95 * <twig:extension id="twig.extension.bar" /> |
|
96 * |
|
97 * And the same configuration in YAML: |
|
98 * |
|
99 * twig.extensions: ['twig.extension.foo', 'twig.extension.bar'] |
|
100 * |
|
101 * @param array $config A config array |
|
102 * @param string $key The key to normalize |
|
103 * @param string $plural The plural form of the key if it is irregular |
|
104 * |
|
105 * @return array |
|
106 */ |
|
107 static public function normalizeConfig($config, $key, $plural = null) |
|
108 { |
|
109 if (null === $plural) { |
|
110 $plural = $key.'s'; |
|
111 } |
|
112 |
|
113 $values = array(); |
|
114 if (isset($config[$plural])) { |
|
115 $values = $config[$plural]; |
|
116 } elseif (isset($config[$key])) { |
|
117 if (is_string($config[$key]) || !is_int(key($config[$key]))) { |
|
118 // only one |
|
119 $values = array($config[$key]); |
|
120 } else { |
|
121 $values = $config[$key]; |
|
122 } |
|
123 } |
|
124 |
|
125 return $values; |
|
126 } |
|
127 } |