|
1
|
|
|
<?php |
|
2
|
|
|
namespace Kotori\Core; |
|
3
|
|
|
|
|
4
|
|
|
use Kotori\Exception\ContainerException; |
|
5
|
|
|
use ReflectionClass; |
|
6
|
|
|
use ReflectionException; |
|
7
|
|
|
|
|
8
|
|
|
class Container |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* container instances |
|
12
|
|
|
* |
|
13
|
|
|
* @var array |
|
14
|
|
|
*/ |
|
15
|
|
|
protected $containers = []; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* bind maps |
|
19
|
|
|
* |
|
20
|
|
|
* @var array |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $bind = []; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* instance handle |
|
26
|
|
|
* |
|
27
|
|
|
* @var object |
|
28
|
|
|
*/ |
|
29
|
|
|
protected static $instance; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Class constructor |
|
33
|
|
|
* |
|
34
|
|
|
* Bind default accessors |
|
35
|
|
|
*/ |
|
36
|
|
|
public function __construct() |
|
37
|
|
|
{ |
|
38
|
|
|
$this->bind([ |
|
39
|
|
|
'cache' => \Kotori\Core\Cache::class, |
|
40
|
|
|
'config' => \Kotori\Core\Config::class, |
|
41
|
|
|
'controller' => \Kotori\Core\Controller::class, |
|
42
|
|
|
'request' => \Kotori\Http\Request::class, |
|
43
|
|
|
'response' => \Kotori\Http\Response::class, |
|
44
|
|
|
'route' => \Kotori\Http\Route::class, |
|
45
|
|
|
'trace' => \Kotori\Debug\Trace::class, |
|
46
|
|
|
'model/provider' => \Kotori\Core\Model\Provider::class, |
|
47
|
|
|
'logger' => \Kotori\Debug\Logger::class, |
|
48
|
|
|
]); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Get singleton |
|
53
|
|
|
* |
|
54
|
|
|
* @return \Kotori\Core\Container |
|
55
|
|
|
*/ |
|
56
|
28 |
|
public static function getInstance() |
|
57
|
|
|
{ |
|
58
|
28 |
|
if (is_null(self::$instance)) { |
|
59
|
|
|
self::$instance = new self(); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
28 |
|
return self::$instance; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Set the object instance in the container |
|
67
|
|
|
* |
|
68
|
|
|
* @param string $abstract |
|
69
|
|
|
* @param string $concrete |
|
70
|
|
|
*/ |
|
71
|
2 |
|
public static function set($abstract, $concrete) |
|
72
|
|
|
{ |
|
73
|
2 |
|
self::getInstance()->containers[$abstract] = $concrete; |
|
74
|
2 |
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Get the object instance in the container |
|
78
|
|
|
* |
|
79
|
|
|
* @param string $abstract |
|
80
|
|
|
* @return object |
|
81
|
|
|
* |
|
82
|
|
|
* @throws \Kotori\Exception\ContainerException |
|
83
|
|
|
*/ |
|
84
|
28 |
|
public static function get($abstract) |
|
85
|
|
|
{ |
|
86
|
28 |
|
if (!isset(self::getInstance()->containers[$abstract])) { |
|
87
|
|
|
try { |
|
88
|
2 |
|
$reflect = new ReflectionClass(self::getInstance()->bind[$abstract]); |
|
89
|
|
|
} catch (ReflectionException $e) { |
|
90
|
|
|
throw new ContainerException('Cannot find "' . $abstract . '" in container'); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
2 |
|
self::set($abstract, $reflect->newInstanceArgs([])); |
|
|
|
|
|
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
28 |
|
return self::getInstance()->containers[$abstract]; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Get the object instance in the container by class name |
|
101
|
|
|
* |
|
102
|
|
|
* @param string $className |
|
103
|
|
|
* @return object |
|
104
|
|
|
* |
|
105
|
|
|
* @throws \Kotori\Exception\ContainerException |
|
106
|
|
|
*/ |
|
107
|
|
|
public static function getByClassName($className) |
|
108
|
|
|
{ |
|
109
|
|
|
$bind = self::getInstance()->bind; |
|
110
|
|
|
|
|
111
|
|
|
foreach ($bind as $abstract => $abstractClassName) { |
|
112
|
|
|
if ($className == $abstractClassName) { |
|
113
|
|
|
return self::get($abstract); |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
throw new ContainerException('Cannot find "' . $className . '" in container'); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* bind object maps for the container |
|
122
|
|
|
* |
|
123
|
|
|
* @param mixed $abstract |
|
124
|
|
|
* @param object $concrete |
|
125
|
|
|
* @return \Kotori\Core\Container |
|
126
|
|
|
*/ |
|
127
|
|
|
public function bind($abstract, $concrete = null) |
|
128
|
|
|
{ |
|
129
|
|
|
if (is_array($abstract)) { |
|
130
|
|
|
$this->bind = array_merge($this->bind, $abstract); |
|
131
|
|
|
} else { |
|
132
|
|
|
$this->bind[$abstract] = $concrete; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
return $this; |
|
136
|
|
|
} |
|
137
|
|
|
} |
|
138
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: