|
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\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
|
|
|
/** |
|
23
|
|
|
* Class JoseFrameworkExtension. |
|
24
|
|
|
*/ |
|
25
|
|
|
final class JoseFrameworkExtension extends Extension implements PrependExtensionInterface |
|
26
|
|
|
{ |
|
27
|
|
|
/** |
|
28
|
|
|
* @var string |
|
29
|
|
|
*/ |
|
30
|
|
|
private $alias; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var Source[] |
|
34
|
|
|
*/ |
|
35
|
|
|
private $sources = []; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* JoseFrameworkExtension constructor. |
|
39
|
|
|
* |
|
40
|
|
|
* @param string $alias |
|
41
|
|
|
* @param Source[] $sources |
|
42
|
|
|
*/ |
|
43
|
|
|
public function __construct(string $alias, array $sources) |
|
44
|
|
|
{ |
|
45
|
|
|
$this->alias = $alias; |
|
46
|
|
|
$this->sources = $sources; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* {@inheritdoc} |
|
51
|
|
|
*/ |
|
52
|
|
|
public function getAlias(): string |
|
53
|
|
|
{ |
|
54
|
|
|
return $this->alias; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* {@inheritdoc} |
|
59
|
|
|
*/ |
|
60
|
|
|
public function load(array $configs, ContainerBuilder $container) |
|
61
|
|
|
{ |
|
62
|
|
|
$processor = new Processor(); |
|
63
|
|
|
$config = $processor->processConfiguration($this->getConfiguration($configs, $container), $configs); |
|
64
|
|
|
|
|
65
|
|
|
foreach ($this->sources as $source) { |
|
66
|
|
|
$source->load($config, $container); |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @param array $configs |
|
72
|
|
|
* @param ContainerBuilder $container |
|
73
|
|
|
* |
|
74
|
|
|
* @return Configuration |
|
75
|
|
|
*/ |
|
76
|
|
|
public function getConfiguration(array $configs, ContainerBuilder $container): Configuration |
|
77
|
|
|
{ |
|
78
|
|
|
return new Configuration($this->getAlias(), $this->sources); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* {@inheritdoc} |
|
83
|
|
|
*/ |
|
84
|
|
|
public function prepend(ContainerBuilder $container) |
|
85
|
|
|
{ |
|
86
|
|
|
$configs = $container->getExtensionConfig($this->getAlias()); |
|
87
|
|
|
$config = $this->processConfiguration($this->getConfiguration($configs, $container), $configs); |
|
88
|
|
|
|
|
89
|
|
|
foreach ($this->sources as $source) { |
|
90
|
|
|
$result = $source->prepend($container, $config); |
|
91
|
|
|
if (!empty($result)) { |
|
92
|
|
|
$container->prependExtensionConfig($this->getAlias(), $result); |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|