-
Notifications
You must be signed in to change notification settings - Fork 142
/
PayumExtension.php
315 lines (264 loc) · 12.4 KB
/
PayumExtension.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
<?php
namespace Payum\Bundle\PayumBundle\DependencyInjection;
use Payum\Bundle\PayumBundle\Sonata\GatewayConfigAdmin;
use Payum\Core\Bridge\Defuse\Security\DefuseCypher;
use Payum\Core\Bridge\Symfony\ReplyToSymfonyResponseConverter;
use Payum\Core\Exception\InvalidArgumentException;
use Payum\Bundle\PayumBundle\DependencyInjection\Factory\Storage\StorageFactoryInterface;
use Payum\Core\Exception\LogicException;
use Payum\Core\Gateway;
use Payum\Core\Registry\DynamicRegistry;
use Payum\Core\Storage\CryptoStorageDecorator;
use Sonata\AdminBundle\Admin\AbstractAdmin;
use Symfony\Component\DependencyInjection\ChildDefinition;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\Config\FileLocator;
use Payum\Bundle\PayumBundle\DependencyInjection\Factory\Storage\CustomStorageFactory;
use Payum\Bundle\PayumBundle\DependencyInjection\Factory\Storage\Propel1StorageFactory;
use Payum\Bundle\PayumBundle\DependencyInjection\Factory\Storage\Propel2StorageFactory;
use Payum\Bundle\PayumBundle\DependencyInjection\Factory\Storage\DoctrineStorageFactory;
use Payum\Bundle\PayumBundle\DependencyInjection\Factory\Storage\FilesystemStorageFactory;
class PayumExtension extends Extension implements PrependExtensionInterface
{
/**
* @var StorageFactoryInterface[]
*/
protected $storagesFactories = array();
/**
* {@inheritDoc}
*/
public function load(array $configs, ContainerBuilder $container)
{
$this->addStorageFactory(new FilesystemStorageFactory);
$this->addStorageFactory(new DoctrineStorageFactory);
$this->addStorageFactory(new CustomStorageFactory);
$this->addStorageFactory(new Propel1StorageFactory);
$this->addStorageFactory(new Propel2StorageFactory);
$mainConfig = $this->getConfiguration($configs, $container);
$config = $this->processConfiguration($mainConfig, $configs);
// load services
$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('payum.xml');
$loader->load('commands.xml');
$loader->load('form.xml');
if ($container->getParameter('kernel.debug')) {
$loader->load('debug.xml');
}
$this->loadStorages($config['storages'], $container);
$this->loadSecurity($config['security'], $container);
$this->loadCoreGateway(isset($config['gateways']['core']) ? $config['gateways']['core'] : [], $container);
unset($config['gateways']['core']);
$this->loadGateways($config['gateways'], $container);
if (isset($config['dynamic_gateways'])) {
$this->loadDynamicGateways($config['dynamic_gateways'], $container);
};
}
/**
* {@inheritDoc}
*/
public function prepend(ContainerBuilder $container)
{
$bundles = $container->getParameter('kernel.bundles');
if (isset($bundles['DoctrineBundle'])) {
$config = array_merge(...$container->getExtensionConfig('doctrine'));
// do not register mappings if dbal not configured.
if (!empty($config['dbal']) && !empty($config['orm'])) {
$rc = new \ReflectionClass(Gateway::class);
$payumRootDir = dirname($rc->getFileName());
$container->prependExtensionConfig('doctrine', array(
'orm' => array(
'mappings' => array(
'payum' => array(
'is_bundle' => false,
'type' => 'xml',
'dir' => $payumRootDir.'/Bridge/Doctrine/Resources/mapping',
'prefix' => 'Payum\Core\Model',
),
),
),
));
}
}
}
/**
* @param array $config
* @param ContainerBuilder $container
*/
protected function loadGateways(array $config, ContainerBuilder $container)
{
$builder = $container->getDefinition('payum.builder');
foreach ($config as $gatewayName => $gatewayConfig) {
$builder->addMethodCall('addGateway', [$gatewayName, $gatewayConfig]);
}
}
/**
* @param array $config
* @param ContainerBuilder $container
*/
protected function loadCoreGateway(array $config, ContainerBuilder $container)
{
$builder = $container->getDefinition('payum.builder');
$defaultConfig = [
'payum.template.layout' => '@PayumCore\layout.html.twig',
'payum.template.obtain_credit_card' => '@PayumSymfonyBridge\obtainCreditCard.html.twig',
'payum.paths' => [
'PayumSymfonyBridge' => dirname((new \ReflectionClass(ReplyToSymfonyResponseConverter::class))->getFileName()).'/Resources/views',
],
'payum.action.get_http_request' => new Reference('payum.action.get_http_request'),
'payum.action.obtain_credit_card' => new Reference('payum.action.obtain_credit_card_builder'),
];
$config = array_replace_recursive($defaultConfig, $config);
$builder->addMethodCall('addCoreGatewayFactoryConfig', [$config]);
}
/**
* @param array $config
* @param ContainerBuilder $container
*/
protected function loadStorages(array $config, ContainerBuilder $container)
{
foreach ($config as $modelClass => $storageConfig) {
$storageFactoryName = $this->findSelectedStorageFactoryNameInStorageConfig($storageConfig);
$storageId = $this->storagesFactories[$storageFactoryName]->create(
$container,
$modelClass,
$storageConfig[$storageFactoryName]
);
$container->getDefinition($storageId)->addTag('payum.storage', array('model_class' => $modelClass));
if (false !== strpos($storageId, '.storage.')) {
$storageExtensionId = str_replace('.storage.', '.extension.storage.', $storageId);
} else {
throw new LogicException(sprintf('In order to add storage to extension the storage "%s" has to contains ".storage." inside.', $storageId));
}
$storageExtension = new ChildDefinition('payum.extension.storage.prototype');
$storageExtension->replaceArgument(0, new Reference($storageId));
$storageExtension->setPublic(true);
$container->setDefinition($storageExtensionId, $storageExtension);
if ($storageConfig['extension']['all']) {
$storageExtension->addTag('payum.extension', array('all' => true));
} else {
foreach ($storageConfig['extension']['gateways'] as $gatewayName) {
$storageExtension->addTag('payum.extension', array('gateway' => $gatewayName));
}
foreach ($storageConfig['extension']['factories'] as $factory) {
$storageExtension->addTag('payum.extension', array('factory' => $factory));
}
}
}
}
/**
* @param array $securityConfig
* @param ContainerBuilder $container
*/
protected function loadSecurity(array $securityConfig, ContainerBuilder $container)
{
foreach ($securityConfig['token_storage'] as $tokenClass => $tokenStorageConfig) {
$storageFactoryName = $this->findSelectedStorageFactoryNameInStorageConfig($tokenStorageConfig);
$storageId = $this->storagesFactories[$storageFactoryName]->create(
$container,
$tokenClass,
$tokenStorageConfig[$storageFactoryName]
);
$container->setDefinition('payum.security.token_storage', new ChildDefinition($storageId));
}
}
/**
* @param array $dynamicGatewaysConfig
* @param ContainerBuilder $container
*/
protected function loadDynamicGateways(array $dynamicGatewaysConfig, ContainerBuilder $container)
{
$configClass = null;
$configStorage = null;
foreach ($dynamicGatewaysConfig['config_storage'] as $configClass => $configStorageConfig) {
$storageFactoryName = $this->findSelectedStorageFactoryNameInStorageConfig($configStorageConfig);
$configStorage = $this->storagesFactories[$storageFactoryName]->create(
$container,
$configClass,
$configStorageConfig[$storageFactoryName]
);
$container->setDefinition('payum.dynamic_gateways.config_storage', new ChildDefinition($configStorage));
}
if (isset($dynamicGatewaysConfig['encryption']['defuse_secret_key'])) {
$container->register('payum.dynamic_gateways.cypher', DefuseCypher::class)
->addArgument($dynamicGatewaysConfig['encryption']['defuse_secret_key'])
;
$container->register('payum.dynamic_gateways.encrypted_config_storage', CryptoStorageDecorator::class)
->setPublic(false)
->setDecoratedService('payum.dynamic_gateways.config_storage')
->addArgument(new Reference('payum.dynamic_gateways.encrypted_config_storage.inner'))
->addArgument(new Reference('payum.dynamic_gateways.cypher'))
;
}
//deprecated
$registry = new Definition(DynamicRegistry::class, array(
new Reference('payum.dynamic_gateways.config_storage'),
new Reference('payum.static_registry')
));
$registry->setPublic(true);
$container->setDefinition('payum.dynamic_registry', $registry);
if ($dynamicGatewaysConfig['sonata_admin']) {
throw new \LogicException('Not supported. Has to wait till Sonata Admin 4.x will be released.');
if (false == class_exists(AbstractAdmin::class)) {
throw new LogicException('Admin class does not exists. Did you install SonataAdmin bundle?');
}
$gatewayConfigAdmin = new Definition(GatewayConfigAdmin::class, [
null,
$configClass,
null
]);
$gatewayConfigAdmin->addMethodCall('setFormFactory', [new Reference('form.factory')]);
if ($container->hasDefinition('payum.dynamic_gateways.cypher')) {
$gatewayConfigAdmin->addMethodCall('setCypher', [new Reference('payum.dynamic_gateways.cypher')]);
}
$gatewayConfigAdmin->addTag('sonata.admin', [
'manager_type' => 'orm',
'group' => "Gateways",
'label' => "Configs",
]);
$container->setDefinition('payum.dynamic_gateways.gateway_config_admin', $gatewayConfigAdmin);
}
$payumBuilder = $container->getDefinition('payum.builder');
$payumBuilder->addMethodCall('setGatewayConfigStorage', [new Reference('payum.dynamic_gateways.config_storage')]);
}
/**
* @param Factory\Storage\StorageFactoryInterface $factory
*
* @throws \Payum\Core\Exception\InvalidArgumentException
*/
public function addStorageFactory(StorageFactoryInterface $factory)
{
$factoryName = $factory->getName();
if (empty($factoryName)) {
throw new InvalidArgumentException(sprintf('The storage factory %s has empty name', get_class($factory)));
}
if (array_key_exists($factoryName, $this->storagesFactories)) {
throw new InvalidArgumentException(sprintf('The storage factory with such name %s already registered', $factoryName));
}
$this->storagesFactories[$factoryName] = $factory;
}
/**
* {@inheritDoc}
*/
public function getConfiguration(array $config, ContainerBuilder $container)
{
return new MainConfiguration($this->storagesFactories);
}
/**
* @param array $storageConfig
*
* @return string
*/
protected function findSelectedStorageFactoryNameInStorageConfig($storageConfig)
{
foreach ($storageConfig as $name => $value) {
if (isset($this->storagesFactories[$name])) {
return $name;
}
}
}
}