1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the Borobudur-Kernel package. |
4
|
|
|
* |
5
|
|
|
* (c) Hexacodelabs <http://hexacodelabs.com> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Borobudur\Kernel\Bundling; |
12
|
|
|
|
13
|
|
|
use Borobudur\Config\Configuration; |
14
|
|
|
use Borobudur\DependencyInjection\ContainerBuilder; |
15
|
|
|
use Borobudur\Kernel\Exception\InvalidArgumentException; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @author Iqbal Maulana <[email protected]> |
19
|
|
|
* @created 8/13/15 |
20
|
|
|
*/ |
21
|
|
|
class ExtensionManager |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var AbstractExtension[] |
25
|
|
|
*/ |
26
|
|
|
private $extension = array(); |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Register sets of extension. |
30
|
|
|
* |
31
|
|
|
* @param array $extensions |
32
|
|
|
*/ |
33
|
|
|
public function add(array $extensions) |
34
|
|
|
{ |
35
|
|
|
foreach ($extensions as $extension) { |
36
|
|
|
$this->set($extension); |
37
|
|
|
} |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Register an extension. |
42
|
|
|
* |
43
|
|
|
* @param AbstractExtension $extension |
44
|
|
|
*/ |
45
|
|
|
public function set(AbstractExtension $extension) |
46
|
|
|
{ |
47
|
|
|
$name = $extension->getAlias(); |
48
|
|
|
|
49
|
|
|
if (isset($this->extension[$name])) { |
50
|
|
|
throw new InvalidArgumentException(sprintf( |
51
|
|
|
'Extension with alias "%s" already registered.', |
52
|
|
|
$name |
53
|
|
|
)); |
54
|
|
|
} |
55
|
|
|
$this->extension[$name] = $extension; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Get extension by alias. |
60
|
|
|
* |
61
|
|
|
* @param string $alias |
62
|
|
|
* |
63
|
|
|
* @return AbstractExtension|null |
64
|
|
|
*/ |
65
|
|
|
public function get($alias) |
66
|
|
|
{ |
67
|
|
|
if (isset($this->extension[$alias])) { |
68
|
|
|
return $this->extension[$alias]; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
return null; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Check if extension with specified alias is defined. |
76
|
|
|
* |
77
|
|
|
* @param string $alias |
78
|
|
|
* |
79
|
|
|
* @return bool |
80
|
|
|
*/ |
81
|
|
|
public function has($alias) |
82
|
|
|
{ |
83
|
|
|
return isset($this->extension[$alias]); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Load registered extensions. |
88
|
|
|
* |
89
|
|
|
* @param Configuration $configuration |
90
|
|
|
* @param ContainerBuilder $container |
91
|
|
|
*/ |
92
|
|
|
public function load(Configuration $configuration, ContainerBuilder $container) |
93
|
|
|
{ |
94
|
|
|
$resolver = $container->getParameterBag()->getResolver(); |
95
|
|
|
foreach ($this->extension as $alias => $extension) { |
96
|
|
|
$extension->initialize($configuration, $container); |
97
|
|
|
if ($extension instanceof PrependConfigInterface) { |
98
|
|
|
$extension->prepend($container); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
$extension->load($resolver->resolveValue($configuration->get($alias)), $container); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|