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\SourceInterface; |
23
|
|
|
use Jose\Bundle\KeyManagement\DependencyInjection\Source\JKUSource; |
24
|
|
|
use Jose\Bundle\KeyManagement\DependencyInjection\Source\JWKSetSource; |
25
|
|
|
use Jose\Bundle\KeyManagement\DependencyInjection\Source\JWKSource; |
26
|
|
|
use Jose\Bundle\Signature\DependencyInjection\Source\JWSBuilder; |
27
|
|
|
use Jose\Bundle\Signature\DependencyInjection\Source\JWSSerializer; |
28
|
|
|
use Jose\Bundle\Signature\DependencyInjection\Source\JWSVerifier; |
29
|
|
|
use Jose\Component\Core\Converter\JsonConverterInterface; |
30
|
|
|
use Jose\Component\Core\Converter\JsonConverter; |
31
|
|
|
use Symfony\Component\Config\Definition\Processor; |
32
|
|
|
use Symfony\Component\Config\FileLocator; |
33
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
34
|
|
|
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface; |
35
|
|
|
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader; |
36
|
|
|
use Symfony\Component\HttpKernel\DependencyInjection\Extension; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Class JoseFrameworkExtension. |
40
|
|
|
*/ |
41
|
|
|
final class JoseFrameworkExtension extends Extension implements PrependExtensionInterface |
42
|
|
|
{ |
43
|
|
|
/** |
44
|
|
|
* @var string |
45
|
|
|
*/ |
46
|
|
|
private $alias; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var SourceInterface[] |
50
|
|
|
*/ |
51
|
|
|
private $serviceSources = []; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* JoseFrameworkExtension constructor. |
55
|
|
|
* |
56
|
|
|
* @param string $alias |
57
|
|
|
*/ |
58
|
|
|
public function __construct(string $alias) |
59
|
|
|
{ |
60
|
|
|
$this->alias = $alias; |
61
|
|
|
$this->addDefaultSources(); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* {@inheritdoc} |
66
|
|
|
*/ |
67
|
|
|
public function getAlias(): string |
68
|
|
|
{ |
69
|
|
|
return $this->alias; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* {@inheritdoc} |
74
|
|
|
*/ |
75
|
|
|
public function load(array $configs, ContainerBuilder $container) |
76
|
|
|
{ |
77
|
|
|
$processor = new Processor(); |
78
|
|
|
$config = $processor->processConfiguration($this->getConfiguration($configs, $container), $configs); |
79
|
|
|
|
80
|
|
|
$loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); |
81
|
|
|
$loader->load('services.yml'); |
82
|
|
|
if (true === $container->getParameter('kernel.debug')) { |
83
|
|
|
$loader->load('dev_services.yml'); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
$container->setAlias(JsonConverterInterface::class, $config['json_converter']); |
87
|
|
|
if (JsonConverter::class === $config['json_converter']) { |
88
|
|
|
$loader->load('json_converter.yml'); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
foreach ($this->serviceSources as $serviceSource) { |
92
|
|
|
$serviceSource->load($config, $container); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param SourceInterface $source |
98
|
|
|
*/ |
99
|
|
|
public function addSource(SourceInterface $source) |
100
|
|
|
{ |
101
|
|
|
$name = $source->name(); |
102
|
|
|
if (in_array($name, $this->serviceSources)) { |
103
|
|
|
throw new \InvalidArgumentException(sprintf('The source "%s" is already set.', $name)); |
104
|
|
|
} |
105
|
|
|
$this->serviceSources[$name] = $source; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @param array $configs |
110
|
|
|
* @param ContainerBuilder $container |
111
|
|
|
* |
112
|
|
|
* @return Configuration |
113
|
|
|
*/ |
114
|
|
|
public function getConfiguration(array $configs, ContainerBuilder $container): Configuration |
115
|
|
|
{ |
116
|
|
|
return new Configuration($this->getAlias(), $this->serviceSources); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
private function addDefaultSources() |
120
|
|
|
{ |
121
|
|
|
if (class_exists(JKUSource::class) && class_exists(HttplugBundle::class)) { |
122
|
|
|
$this->addSource(new JKUSource()); |
123
|
|
|
} |
124
|
|
|
if (class_exists(JWKSource::class)) { |
125
|
|
|
$this->addSource(new JWKSource()); |
126
|
|
|
} |
127
|
|
|
if (class_exists(JWKSetSource::class)) { |
128
|
|
|
$this->addSource(new JWKSetSource()); |
129
|
|
|
} |
130
|
|
|
if (class_exists(ClaimChecker::class)) { |
131
|
|
|
$this->addSource(new ClaimChecker()); |
132
|
|
|
} |
133
|
|
|
if (class_exists(HeaderChecker::class)) { |
134
|
|
|
$this->addSource(new HeaderChecker()); |
135
|
|
|
} |
136
|
|
|
if (class_exists(JWSBuilder::class)) { |
137
|
|
|
$this->addSource(new JWSBuilder()); |
138
|
|
|
} |
139
|
|
|
if (class_exists(JWSSerializer::class)) { |
140
|
|
|
$this->addSource(new JWSSerializer()); |
141
|
|
|
} |
142
|
|
|
if (class_exists(JWSVerifier::class)) { |
143
|
|
|
$this->addSource(new JWSVerifier()); |
144
|
|
|
} |
145
|
|
|
if (class_exists(JWEBuilder::class)) { |
146
|
|
|
$this->addSource(new JWEBuilder()); |
147
|
|
|
} |
148
|
|
|
if (class_exists(JWEDecrypter::class)) { |
149
|
|
|
$this->addSource(new JWEDecrypter()); |
150
|
|
|
} |
151
|
|
|
if (class_exists(JWESerializer::class)) { |
152
|
|
|
$this->addSource(new JWESerializer()); |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* {@inheritdoc} |
158
|
|
|
*/ |
159
|
|
|
public function prepend(ContainerBuilder $container) |
160
|
|
|
{ |
161
|
|
|
$configs = $container->getExtensionConfig($this->getAlias()); |
162
|
|
|
$config = $this->processConfiguration($this->getConfiguration($configs, $container), $configs); |
163
|
|
|
|
164
|
|
|
foreach ($this->serviceSources as $serviceSource) { |
165
|
|
|
$result = $serviceSource->prepend($container, $config); |
166
|
|
|
if (null !== $result) { |
167
|
|
|
$container->prependExtensionConfig($this->getAlias(), $result); |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
|