Skip to content

Commit 4fcfb51

Browse files
committed
some work on agent package, need to come up with more efficient way to handle "nullable" fields...
1 parent 87f1324 commit 4fcfb51

35 files changed

+1837
-173
lines changed

src/ACL/ACLAuthMethod.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,9 @@ class ACLAuthMethod extends AbstractModel
8585

8686
/**
8787
* ACLAuthMethod constructor.
88-
* @param array $data
88+
* @param array|null $data
8989
*/
90-
public function __construct(array $data = [])
90+
public function __construct(?array $data = null)
9191
{
9292
parent::__construct($data);
9393
if (!isset($this->MaxTokenTTL)) {

src/ACL/ACLReplicationStatus.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,9 @@ class ACLReplicationStatus extends AbstractModel
5757

5858
/**
5959
* ACLReplicationStatus constructor.
60-
* @param array $data
60+
* @param array|null $data
6161
*/
62-
public function __construct(array $data = [])
62+
public function __construct(?array $data = null)
6363
{
6464
parent::__construct($data);
6565
if (!isset($this->LastSuccess)) {

src/ACL/ACLToken.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,9 +123,9 @@ class ACLToken extends AbstractModel
123123

124124
/**
125125
* ACLToken constructor.
126-
* @param array $data
126+
* @param array|null $data
127127
*/
128-
public function __construct(array $data = [])
128+
public function __construct(?array $data = null)
129129
{
130130
parent::__construct($data);
131131
if (!isset($this->ExpirationTTL)) {

src/ACL/ACLTokenListEntry.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ class ACLTokenListEntry extends AbstractModel
108108
* ACLToken constructor.
109109
* @param array $data
110110
*/
111-
public function __construct(array $data = [])
111+
public function __construct(?array $data = null)
112112
{
113113
parent::__construct($data);
114114
if (!isset($this->CreateTime)) {

src/AbstractModel.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,14 @@ abstract class AbstractModel implements \JsonSerializable
3333
* Convenience method to help set scalar types. Any extending class must have a constructor that builds any
3434
* array / object properties it may have.
3535
*
36-
* @param array $data
36+
* @param array|null $data
3737
*/
38-
public function __construct(array $data = [])
38+
public function __construct(?array $data = [])
3939
{
40+
// fast path for "empty"
41+
if (null === $data || [] === $data) {
42+
return;
43+
}
4044
foreach ($data as $k => $v) {
4145
$this->hydrateField($k, $v);
4246
}

src/AbstractModels.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ public function __construct(?array $children = [])
4646
)
4747
);
4848
}
49-
if (null === $children) {
49+
// fastpath for "empty"
50+
if (null === $children || [] === $children) {
5051
return;
5152
}
5253
foreach ($children as $child) {

src/Agent/AgentCheck.php

Lines changed: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,29 @@
1919
*/
2020

2121
use DCarbone\PHPConsulAPI\AbstractModel;
22+
use DCarbone\PHPConsulAPI\Health\HealthCheckDefinition;
23+
use DCarbone\PHPConsulAPI\Hydration;
2224

2325
/**
2426
* Class AgentCheck
2527
*/
2628
class AgentCheck extends AbstractModel
2729
{
30+
protected const FIELDS = [
31+
self::FIELD_HEALTH_CHECK_DEFINITION => [
32+
Hydration::FIELD_TYPE => Hydration::OBJECT,
33+
Hydration::FIELD_CLASS => HealthCheckDefinition::class,
34+
Hydration::FIELD_NULLABLE => false,
35+
],
36+
self::FIELD_NAMESPACE => [
37+
Hydration::FIELD_TYPE => Hydration::STRING,
38+
Hydration::FIELD_NULLABLE => true,
39+
],
40+
];
41+
42+
private const FIELD_HEALTH_CHECK_DEFINITION = 'HealthCheckDefinition';
43+
private const FIELD_NAMESPACE = 'Namespace';
44+
2845
/** @var string */
2946
public string $Node = '';
3047
/** @var string */
@@ -41,6 +58,24 @@ class AgentCheck extends AbstractModel
4158
public string $ServiceID = '';
4259
/** @var string */
4360
public string $ServiceName = '';
61+
/** @var string */
62+
public string $Type = '';
63+
/** @var \DCarbone\PHPConsulAPI\Health\HealthCheckDefinition */
64+
public HealthCheckDefinition $HealthCheckDefinition;
65+
/** @var string|null */
66+
public ?string $Namespace = null;
67+
68+
/**
69+
* AgentCheck constructor.
70+
* @param array $data
71+
*/
72+
public function __construct(?array $data = null)
73+
{
74+
parent::__construct($data);
75+
if (!isset($this->HealthCheckDefinition)) {
76+
$this->HealthCheckDefinition = new HealthCheckDefinition([]);
77+
}
78+
}
4479

4580
/**
4681
* @return string
@@ -186,11 +221,47 @@ public function setServiceName(string $serviceName): self
186221
return $this;
187222
}
188223

224+
/**
225+
* @return \DCarbone\PHPConsulAPI\Health\HealthCheckDefinition
226+
*/
227+
public function getHealthCheckDefinition(): HealthCheckDefinition
228+
{
229+
return $this->HealthCheckDefinition;
230+
}
231+
232+
/**
233+
* @param \DCarbone\PHPConsulAPI\Health\HealthCheckDefinition $healthCheckDefinition
234+
* @return \DCarbone\PHPConsulAPI\Agent\AgentCheck
235+
*/
236+
public function setHealthCheckDefinition(HealthCheckDefinition $healthCheckDefinition): self
237+
{
238+
$this->HealthCheckDefinition = $healthCheckDefinition;
239+
return $this;
240+
}
241+
242+
/**
243+
* @return string|null
244+
*/
245+
public function getNamespace(): ?string
246+
{
247+
return $this->Namespace;
248+
}
249+
250+
/**
251+
* @param string|null $namespace
252+
* @return \DCarbone\PHPConsulAPI\Agent\AgentCheck
253+
*/
254+
public function setNamespace(?string $namespace): self
255+
{
256+
$this->Namespace = $namespace;
257+
return $this;
258+
}
259+
189260
/**
190261
* @return string
191262
*/
192263
public function __toString(): string
193264
{
194-
return (string) $this->CheckID;
265+
return (string)$this->CheckID;
195266
}
196267
}

src/Agent/AgentCheckRegistration.php

Lines changed: 56 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,49 +18,90 @@
1818
limitations under the License.
1919
*/
2020

21+
use DCarbone\PHPConsulAPI\Hydration;
22+
2123
/**
2224
* Class AgentCheckRegistration
2325
*/
2426
class AgentCheckRegistration extends AgentServiceCheck
2527
{
26-
/** @var string */
27-
public string $ID = '';
28-
/** @var string */
29-
public string $ServiceID = '';
28+
protected const FIELDS = [
29+
self::FIELD_ID => [
30+
Hydration::FIELD_TYPE => Hydration::STRING,
31+
Hydration::FIELD_NULLABLE => true,
32+
],
33+
self::FIELD_SERVICE_ID => [
34+
Hydration::FIELD_TYPE => Hydration::STRING,
35+
Hydration::FIELD_NULLABLE => true,
36+
],
37+
self::FIELD_NAMESPACE => [
38+
Hydration::FIELD_TYPE => Hydration::STRING,
39+
Hydration::FIELD_NULLABLE => true,
40+
],
41+
];
42+
43+
private const FIELD_ID = 'ID';
44+
private const FIELD_SERVICE_ID = 'ServiceID';
45+
private const FIELD_NAMESPACE = 'Namespace';
46+
47+
/** @var string|null */
48+
public ?string $ID = null;
49+
/** @var string|null */
50+
public ?string $ServiceID = null;
51+
/** @var string|null */
52+
public ?string $Namespace = null;
3053

3154
/**
32-
* @return string
55+
* @return string|null
3356
*/
34-
public function getID(): string
57+
public function getID(): ?string
3558
{
3659
return $this->ID;
3760
}
3861

3962
/**
40-
* @param string $id
63+
* @param string|null $ID
4164
* @return \DCarbone\PHPConsulAPI\Agent\AgentCheckRegistration
4265
*/
43-
public function setID(string $id): self
66+
public function setID(?string $ID): self
4467
{
45-
$this->ID = $id;
68+
$this->ID = $ID;
4669
return $this;
4770
}
4871

4972
/**
50-
* @return string
73+
* @return string|null
5174
*/
52-
public function getServiceID(): string
75+
public function getServiceID(): ?string
5376
{
5477
return $this->ServiceID;
5578
}
5679

5780
/**
58-
* @param string $serviceID
81+
* @param string|null $ServiceID
82+
* @return \DCarbone\PHPConsulAPI\Agent\AgentCheckRegistration
83+
*/
84+
public function setServiceID(?string $ServiceID): self
85+
{
86+
$this->ServiceID = $ServiceID;
87+
return $this;
88+
}
89+
90+
/**
91+
* @return string|null
92+
*/
93+
public function getNamespace(): ?string
94+
{
95+
return $this->Namespace;
96+
}
97+
98+
/**
99+
* @param string|null $Namespace
59100
* @return \DCarbone\PHPConsulAPI\Agent\AgentCheckRegistration
60101
*/
61-
public function setServiceID(string $serviceID): self
102+
public function setNamespace(?string $Namespace): self
62103
{
63-
$this->ServiceID = $serviceID;
104+
$this->Namespace = $Namespace;
64105
return $this;
65106
}
66107

@@ -69,6 +110,6 @@ public function setServiceID(string $serviceID): self
69110
*/
70111
public function __toString(): string
71112
{
72-
return (string) $this->Name;
113+
return (string)$this->Name;
73114
}
74115
}

src/Agent/AgentMember.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
*/
2020

2121
use DCarbone\PHPConsulAPI\AbstractModel;
22+
use DCarbone\PHPConsulAPI\Consul;
2223
use DCarbone\PHPConsulAPI\HasStringTags;
2324

2425
/**
@@ -129,6 +130,32 @@ public function getDelegateCur(): int
129130
return $this->DelegateCur;
130131
}
131132

133+
/**
134+
* @return string
135+
*/
136+
public function ACLMode(): string
137+
{
138+
switch ($this->Tags[Consul::MemberTagKeyACLMode] ?? null) {
139+
case Consul::ACLModeDisabled:
140+
return Consul::ACLModeDisabled;
141+
case Consul::ACLModeEnabled:
142+
return Consul::ACLModeEnabled;
143+
case Consul::ACLModeLegacy:
144+
return Consul::ACLModeLegacy;
145+
default:
146+
return Consul::ACLModeUnknown;
147+
}
148+
}
149+
150+
/**
151+
* @return bool
152+
*/
153+
public function IsConsulServer(): bool
154+
{
155+
return isset($this->Tags[Consul::MemberTagKeyACLMode]) &&
156+
Consul::MemberTagValueRoleServer === $this->Tags[Consul::MemberTagKeyACLMode];
157+
}
158+
132159
/**
133160
* @return string
134161
*/

0 commit comments

Comments
 (0)