Completed
Push — main ( bdd72f...bbe282 )
by Niels
15s queued 12s
created

tension.php$0   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 8
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
wmc 2
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the OpenapiBundle package.
7
 *
8
 * (c) Niels Nijens <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Nijens\OpenapiBundle\DependencyInjection;
15
16
use Nijens\OpenapiBundle\EventListener\JsonResponseExceptionSubscriber;
17
use Nijens\OpenapiBundle\ExceptionHandling\EventSubscriber\ProblemExceptionToJsonResponseSubscriber;
18
use Nijens\OpenapiBundle\ExceptionHandling\EventSubscriber\ThrowableToProblemExceptionSubscriber;
19
use Nijens\OpenapiBundle\ExceptionHandling\ThrowableToProblemExceptionTransformer;
20
use Symfony\Component\Config\FileLocator;
21
use Symfony\Component\Config\Loader\LoaderInterface;
22
use Symfony\Component\DependencyInjection\ContainerBuilder;
23
use Symfony\Component\DependencyInjection\Extension\Extension;
24
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
25
use Symfony\Component\HttpKernel\Kernel;
26
27
/**
28
 * Loads and manages the bundle configuration and services.
29
 *
30
 * @author Niels Nijens <[email protected]>
31
 */
32
class NijensOpenapiExtension extends Extension
33
{
34
    /**
35
     * {@inheritdoc}
36
     */
37
    public function load(array $configs, ContainerBuilder $container): void
38
    {
39
        $loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
40
        $loader->load('services.xml');
41
42
        $this->loadDeprecatedServices($loader);
43
44
        $configuration = new Configuration();
45
        $config = $this->processConfiguration($configuration, $configs);
46
47
        $this->registerExceptionHandlingConfiguration($config['exception_handling'], $container);
48
    }
49
50
    /**
51
     * Loads the deprecated services file with backwards compatibility for XML Schema.
52
     */
53
    private function loadDeprecatedServices(XmlFileLoader $loader): void
54
    {
55
        $deprecatedServicesFileSuffix = '';
56
        if ($this->getSymfonyVersion() >= 50100) {
57
            $deprecatedServicesFileSuffix = '_5.1';
58
        }
59
        $loader->load(sprintf('services_deprecated%s.xml', $deprecatedServicesFileSuffix));
60
    }
61
62
    private function registerExceptionHandlingConfiguration(array $config, ContainerBuilder $container): void
63
    {
64
        $definition = $container->getDefinition(ThrowableToProblemExceptionTransformer::class);
65
        $definition->replaceArgument(
66
            0,
67
            array_replace_recursive(Configuration::DEFAULT_EXCEPTION_HANDLING_EXCEPTIONS, $config['exceptions'])
68
        );
69
70
        if ($config['enabled'] !== true) {
71
            $container->removeDefinition(ThrowableToProblemExceptionSubscriber::class);
72
            $container->removeDefinition(ProblemExceptionToJsonResponseSubscriber::class);
73
        }
74
75
        if ($config['enabled'] !== null) {
76
            $container->removeDefinition(JsonResponseExceptionSubscriber::class);
77
            $container->removeDefinition('nijens_openapi.service.exception_json_response_builder');
78
        }
79
    }
80
81
    private function getSymfonyVersion(): int
82
    {
83
        $kernel = new class('symfony_version', false) extends Kernel {
84
            public function registerBundles(): iterable
85
            {
86
                return [];
87
            }
88
89
            public function registerContainerConfiguration(LoaderInterface $loader)
90
            {
91
            }
92
        };
93
94
        return $kernel::VERSION_ID;
95
    }
96
}
97