1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: lenovo |
5
|
|
|
* Date: 6/15/2018 |
6
|
|
|
* Time: 9:33 AM |
7
|
|
|
*/ |
8
|
|
|
namespace TimSDK\Container; |
9
|
|
|
|
10
|
|
|
use TimSDK\Core\API; |
11
|
|
|
use Pimple\Container; |
12
|
|
|
use GuzzleHttp\Client; |
13
|
|
|
use TimSDK\Foundation\Config; |
14
|
|
|
use TimSDK\Foundation\Log\LogManager; |
15
|
|
|
use Pimple\Exception\UnknownIdentifierException; |
16
|
|
|
use TimSDK\Container\ServiceContainerInterface as ContainerContract; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Class ServiceContainer |
20
|
|
|
* @property Config $config |
21
|
|
|
* @property LogManager $log |
22
|
|
|
* @property Client $httpClient |
23
|
|
|
* @package TimSDK\Container |
24
|
|
|
*/ |
25
|
|
|
class ServiceContainer extends Container implements ContainerContract |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @var static |
29
|
|
|
*/ |
30
|
|
|
protected static $instance; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Base path |
34
|
|
|
* |
35
|
|
|
* @var string |
36
|
|
|
*/ |
37
|
|
|
protected $basePath; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var array |
41
|
|
|
*/ |
42
|
|
|
protected $defaultConfig = []; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var array |
46
|
|
|
*/ |
47
|
|
|
protected $userConfig = []; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var array |
51
|
|
|
*/ |
52
|
|
|
protected $providers = [ |
53
|
|
|
// |
54
|
|
|
]; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @var array |
58
|
|
|
*/ |
59
|
|
|
protected $instances; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* ServiceContainer constructor. |
63
|
|
|
* |
64
|
|
|
* @param array $config |
65
|
|
|
* @param array $prepends |
66
|
|
|
*/ |
67
|
31 |
|
public function __construct(array $config = [], array $prepends = []) |
68
|
|
|
{ |
69
|
31 |
|
$this->userConfig = $config; |
70
|
|
|
|
71
|
31 |
|
$this->setBasePath(dirname(__DIR__)); |
72
|
|
|
|
73
|
31 |
|
$this->registerBaseBindings(); |
74
|
|
|
|
75
|
31 |
|
$this->registerProviders($this->getProviders()); |
76
|
|
|
|
77
|
31 |
|
parent::__construct($prepends); |
78
|
31 |
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Magic get access. |
82
|
|
|
* |
83
|
|
|
* @param string $id |
84
|
|
|
* @return mixed|null |
85
|
|
|
* @throws UnknownIdentifierException |
86
|
|
|
*/ |
87
|
3 |
|
public function __get($id) |
88
|
|
|
{ |
89
|
3 |
|
return $this->offsetGet($id); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Magic set access. |
94
|
|
|
* |
95
|
|
|
* @param string $id |
96
|
|
|
* @param mixed $value |
97
|
|
|
*/ |
98
|
1 |
|
public function __set($id, $value) |
99
|
|
|
{ |
100
|
1 |
|
$this->offsetSet($id, $value); |
101
|
1 |
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Gets a parameter or an object. |
105
|
|
|
* |
106
|
|
|
* @param string $id The unique identifier for the parameter or object |
107
|
|
|
* |
108
|
|
|
* @return mixed The value of the parameter or an object |
109
|
|
|
* |
110
|
|
|
* @throws UnknownIdentifierException If the identifier is not defined |
111
|
|
|
*/ |
112
|
23 |
|
public function offsetGet($id) |
113
|
|
|
{ |
114
|
23 |
|
if (isset($this->instances[$id])) { |
115
|
3 |
|
return $this->instances[$id]; |
116
|
|
|
} |
117
|
|
|
|
118
|
21 |
|
return parent::offsetGet($id); // TODO: Change the autogenerated stub |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @param ServiceContainer $instance |
123
|
|
|
* @return ServiceContainer |
124
|
|
|
*/ |
125
|
31 |
|
public static function setInstance(ServiceContainer $instance = null) |
126
|
|
|
{ |
127
|
31 |
|
return self::$instance = $instance; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Set the globally available instance of the container. |
132
|
|
|
* |
133
|
|
|
* @return static |
134
|
|
|
*/ |
135
|
8 |
|
public static function getInstance() |
136
|
|
|
{ |
137
|
8 |
|
if (is_null(static::$instance)) { |
138
|
|
|
static::$instance = new static; |
139
|
|
|
} |
140
|
8 |
|
return static::$instance; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Register an existing instance as shared in the container. |
145
|
|
|
* |
146
|
|
|
* @param $abstract |
147
|
|
|
* @param $instance |
148
|
|
|
* @return mixed |
149
|
|
|
*/ |
150
|
31 |
|
public function instance($abstract, $instance) |
151
|
|
|
{ |
152
|
31 |
|
$this->instances[$abstract] = $instance; |
153
|
|
|
|
154
|
31 |
|
return $instance; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @param mixed $basePath |
159
|
|
|
*/ |
160
|
31 |
|
public function setBasePath($basePath) |
161
|
|
|
{ |
162
|
31 |
|
$this->basePath = rtrim($basePath, DIRECTORY_SEPARATOR); |
163
|
|
|
|
164
|
31 |
|
$this->instance('path.base', $this->basePath()); |
165
|
31 |
|
$this->instance('path.cert', $this->basePath('Cert')); |
166
|
31 |
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Get the base path |
170
|
|
|
* |
171
|
|
|
* @param string $path Optionally, a path to append to the base path |
172
|
|
|
* @return string |
173
|
|
|
*/ |
174
|
31 |
|
public function basePath($path = '') |
175
|
|
|
{ |
176
|
31 |
|
return $this->basePath.($path ? DIRECTORY_SEPARATOR.$path : $path); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Get the version number of the application. |
181
|
|
|
* |
182
|
|
|
* @return string |
183
|
|
|
*/ |
184
|
1 |
|
public function version() |
185
|
|
|
{ |
186
|
1 |
|
throw new \InvalidArgumentException('Unknow version.'); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* @return array |
191
|
|
|
*/ |
192
|
10 |
|
public function getConfig() |
193
|
|
|
{ |
194
|
|
|
$base = [ |
195
|
|
|
// http://docs.guzzlephp.org/en/stable/request-options.html |
196
|
|
|
'http' => [ |
197
|
10 |
|
'timeout' => 5.0, |
198
|
10 |
|
'base_uri' => API::BASE_URL, |
199
|
|
|
], |
200
|
|
|
]; |
201
|
|
|
|
202
|
10 |
|
return array_replace_recursive($base, $this->defaultConfig, $this->userConfig); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* Register all providers |
207
|
|
|
* @param array $providers |
208
|
|
|
*/ |
209
|
31 |
|
public function registerProviders(array $providers) |
210
|
|
|
{ |
211
|
31 |
|
foreach ($providers as $provider) { |
212
|
31 |
|
parent::register(new $provider()); |
|
|
|
|
213
|
|
|
} |
214
|
31 |
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* Register the basic bindings into the container. |
218
|
|
|
* |
219
|
|
|
* @return void |
220
|
|
|
*/ |
221
|
31 |
|
public function registerBaseBindings() |
222
|
|
|
{ |
223
|
31 |
|
self::setInstance($this); |
224
|
|
|
|
225
|
31 |
|
$this->instance(ServiceContainer::class, $this); |
226
|
|
|
|
227
|
31 |
|
$this->instance('app', $this); |
228
|
31 |
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* Get all providers |
232
|
|
|
* |
233
|
|
|
* @return array |
234
|
|
|
*/ |
235
|
31 |
|
public function getProviders() |
236
|
|
|
{ |
237
|
31 |
|
return array_merge([ |
238
|
31 |
|
\TimSDK\Foundation\ServiceProviders\ConfigServiceProvider::class, |
239
|
|
|
\TimSDK\Foundation\ServiceProviders\LogServiceProvider::class, |
240
|
|
|
\TimSDK\Foundation\ServiceProviders\HttpClientServiceProvider::class, |
241
|
31 |
|
], $this->providers); |
242
|
|
|
} |
243
|
|
|
} |
244
|
|
|
|
This check looks for a call to a parent method whose name is different than the method from which it is called.
Consider the following code:
The
getFirstName()
method in theSon
calls the wrong method in the parent class.