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