1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the Exchange Rate Bundle, an RunOpenCode project. |
4
|
|
|
* |
5
|
|
|
* (c) 2017 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\FileLocator; |
15
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
16
|
|
|
use Symfony\Component\DependencyInjection\Extension\Extension as BaseExtension; |
17
|
|
|
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Class Extension |
21
|
|
|
* |
22
|
|
|
* Bundle extension. |
23
|
|
|
* |
24
|
|
|
* @package RunOpenCode\Bundle\ExchangeRate\DependencyInjection |
25
|
|
|
*/ |
26
|
|
|
class Extension extends BaseExtension |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* {@inheritdoc} |
30
|
|
|
*/ |
31
|
1 |
|
public function getAlias() |
32
|
|
|
{ |
33
|
1 |
|
return "run_open_code_exchange_rate"; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* {@inheritdoc} |
38
|
|
|
*/ |
39
|
1 |
|
public function getNamespace() |
40
|
|
|
{ |
41
|
1 |
|
return 'http://www.runopencode.com/xsd-schema/exchange-rate-bundle'; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* {@inheritdoc} |
46
|
|
|
*/ |
47
|
|
|
public function getXsdValidationBasePath() |
48
|
|
|
{ |
49
|
|
|
return __DIR__.'/../Resources/config/schema'; |
|
|
|
|
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* {@inheritdoc} |
54
|
|
|
*/ |
55
|
|
|
public function load(array $config, ContainerBuilder $container) |
56
|
|
|
{ |
57
|
|
|
|
58
|
|
|
$configuration = new TreeConfiguration(); |
59
|
|
|
$config = $this->processConfiguration($configuration, $config); |
60
|
|
|
|
61
|
|
|
$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config/services')); |
62
|
|
|
$loader->load('repository.xml'); |
63
|
|
|
$loader->load('command.xml'); |
64
|
|
|
$loader->load('controller.xml'); |
65
|
|
|
$loader->load('form_type.xml'); |
66
|
|
|
$loader->load('manager.xml'); |
67
|
|
|
$loader->load('processor.xml'); |
68
|
|
|
$loader->load('source.xml'); |
69
|
|
|
$loader->load('validator.xml'); |
70
|
|
|
|
71
|
|
|
$this |
72
|
|
|
->configureBaseCurrency($config, $container) |
73
|
|
|
->configureRepository($config, $container) |
74
|
|
|
->configureFileRepository($config, $container) |
75
|
|
|
->configureController($config, $container) |
76
|
|
|
->configureRates($config, $container) |
77
|
|
|
->configureSimpleSources($config, $container) |
78
|
|
|
->configureSourceType($config, $container) |
79
|
|
|
->configureRateTypeType($config, $container) |
80
|
|
|
->configureCurrencyCodeType($config, $container) |
81
|
|
|
->configureForeignCurrencyCodeType($config, $container) |
82
|
|
|
->configureRateType($config, $container) |
83
|
|
|
->configureNotifications($config, $container) |
84
|
|
|
; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Configure base currency. |
89
|
|
|
* |
90
|
|
|
* @param array $config Configuration parameters. |
91
|
|
|
* @param ContainerBuilder $container Service container. |
92
|
|
|
* @return Extension $this Fluent interface. |
93
|
|
|
*/ |
94
|
|
|
protected function configureBaseCurrency(array $config, ContainerBuilder $container) |
95
|
|
|
{ |
96
|
|
|
$baseCurrency = CurrencyCodeUtil::clean($config['base_currency']); |
97
|
|
|
$container->setParameter('run_open_code.exchange_rate.base_currency', $baseCurrency); |
98
|
|
|
|
99
|
|
|
return $this; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Configure rates. |
104
|
|
|
* |
105
|
|
|
* @param array $config Configuration parameters. |
106
|
|
|
* @param ContainerBuilder $container Service container. |
107
|
|
|
* @return Extension $this Fluent interface. |
108
|
|
|
*/ |
109
|
|
|
protected function configureRates(array $config, ContainerBuilder $container) |
110
|
|
|
{ |
111
|
|
|
if (count($config['rates']) === 0) { |
112
|
|
|
throw new \RuntimeException('You have to configure which rates you would like to use in your project.'); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
$container->setParameter('run_open_code.exchange_rate.registered_rates', $config['rates']); |
116
|
|
|
return $this; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Configure required processors. |
121
|
|
|
* |
122
|
|
|
* @param array $config Configuration parameters. |
123
|
|
|
* @param ContainerBuilder $container Service container. |
124
|
|
|
* @return Extension $this Fluent interface. |
125
|
|
|
*/ |
126
|
|
|
protected function configureRepository(array $config, ContainerBuilder $container) |
127
|
|
|
{ |
128
|
|
|
$container->setParameter('run_open_code.exchange_rate.repository', $config['repository']); |
129
|
|
|
return $this; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Configure file repository, if used. |
134
|
|
|
* |
135
|
|
|
* @param array $config Configuration parameters. |
136
|
|
|
* @param ContainerBuilder $container Service container. |
137
|
|
|
* @return Extension $this Fluent interface. |
138
|
|
|
*/ |
139
|
|
|
protected function configureFileRepository(array $config, ContainerBuilder $container) |
140
|
|
|
{ |
141
|
|
|
$container->setParameter('run_open_code.exchange_rate.repository.file_repository.path', $config['file_repository']['path']); |
142
|
|
|
return $this; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Configure controller. |
147
|
|
|
* |
148
|
|
|
* @param array $config Configuration parameters. |
149
|
|
|
* @param ContainerBuilder $container Service container. |
150
|
|
|
* @return Extension $this Fluent interface. |
151
|
|
|
*/ |
152
|
|
|
protected function configureController(array $config, ContainerBuilder $container) |
153
|
|
|
{ |
154
|
|
|
$container->setParameter( |
155
|
|
|
'run_open_code.exchange_rate.controller.view_configuration', |
156
|
|
|
$config['view'] |
157
|
|
|
); |
158
|
|
|
|
159
|
|
|
$container->setParameter( |
160
|
|
|
'run_open_code.exchange_rate.controller.access_roles_configuration', |
161
|
|
|
$config['access_roles'] |
162
|
|
|
); |
163
|
|
|
|
164
|
|
|
return $this; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Configure simple sources which does not have to be explicitly added to service container. |
169
|
|
|
* |
170
|
|
|
* @param array $config Configuration parameters. |
171
|
|
|
* @param ContainerBuilder $container Service container. |
172
|
|
|
* @return Extension $this Fluent interface. |
173
|
|
|
*/ |
174
|
|
|
protected function configureSimpleSources(array $config, ContainerBuilder $container) |
175
|
|
|
{ |
176
|
|
|
if (!empty($config['sources']) && count($config['sources']) > 0) { |
177
|
|
|
$container->setParameter('run_open_code.exchange_rate.source.registered_simple_sources', $config['sources']); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
return $this; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Configure "RunOpenCode\\Bundle\\ExchangeRate\\Form\\Type\\SourceType" default settings. |
185
|
|
|
* |
186
|
|
|
* @param array $config Configuration parameters. |
187
|
|
|
* @param ContainerBuilder $container Service container. |
188
|
|
|
* @return Extension $this Fluent interface. |
189
|
|
|
*/ |
190
|
|
View Code Duplication |
protected function configureSourceType(array $config, ContainerBuilder $container) |
|
|
|
|
191
|
|
|
{ |
192
|
|
|
$defaults = array_merge($config['form_types']['source_type'], array('choices' => array())); |
193
|
|
|
|
194
|
|
|
foreach ($config['rates'] as $rate) { |
195
|
|
|
$defaults['choices'][sprintf('exchange_rate.source.%s', $rate['source'])] = $rate['source']; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
$container->setParameter('run_open_code.exchange_rate.form_type.source_type', $defaults); |
199
|
|
|
|
200
|
|
|
return $this; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* Configure "RunOpenCode\\Bundle\\ExchangeRate\\Form\\Type\\RateTypeType" default settings. |
205
|
|
|
* |
206
|
|
|
* @param array $config Configuration parameters. |
207
|
|
|
* @param ContainerBuilder $container Service container. |
208
|
|
|
* @return Extension $this Fluent interface. |
209
|
|
|
*/ |
210
|
|
|
protected function configureRateTypeType(array $config, ContainerBuilder $container) |
211
|
|
|
{ |
212
|
|
|
$defaults = array_merge($config['form_types']['rate_type_type'], array('choices' => array())); |
213
|
|
|
|
214
|
|
|
foreach ($config['rates'] as $rate) { |
215
|
|
|
$defaults['choices'][$rate['rate_type']] = sprintf('exchange_rate.rate_type.%s.%s', $rate['source'], $rate['rate_type']); |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
$defaults['choices'] = array_flip($defaults['choices']); |
219
|
|
|
|
220
|
|
|
$container->setParameter('run_open_code.exchange_rate.form_type.rate_type_type', $defaults); |
221
|
|
|
|
222
|
|
|
return $this; |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* Configure "RunOpenCode\\Bundle\\ExchangeRate\\Form\\Type\\CurrencyCodeType" default settings. |
227
|
|
|
* |
228
|
|
|
* @param array $config Configuration parameters. |
229
|
|
|
* @param ContainerBuilder $container Service container. |
230
|
|
|
* @return Extension $this Fluent interface. |
231
|
|
|
*/ |
232
|
|
|
protected function configureCurrencyCodeType(array $config, ContainerBuilder $container) |
233
|
|
|
{ |
234
|
|
|
$defaults = array_merge($config['form_types']['currency_code_type'], array('choices' => array())); |
235
|
|
|
|
236
|
|
|
foreach ($config['rates'] as $rate) { |
237
|
|
|
$defaults['choices'][$rate['currency_code']] = $rate['currency_code']; |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
asort($defaults['choices']); |
241
|
|
|
|
242
|
|
|
$defaults['choices'] = array_merge(array($config['base_currency'] => $config['base_currency']), $defaults['choices']); |
243
|
|
|
|
244
|
|
|
$container->setParameter('run_open_code.exchange_rate.form_type.currency_code_type', $defaults); |
245
|
|
|
|
246
|
|
|
return $this; |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
/** |
250
|
|
|
* Configure "RunOpenCode\\Bundle\\ExchangeRate\\Form\\Type\\ForeignCurrencyCodeType" default settings. |
251
|
|
|
* |
252
|
|
|
* @param array $config Configuration parameters. |
253
|
|
|
* @param ContainerBuilder $container Service container. |
254
|
|
|
* @return Extension $this Fluent interface. |
255
|
|
|
*/ |
256
|
|
View Code Duplication |
protected function configureForeignCurrencyCodeType(array $config, ContainerBuilder $container) |
|
|
|
|
257
|
|
|
{ |
258
|
|
|
$defaults = array_merge($config['form_types']['currency_code_type'], array('choices' => array())); |
259
|
|
|
|
260
|
|
|
foreach ($config['rates'] as $rate) { |
261
|
|
|
$defaults['choices'][$rate['currency_code']] = $rate['currency_code']; |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
asort($defaults['choices']); |
265
|
|
|
|
266
|
|
|
$container->setParameter('run_open_code.exchange_rate.form_type.foreign_currency_code_type', $defaults); |
267
|
|
|
|
268
|
|
|
return $this; |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
/** |
272
|
|
|
* Configure "RunOpenCode\\Bundle\\ExchangeRate\\Form\\Type\\RateType" default settings. |
273
|
|
|
* |
274
|
|
|
* @param array $config Configuration parameters. |
275
|
|
|
* @param ContainerBuilder $container Service container. |
276
|
|
|
* @return Extension $this Fluent interface. |
277
|
|
|
*/ |
278
|
|
|
protected function configureRateType(array $config, ContainerBuilder $container) |
279
|
|
|
{ |
280
|
|
|
$container->setParameter('run_open_code.exchange_rate.form_type.rate_type', $config['form_types']['rate_type']); |
281
|
|
|
|
282
|
|
|
return $this; |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
/** |
286
|
|
|
* Configure internal notifications. |
287
|
|
|
* |
288
|
|
|
* @param array $config Configuration parameters. |
289
|
|
|
* @param ContainerBuilder $container Service container. |
290
|
|
|
* @return Extension $this Fluent interface. |
291
|
|
|
*/ |
292
|
|
|
protected function configureNotifications(array $config, ContainerBuilder $container) |
293
|
|
|
{ |
294
|
|
|
if (!empty($config['notifications']['fetch']) && !empty($config['notifications']['fetch']['enabled'])) { |
295
|
|
|
$container->setParameter('run_open_code.exchange_rate.notifications.fetch', $config['notifications']['fetch']); |
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
return $this; |
299
|
|
|
} |
300
|
|
|
} |
301
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.