|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Copyright 2016 - 2018, Cake Development Corporation (http://cakedc.com) |
|
4
|
|
|
* |
|
5
|
|
|
* Licensed under The MIT License |
|
6
|
|
|
* Redistributions of files must retain the above copyright notice. |
|
7
|
|
|
* |
|
8
|
|
|
* @copyright Copyright 2016 - 2018, Cake Development Corporation (http://cakedc.com) |
|
9
|
|
|
* @license MIT License (http://www.opensource.org/licenses/mit-license.php) |
|
10
|
|
|
*/ |
|
11
|
|
|
namespace CakeDC\Api\Service; |
|
12
|
|
|
|
|
13
|
|
|
use CakeDC\Api\Service\Locator\LocatorInterface; |
|
14
|
|
|
use Cake\Event\EventDispatcherInterface; |
|
15
|
|
|
use Cake\Event\EventDispatcherTrait; |
|
16
|
|
|
|
|
17
|
|
|
class ServiceRegistry implements EventDispatcherInterface |
|
18
|
|
|
{ |
|
19
|
|
|
|
|
20
|
|
|
use EventDispatcherTrait; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* LocatorInterface implementation instance. |
|
24
|
|
|
* |
|
25
|
|
|
* @var \Cake\ORM\Locator\LocatorInterface |
|
26
|
|
|
*/ |
|
27
|
|
|
protected static $_locator; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Default LocatorInterface implementation class. |
|
31
|
|
|
* |
|
32
|
|
|
* @var string |
|
33
|
|
|
*/ |
|
34
|
|
|
protected static $_defaultLocatorClass = 'CakeDC\Api\Service\Locator\ServiceLocator'; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Sets and returns a singleton instance of LocatorInterface implementation. |
|
38
|
|
|
* |
|
39
|
|
|
* @param \CakeDC\Api\Service\Locator\LocatorInterface|null $locator Instance of a locator to use. |
|
40
|
|
|
* @return \CakeDC\Api\Service\Locator\LocatorInterface |
|
41
|
|
|
* @deprecated 3.5.0 Use getServiceLocator()/setServiceLocator() instead. |
|
42
|
|
|
*/ |
|
43
|
|
|
public static function locator(LocatorInterface $locator = null) |
|
44
|
|
|
{ |
|
45
|
|
|
deprecationWarning( |
|
46
|
|
|
'TableRegistry::locator() is deprecated. ' . |
|
47
|
|
|
'Use setServiceLocator()/getServiceLocator() instead.' |
|
48
|
|
|
); |
|
49
|
|
|
if ($locator) { |
|
50
|
|
|
static::setServiceLocator($locator); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
return static::getServiceLocator(); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Returns a singleton instance of LocatorInterface implementation. |
|
58
|
|
|
* |
|
59
|
|
|
* @return \CakeDC\Api\Service\Locator\LocatorInterface |
|
60
|
|
|
*/ |
|
61
|
108 |
|
public static function getServiceLocator() |
|
62
|
|
|
{ |
|
63
|
108 |
|
if (!static::$_locator) { |
|
64
|
1 |
|
static::$_locator = new static::$_defaultLocatorClass(); |
|
65
|
1 |
|
} |
|
66
|
|
|
|
|
67
|
108 |
|
return static::$_locator; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Sets singleton instance of LocatorInterface implementation. |
|
72
|
|
|
* |
|
73
|
|
|
* @param \CakeDC\Api\Service\Locator\LocatorInterface $serviceLocator Instance of a locator to use. |
|
74
|
|
|
* @return void |
|
75
|
|
|
*/ |
|
76
|
|
|
public static function setServiceLocator(LocatorInterface $serviceLocator) |
|
77
|
|
|
{ |
|
78
|
|
|
static::$_locator = $serviceLocator; |
|
|
|
|
|
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Stores a list of options to be used when instantiating an object |
|
83
|
|
|
* with a matching alias. |
|
84
|
|
|
* |
|
85
|
|
|
* @param string|null $alias Name of the alias |
|
86
|
|
|
* @param array|null $options list of options for the alias |
|
87
|
|
|
* @return array The config data. |
|
88
|
|
|
* @deprecated 3.6.0 Use \CakeDC\Api\Service\Locator\ServiceLocator::getConfig()/setConfig() instead. |
|
89
|
|
|
*/ |
|
90
|
|
|
public static function config($alias = null, $options = null) |
|
91
|
|
|
{ |
|
92
|
|
|
deprecationWarning( |
|
93
|
|
|
'ServiceRegistry::config() is deprecated. ' . |
|
94
|
|
|
'Use \CakeDC\Api\Service\Locator\ServiceLocator::getConfig()/setConfig() instead.' |
|
95
|
|
|
); |
|
96
|
|
|
|
|
97
|
|
|
return static::getServiceLocator()->config($alias, $options); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Get a table instance from the registry. |
|
102
|
|
|
* |
|
103
|
|
|
* @param string $alias The alias name you want to get. |
|
104
|
|
|
* @param array $options The options you want to build the table with. |
|
105
|
|
|
* @return \CakeDC\Api\Service\Service |
|
106
|
|
|
* @deprecated 3.6.0 Use \CakeDC\Api\Service\Locator\ServiceLocator::get() instead. |
|
107
|
|
|
*/ |
|
108
|
35 |
|
public static function get($alias, array $options = []) |
|
109
|
|
|
{ |
|
110
|
35 |
|
return static::getServiceLocator()->get($alias, $options); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Check to see if an instance exists in the registry. |
|
115
|
|
|
* |
|
116
|
|
|
* @param string $alias The alias to check for. |
|
117
|
|
|
* @return bool |
|
118
|
|
|
* @deprecated 3.6.0 Use \CakeDC\Api\Service\Locator\ServiceLocator::exists() instead. |
|
119
|
|
|
*/ |
|
120
|
|
|
public static function exists($alias) |
|
121
|
|
|
{ |
|
122
|
|
|
return static::getServiceLocator()->exists($alias); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Set an instance. |
|
127
|
|
|
* |
|
128
|
|
|
* @param string $alias The alias to set. |
|
129
|
|
|
* @param \CakeDC\Api\Service\Service $object The table to set. |
|
130
|
|
|
* @return \CakeDC\Api\Service\Service |
|
131
|
|
|
* @deprecated 3.6.0 Use \CakeDC\Api\Service\Locator\ServiceLocator::set() instead. |
|
132
|
|
|
*/ |
|
133
|
|
|
public static function set($alias, Service $object) |
|
134
|
|
|
{ |
|
135
|
|
|
return static::getServiceLocator()->set($alias, $object); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* Removes an instance from the registry. |
|
140
|
|
|
* |
|
141
|
|
|
* @param string $alias The alias to remove. |
|
142
|
|
|
* @return void |
|
143
|
|
|
* @deprecated 3.6.0 Use \CakeDC\Api\Service\Locator\ServiceLocator::remove() instead. |
|
144
|
|
|
*/ |
|
145
|
|
|
public static function remove($alias) |
|
146
|
|
|
{ |
|
147
|
|
|
static::getServiceLocator()->remove($alias); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* Clears the registry of configuration and instances. |
|
152
|
|
|
* |
|
153
|
|
|
* @return void |
|
154
|
|
|
* @deprecated 3.6.0 Use \CakeDC\Api\Service\Locator\ServiceLocator::clear() instead. |
|
155
|
|
|
*/ |
|
156
|
108 |
|
public static function clear() |
|
157
|
|
|
{ |
|
158
|
108 |
|
static::getServiceLocator()->clear(); |
|
159
|
108 |
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* Proxy for static calls on a locator. |
|
163
|
|
|
* |
|
164
|
|
|
* @param string $name Method name. |
|
165
|
|
|
* @param array $arguments Method arguments. |
|
166
|
|
|
* @return mixed |
|
167
|
|
|
*/ |
|
168
|
|
|
public static function __callStatic($name, $arguments) |
|
169
|
|
|
{ |
|
170
|
|
|
deprecationWarning( |
|
171
|
|
|
'TableRegistry::' . $name . '() is deprecated. ' . |
|
172
|
|
|
'Use \CakeDC\Api\Service\Locator\ServiceLocator::' . $name . '() instead.' |
|
173
|
|
|
); |
|
174
|
|
|
|
|
175
|
|
|
return call_user_func_array([static::getServiceLocator(), $name], $arguments); |
|
176
|
|
|
} |
|
177
|
|
|
} |
|
178
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..