-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathSymfonyContainerBridge.php
96 lines (82 loc) · 2.48 KB
/
SymfonyContainerBridge.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
<?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 DI\NotFoundException;
use Psr\Container\ContainerInterface;
use Symfony\Component\DependencyInjection\Container as SymfonyContainer;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerInterface as SymfonyContainerInterface;
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
/**
* Replacement for the Symfony service container.
*
* This container extends Symfony's container with a fallback container when an entry is not found.
* That way, we can put PHP-DI's container as a fallback to Symfony's.
*
* @author Matthieu Napoli <[email protected]>
*/
class SymfonyContainerBridge extends SymfonyContainer implements SymfonyContainerInterface, ContainerInterface
{
/**
* @var ContainerInterface|null
*/
private $fallbackContainer;
/**
* @param ContainerInterface $container
*/
public function setFallbackContainer(ContainerInterface $container)
{
$this->fallbackContainer = $container;
}
/**
* @return ContainerInterface
*/
public function getFallbackContainer()
{
return $this->fallbackContainer;
}
/**
* {@inheritdoc}
*/
public function has($id)
{
if (parent::has($id)) {
return true;
}
if (! $this->fallbackContainer) {
return false;
}
return $this->fallbackContainer->has($id);
}
/**
* {@inheritdoc}
*/
public function get($id, $invalidBehavior = self::EXCEPTION_ON_INVALID_REFERENCE)
{
if (parent::has($id)) {
return parent::get($id, $invalidBehavior);
}
if (! $this->fallbackContainer) {
return false;
}
try {
$entry = $this->fallbackContainer->get($id);
// Stupid hack for Symfony's ContainerAwareInterface
if ($entry instanceof ContainerAwareInterface) {
$entry->setContainer($this);
}
return $entry;
} catch (NotFoundException $e) {
if ($invalidBehavior === self::EXCEPTION_ON_INVALID_REFERENCE) {
throw new ServiceNotFoundException($id, null, $e);
}
}
return null;
}
}