Skip to content

Commit c2c43a2

Browse files
committed
Added macros
1 parent 1c4131c commit c2c43a2

File tree

4 files changed

+78
-0
lines changed

4 files changed

+78
-0
lines changed

docs/RESOURCES.md

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
- [Methods & Usage](Usage.md)
66
- [Cluster & Authentication](Cluster.md)
7+
- [General Resources](kinds/Resource.md)
78

89
## Supported Instances
910

docs/kinds/Resource.md

+28
Original file line numberDiff line numberDiff line change
@@ -182,3 +182,31 @@ $rule->addToAttribute('rules', 'some-rule')
182182

183183
// rules: ['some-rule', 'another-rule']
184184
```
185+
186+
# Macros
187+
188+
Beside the custom callers to call custom attributes, you can also define your own custom functions using [Macros](https://tighten.co/blog/the-magic-of-laravel-macros/). In case you are not familiar with Macros, you can see the following example on defining a custom function for the `K8sPod` instance.
189+
190+
```php
191+
use RenokiCo\PhpK8s\Kinds\K8sPod;
192+
193+
// changeDnsPolicy() does not exist in the code
194+
195+
K8sPod::macro('changeDnsPolicy', function ($policy = 'None') {
196+
return $this->setSpec('dnsPolicy', $policy);
197+
});
198+
199+
K8s::pod()->changeDnsPolicy('ClusterFirst');
200+
```
201+
202+
**`$this` keyword used within the closure is going to reference the current K8sPod object in this example. You might as well define how many macros you want. The closure can also contain parameters or no parameters at all, based on your needs.**
203+
204+
Macros also work with custom callers:
205+
206+
```php
207+
use RenokiCo\PhpK8s\Kinds\K8sPod;
208+
209+
K8sPod::macro('changeMetadata', function (array $metadata) {
210+
return $this->setMetadata($metadata);
211+
});
212+
```

src/Traits/HasAttributes.php

+9
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,14 @@
44

55
use Illuminate\Support\Arr;
66
use Illuminate\Support\Str;
7+
use Illuminate\Support\Traits\Macroable;
78

89
trait HasAttributes
910
{
11+
use Macroable {
12+
__call as macroCall;
13+
}
14+
1015
/**
1116
* The Kubernetes resource's attributes.
1217
*
@@ -80,6 +85,10 @@ public function getAttribute(string $name, $default = null)
8085
*/
8186
public function __call(string $method, array $parameters)
8287
{
88+
if (static::hasMacro($method)) {
89+
return $this->macroCall($method, $parameters);
90+
}
91+
8392
// Intercept methods like ->setXXXX(...)
8493
if (Str::startsWith($method, 'set')) {
8594
$attribute = Str::camel(

tests/MacroTest.php

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
namespace RenokiCo\PhpK8s\Test;
4+
5+
use RenokiCo\PhpK8s\Instances\Container;
6+
use RenokiCo\PhpK8s\K8s;
7+
use RenokiCo\PhpK8s\Kinds\K8sPod;
8+
9+
class MacroTest extends TestCase
10+
{
11+
public function test_instances_macro()
12+
{
13+
Container::macro('macroTest', function ($var1, $var2) {
14+
return $this->setMacroField([$var1, $var2]);
15+
});
16+
17+
Container::macro('getMacroTest', function () {
18+
return $this->getMacroField([]);
19+
});
20+
21+
$container = K8s::container()->macroTest('val1', 'val2');
22+
23+
$this->assertEquals(['val1', 'val2'], $container->getMacroTest());
24+
}
25+
26+
public function test_resource_macro()
27+
{
28+
K8sPod::macro('macroTest', function ($var1, $var2) {
29+
return $this->setMacroField([$var1, $var2]);
30+
});
31+
32+
K8sPod::macro('getMacroTest', function () {
33+
return $this->getMacroField([]);
34+
});
35+
36+
$pod = K8s::pod()->macroTest('val1', 'val2');
37+
38+
$this->assertEquals(['val1', 'val2'], $pod->getMacroTest());
39+
}
40+
}

0 commit comments

Comments
 (0)