Skip to content

Commit 5c84134

Browse files
committed
✨ instructions stub
1 parent b62225b commit 5c84134

File tree

6 files changed

+123
-3
lines changed

6 files changed

+123
-3
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
3+
namespace Lomkit\Rest\Console\Commands;
4+
5+
use Illuminate\Console\GeneratorCommand;
6+
use Illuminate\Contracts\Console\PromptsForMissingInput;
7+
use Illuminate\Database\Migrations\MigrationCreator;
8+
use Illuminate\Support\Composer;
9+
use Illuminate\Support\Str;
10+
use Lomkit\Rest\Console\ResolvesStubPath;
11+
12+
class InstructionCommand extends GeneratorCommand implements PromptsForMissingInput
13+
{
14+
use ResolvesStubPath;
15+
16+
/**
17+
* The console command signature.
18+
*
19+
* @var string
20+
*/
21+
protected $signature = 'rest:instruction {name : The name of instruction action}
22+
{--path= : The location where the instruction file should be created}';
23+
24+
/**
25+
* The type of class being generated.
26+
*
27+
* @var string
28+
*/
29+
protected $type = 'Instruction';
30+
31+
/**
32+
* The console command description.
33+
*
34+
* @var string
35+
*/
36+
protected $description = 'Create a new instruction class';
37+
38+
public function handle()
39+
{
40+
parent::handle();
41+
}
42+
43+
/**
44+
* Build the class with the given name.
45+
*
46+
* @param string $name
47+
* @return string
48+
*/
49+
protected function buildClass($name)
50+
{
51+
return parent::buildClass($name);
52+
}
53+
54+
/**
55+
* Get the stub file for the generator.
56+
*
57+
* @return string
58+
*/
59+
protected function getStub()
60+
{
61+
return $this->resolveStubPath('/stubs/rest/instruction.stub');
62+
}
63+
64+
/**
65+
* Get the default namespace for the class.
66+
*
67+
* @param string $rootNamespace
68+
* @return string
69+
*/
70+
protected function getDefaultNamespace($rootNamespace)
71+
{
72+
return $rootNamespace.'\Rest\Instructions';
73+
}
74+
}

src/Console/stubs/action.stub

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class {{ class }} extends RestAction
2121
}
2222

2323
/**
24-
* Called in an action failed.
24+
* The action fields.
2525
*
2626
* @param \Lomkit\Rest\Http\Requests\RestRequest $request
2727
* @return array

src/Console/stubs/instruction.stub

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace {{ namespace }};
4+
5+
use Lomkit\Rest\Instructions\Instruction as RestInstruction;
6+
7+
class {{ class }} extends RestInstruction
8+
{
9+
/**
10+
* Perform the action on the given models.
11+
*
12+
* @param array $fields
13+
* @param \Illuminate\Database\Eloquent\Builder $query
14+
* @return void
15+
*/
16+
public function handle(array $fields, \Illuminate\Database\Eloquent\Builder $query)
17+
{
18+
// ...
19+
}
20+
21+
/**
22+
* The instruction fields.
23+
*
24+
* @param \Lomkit\Rest\Http\Requests\RestRequest $request
25+
* @return array
26+
*/
27+
public function fields(\Lomkit\Rest\Http\Requests\RestRequest $request)
28+
{
29+
return [
30+
'id' => [
31+
'required'
32+
]
33+
];
34+
}
35+
}

src/Console/stubs/resource.stub

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,13 @@ class {{ class }} extends RestResource
6767
public function actions(RestRequest $request): array {
6868
return [];
6969
}
70+
71+
/**
72+
* The instructions that should be linked
73+
* @param RestRequest $request
74+
* @return array
75+
*/
76+
public function instructions(RestRequest $request): array {
77+
return [];
78+
}
7079
}

src/RestServiceProvider.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Lomkit\Rest\Console\Commands\BaseControllerCommand;
99
use Lomkit\Rest\Console\Commands\BaseResourceCommand;
1010
use Lomkit\Rest\Console\Commands\ControllerCommand;
11+
use Lomkit\Rest\Console\Commands\InstructionCommand;
1112
use Lomkit\Rest\Console\Commands\QuickStartCommand;
1213
use Lomkit\Rest\Console\Commands\ResourceCommand;
1314
use Lomkit\Rest\Console\Commands\ResponseCommand;
@@ -55,7 +56,8 @@ protected function registerCommands()
5556
ResourceCommand::class,
5657
ResponseCommand::class,
5758
QuickStartCommand::class,
58-
ActionCommand::class
59+
ActionCommand::class,
60+
InstructionCommand::class
5961
]);
6062
}
6163
}

tests/Support/Rest/Instructions/NumberedInstruction.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class NumberedInstruction extends Instruction
88
{
99
public function handle(array $fields, \Illuminate\Database\Eloquent\Builder $query)
1010
{
11-
return $query->where('number', '>', $fields['number'] ?? 0);
11+
$query->where('number', '>', $fields['number'] ?? 0);
1212
}
1313

1414
public function fields(\Lomkit\Rest\Http\Requests\RestRequest $request): array

0 commit comments

Comments
 (0)