5
5
class ServiceLocator
6
6
{
7
7
/**
8
- * @var array
8
+ * @var array|Service[]
9
9
*/
10
10
private $ services = [];
11
11
12
12
/**
13
- * @var array
13
+ * @var array|Service[]
14
14
*/
15
15
private $ instantiated = [];
16
16
17
- /**
18
- * @var array
19
- */
20
- private $ shared = [];
21
-
22
- /**
23
- * instead of supplying a class here, you could also store a service for an interface
24
- *
25
- * @param string $class
26
- * @param object $service
27
- * @param bool $share
28
- */
29
- public function addInstance (string $ class , $ service , bool $ share = true )
17
+ public function addInstance (string $ class , Service $ service )
30
18
{
31
19
$ this ->services [$ class ] = $ service ;
32
20
$ this ->instantiated [$ class ] = $ service ;
33
- $ this ->shared [$ class ] = $ share ;
34
21
}
35
22
36
- /**
37
- * instead of supplying a class here, you could also store a service for an interface
38
- *
39
- * @param string $class
40
- * @param array $params
41
- * @param bool $share
42
- */
43
- public function addClass (string $ class , array $ params , bool $ share = true )
23
+ public function addClass (string $ class , array $ params )
44
24
{
45
25
$ this ->services [$ class ] = $ params ;
46
- $ this ->shared [$ class ] = $ share ;
47
26
}
48
27
49
28
public function has (string $ interface ): bool
50
29
{
51
30
return isset ($ this ->services [$ interface ]) || isset ($ this ->instantiated [$ interface ]);
52
31
}
53
32
54
- /**
55
- * @param string $class
56
- *
57
- * @return object
58
- */
59
- public function get (string $ class )
33
+ public function get (string $ class ): Service
60
34
{
61
- if (isset ($ this ->instantiated [$ class ]) && $ this -> shared [ $ class ] ) {
35
+ if (isset ($ this ->instantiated [$ class ])) {
62
36
return $ this ->instantiated [$ class ];
63
37
}
64
38
@@ -81,9 +55,7 @@ public function get(string $class)
81
55
throw new \OutOfRangeException ('Too many arguments given ' );
82
56
}
83
57
84
- if ($ this ->shared [$ class ]) {
85
- $ this ->instantiated [$ class ] = $ object ;
86
- }
58
+ $ this ->instantiated [$ class ] = $ object ;
87
59
88
60
return $ object ;
89
61
}
0 commit comments