Skip to content

Commit 6d44ee2

Browse files
committed
Macroable, pint and github action updates
1 parent aca5d54 commit 6d44ee2

File tree

18 files changed

+228
-127
lines changed

18 files changed

+228
-127
lines changed

.github/workflows/ci.yml

Lines changed: 0 additions & 24 deletions
This file was deleted.

.github/workflows/code-style.yml

Lines changed: 0 additions & 20 deletions
This file was deleted.

.github/workflows/run-tests.yml

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
on: push
2+
name: CI
3+
4+
jobs:
5+
phpunit:
6+
runs-on: ubuntu-24.04
7+
timeout-minutes: 5
8+
9+
strategy:
10+
fail-fast: true
11+
matrix:
12+
php: [ 8.1, 8.2, 8.3, 8.4 ]
13+
laravel: [ 10.*, 11.*, 12.* ]
14+
include:
15+
- php: 8.1
16+
laravel: 10.*
17+
pest: 2.*
18+
testbench: 8.*
19+
- php: 8.2
20+
laravel: 10.*
21+
pest: 2.*
22+
testbench: 8.*
23+
- php: 8.3
24+
laravel: 10.*
25+
pest: 2.*
26+
testbench: 8.*
27+
- php: 8.4
28+
laravel: 10.*
29+
pest: 2.*
30+
testbench: 8.*
31+
- php: 8.2
32+
laravel: 11.*
33+
pest: 3.*
34+
testbench: 9.*
35+
- php: 8.3
36+
laravel: 11.*
37+
pest: 3.*
38+
testbench: 9.*
39+
- php: 8.4
40+
laravel: 11.*
41+
pest: 3.*
42+
testbench: 9.*
43+
- php: 8.2
44+
laravel: 12.*
45+
pest: 3.*
46+
testbench: 10.*
47+
- php: 8.3
48+
laravel: 12.*
49+
pest: 3.*
50+
testbench: 10.*
51+
- php: 8.4
52+
laravel: 12.*
53+
pest: 3.*
54+
testbench: 10.*
55+
exclude:
56+
- php: 8.1
57+
laravel: 11.*
58+
- php: 8.1
59+
laravel: 12.*
60+
61+
name: Laravel Actions Tests - PHP ${{ matrix.php }} - Laravel ${{ matrix.laravel }}
62+
63+
steps:
64+
- name: Checkout code
65+
uses: actions/checkout@v4
66+
67+
- name: Cache dependencies
68+
uses: actions/cache@v4
69+
with:
70+
path: ~/.composer/cache/files
71+
key: dependencies-${{ matrix.laravel }}-php-${{ matrix.php }}-composer-${{ hashFiles('composer.json') }}
72+
73+
- name: Setup PHP
74+
uses: shivammathur/setup-php@v2
75+
with:
76+
php-version: ${{ matrix.php }}
77+
extensions: curl, mbstring, zip, pcntl, iconv
78+
coverage: none
79+
tools: composer:v2
80+
81+
- name: Install dependencies
82+
run: |
83+
composer require "laravel/framework:${{ matrix.laravel }}" --no-interaction --no-update
84+
composer require "orchestra/testbench:${{ matrix.testbench }}" --no-interaction --no-update
85+
composer require "pestphp/pest:${{ matrix.pest }}" "pestphp/pest-plugin-laravel:${{ matrix.pest }}" "pestphp/pest-plugin-type-coverage:${{ matrix.pest }}" --no-interaction --no-update
86+
composer update --prefer-dist --no-interaction
87+
composer dump
88+
89+
- name: Execute tests
90+
run: vendor/bin/phpunit tests/
91+
92+
pint:
93+
runs-on: ubuntu-24.04
94+
timeout-minutes: 5
95+
96+
name: Pint Style Check
97+
steps:
98+
- name: Checkout code
99+
uses: actions/checkout@v4
100+
101+
- name: Cache dependencies
102+
uses: actions/cache@v4
103+
with:
104+
path: ~/.composer/cache/files
105+
key: dependencies-composer-${{ hashFiles('composer.json') }}
106+
107+
- name: Setup PHP
108+
uses: shivammathur/setup-php@v2
109+
with:
110+
php-version: 8.3
111+
tools: composer:v2
112+
113+
- name: Install dependencies
114+
run: |
115+
composer install --no-interaction
116+
composer dump
117+
118+
- name: Execute Pint
119+
run: composer pint-check

README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,29 @@ public function index (Action $action)
141141
}
142142
```
143143

144+
## Macros
145+
146+
There are times when you may want to add something extra to your actions. We can leverage macros for this!
147+
148+
Here is an example were we are leveraging Inertia's defer functionality directly on our action. The macro then just calls `act()` on the action class when the deferred prop is requested!
149+
150+
```php
151+
// In your service provider
152+
Action::macro('defer', function($action, ...$arguments) {
153+
return Inertia::defer(fn () => $action::act(...$arguments));
154+
});
155+
156+
// In your controller
157+
return Inertia::render('Users/Index')
158+
->with('users', GetUsers::defer());
159+
```
160+
161+
Note how the originating class is passed into the macro function as the first parameter. This is very important, otherwise the macro will be unaware of which action you are actually running as macros are technically run on the parent action class. You are also free to do what you want regarding the subsequent $arguments, but it is considered best practice to pack/unpack the arguments with the spread operator to ensure the actions are as flexible as possible.
162+
163+
Also take note the `defer()` is defined on the `Kirschbaum\Actions\Action` class, not on the `GetUsers` action class. Individual actions are not macroable in and of themselves. The macro `defer()` will also be available to every action in your application, not just the `GetUsers` action!
164+
165+
Before and after events will not be fired when using macros. They will get fired however if you use an action's `act()`, `actWhen()`, or `actUnless()` methods with the macro function.
166+
144167
## Handling Failures
145168

146169
We all know Chuck Norris isn't going to fail us, but he isn't the only one using this... Handling failures is pretty easy with Actions. Out of the box, any exceptions thrown by your Action classes get handled by Laravel's exception handler. If you'd rather implement your own logic during a failure, add a `failed()` method to your Action. It's that easy! You can return data from your `failed()` method if you choose as well.

pint.json

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,26 @@
2525
"yield"
2626
]
2727
},
28+
"class_definition": {
29+
"multi_line_extends_each_single_line": true,
30+
"single_item_single_line": true,
31+
"single_line": false
32+
},
2833
"concat_space": {
2934
"spacing": "one"
3035
},
3136
"explicit_string_variable": true,
3237
"method_chaining_indentation": true,
3338
"new_with_braces": true,
39+
"nullable_type_declaration_for_default_null_value": true,
3440
"ordered_traits": true,
35-
"phpdoc_separation": true
41+
"php_unit_method_casing": {
42+
"case": "camel_case"
43+
},
44+
"php_unit_test_annotation": {
45+
"style": "prefix"
46+
},
47+
"phpdoc_separation": true,
48+
"single_line_empty_body": true
3649
}
3750
}

src/Action.php

Lines changed: 9 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,23 @@
22

33
namespace Kirschbaum\Actions;
44

5+
use Illuminate\Support\Traits\Macroable;
56
use Kirschbaum\Actions\Contracts\Actionable;
67
use Kirschbaum\Actions\Exceptions\ActionableInterfaceNotFoundException;
78
use Throwable;
89

910
class Action
1011
{
12+
use Macroable;
13+
1114
/**
1215
* Arguments to pass into the action's constructor.
13-
*
14-
* @var array
1516
*/
16-
protected $arguments;
17+
protected array $arguments;
1718

1819
/**
1920
* Initiate the given action.
2021
*
21-
* @param string $action
2222
* @param mixed ...$arguments
2323
*
2424
* @return mixed
@@ -35,8 +35,6 @@ public function act(string $action, ...$arguments)
3535
/**
3636
* Initiate the given action if the given condition is true.
3737
*
38-
* @param $condition
39-
* @param string $action
4038
* @param mixed ...$arguments
4139
*
4240
* @return mixed|void
@@ -55,8 +53,6 @@ public function actWhen($condition, string $action, ...$arguments)
5553
/**
5654
* Initiate the action if the given condition is false.
5755
*
58-
* @param $condition
59-
* @param string $action
6056
* @param mixed ...$arguments
6157
*
6258
* @return mixed|void
@@ -75,7 +71,6 @@ public function actUnless($condition, string $action, ...$arguments)
7571
/**
7672
* Handle the given action.
7773
*
78-
* @param string $action
7974
*
8075
* @return mixed|void
8176
*
@@ -101,16 +96,17 @@ protected function handle(string $action)
10196

10297
/**
10398
* Determine if the action has a `failed()` method defined.
104-
*
105-
* @param Actionable $action
106-
*
107-
* @return bool
10899
*/
109100
protected function actionHasFailedMethod(Actionable $action): bool
110101
{
111102
return method_exists($action, 'failed');
112103
}
113104

105+
/**
106+
* Determine if the action has the proper interface.
107+
*
108+
* @throws Throwable
109+
*/
114110
protected function checkActionForInterface($action): void
115111
{
116112
throw_unless(
@@ -121,11 +117,6 @@ protected function checkActionForInterface($action): void
121117

122118
/**
123119
* Dispatch appropriate action event.
124-
*
125-
* @param string $event
126-
* @param Actionable $action
127-
*
128-
* @return void
129120
*/
130121
protected function dispatchEvent(string $event, Actionable $action): void
131122
{
@@ -139,11 +130,6 @@ protected function dispatchEvent(string $event, Actionable $action): void
139130

140131
/**
141132
* Check if the given event exists in the action.
142-
*
143-
* @param Actionable $action
144-
* @param string $event
145-
*
146-
* @return bool
147133
*/
148134
protected function eventExists(Actionable $action, string $event): bool
149135
{
@@ -154,8 +140,6 @@ protected function eventExists(Actionable $action, string $event): bool
154140
/**
155141
* Fire failure event and/or call failed action method if they exist.
156142
*
157-
* @param Actionable $action
158-
* @param Throwable $exception
159143
*
160144
* @return mixed
161145
*
@@ -178,10 +162,6 @@ protected function handleFailure(Actionable $action, Throwable $exception)
178162

179163
/**
180164
* Check if action has a custom exception.
181-
*
182-
* @param Actionable $action
183-
*
184-
* @return bool
185165
*/
186166
protected function hasCustomException(Actionable $action): bool
187167
{
@@ -191,10 +171,6 @@ protected function hasCustomException(Actionable $action): bool
191171

192172
/**
193173
* Raise the before action event.
194-
*
195-
* @param Actionable $action
196-
*
197-
* @return void
198174
*/
199175
protected function raiseBeforeActionEvent(Actionable $action): void
200176
{
@@ -203,10 +179,6 @@ protected function raiseBeforeActionEvent(Actionable $action): void
203179

204180
/**
205181
* Raise the after action event.
206-
*
207-
* @param Actionable $action
208-
*
209-
* @return void
210182
*/
211183
protected function raiseAfterActionEvent(Actionable $action): void
212184
{

0 commit comments

Comments
 (0)