|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Lamoda\Metric\MetricBundle\DependencyInjection; |
|
4
|
|
|
|
|
5
|
|
|
use Lamoda\Metric\MetricBundle\Controller\HttpFoundationResponder; |
|
6
|
|
|
use Lamoda\Metric\MetricBundle\DependencyInjection\DefinitionFactory\Collector; |
|
7
|
|
|
use Lamoda\Metric\MetricBundle\DependencyInjection\DefinitionFactory\Responder; |
|
8
|
|
|
use Lamoda\Metric\MetricBundle\DependencyInjection\DefinitionFactory\ResponseFactory; |
|
9
|
|
|
use Lamoda\Metric\MetricBundle\DependencyInjection\DefinitionFactory\Source; |
|
10
|
|
|
use Lamoda\Metric\MetricBundle\DependencyInjection\DefinitionFactory\Storage; |
|
11
|
|
|
use Lamoda\Metric\Responder\PsrResponder; |
|
12
|
|
|
use Symfony\Component\Config\FileLocator; |
|
13
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
|
14
|
|
|
use Symfony\Component\DependencyInjection\Definition; |
|
15
|
|
|
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader; |
|
16
|
|
|
use Symfony\Component\HttpKernel\DependencyInjection\ConfigurableExtension; |
|
17
|
|
|
|
|
18
|
|
|
final class LamodaMetricExtension extends ConfigurableExtension |
|
19
|
|
|
{ |
|
20
|
4 |
|
public function getAlias(): string |
|
21
|
|
|
{ |
|
22
|
4 |
|
return 'lamoda_metrics'; |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
/** {@inheritdoc} */ |
|
26
|
4 |
|
protected function loadInternal(array $mergedConfig, ContainerBuilder $container) |
|
27
|
|
|
{ |
|
28
|
4 |
|
$loader = new YamlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config')); |
|
29
|
4 |
|
$loader->load('response_factories.yml'); |
|
30
|
4 |
|
$loader->load('services.yml'); |
|
31
|
|
|
|
|
32
|
4 |
|
$this->processFactories($container, $mergedConfig['response_factories'] ?? []); |
|
33
|
4 |
|
$this->processSources($container, $mergedConfig['sources'] ?? []); |
|
34
|
4 |
|
$this->processCollectors($container, $mergedConfig['collectors'] ?? []); |
|
35
|
4 |
|
$this->processResponders($container, $mergedConfig['responders'] ?? []); |
|
36
|
4 |
|
$this->processStorages($container, $mergedConfig['storages'] ?? []); |
|
37
|
4 |
|
} |
|
38
|
|
|
|
|
39
|
4 |
|
private function processFactories(ContainerBuilder $container, array $config): void |
|
40
|
|
|
{ |
|
41
|
4 |
|
foreach ($config as $name => $factoryConfig) { |
|
42
|
|
|
ResponseFactory::register($container, $name, $factoryConfig); |
|
43
|
|
|
} |
|
44
|
4 |
|
} |
|
45
|
|
|
|
|
46
|
4 |
|
private function processCollectors(ContainerBuilder $container, array $config): void |
|
47
|
|
|
{ |
|
48
|
4 |
|
foreach ($config as $name => $collectorConfig) { |
|
49
|
4 |
|
if (!$collectorConfig['enabled']) { |
|
50
|
|
|
continue; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
4 |
|
Collector::register($container, $name, $collectorConfig); |
|
54
|
|
|
} |
|
55
|
4 |
|
} |
|
56
|
|
|
|
|
57
|
4 |
|
private function processStorages(ContainerBuilder $container, array $config): void |
|
58
|
|
|
{ |
|
59
|
4 |
|
foreach ($config as $name => $storageConfig) { |
|
60
|
4 |
|
if (!$storageConfig['enabled']) { |
|
61
|
|
|
continue; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
4 |
|
Storage::register($container, $name, $storageConfig); |
|
65
|
|
|
} |
|
66
|
4 |
|
} |
|
67
|
|
|
|
|
68
|
4 |
|
private function processSources(ContainerBuilder $container, array $sources): void |
|
69
|
|
|
{ |
|
70
|
4 |
|
foreach ($sources as $name => $sourceConfig) { |
|
71
|
4 |
|
if (!$sourceConfig['enabled']) { |
|
72
|
|
|
continue; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
4 |
|
Source::register($container, $name, $sourceConfig); |
|
76
|
|
|
} |
|
77
|
4 |
|
} |
|
78
|
|
|
|
|
79
|
4 |
|
private function processResponders(ContainerBuilder $container, array $config): void |
|
80
|
|
|
{ |
|
81
|
4 |
|
$routerLoader = $container->getDefinition('lamoda_metrics.route_loader'); |
|
82
|
|
|
|
|
83
|
4 |
|
foreach ($config as $name => $responderConfig) { |
|
84
|
4 |
|
if (!$responderConfig['enabled']) { |
|
85
|
|
|
continue; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
4 |
|
$controllerId = Responder::createId($name); |
|
89
|
|
|
|
|
90
|
4 |
|
$psrController = new Definition(PsrResponder::class); |
|
91
|
4 |
|
$psrController->setPublic(false); |
|
92
|
4 |
|
$psrController->setArguments( |
|
93
|
|
|
[ |
|
94
|
4 |
|
Collector::createReference($responderConfig['collector']), |
|
95
|
4 |
|
ResponseFactory::createReference($responderConfig['response_factory'] ?? $name), |
|
96
|
4 |
|
$responderConfig['format_options'] ?? [], |
|
97
|
|
|
] |
|
98
|
|
|
); |
|
99
|
|
|
|
|
100
|
4 |
|
$controller = $container->register($controllerId, HttpFoundationResponder::class); |
|
101
|
4 |
|
$controller->setPublic(true); |
|
102
|
4 |
|
$controller->setArguments([$psrController]); |
|
103
|
|
|
|
|
104
|
4 |
|
$path = $responderConfig['path'] ?? '/' . $name; |
|
105
|
4 |
|
$routerLoader->addMethodCall('registerController', [$name, $path, $controllerId . ':createResponse']); |
|
106
|
|
|
} |
|
107
|
4 |
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|