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\Processor; |
12
|
|
|
|
13
|
|
|
use Borobudur\Config\Configuration; |
14
|
|
|
use Borobudur\DependencyInjection\ContainerBuilder; |
15
|
|
|
use Borobudur\DependencyInjection\ParameterBag; |
16
|
|
|
use Borobudur\EventDispatcher\EventDispatcher; |
17
|
|
|
use Borobudur\Kernel\Bundling\ExtensionManager; |
18
|
|
|
use Borobudur\Kernel\KernelInterface; |
19
|
|
|
use Borobudur\Kernel\ResourceBuilder; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @author Iqbal Maulana <[email protected]> |
23
|
|
|
* @created 8/17/15 |
24
|
|
|
*/ |
25
|
|
|
class ContainerProcessor implements ProcessorInterface |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @var ContainerBuilder |
29
|
|
|
*/ |
30
|
|
|
private $container; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var ExtensionManager |
34
|
|
|
*/ |
35
|
|
|
private $extension; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var Configuration |
39
|
|
|
*/ |
40
|
|
|
private $configuration; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var ResourceBuilder |
44
|
|
|
*/ |
45
|
|
|
private $resource; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* {@inheritdoc} |
49
|
|
|
*/ |
50
|
|
|
public function initialize(KernelInterface $kernel) |
51
|
|
|
{ |
52
|
|
|
$bundleManager = $kernel->getBundleManager(); |
53
|
|
|
$this->buildContainer($kernel); |
54
|
|
|
|
55
|
|
|
foreach ($bundleManager->all() as $bundle) { |
56
|
|
|
if (null !== $extensions = $bundle->getContainerExtensions()) { |
57
|
|
|
$this->extension->add($extensions); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
$bundle->setContainer($this->container); |
61
|
|
|
$bundle->build($this->container); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
$kernel->setContainer($this->container); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Build container |
69
|
|
|
* |
70
|
|
|
* @param KernelInterface $kernel |
71
|
|
|
*/ |
72
|
|
|
private function buildContainer(KernelInterface $kernel) |
73
|
|
|
{ |
74
|
|
|
$this->container = new ContainerBuilder(new ParameterBag($kernel->getKernelParameters())); |
75
|
|
|
$this->extension = new ExtensionManager(); |
76
|
|
|
$this->configuration = new Configuration(); |
77
|
|
|
$this->resource = new ResourceBuilder($this->container, $kernel->getBundleManager()->all()); |
78
|
|
|
|
79
|
|
|
// register some services |
80
|
|
|
foreach ($this->getDefaultServices($kernel) as $id => $service) { |
81
|
|
|
$this->container->register($id, $service, true); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
// register it self. |
85
|
|
|
$this->container->register('container', $this->container, true); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Get default services. |
90
|
|
|
* |
91
|
|
|
* @param KernelInterface $kernel |
92
|
|
|
* |
93
|
|
|
* @return array |
94
|
|
|
*/ |
95
|
|
|
private function getDefaultServices(KernelInterface $kernel) |
96
|
|
|
{ |
97
|
|
|
$services = array( |
98
|
|
|
'kernel' => $kernel, |
99
|
|
|
'config' => $this->configuration, |
100
|
|
|
'extension_manager' => $this->extension, |
101
|
|
|
'middleware' => $kernel->getMiddleware(), |
102
|
|
|
'resource' => $this->resource, |
103
|
|
|
'event_dispatcher' => new EventDispatcher(), |
104
|
|
|
); |
105
|
|
|
|
106
|
|
|
if (null !== $request = $kernel->getRequest()) { |
107
|
|
|
$services['request'] = $request; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
return $services; |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|