|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* This file is part of the Exchange Rate Bundle, an RunOpenCode project. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) 2016 RunOpenCode |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
namespace RunOpenCode\Bundle\ExchangeRate\DependencyInjection; |
|
11
|
|
|
|
|
12
|
|
|
use RunOpenCode\Bundle\ExchangeRate\DependencyInjection\Configuration as TreeConfiguration; |
|
13
|
|
|
use RunOpenCode\ExchangeRate\Utils\CurrencyCodeUtil; |
|
14
|
|
|
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException; |
|
15
|
|
|
use Symfony\Component\Config\FileLocator; |
|
16
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
|
17
|
|
|
use Symfony\Component\DependencyInjection\Definition; |
|
18
|
|
|
use Symfony\Component\DependencyInjection\Extension\Extension as BaseExtension; |
|
19
|
|
|
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader; |
|
20
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Class Extension |
|
24
|
|
|
* |
|
25
|
|
|
* Bundle extension. |
|
26
|
|
|
* |
|
27
|
|
|
* @package RunOpenCode\Bundle\ExchangeRate\DependencyInjection |
|
28
|
|
|
*/ |
|
29
|
|
|
class Extension extends BaseExtension |
|
30
|
|
|
{ |
|
31
|
|
|
/** |
|
32
|
4 |
|
* {@inheritdoc} |
|
33
|
|
|
*/ |
|
34
|
4 |
|
public function getAlias() |
|
35
|
|
|
{ |
|
36
|
|
|
return "run_open_code_exchange_rate"; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
2 |
|
* {@inheritdoc} |
|
41
|
|
|
*/ |
|
42
|
2 |
|
public function load(array $config, ContainerBuilder $container) |
|
43
|
2 |
|
{ |
|
44
|
|
|
|
|
45
|
2 |
|
$configuration = new TreeConfiguration(); |
|
46
|
2 |
|
$config = $this->processConfiguration($configuration, $config); |
|
47
|
|
|
|
|
48
|
2 |
|
$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config/services')); |
|
49
|
2 |
|
$loader->load('repository.xml'); |
|
50
|
2 |
|
$loader->load('command.xml'); |
|
51
|
2 |
|
$loader->load('controller.xml'); |
|
52
|
2 |
|
$loader->load('form_type.xml'); |
|
53
|
2 |
|
$loader->load('manager.xml'); |
|
54
|
2 |
|
$loader->load('processor.xml'); |
|
55
|
2 |
|
$loader->load('source.xml'); |
|
56
|
2 |
|
$loader->load('validator.xml'); |
|
57
|
|
|
|
|
58
|
|
|
$this |
|
59
|
|
|
->configureBaseCurrency($config, $container) |
|
60
|
|
|
->configureRepository($config, $container) |
|
61
|
|
|
->configureFileRepository($config, $container) |
|
62
|
|
|
->configureController($config, $container) |
|
63
|
|
|
->configureRates($config, $container) |
|
64
|
2 |
|
->configureSimpleSources($config, $container) |
|
65
|
|
|
; |
|
66
|
2 |
|
} |
|
67
|
|
|
|
|
68
|
2 |
|
/** |
|
69
|
|
|
* Configure base currency. |
|
70
|
2 |
|
* |
|
71
|
2 |
|
* @param array $config Configuration parameters. |
|
72
|
2 |
|
* @param ContainerBuilder $container Service container. |
|
73
|
2 |
|
* @return Extension $this Fluent interface. |
|
74
|
2 |
|
*/ |
|
75
|
2 |
|
protected function configureBaseCurrency(array $config, ContainerBuilder $container) |
|
76
|
2 |
|
{ |
|
77
|
2 |
|
$baseCurrency = CurrencyCodeUtil::clean($config['base_currency']); |
|
78
|
2 |
|
$container->setParameter('run_open_code.exchange_rate.base_currency', $baseCurrency); |
|
79
|
|
|
|
|
80
|
|
|
return $this; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Configure rates. |
|
85
|
|
|
* |
|
86
|
2 |
|
* @param array $config Configuration parameters. |
|
87
|
|
|
* @param ContainerBuilder $container Service container. |
|
88
|
2 |
|
* @return Extension $this Fluent interface. |
|
89
|
|
|
*/ |
|
90
|
2 |
|
protected function configureRates(array $config, ContainerBuilder $container) |
|
91
|
2 |
|
{ |
|
92
|
|
|
if (count($config['rates']) === 0) { |
|
93
|
|
|
throw new \RuntimeException('You have to configure which rates you would like to use in your project.'); |
|
94
|
2 |
|
} |
|
95
|
|
|
|
|
96
|
2 |
|
$container->setParameter('run_open_code.exchange_rate.registered_rates', $config['rates']); |
|
97
|
|
|
return $this; |
|
98
|
2 |
|
} |
|
99
|
|
|
|
|
100
|
2 |
|
/** |
|
101
|
2 |
|
* Configure required processors. |
|
102
|
2 |
|
* |
|
103
|
|
|
* @param array $config Configuration parameters. |
|
104
|
2 |
|
* @param ContainerBuilder $container Service container. |
|
105
|
2 |
|
* @return Extension $this Fluent interface. |
|
106
|
2 |
|
*/ |
|
107
|
2 |
|
protected function configureRepository(array $config, ContainerBuilder $container) |
|
108
|
|
|
{ |
|
109
|
2 |
|
$container->setParameter('run_open_code.exchange_rate.repository', $config['repository']); |
|
110
|
|
|
return $this; |
|
111
|
|
|
} |
|
112
|
2 |
|
|
|
113
|
2 |
|
/** |
|
114
|
|
|
* Configure file repository, if used. |
|
115
|
|
|
* |
|
116
|
|
|
* @param array $config Configuration parameters. |
|
117
|
|
|
* @param ContainerBuilder $container Service container. |
|
118
|
|
|
* @return Extension $this Fluent interface. |
|
119
|
|
|
*/ |
|
120
|
|
|
protected function configureFileRepository(array $config, ContainerBuilder $container) |
|
121
|
2 |
|
{ |
|
122
|
|
|
$container->setParameter('run_open_code.exchange_rate.repository.file_repository.path', $config['file_repository']['path']); |
|
123
|
2 |
|
return $this; |
|
124
|
|
|
} |
|
125
|
2 |
|
|
|
126
|
|
|
/** |
|
127
|
2 |
|
* Configure controller. |
|
128
|
|
|
* |
|
129
|
2 |
|
* @param array $config Configuration parameters. |
|
130
|
|
|
* @param ContainerBuilder $container Service container. |
|
131
|
2 |
|
* @return Extension $this Fluent interface. |
|
132
|
2 |
|
*/ |
|
133
|
|
|
protected function configureController(array $config, ContainerBuilder $container) |
|
134
|
|
|
{ |
|
135
|
2 |
|
$container->setParameter( |
|
136
|
2 |
|
'run_open_code.exchange_rate.controller.view_configuration', |
|
137
|
2 |
|
$config['view'] |
|
138
|
2 |
|
); |
|
139
|
|
|
|
|
140
|
2 |
|
return $this; |
|
141
|
|
|
} |
|
142
|
2 |
|
|
|
143
|
2 |
|
/** |
|
144
|
2 |
|
* Configure simple sources which does not have to be explicitly added to service container. |
|
145
|
2 |
|
* |
|
146
|
2 |
|
* @param array $config Configuration parameters. |
|
147
|
2 |
|
* @param ContainerBuilder $container Service container. |
|
148
|
2 |
|
* @return Extension $this Fluent interface. |
|
149
|
|
|
*/ |
|
150
|
|
|
protected function configureSimpleSources(array $config, ContainerBuilder $container) |
|
151
|
|
|
{ |
|
152
|
|
|
if (!empty($config['sources']) && count($config['sources']) > 0) { |
|
153
|
|
|
$container->setParameter('run_open_code.exchange_rate.source.registered_simple_sources', $config['sources']); |
|
154
|
|
|
} |
|
155
|
|
|
} |
|
156
|
|
|
} |
|
157
|
|
|
|