-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAppService.php
167 lines (133 loc) · 3.56 KB
/
AppService.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2017/11/15
* Time: 16:06
*/
namespace WeiFuTong;
use WeiFuTong\Config\LoadConfig;
use WeiFuTong\Container\Container;
use WeiFuTong\Interfaces\ServiceProvider;
use WeiFuTong\Reflection\ReflectionClassEx;
use WeiFuTong\Support\System\Constant;
class AppService
{
private $container;
/**
* 初始化
*/
public function bootstrap()
{
// 加载容器
$this->bootContainer();
// 加载初始服务配置
$this->loadConfig();
// 服务注册
$this->bootProviders();
}
/**
* 获取对象
* @param $name
* @return mixed
* @throws \Exception
*/
public function get($name)
{
if (!$this->container->bound($name)) {
throw new \Exception("object:{$name} is not defined");
}
$object = $this->container->make($name);
return $object;
}
/**
* 绑定至容器中
* @param $abstract
* @param $concrete
*/
public function bind($abstract, $concrete)
{
$abstract = $this->abstractFilter($abstract);
$this->container->bind($abstract, $concrete);
}
/**
* 获取支付基类所需的参数
* @return mixed
*/
public function getPayConstructParams()
{
$loadConfig = $this->container->make(Constant::LOAD_CONFIG);
return $loadConfig->getAccount();
}
/**
* get container
* @return mixed
*/
public function getContainer()
{
return $this->container;
}
/**
* 绑定初始服务配置
*/
private function loadConfig()
{
$loadConfig = new LoadConfig();
$this->container->bind(Constant::LOAD_CONFIG, $loadConfig);
}
/**
* 绑定闭包
*/
private function bootContainer()
{
$this->container = new Container();
}
/**
* 自动加载服务
*/
private function bootProviders()
{
$providers = $this->getProviders();
// 获取接口方法
$interfaceClass = new ReflectionClassEx(ServiceProvider::class);
$IMethods = $interfaceClass->getMethodsStrArr();
// 将所有服务绑定容器中
$this->reflectionProviders($providers, $IMethods);
}
/**
* 通过反射去调用provider方法,实现接口的方法
* @param array $providers
* @param array $methods
*/
private function reflectionProviders($providers, $methods)
{
foreach ($providers as $provider) {
$providerClass = new ReflectionClassEx($provider);
$ins = $providerClass->newInstance($this);
if ($providerClass->implementsInterface(ServiceProvider::class)) {
// 是否继承指定服务接口,继承该接口则调用接口指定方法
$providerClass->callMethods($ins, $methods);
}
}
}
/**
* 获取所有服务
* @return array
*/
private function getProviders()
{
$loadConfig = $this->container->make(Constant::LOAD_CONFIG);
return $loadConfig->getProviders();
}
/**
* 对abstract名进行调整
* @param $abstract
* @return string
*/
private function abstractFilter($abstract)
{
$abstract = trim($abstract);
$abstract = strtolower($abstract);
return $abstract;
}
}