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 Pimple\Container; |
11
|
|
|
use Pimple\Exception\UnknownIdentifierException; |
12
|
|
|
|
13
|
|
|
class ServiceContainer extends Container |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var static |
17
|
|
|
*/ |
18
|
|
|
protected static $instance; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var array |
22
|
|
|
*/ |
23
|
|
|
protected $instances; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Magic get access. |
27
|
|
|
* |
28
|
|
|
* @param string $id |
29
|
|
|
* @return mixed|null |
30
|
|
|
* @throws UnknownIdentifierException |
31
|
|
|
*/ |
32
|
|
|
public function __get($id) |
33
|
|
|
{ |
34
|
|
|
return $this->offsetGet($id); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Magic set access. |
39
|
|
|
* |
40
|
|
|
* @param string $id |
41
|
|
|
* @param mixed $value |
42
|
|
|
*/ |
43
|
|
|
public function __set($id, $value) |
44
|
|
|
{ |
45
|
|
|
$this->offsetSet($id, $value); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param ServiceContainer $instance |
50
|
|
|
* @return ServiceContainer |
51
|
|
|
*/ |
52
|
|
|
public static function setInstance(ServiceContainer $instance = null) |
53
|
|
|
{ |
54
|
|
|
return self::$instance = $instance; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Set the globally available instance of the container. |
59
|
|
|
* |
60
|
|
|
* @return static |
61
|
|
|
*/ |
62
|
|
|
public static function getInstance() |
63
|
|
|
{ |
64
|
|
|
if (is_null(static::$instance)) { |
65
|
|
|
static::$instance = new static; |
66
|
|
|
} |
67
|
|
|
return static::$instance; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Register an existing instance as shared in the container. |
72
|
|
|
* |
73
|
|
|
* @param $abstract |
74
|
|
|
* @param $instance |
75
|
|
|
* @return mixed |
76
|
|
|
*/ |
77
|
|
|
public function instance($abstract, $instance) |
78
|
|
|
{ |
79
|
|
|
$this->instances[$abstract] = $instance; |
80
|
|
|
|
81
|
|
|
return $instance; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Gets a parameter or an object. |
86
|
|
|
* |
87
|
|
|
* @param string $id The unique identifier for the parameter or object |
88
|
|
|
* |
89
|
|
|
* @return mixed The value of the parameter or an object |
90
|
|
|
* |
91
|
|
|
* @throws UnknownIdentifierException If the identifier is not defined |
92
|
|
|
*/ |
93
|
|
|
public function offsetGet($id) |
94
|
|
|
{ |
95
|
|
|
if (isset($this->instances[$id])) { |
96
|
|
|
return $this->instances[$id]; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
return parent::offsetGet($id); // TODO: Change the autogenerated stub |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|