Skip to content

Commit cc98a77

Browse files
committed
Major overhaul
- Objects and arrays are now "null" by default - No longer attempting to create Query or WriteMeta objects on error - Lots of fixes in PSR-7 objects - Minor documentation updates
1 parent e45bdf8 commit cc98a77

40 files changed

+555
-537
lines changed

src/ACL/ACLClient.php

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,16 @@ public function create(ACLEntry $acl, WriteOptions $writeOptions = null)
4343

4444
/** @var \Psr\Http\Message\ResponseInterface $response */
4545
list($duration, $response, $err) = $this->requireOK($this->doRequest($r));
46-
$wm = $this->buildWriteMeta($duration);
47-
4846
if (null !== $err)
49-
return [null, $wm, $err];
47+
return [null, $err];
48+
49+
$wm = $this->buildWriteMeta($duration);
5050

5151
list($data, $err) = $this->decodeBody($response->getBody());
52+
if (null !== $err)
53+
return ['', $wm, $err];
5254

53-
return [$data, $wm, $err];
55+
return [$data, $wm, null];
5456
}
5557

5658
/**
@@ -68,7 +70,10 @@ public function update(ACLEntry $acl, WriteOptions $writeOptions = null)
6870

6971
list($duration, $_, $err) = $this->requireOK($this->doRequest($r));
7072

71-
return [$this->buildWriteMeta($duration), $err];
73+
if (null !== $err)
74+
return [null, $err];
75+
76+
return [$this->buildWriteMeta($duration), null];
7277
}
7378

7479
/**
@@ -86,7 +91,10 @@ public function destroy($id, WriteOptions $writeOptions = null)
8691

8792
list($duration, $_, $err) = $this->requireOK($this->doRequest($r));
8893

89-
return [$this->buildWriteMeta($duration), $err];
94+
if (null !== $err)
95+
return [null, $err];
96+
97+
return [$this->buildWriteMeta($duration), null];
9098
}
9199

92100
/**
@@ -105,14 +113,16 @@ public function cloneACL($id, WriteOptions $writeOptions = null)
105113

106114
/** @var \Psr\Http\Message\ResponseInterface $response */
107115
list($duration, $response, $err) = $this->requireOK($this->doRequest($r));
108-
$wm = $this->buildWriteMeta($duration);
109-
110116
if (null !== $err)
111-
return [null, $wm, $err];
117+
return [null, null, $err];
118+
119+
$wm = $this->buildWriteMeta($duration);
112120

113121
list($data, $err) = $this->decodeBody($response->getBody());
122+
if (null !== $err)
123+
return ['', $wm, $err];
114124

115-
return [$data, $wm, $err];
125+
return [$data, $wm, null];
116126
}
117127

118128
/**
@@ -131,9 +141,10 @@ public function info($id, QueryOptions $queryOptions = null)
131141

132142
/** @var \Psr\Http\Message\ResponseInterface $response */
133143
list($duration, $response, $err) = $this->requireOK($this->doRequest($r));
134-
$qm = $this->buildQueryMeta($duration, $response, $r->getUri());
135144
if (null !== $err)
136-
return [null, $qm, $err];
145+
return [null, null, $err];
146+
147+
$qm = $this->buildQueryMeta($duration, $response, $r->getUri());
137148

138149
list($data, $err) = $this->decodeBody($response->getBody());
139150
if (null !== $err)
@@ -163,10 +174,11 @@ public function listACLs(QueryOptions $queryOptions = null)
163174

164175
/** @var \Psr\Http\Message\ResponseInterface $response */
165176
list($duration, $response, $err) = $this->requireOK($this->doRequest($r));
177+
if (null !== $err)
178+
return [null, null, $err];
179+
166180
$qm = $this->buildQueryMeta($duration, $response, $r->getUri());
167181

168-
if (null !== $err)
169-
return [null, $qm, $err];
170182

171183
list($data, $err) = $this->decodeBody($response->getBody());
172184
if (null !== $err)

src/AbstractCollection.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@
2525
abstract class AbstractCollection implements \JsonSerializable, \ArrayAccess, \Iterator, \Countable
2626
{
2727
/** @var array */
28-
protected $_storage = array();
28+
protected $_storage = [];
2929

3030
/**
3131
* AbstractResponseModel constructor.
3232
* @param array $data
3333
*/
34-
public function __construct(array $data = array())
34+
public function __construct(array $data = [])
3535
{
3636
foreach($data as $k => $v)
3737
{
@@ -227,7 +227,7 @@ protected function _triggerOutOfBoundsError($key, $level = E_USER_NOTICE)
227227
*/
228228
protected function _findKeyMatches($key)
229229
{
230-
$possibleMatches = array();
230+
$possibleMatches = [];
231231
if (is_string($key))
232232
{
233233
$regex = sprintf('{^.*%s.*$}i', substr($key, 0, 2));

src/AbstractModel.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ abstract class AbstractModel implements \JsonSerializable
3030
*
3131
* @param array $data
3232
*/
33-
public function __construct(array $data = array())
33+
public function __construct(array $data = [])
3434
{
3535
foreach($data as $k => $v)
3636
{

src/AbstractOptions.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,13 @@
2323
abstract class AbstractOptions extends AbstractCollection
2424
{
2525
/** @var array */
26-
protected $_definition = array();
26+
protected $_definition = [];
2727

2828
/**
2929
* AbstractConsulConfig constructor.
3030
* @param array $data
3131
*/
32-
public function __construct(array $data = array())
32+
public function __construct(array $data = [])
3333
{
3434
$this->_definition = $this->getDefinition();
3535
parent::__construct($data + $this->_definition);

src/Agent/AgentClient.php

Lines changed: 9 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,10 @@ public function self()
4141

4242
/** @var \Psr\Http\Message\ResponseInterface $response */
4343
list($duration, $response, $err) = $this->requireOK($this->doRequest($r));
44-
$qm = $this->buildQueryMeta($duration, $response, $r->getUri());
45-
4644
if (null !== $err)
47-
return [null, $qm, $err];
45+
return [null, null, $err];
46+
47+
$qm = $this->buildQueryMeta($duration, $response, $r->getUri());
4848

4949
list($data, $err) = $this->decodeBody($response->getBody());
5050

@@ -95,7 +95,7 @@ public function checks()
9595
if (null !== $err)
9696
return [null, $err];
9797

98-
$checks = array();
98+
$checks = [];
9999
foreach($data as $k => $v)
100100
{
101101
$checks[$k] = new AgentCheck($v);
@@ -125,7 +125,7 @@ public function services()
125125
if (null !== $err)
126126
return [null, $err];
127127

128-
$services = array();
128+
$services = [];
129129
foreach($data as $k => $v)
130130
{
131131
$services[$k] = new AgentService($v);
@@ -155,7 +155,7 @@ public function members()
155155
if (null !== $err)
156156
return [null, $err];
157157

158-
$members = array();
158+
$members = [];
159159
foreach($data as $v)
160160
{
161161
$members[] = new AgentMember($v);
@@ -172,8 +172,7 @@ public function members()
172172
*/
173173
public function serviceRegister(AgentServiceRegistration $agentServiceRegistration)
174174
{
175-
$r = new Request('put', 'v1/agent/service/register', $this->c);
176-
$r->body = ($agentServiceRegistration);
175+
$r = new Request('put', 'v1/agent/service/register', $this->c, $agentServiceRegistration);
177176

178177
list($_, $_, $err) = $this->requireOK($this->doRequest($r));
179178

@@ -241,8 +240,7 @@ public function failTTL($checkID, $note)
241240
*/
242241
public function updateTTL($checkID, $output, $status)
243242
{
244-
$r = new Request('put', sprintf('v1/agent/check/update/%s', $checkID), $this->c);
245-
$r->body = (new AgentCheckUpdate(['Output' => $output, 'Status' => $status]));
243+
$r = new Request('put', sprintf('v1/agent/check/update/%s', $checkID), $this->c, new AgentCheckUpdate(['Output' => $output, 'Status' => $status]));
246244

247245
list($_, $_, $err) = $this->requireOK($this->doRequest($r));
248246

@@ -255,8 +253,7 @@ public function updateTTL($checkID, $output, $status)
255253
*/
256254
public function checkRegister(AgentCheckRegistration $agentCheckRegistration)
257255
{
258-
$r = new Request('put', 'v1/agent/check/register', $this->c);
259-
$r->body = ($agentCheckRegistration);
256+
$r = new Request('put', 'v1/agent/check/register', $this->c, $agentCheckRegistration);
260257

261258
list($_, $_, $err) = $this->requireOK($this->doRequest($r));
262259

@@ -363,57 +360,6 @@ public function disableNodeMaintenance()
363360
return $err;
364361
}
365362

366-
/**
367-
* Set non-ttl check's state to passing with optional note
368-
*
369-
* @param string $checkID
370-
* @param string $note
371-
* @return \DCarbone\PHPConsulAPI\Error|null
372-
*/
373-
public function checkPass($checkID, $note = '')
374-
{
375-
$r = new Request('get', sprintf('v1/agent/check/pass/%s', $checkID), $this->c);
376-
$r->params->set('note', $note);
377-
378-
list($_, $_, $err) = $this->requireOK($this->doRequest($r));
379-
380-
return $err;
381-
}
382-
383-
/**
384-
* Set non-ttl check's state to warning with optional note
385-
*
386-
* @param string $checkID
387-
* @param string $note
388-
* @return \DCarbone\PHPConsulAPI\Error|null
389-
*/
390-
public function checkWarn($checkID, $note = '')
391-
{
392-
$r = new Request('get', sprintf('v1/agent/check/warn/%s', $checkID), $this->c);
393-
$r->params->set('note', $note);
394-
395-
list($_, $_, $err) = $this->requireOK($this->doRequest($r));
396-
397-
return $err;
398-
}
399-
400-
/**
401-
* Set non-ttl check's state to critical with optional note
402-
*
403-
* @param string $checkID
404-
* @param string $note
405-
* @return \DCarbone\PHPConsulAPI\Error|null
406-
*/
407-
public function checkFail($checkID, $note = '')
408-
{
409-
$r = new Request('get', sprintf('v1/agent/check/fail/%s', $checkID), $this->c);
410-
$r->params->set('note', $note);
411-
412-
list($_, $_, $err) = $this->requireOK($this->doRequest($r));
413-
414-
return $err;
415-
}
416-
417363
/**
418364
* @return \DCarbone\PHPConsulAPI\Error|null
419365
*/

src/Agent/AgentSelf.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,14 @@ class AgentSelf extends AbstractModel
3535
* AgentSelf constructor.
3636
* @param array $data
3737
*/
38-
public function __construct(array $data = array())
38+
public function __construct(array $data = [])
3939
{
4040
parent::__construct($data);
41-
if (!($this->Config instanceof AgentSelfConfig))
41+
if (null !== $this->Config && !($this->Config instanceof AgentSelfConfig))
4242
$this->Config = new AgentSelfConfig((array)$this->Config);
43-
if (!($this->Coord instanceof AgentSelfCoord))
43+
if (null !== $this->Coord && !($this->Coord instanceof AgentSelfCoord))
4444
$this->Coord = new AgentSelfCoord((array)$this->Coord);
45-
if (!($this->Member instanceof AgentMember))
45+
if (null !== $this->Member && !($this->Member instanceof AgentMember))
4646
$this->Member = new AgentMember((array)$this->Member);
4747
}
4848

src/Agent/AgentSelfConfig.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ class AgentSelfConfig extends AbstractModel
3939
/** @var string */
4040
public $DNSRecursor = '';
4141
/** @var string[] */
42-
public $DNSRecursors = array();
42+
public $DNSRecursors = [];
4343
/** @var string[] */
44-
public $DNSConfig = array();
44+
public $DNSConfig = [];
4545
/** @var string */
4646
public $Domain = '';
4747
/** @var string */
@@ -55,13 +55,13 @@ class AgentSelfConfig extends AbstractModel
5555
/** @var string */
5656
public $AdvertiseAddr = '';
5757
/** @var string[] */
58-
public $Ports = array();
58+
public $Ports = [];
5959
/** @var bool */
6060
public $LeaveOnTerm = false;
6161
/** @var bool */
6262
public $SkipLeaveOnInt = false;
6363
/** @var string[] */
64-
public $Telemetry = array();
64+
public $Telemetry = [];
6565
/** @var int */
6666
public $Protocol = 0;
6767
/** @var bool */
@@ -77,7 +77,7 @@ class AgentSelfConfig extends AbstractModel
7777
/** @var string */
7878
public $KeyFile = '';
7979
/** @var string[] */
80-
public $StartJoin = array();
80+
public $StartJoin = [];
8181
/** @var string */
8282
public $UiDir = '';
8383
/** @var string */

src/Agent/AgentSelfCoord.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
class AgentSelfCoord extends AbstractModel
2626
{
2727
/** @var array */
28-
public $Vec = array();
28+
public $Vec = [];
2929
/** @var float */
3030
public $Error = 0.0;
3131
/** @var float */

src/Agent/AgentServiceRegistration.php

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,25 +41,30 @@ class AgentServiceRegistration extends AbstractModel
4141
/** @var \DCarbone\PHPConsulAPI\Agent\AgentCheck */
4242
public $Check = null;
4343
/** @var \DCarbone\PHPConsulAPI\Agent\AgentCheck[] */
44-
public $Checks = array();
44+
public $Checks = [];
4545

4646
/**
4747
* AgentServiceRegistration constructor.
4848
* @param array $data
4949
*/
50-
public function __construct(array $data = array())
50+
public function __construct(array $data = [])
5151
{
5252
parent::__construct($data);
5353

54-
if (!($this->Check instanceof AgentCheck))
54+
if (null !== $this->Check && !($this->Check instanceof AgentCheck))
5555
$this->Check = new AgentCheck((array)$this->Check);
5656

57-
for ($i = 0, $cnt = count($this->Checks); $i < $cnt; $i++)
57+
if (0 < count($this->Checks))
5858
{
59-
if ($this->Checks[$i] instanceof AgentCheck)
60-
continue;
61-
62-
$this->Checks[$i] = new AgentCheck($this->Checks[$i]);
59+
$this->Checks = array_filter($this->Checks);
60+
if (0 < ($cnt = count($this->Checks)))
61+
{
62+
for ($i = 0, $cnt = count($this->Checks); $i < $cnt; $i++)
63+
{
64+
if (!($this->Checks[$i] instanceof AgentCheck))
65+
$this->Checks[$i] = new AgentCheck($this->Checks[$i]);
66+
}
67+
}
6368
}
6469
}
6570

0 commit comments

Comments
 (0)