1 | <?php |
||
27 | class Extension extends BaseExtension |
||
28 | { |
||
29 | /** |
||
30 | * {@inheritdoc} |
||
31 | */ |
||
32 | 4 | public function getAlias() |
|
36 | |||
37 | /** |
||
38 | * {@inheritdoc} |
||
39 | */ |
||
40 | 2 | public function load(array $config, ContainerBuilder $container) |
|
41 | { |
||
42 | 2 | $configuration = new TreeConfiguration(); |
|
43 | 2 | $config = $this->processConfiguration($configuration, $config); |
|
44 | |||
45 | 2 | $loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); |
|
46 | 2 | $loader->load('services.xml'); |
|
47 | |||
48 | 2 | $this->configureExchangeRateService($config, $container); |
|
49 | 2 | $this->configureSourcesRegistry($config, $container); |
|
50 | 2 | $this->configureProcessorsRegistry($config, $container); |
|
51 | 2 | $this->configureRatesRegistry($config, $container); |
|
52 | 2 | $this->configureFileRepository($config, $container); |
|
53 | 2 | $this->configureController($config, $container); |
|
54 | 2 | $this->configureDebugCommand($config, $container); |
|
55 | 2 | } |
|
56 | |||
57 | /** |
||
58 | * Configure exchange rate service. |
||
59 | * |
||
60 | * @param array $config |
||
61 | * @param ContainerBuilder $container |
||
62 | */ |
||
63 | 2 | protected function configureExchangeRateService(array $config, ContainerBuilder $container) |
|
64 | { |
||
65 | 2 | if ($container->hasDefinition('run_open_code.exchange_rate')) { |
|
66 | |||
67 | 2 | $definition = $container->getDefinition('run_open_code.exchange_rate'); |
|
68 | |||
69 | 2 | $definition->setArguments(array( |
|
70 | 2 | $config['base_currency'], |
|
71 | 2 | new Reference($config['repository']), |
|
72 | 2 | new Reference('run_open_code.exchange_rate.registry.sources'), |
|
73 | 2 | new Reference('run_open_code.exchange_rate.registry.processors'), |
|
74 | 2 | new Reference('run_open_code.exchange_rate.registry.rates') |
|
75 | 1 | )); |
|
76 | 1 | } |
|
77 | 2 | } |
|
78 | |||
79 | /** |
||
80 | * Configure sources registry. |
||
81 | * |
||
82 | * @param array $config |
||
83 | * @param ContainerBuilder $container |
||
84 | */ |
||
85 | 2 | protected function configureSourcesRegistry(array $config, ContainerBuilder $container) |
|
113 | |||
114 | /** |
||
115 | * Configure processors registry. |
||
116 | * |
||
117 | * @param array $config |
||
118 | * @param ContainerBuilder $container |
||
119 | */ |
||
120 | 2 | protected function configureProcessorsRegistry(array $config, ContainerBuilder $container) |
|
121 | { |
||
122 | 2 | if ($container->hasDefinition('run_open_code.exchange_rate.registry.processors')) { |
|
123 | |||
124 | 2 | $definition = $container->getDefinition('run_open_code.exchange_rate.registry.processors'); |
|
125 | |||
126 | 2 | $processors = array(); |
|
127 | |||
128 | 2 | foreach ($container->findTaggedServiceIds('run_open_code.exchange_rate.processor') as $id => $tags) { |
|
129 | |||
130 | 2 | if (!in_array($id, $config['processors'], true)) { |
|
131 | 2 | continue; |
|
132 | } |
||
133 | |||
134 | 2 | foreach ($tags as $attributes) { |
|
135 | 2 | $processors[$id] = (isset($attributes['priority'])) ? intval($attributes['priority']) : 0; |
|
136 | 1 | } |
|
137 | 1 | } |
|
138 | |||
139 | 2 | asort($processors); |
|
140 | |||
141 | 2 | foreach ($processors as $id => $processor) { |
|
142 | 2 | $definition->addMethodCall('add', array( |
|
143 | 2 | new Reference($id) |
|
144 | 1 | )); |
|
145 | 1 | } |
|
146 | 1 | } |
|
147 | 2 | } |
|
148 | |||
149 | /** |
||
150 | * Configure rates. |
||
151 | * |
||
152 | * @param array $config |
||
153 | * @param ContainerBuilder $container |
||
154 | */ |
||
155 | 2 | protected function configureRatesRegistry(array $config, ContainerBuilder $container) |
|
156 | { |
||
157 | 2 | if ($container->hasDefinition('run_open_code.exchange_rate.registry.rates')) { |
|
158 | |||
159 | 2 | $definition = $container->getDefinition('run_open_code.exchange_rate.registry.rates'); |
|
160 | |||
161 | 2 | $aliases = array(); |
|
162 | |||
163 | 2 | foreach ($config['rates'] as $rateConfiguration) { |
|
164 | |||
165 | 2 | if ($rateConfiguration['alias'] === null) { |
|
166 | 2 | continue; |
|
167 | } |
||
168 | |||
169 | if (array_key_exists($rateConfiguration['alias'], $aliases)) { |
||
170 | throw new InvalidConfigurationException(sprintf('Rate with alias "%s" is already defined.', $rateConfiguration['alias'])); |
||
171 | } |
||
172 | |||
173 | $aliases[$rateConfiguration['alias']] = $rateConfiguration; |
||
174 | 1 | } |
|
175 | |||
176 | 2 | $definition->setArguments(array(array_values($config['rates']))); |
|
177 | 1 | } |
|
178 | 2 | } |
|
179 | |||
180 | /** |
||
181 | * Configure file registry, if used. |
||
182 | * |
||
183 | * @param array $config |
||
184 | * @param ContainerBuilder $container |
||
185 | */ |
||
186 | 2 | protected function configureFileRepository(array $config, ContainerBuilder $container) |
|
187 | { |
||
188 | if ( |
||
189 | 2 | $config['repository'] === 'run_open_code.exchange_rate.repository.file_repository' |
|
190 | 1 | && |
|
191 | 2 | $container->hasDefinition('run_open_code.exchange_rate.repository.file_repository') |
|
192 | 1 | ) { |
|
193 | |||
194 | 2 | if (!empty($config['file_repository']) && !empty($config['file_repository']['path'])) { |
|
195 | 2 | $definition = $container->getDefinition('run_open_code.exchange_rate.repository.file_repository'); |
|
196 | 2 | $definition->setArguments(array( |
|
197 | 2 | $config['file_repository']['path'] |
|
198 | 1 | )); |
|
199 | 1 | } else { |
|
200 | 1 | throw new InvalidConfigurationException('You must configure location to the file where file repository will store exchange rates.'); |
|
201 | } |
||
202 | |||
203 | 1 | } elseif ($config['repository'] === 'run_open_code.exchange_rate.repository.file_repository') { |
|
204 | throw new InvalidConfigurationException('File repository is used to store exchange rates, but it is not available in container.'); |
||
205 | } else { |
||
206 | $container->removeDefinition('run_open_code.exchange_rate.repository.file_repository'); |
||
207 | } |
||
208 | 2 | } |
|
209 | |||
210 | /** |
||
211 | * Configure controller - view layer. |
||
212 | * |
||
213 | * @param array $config |
||
214 | * @param ContainerBuilder $container |
||
215 | */ |
||
216 | 2 | protected function configureController(array $config, ContainerBuilder $container) |
|
217 | { |
||
218 | 2 | if ($container->has('run_open_code.exchange_rate.controller')) { |
|
219 | 2 | $definition = $container->getDefinition('run_open_code.exchange_rate.controller'); |
|
220 | 2 | $definition->setArguments(array( |
|
221 | 2 | new Reference($config['repository']), |
|
222 | 2 | $config['base_currency'], |
|
223 | 2 | $config['view'] |
|
224 | 1 | )); |
|
225 | 1 | } |
|
226 | 2 | } |
|
227 | |||
228 | /** |
||
229 | * Configure debug command. |
||
230 | * |
||
231 | * @param array $config |
||
232 | * @param ContainerBuilder $container |
||
233 | */ |
||
234 | 2 | protected function configureDebugCommand(array $config, ContainerBuilder $container) |
|
235 | { |
||
236 | 2 | if ($container->has('run_open_code.exchange_rate.command.configuration_debug')) { |
|
237 | 2 | $definition = $container->getDefinition('run_open_code.exchange_rate.command.configuration_debug'); |
|
238 | |||
239 | 2 | $arguments = $definition->getArguments(); |
|
240 | 2 | $arguments[3] = new Reference($config['repository']); |
|
241 | |||
242 | 2 | $definition->setArguments($arguments); |
|
243 | 1 | } |
|
244 | 2 | } |
|
245 | |||
246 | /** |
||
247 | * Extract name of only required sources from configuration. |
||
248 | * |
||
249 | * @param array $config |
||
250 | * @return array |
||
251 | */ |
||
252 | 2 | protected function getRequiredSources(array $config) |
|
262 | } |
||
263 |