Failed Conditions
Push — master ( 6560de...8483ce )
by Florent
05:17
created

JoseFrameworkExtension::getAlias()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * The MIT License (MIT)
7
 *
8
 * Copyright (c) 2014-2017 Spomky-Labs
9
 *
10
 * This software may be modified and distributed under the terms
11
 * of the MIT license.  See the LICENSE file for details.
12
 */
13
14
namespace Jose\Bundle\JoseFramework\DependencyInjection;
15
16
use Http\HttplugBundle\HttplugBundle;
17
use Jose\Bundle\Checker\DependencyInjection\Source\ClaimChecker;
18
use Jose\Bundle\Checker\DependencyInjection\Source\HeaderChecker;
19
use Jose\Bundle\Encryption\DependencyInjection\Source\JWEBuilder;
20
use Jose\Bundle\Encryption\DependencyInjection\Source\JWEDecrypter;
21
use Jose\Bundle\Encryption\DependencyInjection\Source\JWESerializer;
22
use Jose\Bundle\JoseFramework\DependencyInjection\Source\Source;
23
use Jose\Bundle\KeyManagement\DependencyInjection\Source\JKUSource;
24
use Jose\Bundle\KeyManagement\DependencyInjection\Source\JWKUriSource;
25
use Jose\Bundle\KeyManagement\DependencyInjection\Source\JWKSetSource;
26
use Jose\Bundle\KeyManagement\DependencyInjection\Source\JWKSource;
27
use Jose\Bundle\Signature\DependencyInjection\Source\JWSBuilder;
28
use Jose\Bundle\Signature\DependencyInjection\Source\JWSSerializer;
29
use Jose\Bundle\Signature\DependencyInjection\Source\JWSVerifier;
30
use Jose\Component\Core\Converter\JsonConverter;
31
use Jose\Component\Core\Converter\StandardConverter;
32
use Symfony\Component\Config\Definition\Processor;
33
use Symfony\Component\Config\FileLocator;
34
use Symfony\Component\DependencyInjection\ContainerBuilder;
35
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
36
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
37
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
38
39
/**
40
 * Class JoseFrameworkExtension.
41
 */
42
final class JoseFrameworkExtension extends Extension implements PrependExtensionInterface
43
{
44
    /**
45
     * @var string
46
     */
47
    private $alias;
48
49
    /**
50
     * @var Source[]
51
     */
52
    private $serviceSources = [];
53
54
    /**
55
     * JoseFrameworkExtension constructor.
56
     *
57
     * @param string $alias
58
     */
59
    public function __construct(string $alias)
60
    {
61
        $this->alias = $alias;
62
        $this->addDefaultSources();
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68
    public function getAlias(): string
69
    {
70
        return $this->alias;
71
    }
72
73
    /**
74
     * {@inheritdoc}
75
     */
76
    public function load(array $configs, ContainerBuilder $container)
77
    {
78
        $processor = new Processor();
79
        $config = $processor->processConfiguration($this->getConfiguration($configs, $container), $configs);
80
81
        $loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
82
        $loader->load('services.yml');
83
        if (true === $container->getParameter('kernel.debug')) {
84
            $loader->load('dev_services.yml');
85
        }
86
87
        $container->setAlias(JsonConverter::class, $config['json_converter']);
88
        if (StandardConverter::class === $config['json_converter']) {
89
            $loader->load('json_converter.yml');
90
        }
91
92
        foreach ($this->serviceSources as $serviceSource) {
93
            $serviceSource->load($config, $container);
94
        }
95
    }
96
97
    /**
98
     * @param Source $source
99
     */
100
    public function addSource(Source $source)
101
    {
102
        $name = $source->name();
103
        if (in_array($name, $this->serviceSources)) {
104
            throw new \InvalidArgumentException(sprintf('The source "%s" is already set.', $name));
105
        }
106
        $this->serviceSources[$name] = $source;
107
    }
108
109
    /**
110
     * @param array            $configs
111
     * @param ContainerBuilder $container
112
     *
113
     * @return Configuration
114
     */
115
    public function getConfiguration(array $configs, ContainerBuilder $container): Configuration
116
    {
117
        return new Configuration($this->getAlias(), $this->serviceSources);
118
    }
119
120
    private function addDefaultSources()
121
    {
122
        if (class_exists(JKUSource::class) && class_exists(HttplugBundle::class)) {
123
            $this->addSource(new JKUSource());
124
        }
125
        if (class_exists(JWKSource::class)) {
126
            $this->addSource(new JWKSource());
127
        }
128
        if (class_exists(JWKUriSource::class)) {
129
            $this->addSource(new JWKUriSource());
130
        }
131
        if (class_exists(JWKSetSource::class)) {
132
            $this->addSource(new JWKSetSource());
133
        }
134
        if (class_exists(ClaimChecker::class)) {
135
            $this->addSource(new ClaimChecker());
136
        }
137
        if (class_exists(HeaderChecker::class)) {
138
            $this->addSource(new HeaderChecker());
139
        }
140
        if (class_exists(JWSBuilder::class)) {
141
            $this->addSource(new JWSBuilder());
142
        }
143
        if (class_exists(JWSSerializer::class)) {
144
            $this->addSource(new JWSSerializer());
145
        }
146
        if (class_exists(JWSVerifier::class)) {
147
            $this->addSource(new JWSVerifier());
148
        }
149
        if (class_exists(JWEBuilder::class)) {
150
            $this->addSource(new JWEBuilder());
151
        }
152
        if (class_exists(JWEDecrypter::class)) {
153
            $this->addSource(new JWEDecrypter());
154
        }
155
        if (class_exists(JWESerializer::class)) {
156
            $this->addSource(new JWESerializer());
157
        }
158
    }
159
160
    /**
161
     * {@inheritdoc}
162
     */
163
    public function prepend(ContainerBuilder $container)
164
    {
165
        $configs = $container->getExtensionConfig($this->getAlias());
166
        $config = $this->processConfiguration($this->getConfiguration($configs, $container), $configs);
167
168
        foreach ($this->serviceSources as $serviceSource) {
169
            $result = $serviceSource->prepend($container, $config);
170
            if (null !== $result) {
171
                $container->prependExtensionConfig($this->getAlias(), $result);
172
            }
173
        }
174
    }
175
}
176