-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathService.php
201 lines (172 loc) · 4.51 KB
/
Service.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
<?php
/**
* @author simon <[email protected]>
* @datetime 2018/7/2 6:14
*
* @link http://crcms.cn/
*
* @copyright Copyright © 2018 Rights Reserved CRCMS
*/
namespace CrCms\Microservice\Client;
use Exception;
use DomainException;
use CrCms\Microservice\Bridging\DataPacker;
use Illuminate\Contracts\Container\Container;
use CrCms\Microservice\Client\Contracts\SelectorContract;
use CrCms\Microservice\Client\Exceptions\ServiceException;
/**
* Class Service.
*/
class Service
{
/**
* @var Container
*/
protected $app;
/**
* @var SelectorContract
*/
protected $selector;
/**
* @var DataPacker
*/
protected $packer;
/**
* @var ServiceData|null
*/
protected $content;
/**
* @var int
*/
protected $statusCode;
/**
* @var ServiceFactory
*/
protected $factory;
/**
* @var string
*/
protected $driver;
/**
* @var string
*/
protected $connection;
/**
* Service constructor.
*
* @param Container $container
* @param DataPacker $packer
* @param SelectorContract $selector
* @param ServiceFactory $factory
*/
public function __construct(Container $container, DataPacker $packer, SelectorContract $selector, ServiceFactory $factory)
{
$this->app = $container;
$this->factory = $factory;
$this->selector = $selector;
$this->packer = $packer;
$this->driver();
$this->connection();
}
/**
* @param string $service
* @param string|array $uri
* @param array $params
*
* @return object
*/
public function call(string $service, $uri = '', array $params = [])
{
if (is_array($uri) || empty($uri)) {
$params = $uri ? $uri : [];
$url = explode('.', $service);
$service = array_shift($url);
$uri = implode('.', $url);
}
$data = $this->packer->pack($params ? ['call' => $uri, 'data' => $params] : ['call' => $uri]);
try {
$client = $this->factory->make($this->driver)->call($this->selector->select($service), $data);
} catch (Exception $exception) {
throw new ServiceException($exception);
} finally {
/* 服务上报,事件触发 */
$serverInfo = compact('service', 'uri', 'params');
$callParams = isset($exception) ? ['microservice.call.failed', [$this, $exception, $serverInfo]] : ['microservice.call', [$this, $serverInfo]];
$this->app['events']->dispatch(...$callParams);
}
$this->statusCode = $client->getStatusCode();
$content = $client->getContent();
$this->content = $content ?
new ServiceData($this->packer->unpack($content)) :
null;
return $this->content;
}
/**
* @return bool
*/
public function status(): bool
{
return $this->statusCode >= 200 && $this->statusCode < 300;
}
/**
* @return ServiceData|null
*/
public function getContent()
{
return $this->content;
}
/**
* @return int
*/
public function getStatusCode(): int
{
return $this->statusCode;
}
/**
* @param null|string $driver
*
* @return Service
*/
public function driver(?string $driver = null): self
{
$driver = $driver ? $driver : $this->app->make('config')->get('microservice-client.client');
$connections = array_keys($this->app->make('config')->get('microservice-client.clients'));
if (! in_array($driver, $connections, true)) {
throw new DomainException("The Driver[{$driver}] not exists");
}
$this->driver = $driver;
return $this;
}
/**
* @param null|string $name
*
* @return Service
*/
public function connection(?string $name = null): self
{
$this->connection = $name ? $name : $this->app->make('config')->get('microservice-client.connection');
return $this;
}
/**
* @param string $name
*
* @return mixed
*/
public function __get(string $name)
{
if (is_null($this->content)) {
return;
}
return $this->content->data($name);
}
/**
* @param string $name
* @param array $arguments
*
* @return mixed
*/
public function __call(string $name, array $arguments)
{
return $this->call($name, ...$arguments);
}
}