Skip to content

Commit

Permalink
✨ Response stub
Browse files Browse the repository at this point in the history
  • Loading branch information
GautierDele committed Jul 29, 2023
1 parent 323f412 commit b943b98
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/Console/Commands/ResourceCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class ResourceCommand extends GeneratorCommand implements PromptsForMissingInput
*/
protected $signature = 'rest:resource {name : The name of the resource}
{--model= : The model to be associated with}
{--path= : The location where the controller file should be created}';
{--path= : The location where the resource file should be created}';

/**
* The type of class being generated.
Expand Down
78 changes: 78 additions & 0 deletions src/Console/Commands/ResponseCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

namespace Lomkit\Rest\Console\Commands;

use Illuminate\Console\GeneratorCommand;
use Illuminate\Contracts\Console\PromptsForMissingInput;
use Illuminate\Database\Migrations\MigrationCreator;
use Illuminate\Support\Composer;
use Illuminate\Support\Str;
use Lomkit\Rest\Console\ResolvesStubPath;

class ResponseCommand extends GeneratorCommand implements PromptsForMissingInput
{
use ResolvesStubPath;

/**
* The console command signature.
*
* @var string
*/
protected $signature = 'rest:response {name : The name of the response}
{--path= : The location where the response file should be created}';

/**
* The type of class being generated.
*
* @var string
*/
protected $type = 'Response';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Create a new response class';

public function handle()
{
parent::handle();
}

/**
* Build the class with the given name.
*
* @param string $name
* @return string
*/
protected function buildClass($name)
{
$replace = [];

return str_replace(
array_keys($replace), array_values($replace), parent::buildClass($name)
);
}

/**
* Get the stub file for the generator.
*
* @return string
*/
protected function getStub()
{
return $this->resolveStubPath('/stubs/rest/response.stub');
}

/**
* Get the default namespace for the class.
*
* @param string $rootNamespace
* @return string
*/
protected function getDefaultNamespace($rootNamespace)
{
return $rootNamespace.'\Rest\Responses';
}
}
20 changes: 20 additions & 0 deletions src/Console/stubs/response.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace {{ namespace }};

use Lomkit\Rest\Http\Response as RestResponse;

class {{ class }} extends RestResponse
{
/**
* This map on each model returned by the API, use it at your ease.
*
* @var \Illuminate\Database\Eloquent\Model $model
* @var array $responseModel
*
* @return array
*/
protected function map(\Illuminate\Database\Eloquent\Model $model, array $responseModel) : array {
return $responseModel;
}
}
18 changes: 13 additions & 5 deletions src/Http/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,20 @@ public function toResponse($request) {
];
}

return $this->map($this->responsable, [
'data' => $this->modelToResponse($this->responsable, $this->resource, $request->input())
]);
return [
'data' => $this->map($this->responsable, $this->modelToResponse($this->responsable, $this->resource, $request->input()))
];
}

protected function map(Model $model, array $response) {
return $response;
/**
* This map on each model returned by the API, use it at your ease.
*
* @var \Illuminate\Database\Eloquent\Model $model
* @var array $responseModel
*
* @return array
*/
protected function map(\Illuminate\Database\Eloquent\Model $model, array $responseModel) : array {
return $responseModel;
}
}
4 changes: 3 additions & 1 deletion src/RestServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Lomkit\Rest\Console\Commands\BaseResourceCommand;
use Lomkit\Rest\Console\Commands\ControllerCommand;
use Lomkit\Rest\Console\Commands\ResourceCommand;
use Lomkit\Rest\Console\Commands\ResponseCommand;
use Lomkit\Rest\Contracts\QueryBuilder;
use Lomkit\Rest\Query\Builder;

Expand Down Expand Up @@ -44,7 +45,8 @@ protected function registerCommands()
BaseControllerCommand::class,
ControllerCommand::class,
BaseResourceCommand::class,
ResourceCommand::class
ResourceCommand::class,
ResponseCommand::class
]);
}
}
Expand Down

0 comments on commit b943b98

Please sign in to comment.