-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathDi.php
114 lines (103 loc) · 3.01 KB
/
Di.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<?php
/**
* Created by PhpStorm.
* User: yf
* Date: 2018/5/24
* Time: 下午4:17
*/
namespace EasySwoole\Component;
class Di
{
use Singleton;
private $container = [];
private $onKeyMiss = null;
private $alias = [];
public function alias($alias,$key): Di
{
if(!array_key_exists($alias,$this->container)){
$this->alias[$alias] = $key;
return $this;
}else{
throw new \InvalidArgumentException("can not alias a real key: {$alias}");
}
}
public function setOnKeyMiss(callable $call):Di
{
$this->onKeyMiss = $call;
return $this;
}
public function deleteAlias($alias): Di
{
unset($this->alias[$alias]);
return $this;
}
public function set($key, $obj,...$arg):void
{
/*
* 注入的时候不做任何的类型检测与转换
* 由于编程人员为问题,该注入资源并不一定会被用到
*/
$this->container[$key] = [
"obj"=>$obj,
"params"=>$arg
];
}
function delete($key):Di
{
unset($this->container[$key]);
return $this;
}
function clear():Di
{
$this->container = [];
return $this;
}
/**
* @param $key
* @return null
* @throws \Throwable
*/
function get($key)
{
if(isset($this->alias[$key])){
$key = $this->alias[$key];
}
if(isset($this->container[$key])){
$obj = $this->container[$key]['obj'];
$params = $this->container[$key]['params'];
if(is_object($obj) || is_callable($obj)){
return $obj;
}else if(is_string($obj) && class_exists($obj)){
try{
$ref = new \ReflectionClass($obj);
if(empty($params)){
$constructor = $ref->getConstructor();
if($constructor){
$list = $constructor->getParameters();
foreach ($list as $p){
$class = $p->getClass();
if($class){
$temp = $this->get($class->getName());
}else{
$temp = $this->get($p->getName()) ?? $p->getDefaultValue();
}
$params[] = $temp;
}
}
}
$this->container[$key]['obj'] = $ref->newInstanceArgs($params);
return $this->container[$key]['obj'];
}catch (\Throwable $throwable){
throw $throwable;
}
}else{
return $obj;
}
}else{
if(is_callable($this->onKeyMiss)){
return call_user_func($this->onKeyMiss,$key);
}
return null;
}
}
}