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