|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Saikootau\ApiBundle\DependencyInjection; |
|
6
|
|
|
|
|
7
|
|
|
use Symfony\Component\Config\FileLocator; |
|
8
|
|
|
use Symfony\Component\DependencyInjection\Extension\Extension; |
|
9
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
|
10
|
|
|
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader; |
|
11
|
|
|
|
|
12
|
|
|
class SaikootauApiExtension extends Extension |
|
13
|
|
|
{ |
|
14
|
|
|
private const SERVICE_EXCEPTION_RESPONSE_LISTENER = 'saikootau_api.event.listener.exception_response'; |
|
15
|
|
|
private const SERVICE_RESOURCE_RESPONSE_LISTENER = 'saikootau_api.event.listener.resource_response'; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Return an instance of the bundles configuration class. |
|
19
|
|
|
* |
|
20
|
|
|
* @param array $config |
|
21
|
|
|
* @param ContainerBuilder $container |
|
22
|
|
|
* |
|
23
|
|
|
* @return Configuration |
|
24
|
|
|
*/ |
|
25
|
1 |
|
public function getConfiguration(array $config, ContainerBuilder $container): Configuration |
|
26
|
|
|
{ |
|
27
|
1 |
|
return new Configuration(); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* {@inheritdoc} |
|
32
|
|
|
*/ |
|
33
|
1 |
|
public function load(array $configs, ContainerBuilder $container): void |
|
34
|
|
|
{ |
|
35
|
1 |
|
$this->loadServices($container); |
|
36
|
|
|
|
|
37
|
1 |
|
$configs = $this->loadConfiguration($configs, $container); |
|
38
|
1 |
|
$this->configureResponseListenerDefaultContentType(self::SERVICE_EXCEPTION_RESPONSE_LISTENER, $configs, $container); |
|
39
|
1 |
|
$this->configureResponseListenerDefaultContentType(self::SERVICE_RESOURCE_RESPONSE_LISTENER, $configs, $container); |
|
40
|
1 |
|
$this->configureErrorResponseListener($configs, $container); |
|
41
|
1 |
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Load the service configuration. |
|
45
|
|
|
* |
|
46
|
|
|
* @param ContainerBuilder $container |
|
47
|
|
|
*/ |
|
48
|
1 |
|
private function loadServices(ContainerBuilder $container): void |
|
49
|
|
|
{ |
|
50
|
1 |
|
$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/..')); |
|
51
|
1 |
|
$loader->load('Resources/config/services.xml'); |
|
52
|
1 |
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Load the bundle configuration. Returns the normalized config. |
|
56
|
|
|
* |
|
57
|
|
|
* @param array $configs |
|
58
|
|
|
* @param ContainerBuilder $container |
|
59
|
|
|
* |
|
60
|
|
|
* @return array |
|
61
|
|
|
*/ |
|
62
|
1 |
|
private function loadConfiguration(array $configs, ContainerBuilder $container): array |
|
63
|
|
|
{ |
|
64
|
1 |
|
$configuration = $this->getConfiguration($configs, $container); |
|
65
|
|
|
|
|
66
|
1 |
|
return $this->processConfiguration($configuration, $configs); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Set the default content type for given response listener service. |
|
71
|
|
|
* |
|
72
|
|
|
* @param string $serviceId |
|
73
|
|
|
* @param array $configs |
|
74
|
|
|
* @param ContainerBuilder $container |
|
75
|
|
|
*/ |
|
76
|
1 |
|
private function configureResponseListenerDefaultContentType(string $serviceId, array $configs, ContainerBuilder $container): void |
|
77
|
|
|
{ |
|
78
|
1 |
|
$definition = $container->getDefinition($serviceId); |
|
79
|
1 |
|
$definition->setArgument(1, $configs['default_content_type']); |
|
80
|
1 |
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Configure special error response listener arguments. Ex. error expose state. |
|
84
|
|
|
* |
|
85
|
|
|
* @param array $configs |
|
86
|
|
|
* @param ContainerBuilder $container |
|
87
|
|
|
*/ |
|
88
|
1 |
|
private function configureErrorResponseListener(array $configs, ContainerBuilder $container): void |
|
89
|
|
|
{ |
|
90
|
1 |
|
$definition = $container->getDefinition(self::SERVICE_EXCEPTION_RESPONSE_LISTENER); |
|
91
|
1 |
|
$definition->setArgument(2, $configs['expose_all_errors']); |
|
92
|
1 |
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|