|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Doctrine\Bundle\DoctrineBundle\Mapping; |
|
4
|
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
|
6
|
|
|
use Psr\Container\ContainerInterface; |
|
7
|
|
|
use RuntimeException; |
|
8
|
|
|
|
|
9
|
|
|
class ContainerEntityListenerResolver implements EntityListenerServiceResolver |
|
10
|
|
|
{ |
|
11
|
|
|
/** @var ContainerInterface */ |
|
12
|
|
|
private $container; |
|
13
|
|
|
|
|
14
|
|
|
/** @var object[] Map to store entity listener instances. */ |
|
15
|
|
|
private $instances = []; |
|
16
|
|
|
|
|
17
|
|
|
/** @var string[] Map to store registered service ids */ |
|
18
|
|
|
private $serviceIds = []; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @param ContainerInterface $container a service locator for listeners |
|
22
|
|
|
*/ |
|
23
|
|
|
public function __construct(ContainerInterface $container) |
|
24
|
|
|
{ |
|
25
|
|
|
$this->container = $container; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* {@inheritdoc} |
|
30
|
|
|
*/ |
|
31
|
|
|
public function clear($className = null) |
|
32
|
|
|
{ |
|
33
|
|
|
if ($className === null) { |
|
34
|
|
|
$this->instances = []; |
|
35
|
|
|
|
|
36
|
|
|
return; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
$className = $this->normalizeClassName($className); |
|
40
|
|
|
|
|
41
|
|
|
unset($this->instances[$className]); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* {@inheritdoc} |
|
46
|
|
|
*/ |
|
47
|
|
|
public function register($object) |
|
48
|
|
|
{ |
|
49
|
|
|
if (! is_object($object)) { |
|
50
|
|
|
throw new InvalidArgumentException(sprintf('An object was expected, but got "%s".', gettype($object))); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
$className = $this->normalizeClassName(get_class($object)); |
|
54
|
|
|
|
|
55
|
|
|
$this->instances[$className] = $object; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* {@inheritdoc} |
|
60
|
|
|
*/ |
|
61
|
|
|
public function registerService($className, $serviceId) |
|
62
|
|
|
{ |
|
63
|
|
|
$this->serviceIds[$this->normalizeClassName($className)] = $serviceId; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* {@inheritdoc} |
|
68
|
|
|
*/ |
|
69
|
|
|
public function resolve($className) |
|
70
|
|
|
{ |
|
71
|
|
|
$className = $this->normalizeClassName($className); |
|
72
|
|
|
|
|
73
|
|
|
if (! isset($this->instances[$className])) { |
|
74
|
|
|
if (isset($this->serviceIds[$className])) { |
|
75
|
|
|
$this->instances[$className] = $this->resolveService($this->serviceIds[$className]); |
|
76
|
|
|
} else { |
|
77
|
|
|
$this->instances[$className] = new $className(); |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
return $this->instances[$className]; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @param string $serviceId |
|
86
|
|
|
* |
|
87
|
|
|
* @return object |
|
88
|
|
|
*/ |
|
89
|
|
|
private function resolveService($serviceId) |
|
90
|
|
|
{ |
|
91
|
|
|
if (! $this->container->has($serviceId)) { |
|
92
|
|
|
throw new RuntimeException(sprintf('There is no service named "%s"', $serviceId)); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
return $this->container->get($serviceId); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* @param string $className |
|
100
|
|
|
* |
|
101
|
|
|
* @return string |
|
102
|
|
|
*/ |
|
103
|
|
|
private function normalizeClassName($className) |
|
104
|
|
|
{ |
|
105
|
|
|
return trim($className, '\\'); |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|