-
Notifications
You must be signed in to change notification settings - Fork 0
/
DependencyRegistry.php
137 lines (119 loc) · 4.04 KB
/
DependencyRegistry.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<?php
namespace HCO\AutoWiringBundle;
use HCO\AutoWiringBundle\Exception;
/**
* Provides a map of provided classes to service ids.
*
* You can register a provided class using the register method and find services that provide a class using the find* methods.
* Please see the README.md for an explanation of qualifiers and primary.
*/
class DependencyRegistry
{
private $storage;
public function __construct()
{
$this->storage = array();
}
/**
* Registers a provided class for a give service id.
*
* Only the given class name is registered, not its parent classes.
*
* @param $className
* @param $serviceId
* @param string $qualifier
* @param bool $primary
*/
public function register($className, $serviceId, $qualifier = null, $primary = false)
{
if (!isset($this->storage[$className])) {
$this->storage[$className] = array(
'unqualified' => array(),
'qualified' => array(),
);
}
$this->storage[$className]['unqualified'][] = $serviceId;
if ($qualifier !== null) {
$this->registerQualified($className, $serviceId, $qualifier);
}
if($primary) {
if(isset($this->storage[$className]['primary'])) {
throw new Exception(
sprintf(
'Multiple primary services registered for class %s: %s and %s',
$className,
$serviceId,
$this->storage[$className]['primary']
)
);
}
$this->storage[$className]['primary'] = $serviceId;
}
}
/**
* Find a class without looking for a qualifier.
*
* Qualified classes will be retrieved, too.
*
* @param string $className
*/
public function findUnqualified($className)
{
if (!isset($this->storage[$className])) {
throw new Exception(
sprintf(
'Class %s is not registered with the DependencyRegistry',
$className
)
);
}
if(isset($this->storage[$className]['primary'])) {
return $this->storage[$className]['primary'];
}
if (count($this->storage[$className]['unqualified']) !== 1) {
throw new Exception(
sprintf(
'There\'s not exactly one unqualified service registered for %s, thus autowiring is impossible.',
$className
)
);
}
return $this->storage[$className]['unqualified'][0];
}
/**
* Find a class with a given qualifier.
*
* Classes which were registered without a qualifier will not be found.
*
* @param string $className
* @param string $qualifier
*/
public function findQualified($className, $qualifier)
{
if (!isset($this->storage[$className]) || !isset($this->storage[$className]['qualified'][$qualifier])) {
throw new Exception(
sprintf(
'Class %s with qualifier %s is not registered with the DependencyRegistry',
$className,
$qualifier
)
);
}
if (count($this->storage[$className]['qualified'][$qualifier]) !== 1) {
throw new Exception(
sprintf(
'There\'s not exactly one service registered for %s with qualifier %s, thus autowiring is impossible.',
$className
)
);
}
return $this->storage[$className]['qualified'][$qualifier][0];
}
private function registerQualified($className, $serviceId, $qualifier)
{
if (!isset($this->storage[$className]['qualified'][$qualifier])) {
$this->storage[$className]['qualified'][$qualifier] = array();
}
$this->storage[$className]['qualified'][$qualifier][] = $serviceId;
}
}