1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Paysera\Bundle\ApiBundle\DependencyInjection; |
5
|
|
|
|
6
|
|
|
use Doctrine\Common\Persistence\ObjectRepository; |
7
|
|
|
use Paysera\Bundle\ApiBundle\Service\PathAttributeResolver\DoctrinePathAttributeResolver; |
8
|
|
|
use RuntimeException; |
9
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
10
|
|
|
use Symfony\Component\Config\FileLocator; |
11
|
|
|
use Symfony\Component\DependencyInjection\Definition; |
12
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
13
|
|
|
use Symfony\Component\HttpKernel\DependencyInjection\Extension; |
14
|
|
|
use Symfony\Component\DependencyInjection\Loader; |
15
|
|
|
|
16
|
|
|
class PayseraApiExtension extends Extension |
17
|
|
|
{ |
18
|
96 |
|
public function load(array $configs, ContainerBuilder $container) |
19
|
|
|
{ |
20
|
96 |
|
$configuration = new Configuration(); |
21
|
96 |
|
$config = $this->processConfiguration($configuration, $configs); |
22
|
|
|
|
23
|
96 |
|
$loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config')); |
24
|
96 |
|
$loader->load('services.xml'); |
25
|
|
|
|
26
|
96 |
|
$container->setParameter('paysera_api.locales', $config['locales']); |
27
|
96 |
|
if (count($config['locales']) === 0) { |
28
|
96 |
|
$container->removeDefinition('paysera_api.listener.locale'); |
29
|
|
|
} |
30
|
|
|
|
31
|
96 |
|
if ($config['validation']['property_path_converter'] !== null) { |
32
|
1 |
|
$container->setAlias( |
33
|
1 |
|
'paysera_api.validation.property_path_converter', |
34
|
1 |
|
$config['validation']['property_path_converter'] |
35
|
|
|
); |
36
|
|
|
} |
37
|
|
|
|
38
|
96 |
|
if (count($config['path_attribute_resolvers']) > 0 && !class_exists('Doctrine\ORM\EntityManager')) { |
39
|
|
|
throw new RuntimeException( |
40
|
|
|
'Please install doctrine/orm before configuring paysera_api.path_attribute_resolvers' |
41
|
|
|
); |
42
|
|
|
} |
43
|
|
|
|
44
|
96 |
|
foreach ($config['path_attribute_resolvers'] as $className => $resolverConfig) { |
45
|
96 |
|
$container->setDefinition( |
46
|
96 |
|
'paysera_api.auto_registered.path_attribute_resolver.' . $className, |
47
|
96 |
|
$this->buildPathAttributeResolverDefinition($className, $resolverConfig['field']) |
48
|
|
|
); |
49
|
|
|
} |
50
|
|
|
|
51
|
96 |
|
$this->configurePagination($container, $config['pagination']); |
52
|
96 |
|
} |
53
|
|
|
|
54
|
96 |
|
private function buildPathAttributeResolverDefinition(string $className, string $field): Definition |
55
|
|
|
{ |
56
|
96 |
|
$repositoryDefinition = (new Definition(ObjectRepository::class, [$className])) |
57
|
96 |
|
->setFactory([new Reference('doctrine.orm.entity_manager'), 'getRepository']) |
58
|
|
|
; |
59
|
|
|
|
60
|
96 |
|
return (new Definition(DoctrinePathAttributeResolver::class, [ |
61
|
96 |
|
$repositoryDefinition, |
62
|
96 |
|
$field, |
63
|
96 |
|
]))->addTag('paysera_api.path_attribute_resolver', ['type' => $className]); |
64
|
|
|
} |
65
|
|
|
|
66
|
96 |
|
private function configurePagination(ContainerBuilder $container, array $paginationConfig) |
67
|
|
|
{ |
68
|
96 |
|
$container->setParameter( |
69
|
96 |
|
'paysera_api.pagination.default_total_count_strategy', |
70
|
96 |
|
$paginationConfig['total_count_strategy'] |
71
|
|
|
); |
72
|
96 |
|
$container->setParameter( |
73
|
96 |
|
'paysera_api.pagination.maximum_offset', |
74
|
96 |
|
$paginationConfig['maximum_offset'] |
75
|
|
|
); |
76
|
96 |
|
$container->setParameter( |
77
|
96 |
|
'paysera_api.pagination.default_limit', |
78
|
96 |
|
$paginationConfig['default_limit'] |
79
|
|
|
); |
80
|
96 |
|
$container->setParameter( |
81
|
96 |
|
'paysera_api.pagination.maximum_limit', |
82
|
96 |
|
$paginationConfig['maximum_limit'] |
83
|
|
|
); |
84
|
96 |
|
} |
85
|
|
|
} |
86
|
|
|
|