Skip to content

Commit b943b98

Browse files
committed
✨ Response stub
1 parent 323f412 commit b943b98

File tree

5 files changed

+115
-7
lines changed

5 files changed

+115
-7
lines changed

src/Console/Commands/ResourceCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class ResourceCommand extends GeneratorCommand implements PromptsForMissingInput
2020
*/
2121
protected $signature = 'rest:resource {name : The name of the resource}
2222
{--model= : The model to be associated with}
23-
{--path= : The location where the controller file should be created}';
23+
{--path= : The location where the resource file should be created}';
2424

2525
/**
2626
* The type of class being generated.
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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 ResponseCommand extends GeneratorCommand implements PromptsForMissingInput
13+
{
14+
use ResolvesStubPath;
15+
16+
/**
17+
* The console command signature.
18+
*
19+
* @var string
20+
*/
21+
protected $signature = 'rest:response {name : The name of the response}
22+
{--path= : The location where the response file should be created}';
23+
24+
/**
25+
* The type of class being generated.
26+
*
27+
* @var string
28+
*/
29+
protected $type = 'Response';
30+
31+
/**
32+
* The console command description.
33+
*
34+
* @var string
35+
*/
36+
protected $description = 'Create a new response 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+
$replace = [];
52+
53+
return str_replace(
54+
array_keys($replace), array_values($replace), parent::buildClass($name)
55+
);
56+
}
57+
58+
/**
59+
* Get the stub file for the generator.
60+
*
61+
* @return string
62+
*/
63+
protected function getStub()
64+
{
65+
return $this->resolveStubPath('/stubs/rest/response.stub');
66+
}
67+
68+
/**
69+
* Get the default namespace for the class.
70+
*
71+
* @param string $rootNamespace
72+
* @return string
73+
*/
74+
protected function getDefaultNamespace($rootNamespace)
75+
{
76+
return $rootNamespace.'\Rest\Responses';
77+
}
78+
}

src/Console/stubs/response.stub

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace {{ namespace }};
4+
5+
use Lomkit\Rest\Http\Response as RestResponse;
6+
7+
class {{ class }} extends RestResponse
8+
{
9+
/**
10+
* This map on each model returned by the API, use it at your ease.
11+
*
12+
* @var \Illuminate\Database\Eloquent\Model $model
13+
* @var array $responseModel
14+
*
15+
* @return array
16+
*/
17+
protected function map(\Illuminate\Database\Eloquent\Model $model, array $responseModel) : array {
18+
return $responseModel;
19+
}
20+
}

src/Http/Response.php

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -97,12 +97,20 @@ public function toResponse($request) {
9797
];
9898
}
9999

100-
return $this->map($this->responsable, [
101-
'data' => $this->modelToResponse($this->responsable, $this->resource, $request->input())
102-
]);
100+
return [
101+
'data' => $this->map($this->responsable, $this->modelToResponse($this->responsable, $this->resource, $request->input()))
102+
];
103103
}
104104

105-
protected function map(Model $model, array $response) {
106-
return $response;
105+
/**
106+
* This map on each model returned by the API, use it at your ease.
107+
*
108+
* @var \Illuminate\Database\Eloquent\Model $model
109+
* @var array $responseModel
110+
*
111+
* @return array
112+
*/
113+
protected function map(\Illuminate\Database\Eloquent\Model $model, array $responseModel) : array {
114+
return $responseModel;
107115
}
108116
}

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\BaseResourceCommand;
99
use Lomkit\Rest\Console\Commands\ControllerCommand;
1010
use Lomkit\Rest\Console\Commands\ResourceCommand;
11+
use Lomkit\Rest\Console\Commands\ResponseCommand;
1112
use Lomkit\Rest\Contracts\QueryBuilder;
1213
use Lomkit\Rest\Query\Builder;
1314

@@ -44,7 +45,8 @@ protected function registerCommands()
4445
BaseControllerCommand::class,
4546
ControllerCommand::class,
4647
BaseResourceCommand::class,
47-
ResourceCommand::class
48+
ResourceCommand::class,
49+
ResponseCommand::class
4850
]);
4951
}
5052
}

0 commit comments

Comments
 (0)