Skip to content

Commit 1307cfe

Browse files
authored
Merge pull request #14 from Lomkit/feature/actions
✨ actions
2 parents 46d7d2d + aeeecc1 commit 1307cfe

32 files changed

+1408
-16
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
name: Question / Enchancement proposal
2+
description: "Ask a question or for a feature"
3+
body:
4+
- type: textarea
5+
attributes:
6+
label: Description
7+
description: Leave a comment
8+
validations:
9+
required: true

README.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,12 +84,17 @@ TODO
8484

8585
## Contributing
8686

87-
8887
TODO
8988

9089
## Roadmap for the end of bêta (Estimated delivery October 2023)
9190

9291
- Custom directives (Filters / sorting)
93-
- Actions / Metrics
9492
- Automatic documentation with extension possible
93+
- Alias for includes / aggregates
94+
-
95+
96+
## Roadmap
97+
98+
- Metrics support
9599
- Refactor the response class
100+
- Plain text search using Laravel Scout

src/Actions/Action.php

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
<?php
2+
3+
namespace Lomkit\Rest\Actions;
4+
5+
use Illuminate\Bus\Batchable;
6+
use Illuminate\Bus\PendingBatch;
7+
use Illuminate\Bus\Queueable;
8+
use Illuminate\Foundation\Bus\Dispatchable;
9+
use Illuminate\Queue\InteractsWithQueue;
10+
use Illuminate\Queue\SerializesModels;
11+
use Illuminate\Support\Collection;
12+
use Illuminate\Support\Str;
13+
use Lomkit\Rest\Concerns\Makeable;
14+
use Lomkit\Rest\Concerns\Metable;
15+
use Lomkit\Rest\Http\Requests\OperateRequest;
16+
use Lomkit\Rest\Http\Requests\RestRequest;
17+
18+
class Action implements \JsonSerializable
19+
{
20+
use Makeable, Metable, Batchable, Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
21+
22+
/**
23+
* The name of the connection the job should be sent to.
24+
*
25+
* @var string|null
26+
*/
27+
public $connection;
28+
29+
/**
30+
* The name of the queue the job should be sent to.
31+
*
32+
* @var string|null
33+
*/
34+
public $queue;
35+
36+
/**
37+
* The displayable name of the action.
38+
*
39+
* @var string
40+
*/
41+
public $name;
42+
43+
/**
44+
* The number of models that should be included in each chunk.
45+
*
46+
* @var int
47+
*/
48+
public $chunkCount = 100;
49+
50+
/**
51+
* Get the name of the action.
52+
*
53+
* @return string
54+
*/
55+
public function name()
56+
{
57+
return $this->name ?: Str::of(class_basename(get_class($this)))->beforeLast('Action')->snake(' ')->title();
58+
}
59+
60+
/**
61+
* Get the URI key for the action.
62+
*
63+
* @return string
64+
*/
65+
public function uriKey()
66+
{
67+
return Str::slug($this->name(), '-', null);
68+
}
69+
70+
/**
71+
* Prepare the action for JSON serialization.
72+
*
73+
* @return array<string, mixed>
74+
*/
75+
public function jsonSerialize(): array
76+
{
77+
$request = app()->make(RestRequest::class);
78+
79+
return [
80+
'name' => $this->name(),
81+
'uriKey' => $this->uriKey(),
82+
'fields' => $this->fields($request),
83+
'meta' => $this->meta()
84+
];
85+
}
86+
87+
/**
88+
* Perform the action on the given models.
89+
*
90+
* @param array $fields
91+
* @param \Illuminate\Support\Collection $models
92+
* @return mixed
93+
*/
94+
public function handle(array $fields, \Illuminate\Support\Collection $models)
95+
{
96+
//
97+
}
98+
99+
/**
100+
* Called in an action failed.
101+
*
102+
* @param \Lomkit\Rest\Http\Requests\RestRequest $request
103+
* @return array
104+
*/
105+
public function fields(\Lomkit\Rest\Http\Requests\RestRequest $request)
106+
{
107+
return [];
108+
}
109+
110+
/**
111+
* Register callbacks on the pending batch.
112+
*
113+
* @param array $fields
114+
* @param \Illuminate\Bus\PendingBatch $batch
115+
* @return void
116+
*/
117+
public function withBatch(array $fields, PendingBatch $batch)
118+
{
119+
//
120+
}
121+
122+
/**
123+
* Execute the action for the given request.
124+
*
125+
* @param OperateRequest $request
126+
* @return int
127+
*
128+
* @throws \Throwable
129+
*/
130+
public function handleRequest(OperateRequest $request)
131+
{
132+
$fields = $request->resolveFields($this);
133+
134+
$dispatcher = new DispatchAction($request, $this, $fields);
135+
136+
$count = $dispatcher->dispatch($this->chunkCount);
137+
138+
return $count;
139+
}
140+
}

src/Actions/Actionable.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace Lomkit\Rest\Actions;
4+
5+
use Lomkit\Rest\Http\Requests\RestRequest;
6+
7+
trait Actionable
8+
{
9+
/**
10+
* The actions that should be linked
11+
* @param RestRequest $request
12+
* @return array
13+
*/
14+
public function actions(RestRequest $request): array {
15+
return [];
16+
}
17+
18+
public function actionExists(RestRequest $request, string $actionKey): bool {
19+
return collect($this->actions($request))
20+
->contains(function (Action $action) use ($actionKey) {
21+
return $action->uriKey() === $actionKey;
22+
});
23+
}
24+
25+
public function action(RestRequest $request, string $actionKey): Action {
26+
return collect($this->actions($request))
27+
->sole(function (Action $action) use ($actionKey) {
28+
return $action->uriKey() === $actionKey;
29+
});
30+
}
31+
}

src/Actions/CallRestApiAction.php

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
3+
namespace Lomkit\Rest\Actions;
4+
5+
use Illuminate\Bus\Batchable;
6+
use Illuminate\Bus\Queueable;
7+
use Illuminate\Foundation\Bus\Dispatchable;
8+
use Illuminate\Queue\InteractsWithQueue;
9+
use Illuminate\Queue\SerializesModels;
10+
use Illuminate\Support\Collection;
11+
12+
class CallRestApiAction
13+
{
14+
use Batchable, Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
15+
16+
/**
17+
* Models collection.
18+
*
19+
* @var \Illuminate\Database\Eloquent\Collection|\Illuminate\Support\Collection
20+
*/
21+
protected $models;
22+
23+
/**
24+
* The action instance.
25+
*
26+
* @var \Lomkit\Rest\Actions\Action
27+
*/
28+
protected \Lomkit\Rest\Actions\Action $action;
29+
30+
/**
31+
* The fields for the action instance.
32+
*
33+
* @var array
34+
*/
35+
protected array $fields;
36+
37+
/**
38+
* Create a new job instance.
39+
*
40+
* @param \Lomkit\Rest\Actions\Action $action
41+
* @param array $fields
42+
* @param \Illuminate\Database\Eloquent\Collection|\Illuminate\Support\Collection $models
43+
* @return void
44+
*/
45+
public function __construct(Action $action, array $fields, Collection $models)
46+
{
47+
$this->action = $action;
48+
$this->fields = $fields;
49+
$this->models = $models;
50+
}
51+
52+
/**
53+
* Execute the job.
54+
*
55+
* @return void
56+
*/
57+
public function handle()
58+
{
59+
$this->action->setJob($this->job)->handle($this->fields, $this->models);
60+
}
61+
62+
/**
63+
* Get the display name for the queued job.
64+
*
65+
* @return string
66+
*/
67+
public function displayName()
68+
{
69+
return get_class($this->action);
70+
}
71+
}

0 commit comments

Comments
 (0)