1
|
|
|
<?php |
2
|
|
|
namespace ElevenLabs\ApiServiceBundle\DependencyInjection; |
3
|
|
|
|
4
|
|
|
use ElevenLabs\Api\Decoder\Adapter\SymfonyDecoderAdapter; |
5
|
|
|
use ElevenLabs\Api\Service\ApiService; |
6
|
|
|
use ElevenLabs\Api\Service\Denormalizer\ResourceDenormalizer; |
7
|
|
|
use ElevenLabs\Api\Service\Pagination\Provider\PaginationHeader; |
8
|
|
|
use ElevenLabs\ApiServiceBundle\Pagination\PaginationProviderChain; |
9
|
|
|
use JsonSchema\Validator; |
10
|
|
|
use Symfony\Component\Config\FileLocator; |
11
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
12
|
|
|
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader; |
13
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
14
|
|
|
use Symfony\Component\HttpKernel\DependencyInjection\Extension; |
15
|
|
|
use Symfony\Component\Serializer\Encoder\ChainDecoder; |
16
|
|
|
|
17
|
|
|
class ApiServiceExtension extends Extension |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* {@inheritdoc} |
21
|
|
|
*/ |
22
|
6 |
|
public function load(array $configs, ContainerBuilder $container) |
23
|
|
|
{ |
24
|
6 |
|
$configuration = $this->getConfiguration($configs, $container); |
25
|
6 |
|
$config = $this->processConfiguration($configuration, $configs); |
26
|
|
|
|
27
|
6 |
|
$loader = new XmlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config')); |
28
|
6 |
|
$loader->load('services.xml'); |
29
|
|
|
|
30
|
6 |
|
foreach (['client', 'message_factory', 'uri_factory'] as $type) { |
31
|
6 |
|
$container->setAlias(sprintf('api_service.%s', $type), $config['default_services'][$type]); |
32
|
|
|
} |
33
|
|
|
|
34
|
6 |
|
$this->configureSerializer($container, $config['pagination']); |
35
|
6 |
|
$this->configureApiServices($container, $config['apis'], $config['cache']); |
36
|
6 |
|
} |
37
|
|
|
|
38
|
6 |
|
public function configureSerializer(ContainerBuilder $container, array $paginationProviders) |
39
|
|
|
{ |
40
|
6 |
|
$container->setAlias('api_service.serializer', 'serializer'); |
41
|
6 |
|
$denormalizer = $container->getDefinition('api_service.denormalizer.resource'); |
42
|
|
|
|
43
|
6 |
|
if (!empty($paginationProviders)) { |
44
|
2 |
|
$providers = []; |
45
|
|
|
|
46
|
2 |
|
foreach ($paginationProviders as $name => $config) { |
47
|
2 |
|
$providerId = 'api_service.pagination_provider.'.$name; |
48
|
2 |
|
$provider = $container->getDefinition($providerId); |
49
|
2 |
|
$provider->replaceArgument(0, $config); |
50
|
2 |
|
$providers[] = new Reference($providerId); |
51
|
|
|
} |
52
|
|
|
|
53
|
2 |
|
$pagination = $container->getDefinition('api_service.pagination_provider.chain'); |
54
|
2 |
|
$pagination->replaceArgument(0, $providers); |
55
|
|
|
|
56
|
2 |
|
$denormalizer->replaceArgument(0, new Reference('api_service.pagination_provider.chain')); |
57
|
|
|
} else { |
58
|
4 |
|
$denormalizer->replaceArgument(0, null); |
59
|
|
|
} |
60
|
6 |
|
} |
61
|
|
|
|
62
|
6 |
|
public function configureApiServices(ContainerBuilder $container, array $apiServices, array $cache) |
63
|
|
|
{ |
64
|
6 |
|
$serviceFactoryRef = new Reference('api_service.factory'); |
65
|
|
|
|
66
|
|
|
// Register decoder |
67
|
6 |
|
$definition = $container->register('api_service.decoder.symfony', ChainDecoder::class); |
68
|
6 |
|
$definition->setArguments([ |
69
|
|
|
[ |
70
|
6 |
|
new Reference('serializer.encoder.json'), |
71
|
6 |
|
new Reference('serializer.encoder.xml') |
72
|
|
|
] |
73
|
|
|
]); |
74
|
|
|
|
75
|
6 |
|
$definition = $container->register('api_service.decoder', SymfonyDecoderAdapter::class); |
76
|
6 |
|
$definition->setArguments([new Reference('api_service.decoder.symfony')]); |
77
|
|
|
|
78
|
|
|
// Register validator |
79
|
6 |
|
$validator = $container->register('api_service.json_schema_validator', Validator::class); |
80
|
6 |
|
$validator->setPublic(false); |
81
|
|
|
|
82
|
|
|
// Configure schema factory |
83
|
6 |
|
$schemaFactoryId = 'api_service.schema_factory.swagger'; |
84
|
6 |
|
if ($cache['enabled']) { |
85
|
2 |
|
$schemaFactory = $container->getDefinition('api_service.schema_factory.cached_factory'); |
86
|
2 |
|
$schemaFactory->replaceArgument(0, new Reference($cache['service'])); |
87
|
2 |
|
$schemaFactory->replaceArgument(1, new Reference($schemaFactoryId)); |
88
|
2 |
|
$schemaFactoryId = 'api_service.schema_factory.cached_factory'; |
89
|
|
|
} |
90
|
|
|
|
91
|
6 |
|
$container->setAlias('api_service.schema_factory', $schemaFactoryId); |
92
|
|
|
|
93
|
|
|
// Configure each api services |
94
|
6 |
|
foreach ($apiServices as $name => $arguments) { |
95
|
|
|
$container |
96
|
2 |
|
->register('api_service.api.'.$name, ApiService::class) |
97
|
2 |
|
->setFactory([$serviceFactoryRef, 'getService']) |
98
|
2 |
|
->addArgument(new Reference($arguments['client'])) |
99
|
2 |
|
->addArgument(new Reference($schemaFactoryId)) |
100
|
2 |
|
->addArgument($arguments['schema']) |
101
|
2 |
|
->addArgument($arguments['config']); |
102
|
|
|
} |
103
|
6 |
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* {@inheritdoc} |
107
|
|
|
*/ |
108
|
6 |
|
public function getConfiguration(array $config, ContainerBuilder $container) |
109
|
|
|
{ |
110
|
6 |
|
return new Configuration($container->getParameter('kernel.debug')); |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|