1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* The MIT License (MIT) |
7
|
|
|
* |
8
|
|
|
* Copyright (c) 2014-2019 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\JoseFramework\DependencyInjection\Source\Source; |
17
|
|
|
use Symfony\Component\Config\Definition\Processor; |
18
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
19
|
|
|
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface; |
20
|
|
|
use Symfony\Component\HttpKernel\DependencyInjection\Extension; |
21
|
|
|
|
22
|
|
|
final class JoseFrameworkExtension extends Extension implements PrependExtensionInterface |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
private $alias; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var Source[] |
31
|
|
|
*/ |
32
|
|
|
private $sources = []; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param Source[] $sources |
36
|
|
|
*/ |
37
|
|
|
public function __construct(string $alias, array $sources) |
38
|
|
|
{ |
39
|
|
|
$this->alias = $alias; |
40
|
|
|
$this->sources = $sources; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function getAlias(): string |
44
|
|
|
{ |
45
|
|
|
return $this->alias; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* {@inheritdoc} |
50
|
|
|
*/ |
51
|
|
|
public function load(array $configs, ContainerBuilder $container): void |
52
|
|
|
{ |
53
|
|
|
$processor = new Processor(); |
54
|
|
|
$config = $processor->processConfiguration($this->getConfiguration($configs, $container), $configs); |
|
|
|
|
55
|
|
|
|
56
|
|
|
foreach ($this->sources as $source) { |
57
|
|
|
$source->load($config, $container); |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* {@inheritdoc} |
63
|
|
|
*/ |
64
|
|
|
public function getConfiguration(array $configs, ContainerBuilder $container): Configuration |
65
|
|
|
{ |
66
|
|
|
return new Configuration($this->getAlias(), $this->sources); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* {@inheritdoc} |
71
|
|
|
*/ |
72
|
|
|
public function prepend(ContainerBuilder $container): void |
73
|
|
|
{ |
74
|
|
|
$configs = $container->getExtensionConfig($this->getAlias()); |
75
|
|
|
$config = $this->processConfiguration($this->getConfiguration($configs, $container), $configs); |
|
|
|
|
76
|
|
|
|
77
|
|
|
foreach ($this->sources as $source) { |
78
|
|
|
$result = $source->prepend($container, $config); |
79
|
|
|
if (0 !== \count($result)) { |
80
|
|
|
$container->prependExtensionConfig($this->getAlias(), $result); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: