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