Skip to content

Commit 3bf3b47

Browse files
committed
Major refactoring of underlying object model for performance reasons.
1 parent 3e3bdef commit 3bf3b47

File tree

12 files changed

+252
-108
lines changed

12 files changed

+252
-108
lines changed

src/AbstractObjectModel.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
* Class AbstractObjectModel
2121
* @package DCarbone\PHPConsulAPI
2222
*/
23-
abstract class AbstractObjectModel
23+
abstract class AbstractObjectModel implements \JsonSerializable
2424
{
2525
/**
2626
* AbstractObjectModel constructor.
@@ -34,6 +34,16 @@ public function __construct(array $data = array())
3434
}
3535
}
3636

37+
/**
38+
* Specify data which should be serialized to JSON
39+
* @link http://php.net/manual/en/jsonserializable.jsonserialize.php
40+
* @return mixed data which can be serialized by json_encode, which is a value of any type other than a resource.
41+
*/
42+
function jsonSerialize()
43+
{
44+
return array_filter((array)$this);
45+
}
46+
3747
/**
3848
* @return string
3949
*/

src/Agent/AgentClient.php

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
*/
1818

1919
use DCarbone\PHPConsulAPI\AbstractConsulClient;
20+
use DCarbone\PHPConsulAPI\Hydrator;
2021
use DCarbone\PHPConsulAPI\Request;
2122

2223
/**
@@ -50,9 +51,9 @@ public function self()
5051
if (null !== $err)
5152
return [null, $qm, $err];
5253

53-
$this->_self = new AgentSelf($data);
54+
$this->_self = Hydrator::AgentSelf($data);
5455

55-
return [new AgentSelf($data), $qm, null];
56+
return [$this->_self, $qm, null];
5657
}
5758

5859
/**
@@ -70,7 +71,7 @@ public function nodeName()
7071
return ['', $err];
7172
}
7273

73-
return [$this->_self->getConfig()->getNodeName(), null];
74+
return [$this->_self->Config->NodeName, null];
7475
}
7576

7677
/**
@@ -96,7 +97,7 @@ public function checks()
9697
$checks = array();
9798
foreach($data as $k => $v)
9899
{
99-
$checks[$k] = new AgentCheck($v);
100+
$checks[$k] = Hydrator::AgentCheck($v);
100101
}
101102

102103
return [$checks, null];
@@ -125,7 +126,7 @@ public function services()
125126
$services = array();
126127
foreach($data as $k => $v)
127128
{
128-
$services[$k] = new AgentService($v);
129+
$services[$k] = Hydrator::AgentService($v);
129130
}
130131

131132
return [$services, null];
@@ -154,7 +155,7 @@ public function members()
154155
$members = array();
155156
foreach($data as $v)
156157
{
157-
$members[] = new AgentMember($v);
158+
$members[] = Hydrator::AgentMember($v);
158159
}
159160

160161
return [$members, null];
@@ -169,7 +170,7 @@ public function members()
169170
public function serviceRegister(AgentServiceRegistration $agentServiceRegistration)
170171
{
171172
$r = new Request('put', 'v1/agent/service/register', $this->_Config);
172-
$r->setBody($agentServiceRegistration);
173+
$r->body = ($agentServiceRegistration);
173174

174175
list($_, $_, $err) = $this->requireOK($this->doRequest($r));
175176

@@ -238,7 +239,7 @@ public function failTTL($checkID, $note)
238239
public function updateTTL($checkID, $output, $status)
239240
{
240241
$r = new Request('put', sprintf('v1/agent/check/update/%s', rawurlencode($checkID)), $this->_Config);
241-
$r->setBody(new AgentCheckUpdate(['Output' => $output, 'Status' => $status]));
242+
$r->body = (new AgentCheckUpdate(['Output' => $output, 'Status' => $status]));
242243

243244
list($_, $_, $err) = $this->requireOK($this->doRequest($r));
244245

@@ -252,7 +253,7 @@ public function updateTTL($checkID, $output, $status)
252253
public function checkRegister(AgentCheckRegistration $agentCheckRegistration)
253254
{
254255
$r = new Request('put', 'v1/agent/check/register', $this->_Config);
255-
$r->setBody($agentCheckRegistration);
256+
$r->body = ($agentCheckRegistration);
256257

257258
list($_, $_, $err) = $this->requireOK($this->doRequest($r));
258259

@@ -281,7 +282,7 @@ public function join($addr, $wan = false)
281282
{
282283
$r = new Request('put', sprintf('v1/agent/join/%s', rawurlencode($addr)), $this->_Config);
283284
if ($wan)
284-
$r->params()->set('wan', 1);
285+
$r->params->set('wan', 1);
285286

286287
list($_, $_, $err) = $this->requireOK($this->doRequest($r));
287288

@@ -309,8 +310,8 @@ public function forceLeave($node)
309310
public function enableServiceMaintenance($serviceID, $reason = '')
310311
{
311312
$r = new Request('put', sprintf('v1/agent/service/maintenance/%s', rawurlencode($serviceID)), $this->_Config);
312-
$r->params()->set('enable', 'true');
313-
$r->params()->set('reason', $reason);
313+
$r->params->set('enable', 'true');
314+
$r->params->set('reason', $reason);
314315

315316
list($_, $_, $err) = $this->requireOK($this->doRequest($r));
316317

@@ -324,7 +325,7 @@ public function enableServiceMaintenance($serviceID, $reason = '')
324325
public function disableServiceMaintenance($serviceID)
325326
{
326327
$r = new Request('put', sprintf('v1/agent/service/maintenance/%s', rawurlencode($serviceID)), $this->_Config);
327-
$r->params()->set('enable', 'false');
328+
$r->params->set('enable', 'false');
328329

329330
list($_, $_, $err) = $this->requireOK($this->doRequest($r));
330331

@@ -338,8 +339,8 @@ public function disableServiceMaintenance($serviceID)
338339
public function enableNodeMaintenance($reason = '')
339340
{
340341
$r = new Request('put', 'v1/agent/maintenance', $this->_Config);
341-
$r->params()->set('enable', 'true');
342-
$r->params()->set('reason', $reason);
342+
$r->params->set('enable', 'true');
343+
$r->params->set('reason', $reason);
343344

344345
list($_, $_, $err) = $this->requireOK($this->doRequest($r));
345346

@@ -352,7 +353,7 @@ public function enableNodeMaintenance($reason = '')
352353
public function disableNodeMaintenance()
353354
{
354355
$r = new Request('put', 'v1/agent/maintenance', $this->_Config);
355-
$r->params()->set('enable', 'false');
356+
$r->params->set('enable', 'false');
356357

357358
list($_, $_, $err) = $this->requireOK($this->doRequest($r));
358359

@@ -369,7 +370,7 @@ public function disableNodeMaintenance()
369370
public function checkPass($checkID, $note = '')
370371
{
371372
$r = new Request('get', sprintf('v1/agent/check/pass/%s', rawurlencode($checkID)), $this->_Config);
372-
$r->params()->set('note', $note);
373+
$r->params->set('note', $note);
373374

374375
list($_, $_, $err) = $this->requireOK($this->doRequest($r));
375376

@@ -386,7 +387,7 @@ public function checkPass($checkID, $note = '')
386387
public function checkWarn($checkID, $note = '')
387388
{
388389
$r = new Request('get', sprintf('v1/agent/check/warn/%s', rawurlencode($checkID)), $this->_Config);
389-
$r->params()->set('note', $note);
390+
$r->params->set('note', $note);
390391

391392
list($_, $_, $err) = $this->requireOK($this->doRequest($r));
392393

@@ -403,7 +404,7 @@ public function checkWarn($checkID, $note = '')
403404
public function checkFail($checkID, $note = '')
404405
{
405406
$r = new Request('get', sprintf('v1/agent/check/fail/%s', rawurlencode($checkID)), $this->_Config);
406-
$r->params()->set('note', $note);
407+
$r->params->set('note', $note);
407408

408409
list($_, $_, $err) = $this->requireOK($this->doRequest($r));
409410

src/Agent/AgentSelf.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@
2424
*/
2525
class AgentSelf extends AbstractObjectModel
2626
{
27-
/** @var array */
27+
/** @var AgentSelfConfig */
2828
public $Config = null;
29-
/** @var array */
29+
/** @var AgentSelfCoord */
3030
public $Coord = null;
3131
/** @var AgentMember */
3232
public $Member = null;

src/Agent/AgentSelfConfig.php

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<?php namespace DCarbone\PHPConsulAPI\Agent;
2+
3+
/*
4+
Copyright 2016 Daniel Carbone ([email protected])
5+
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
*/
18+
19+
use DCarbone\PHPConsulAPI\AbstractObjectModel;
20+
21+
/**
22+
* Class AgentSelfConfig
23+
* @package DCarbone\PHPConsulAPI\Agent
24+
*/
25+
class AgentSelfConfig extends AbstractObjectModel
26+
{
27+
/** @var bool */
28+
public $Bootstrap = false;
29+
/** @var bool */
30+
public $DevMode = false;
31+
/** @var int */
32+
public $BootstrapExpect = 0;
33+
/** @var bool */
34+
public $Server = false;
35+
/** @var string */
36+
public $Datacenter = '';
37+
/** @var string */
38+
public $DataDir = '';
39+
/** @var string */
40+
public $DNSRecursor = '';
41+
/** @var string[] */
42+
public $DNSRecursors = array();
43+
/** @var string[] */
44+
public $DNSConfig = array();
45+
/** @var string */
46+
public $Domain = '';
47+
/** @var string */
48+
public $LogLevel = '';
49+
/** @var string */
50+
public $NodeName = '';
51+
/** @var string */
52+
public $ClientAddr = '';
53+
/** @var string */
54+
public $BindAddr = '';
55+
/** @var string */
56+
public $AdvertiseAddr = '';
57+
/** @var string[] */
58+
public $Ports = array();
59+
/** @var bool */
60+
public $LeaveOnTerm = false;
61+
/** @var bool */
62+
public $SkipLeaveOnInt = false;
63+
/** @var string[] */
64+
public $Telemetry = array();
65+
/** @var int */
66+
public $Protocol = 0;
67+
/** @var bool */
68+
public $EnableDebug = false;
69+
/** @var bool */
70+
public $VerifyIncoming = false;
71+
/** @var bool */
72+
public $VerifyOutgoing = false;
73+
/** @var string */
74+
public $CAFile = '';
75+
/** @var string */
76+
public $CertFile = '';
77+
/** @var string */
78+
public $KeyFile = '';
79+
/** @var string[] */
80+
public $StartJoin = array();
81+
/** @var string */
82+
public $UiDir = '';
83+
/** @var string */
84+
public $PidFile = '';
85+
/** @var bool */
86+
public $EnableSyslog = false;
87+
/** @var bool */
88+
public $RejoinAfterLeave = false;
89+
}

src/Agent/AgentSelfCoord.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php namespace DCarbone\PHPConsulAPI\Agent;
2+
3+
/*
4+
Copyright 2016 Daniel Carbone ([email protected])
5+
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
*/
18+
19+
use DCarbone\PHPConsulAPI\AbstractObjectModel;
20+
21+
/**
22+
* Class AgentSelfCoord
23+
* @package DCarbone\PHPConsulAPI\Agent
24+
*/
25+
class AgentSelfCoord extends AbstractObjectModel
26+
{
27+
/** @var array */
28+
public $Vec = array();
29+
/** @var float */
30+
public $Error = 0.0;
31+
/** @var float */
32+
public $Adjustment = 0.0;
33+
/** @var float */
34+
public $Height = 0.0;
35+
}

src/Catalog/CatalogClient.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
*/
1818

1919
use DCarbone\PHPConsulAPI\AbstractConsulClient;
20+
use DCarbone\PHPConsulAPI\Hydrator;
2021
use DCarbone\PHPConsulAPI\QueryOptions;
2122
use DCarbone\PHPConsulAPI\Request;
2223
use DCarbone\PHPConsulAPI\WriteOptions;
@@ -110,8 +111,8 @@ public function nodes(QueryOptions $queryOptions = null)
110111
$nodes = array();
111112
foreach($data as $v)
112113
{
113-
$node = new CatalogNode($v);
114-
$nodes[$node->getNode()] = $node;
114+
$node = Hydrator::CatalogNode($v);
115+
$nodes[$node->Node] = $node;
115116
}
116117

117118
return [$nodes, $qm, null];
@@ -156,7 +157,7 @@ public function service($service, $tag = '', QueryOptions $queryOptions = null)
156157
$r = new Request('get', sprintf('v1/catalog/service/%s', rawurlencode($service)), $this->_Config);
157158
$r->setQueryOptions($queryOptions);
158159
if ('' !== $tag)
159-
$r->params()->set('tag', $tag);
160+
$r->params->set('tag', $tag);
160161

161162
list($duration, $response, $err) = $this->requireOK($this->doRequest($r));
162163
$qm = $this->buildQueryMeta($duration, $response);
@@ -172,8 +173,8 @@ public function service($service, $tag = '', QueryOptions $queryOptions = null)
172173
$services = array();
173174
foreach($data as $v)
174175
{
175-
$service = new CatalogService($v);
176-
$services[$service->getServiceID()] = $service;
176+
$service = Hydrator::CatalogService($v);
177+
$services[$service->ServiceID] = $service;
177178
}
178179

179180
return [$services, $qm, null];
@@ -204,6 +205,6 @@ public function node($node, QueryOptions $queryOptions = null)
204205
if (null !== $err)
205206
return [null, $qm, $err];
206207

207-
return [new CatalogNode($data), $qm, null];
208+
return [Hydrator::CatalogNode($data), $qm, null];
208209
}
209210
}

0 commit comments

Comments
 (0)