-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathservicelocator.php
94 lines (75 loc) · 2.15 KB
/
servicelocator.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
<?php
/**
* Created by PhpStorm.
* User: lock
* Date: 2017/8/14
* Time: 16:53
* 服务定位模式
* 实现服务使用者和服务的解耦,无需改变代码而只是通过简单配置更服服务实现。
*/
/**
* 服务实例化实现
* Class Creator
*/
class Creator {
public static function createObject($obj) {
}
}
class ServiceLocator {
/**
* 服务实例索引
*/
private $_services = array();
/**
* 服务定义索引
*/
private $_definitions = [];
/**
* 是否全局服务共享(单例模式)
*/
private $_shared = [];
public function has($id) {
return isset($this->_services[$id]) || isset($this->_definitions[$id]);
}
public function __get($id) {
if ($this->has($this->id)) {
$this->get($id);
}
// another implement
}
public function get($id) {
if (isset($this->_services[$id]) && $this->_shared[$id]) {
return $this->_services[$id];
}
if (isset($this->_definitions[$id])) {
// 实例化
$definition = $this->_definitions[$id];
$object = Creator::createObject($definition);//省略服务实例化实现
if ($this->_shared[$id]) {
$this->_services[$id] = $object;
}
return $object;
}
throw new Exception("无法定位服务{$id}");
}
public function set($id, $definition, $share = false) {
if ($definition === null) {
unset($this->_services[$id], $this->_definitions[$id]);
return;
}
unset($this->_services[$id]);
$this->_shared[$id] = $share;
if (is_string($definition)) {
return $this->_definitions[$id] = $definition;
}
if (is_object($definition) || is_callable($definition, true)) {
return $this->_definitions[$id] = $definition;
}
if (is_array($definition)) {
if (isset($definition['class'])) {
return $this->_definitions[$id] = $definition;
}
}
throw new Exception("服务添加失败");
}
}