1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Laravel\Forge; |
4
|
|
|
|
5
|
|
|
use Iterator; |
6
|
|
|
use ArrayAccess; |
7
|
|
|
use Laravel\Forge\Servers\Factory; |
8
|
|
|
use Laravel\Forge\Traits\LazyIterator; |
9
|
|
|
use Laravel\Forge\Traits\LazyArrayAccess; |
10
|
|
|
use GuzzleHttp\Exception\RequestException; |
11
|
|
|
use Laravel\Forge\Traits\AbstractCollection; |
12
|
|
|
use Laravel\Forge\Exceptions\Servers\ServerWasNotFoundException; |
13
|
|
|
|
14
|
|
|
class Forge implements ArrayAccess, Iterator |
15
|
|
|
{ |
16
|
|
|
use AbstractCollection, LazyIterator, LazyArrayAccess; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* API provider. |
20
|
|
|
* |
21
|
|
|
* @var \Laravel\Forge\ApiProvider |
22
|
|
|
*/ |
23
|
|
|
protected $api; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Servers [id => name] map. |
27
|
|
|
* |
28
|
|
|
* @var array |
29
|
|
|
*/ |
30
|
|
|
protected $serversMap = []; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Single servers cache. |
34
|
|
|
* |
35
|
|
|
* @var array |
36
|
|
|
*/ |
37
|
|
|
protected $serversCache = []; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Create new Servers manager instance. |
41
|
|
|
* |
42
|
|
|
* @param \Laravel\Forge\ApiProvider $api |
43
|
|
|
*/ |
44
|
|
|
public function __construct(ApiProvider $api) |
45
|
|
|
{ |
46
|
|
|
$this->api = $api; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @{inheritdocs} |
51
|
|
|
*/ |
52
|
|
|
public function lazyLoad() |
53
|
|
|
{ |
54
|
|
|
$response = $this->api->getClient()->request('GET', 'servers'); |
55
|
|
|
$data = json_decode((string) $response->getBody(), true); |
56
|
|
|
|
57
|
|
|
$this->items = []; |
58
|
|
|
$this->serversMap = []; |
59
|
|
|
|
60
|
|
|
if (empty($data['servers'])) { |
61
|
|
|
return $this->items; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
foreach ($data['servers'] as $server) { |
65
|
|
|
$this->items[$server['name']] = new Server($this->api, $server); |
66
|
|
|
$this->serversMap[$server['id']] = $server['name']; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return $this->items; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Generate items keys. |
74
|
|
|
*/ |
75
|
|
|
public function generateKeys() |
76
|
|
|
{ |
77
|
|
|
$this->keys = array_keys($this->items); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Initialize servers factory. |
82
|
|
|
* |
83
|
|
|
* @return \Laravel\Forge\Servers\Factory |
84
|
|
|
*/ |
85
|
|
|
public function create() |
86
|
|
|
{ |
87
|
|
|
return new Factory($this->api); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Returns single server. |
92
|
|
|
* |
93
|
|
|
* @param int $serverId |
94
|
|
|
* |
95
|
|
|
* @return \Laravel\Forge\Server |
96
|
|
|
*/ |
97
|
|
|
public function get(int $serverId) |
98
|
|
|
{ |
99
|
|
|
if ($this->lazyLoadInitiated() && isset($this->serversMap[$serverId])) { |
100
|
|
|
return $this->items[$this->serversMap[$serverId]]; |
101
|
|
|
} elseif (isset($this->serversCache[$serverId])) { |
102
|
|
|
return $this->serversCache[$serverId]; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
return $this->loadSingleServer($serverId); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Get server provider credentials. |
110
|
|
|
* |
111
|
|
|
* @return array |
112
|
|
|
*/ |
113
|
|
|
public function credentials(): array |
114
|
|
|
{ |
115
|
|
|
$response = $this->api->getClient()->request('GET', 'credentials'); |
116
|
|
|
$json = json_decode((string) $response->getBody(), true); |
117
|
|
|
|
118
|
|
|
if (empty($json['credentials'])) { |
119
|
|
|
return []; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
return $json['credentials']; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Get first credential for given provider. |
127
|
|
|
* |
128
|
|
|
* @param string $provider |
129
|
|
|
* |
130
|
|
|
* @return int|null |
131
|
|
|
*/ |
132
|
|
|
public function credentialFor(string $provider) |
133
|
|
|
{ |
134
|
|
|
$credentials = $this->credentials(); |
135
|
|
|
|
136
|
|
|
if (!count($credentials)) { |
137
|
|
|
return; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
foreach ($credentials as $credential) { |
141
|
|
|
if ($credential['type'] === $provider) { |
142
|
|
|
return intval($credential['id']); |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Load single server from API and save it to memory cache. |
149
|
|
|
* |
150
|
|
|
* @param int $serverId |
151
|
|
|
* |
152
|
|
|
* @throws \Laravel\Forge\Exceptions\Servers\ServerWasNotFoundException |
153
|
|
|
* |
154
|
|
|
* @return \Laravel\Forge\Server |
155
|
|
|
*/ |
156
|
|
|
protected function loadSingleServer(int $serverId) |
157
|
|
|
{ |
158
|
|
|
try { |
159
|
|
|
$response = $this->api->getClient()->request('GET', 'servers/'.$serverId); |
160
|
|
|
} catch (RequestException $e) { |
161
|
|
|
if ($e->getResponse()->getStatusCode() === 404) { |
162
|
|
|
throw new ServerWasNotFoundException('Server #'.$serverId.' was not found.', 404); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
throw $e; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
return $this->serversCache[$serverId] = Server::createFromResponse($response, $this->api); |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
|