1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the ONGR package. |
5
|
|
|
* |
6
|
|
|
* (c) NFQ Technologies UAB <[email protected]> |
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 ONGR\CurrencyExchangeBundle\DependencyInjection; |
13
|
|
|
|
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\Exception\ServiceNotFoundException; |
19
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
20
|
|
|
use Symfony\Component\DependencyInjection\Loader; |
21
|
|
|
use Symfony\Component\HttpKernel\DependencyInjection\Extension; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* This is the class that loads and manages your bundle configuration. |
25
|
|
|
*/ |
26
|
|
|
class ONGRCurrencyExchangeExtension extends Extension |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* {@inheritdoc} |
30
|
5 |
|
*/ |
31
|
|
|
public function load(array $configs, ContainerBuilder $container) |
32
|
5 |
|
{ |
33
|
5 |
|
$configuration = new Configuration(); |
34
|
|
|
$config = $this->processConfiguration($configuration, $configs); |
35
|
5 |
|
|
36
|
5 |
|
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config')); |
37
|
|
|
$loader->load('services.yml'); |
38
|
5 |
|
|
39
|
5 |
|
$this->configCurrencyRatesService($config, $container); |
40
|
5 |
|
$this->configTwigExtension($config, $container); |
41
|
5 |
|
$this->configCurrencyExchangeService($container); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Defines currency rates service. |
46
|
|
|
* |
47
|
|
|
* @param array $config |
48
|
|
|
* @param ContainerBuilder $container |
49
|
|
|
* |
50
|
|
|
* @throws ServiceNotFoundException |
51
|
5 |
|
*/ |
52
|
|
|
private function configCurrencyRatesService(array $config, ContainerBuilder $container) |
53
|
5 |
|
{ |
54
|
5 |
|
$driver = $config['driver']; |
55
|
5 |
|
$ecbApiId = $config['open_exchange_rates_api_id']; |
56
|
5 |
|
|
57
|
1 |
|
if ($driver == 'ongr_currency_exchange.open_exchange_driver' && !$ecbApiId) { |
58
|
1 |
|
throw new InvalidConfigurationException( |
59
|
1 |
|
'"open_exchange_rates_api_id" must be set when using ' . |
60
|
1 |
|
'"ongr_currency_exchange.open_exchange_driver" driver.' |
61
|
5 |
|
); |
62
|
5 |
|
} |
63
|
|
|
|
64
|
5 |
|
if ($container->hasDefinition($driver)) { |
65
|
5 |
|
$def = new Definition( |
66
|
5 |
|
'ONGR\CurrencyExchangeBundle\Service\CurrencyRatesService', |
67
|
|
|
[ |
68
|
5 |
|
new Reference($driver), |
69
|
5 |
|
new Reference(sprintf('es.manager.%s', $config['es_manager'])), |
70
|
5 |
|
new Reference($config['cache']), |
71
|
5 |
|
] |
72
|
5 |
|
); |
73
|
|
|
$def->addMethodCall('setLogger', [new Reference('logger')]); |
74
|
|
|
$def->addTag('monolog.logger', ['channel' => 'ongr_currency']); |
75
|
5 |
|
$container->setDefinition('ongr_currency_exchange.currency_rates_service', $def); |
76
|
|
|
} else { |
77
|
|
|
throw new ServiceNotFoundException($driver); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Twig extension service. |
83
|
5 |
|
* |
84
|
|
|
* @param array $config |
85
|
5 |
|
* @param ContainerBuilder $container |
86
|
5 |
|
*/ |
87
|
5 |
|
private function configTwigExtension(array $config, ContainerBuilder $container) |
88
|
5 |
|
{ |
89
|
5 |
|
$container->setParameter('ongr_currency_exchange.default_currency', $config['default_currency']); |
90
|
|
|
$container->setParameter( |
91
|
5 |
|
'ongr_currency_exchange.twig.price_extension.to_print_list', |
92
|
5 |
|
array_keys($config['currencies']) |
93
|
5 |
|
); |
94
|
5 |
|
if (isset($config['currency_sign'])) { |
95
|
5 |
|
$container->setParameter( |
96
|
5 |
|
'ongr_currency_exchange.twig.price_extension.currency.sign', |
97
|
5 |
|
$config['currency_sign'] |
98
|
5 |
|
); |
99
|
5 |
|
} |
100
|
5 |
|
if (isset($config['default_currency'])) { |
101
|
5 |
|
$container->setParameter( |
102
|
5 |
|
'ongr_currency_exchange.twig.price_extension.currency.name', |
103
|
5 |
|
$config['default_currency'] |
104
|
5 |
|
); |
105
|
5 |
|
} |
106
|
5 |
|
if (isset($config['separators']['decimal'])) { |
107
|
5 |
|
$container->setParameter( |
108
|
|
|
'ongr_currency_exchange.twig.price_extension.currency.dec_point_separator', |
109
|
|
|
$config['separators']['decimal'] |
110
|
|
|
); |
111
|
|
|
} |
112
|
|
|
if (isset($config['separators']['thousands'])) { |
113
|
|
|
$container->setParameter( |
114
|
5 |
|
'ongr_currency_exchange.twig.price_extension.currency.thousands_separator', |
115
|
|
|
$config['separators']['thousands'] |
116
|
|
|
); |
117
|
5 |
|
} |
118
|
5 |
|
if (isset($config['templates']['currency_list'])) { |
119
|
5 |
|
$container->setParameter( |
120
|
5 |
|
'ongr_currency_exchange.twig.price_extension.currency.currency_list_template', |
121
|
5 |
|
$config['templates']['currency_list'] |
122
|
5 |
|
); |
123
|
5 |
|
} |
124
|
5 |
|
if (isset($config['templates']['price_list'])) { |
125
|
|
|
$container->setParameter( |
126
|
|
|
'ongr_currency_exchange.twig.price_extension.currency.price_list_template', |
127
|
|
|
$config['templates']['price_list'] |
128
|
|
|
); |
129
|
|
|
} |
130
|
|
|
$container->setParameter( |
131
|
|
|
'ongr_currency_exchange.twig.price_extension.display_map', |
132
|
|
|
$config['currencies'] |
133
|
|
|
); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Defines currency exchange service. |
138
|
|
|
* |
139
|
|
|
* @param ContainerBuilder $container |
140
|
|
|
*/ |
141
|
|
|
private function configCurrencyExchangeService(ContainerBuilder $container) |
142
|
|
|
{ |
143
|
|
|
// Apply exchange service to price extension. |
144
|
|
|
$def = $container->getDefinition('ongr_currency_exchange.twig.price_extension'); |
145
|
|
|
$def->addMethodCall('setLogger', [new Reference('logger')]); |
146
|
|
|
$def->addTag('monolog.logger', ['channel' => 'ongr_currency']); |
147
|
|
|
$def->addMethodCall( |
148
|
|
|
'setCurrencyExchangeService', |
149
|
|
|
[new Reference('ongr_currency_exchange.currency_exchange_service')] |
150
|
|
|
); |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|