-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathKernel.php
116 lines (96 loc) · 3.21 KB
/
Kernel.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<?php
/**
* PHP-DI
*
* @link http://php-di.org/
* @copyright Matthieu Napoli (http://mnapoli.fr/)
* @license http://www.opensource.org/licenses/mit-license.php MIT (see the LICENSE file)
*/
namespace DI\Bridge\Symfony;
use Psr\Container\ContainerInterface;
use Symfony\Component\Debug\DebugClassLoader;
use Symfony\Component\DependencyInjection\Compiler\CheckExceptionOnInvalidReferenceBehaviorPass;
use Symfony\Component\DependencyInjection\ContainerBuilder;
/**
* Customization of Symfony's kernel to setup PHP-DI.
*
* Extend this class instead of Symfony's base kernel.
*
* @author Matthieu Napoli <[email protected]>
*/
abstract class Kernel extends \Symfony\Component\HttpKernel\Kernel
{
/**
* @var ContainerInterface
*/
private $phpdiContainer;
public function __construct($environment, $debug)
{
parent::__construct($environment, $debug);
$this->disableDebugClassLoader();
}
/**
* Implement this method to configure PHP-DI.
*
* @return ContainerInterface
*/
abstract protected function buildPHPDIContainer(\DI\ContainerBuilder $builder);
protected function getContainerBaseClass()
{
return SymfonyContainerBridge::class;
}
protected function buildContainer()
{
$containerBuilder = parent::buildContainer();
$this->removeInvalidReferenceBehaviorPass($containerBuilder);
return $containerBuilder;
}
protected function initializeContainer()
{
parent::initializeContainer();
/** @var SymfonyContainerBridge $rootContainer */
$rootContainer = $this->getContainer();
$rootContainer->setFallbackContainer($this->getPHPDIContainer());
}
/**
* Remove the CheckExceptionOnInvalidReferenceBehaviorPass because
* it was not looking into PHP-DI's entries and thus throwing exceptions.
*
* @todo Replace it by an alternative that can search into PHP-DI too
* Problem: PHP-DI is not initialized when Symfony's container is compiled, because
* it depends on Symfony's container for fallback (cycle…)
*
* @param ContainerBuilder $container
*/
private function removeInvalidReferenceBehaviorPass(ContainerBuilder $container)
{
$passConfig = $container->getCompilerPassConfig();
$compilationPasses = $passConfig->getRemovingPasses();
foreach ($compilationPasses as $i => $pass) {
if ($pass instanceof CheckExceptionOnInvalidReferenceBehaviorPass) {
unset($compilationPasses[$i]);
break;
}
}
$passConfig->setRemovingPasses($compilationPasses);
}
private function disableDebugClassLoader()
{
if (!class_exists(DebugClassLoader::class)) {
return;
}
DebugClassLoader::disable();
}
/**
* @return ContainerInterface
*/
protected function getPHPDIContainer()
{
if ($this->phpdiContainer === null) {
$builder = new \DI\ContainerBuilder();
$builder->wrapContainer($this->getContainer());
$this->phpdiContainer = $this->buildPHPDIContainer($builder);
}
return $this->phpdiContainer;
}
}