2022-12-20
This commit is contained in:
179
api/common/Service/Router.php
Normal file
179
api/common/Service/Router.php
Normal file
@ -0,0 +1,179 @@
|
||||
<?php
|
||||
namespace Common\Service;
|
||||
|
||||
use Slim\App;
|
||||
use ProVM\Concept\API\Controller;
|
||||
use ProVM\Implement\API\Route\Route;
|
||||
|
||||
class Router
|
||||
{
|
||||
public function __construct(App &$app, Documenter $documenter)
|
||||
{
|
||||
$this->setApp($app);
|
||||
$this->setDocumenter($documenter);
|
||||
}
|
||||
|
||||
protected App $app;
|
||||
public function setApp(App &$app): Router
|
||||
{
|
||||
$this->app = $app;
|
||||
return $this;
|
||||
}
|
||||
public function getApp(): App
|
||||
{
|
||||
return $this->app;
|
||||
}
|
||||
protected Documenter $documenter;
|
||||
public function setDocumenter(Documenter $documenter): Router
|
||||
{
|
||||
$this->documenter = $documenter;
|
||||
return $this;
|
||||
}
|
||||
public function getDocumenter(): Documenter
|
||||
{
|
||||
return $this->documenter;
|
||||
}
|
||||
protected Controller $controller;
|
||||
public function setController(Controller $controller): Router
|
||||
{
|
||||
$this->controller = $controller;
|
||||
return $this;
|
||||
}
|
||||
public function getController(): Controller
|
||||
{
|
||||
return $this->controller;
|
||||
}
|
||||
|
||||
protected function document(): void
|
||||
{
|
||||
$docs = [
|
||||
"/{$this->getController()->getPlural()}" => [
|
||||
'get' => [
|
||||
'description' => "Entrega una lista de {$this->getController()->getPlural()}",
|
||||
'responses' => [
|
||||
'200' => [
|
||||
'description' => "Lista de {$this->getController()->getPlural()}"
|
||||
]
|
||||
]
|
||||
]
|
||||
],
|
||||
"/{$this->getController()->getPlural()}/add" => [
|
||||
'post' => [
|
||||
'description' => "Agregar {$this->getController()->getPlural()} con una lista",
|
||||
'parameters' => [
|
||||
[
|
||||
'name' => $this->getController()->getPlural(),
|
||||
'in' => 'query',
|
||||
'description' => 'Lista de datos para agregar',
|
||||
'required' => true
|
||||
]
|
||||
],
|
||||
'responses' => [
|
||||
'200' => [
|
||||
'description' => "Entrega un listado de {$this->getController()->getPlural()} y si fueron agregadas"
|
||||
]
|
||||
]
|
||||
]
|
||||
],
|
||||
"/{$this->getController()->getPlural()}/edit" => [
|
||||
'put' => [
|
||||
'description' => "Editar multiples {$this->getController()->getPlural()} con una lista",
|
||||
'parameters' => [
|
||||
[
|
||||
'name' => $this->getController()->getPlural(),
|
||||
'in' => 'query',
|
||||
'description' => 'Lista de datos para editar',
|
||||
'required' => true
|
||||
]
|
||||
],
|
||||
'responses' => [
|
||||
'200' => [
|
||||
'description' => "Entrega un listado de {$this->getController()->getPlural()} identificando si fueron editados"
|
||||
]
|
||||
]
|
||||
]
|
||||
],
|
||||
"/{$this->getController()->getSingular()}/{model_id}" => [
|
||||
'get' => [
|
||||
'description' => "Entrega {$this->getController()->getSingular()}",
|
||||
'parameters' => [
|
||||
[
|
||||
'name' => 'id',
|
||||
'in' => 'path',
|
||||
'description' => "{$this->getController()->getSingular()} id",
|
||||
'required' => true
|
||||
]
|
||||
],
|
||||
'responses' => [
|
||||
'200' => [
|
||||
'description' => "{$this->getController()->getSingular()} found or null"
|
||||
]
|
||||
]
|
||||
]
|
||||
],
|
||||
"/{$this->getController()->getSingular()}/{model_id}/edit" => [
|
||||
'put' => [
|
||||
'description' => '',
|
||||
'parameters' => [
|
||||
[
|
||||
'name' => 'model_id',
|
||||
'in' => 'path',
|
||||
'description' => "{$this->getController()->getSingular()} id",
|
||||
'required' => true
|
||||
],
|
||||
[
|
||||
'name' => "{$this->getController()->getSingular()}",
|
||||
'in' => 'query',
|
||||
'description' => '',
|
||||
'required' => true
|
||||
]
|
||||
],
|
||||
'responses' => [
|
||||
'200' => [
|
||||
'description' => ''
|
||||
]
|
||||
]
|
||||
]
|
||||
],
|
||||
"/{$this->getController()->getSingular()}/{model_id}/remove" => [
|
||||
'delete' => [
|
||||
'description' => '',
|
||||
'parameters' => [
|
||||
[
|
||||
'name' => 'model_id',
|
||||
'in' => 'path',
|
||||
'description' => "{$this->getController()->getSingular()} id",
|
||||
'required' => true
|
||||
]
|
||||
],
|
||||
'responses' => [
|
||||
'200' => [
|
||||
'description' => ''
|
||||
]
|
||||
]
|
||||
]
|
||||
]
|
||||
];
|
||||
|
||||
foreach ($docs as $path => $r) {
|
||||
$route = Route::add($r);
|
||||
$this->getDocumenter()->addRoute($path, $route);
|
||||
}
|
||||
$this->getDocumenter()->save();
|
||||
}
|
||||
public function build(): void
|
||||
{
|
||||
$this->getApp()->group('/' . $this->getController()->getPlural(), function($app) {
|
||||
$app->post('/add[/]', [get_class($this->getController()), 'add']);
|
||||
$app->put('/edit[/]', [get_class($this->getController()), 'edit']);
|
||||
$app->get('[/]', get_class($this->getController()));
|
||||
});
|
||||
$this->getApp()->group('/' . $this->getController()->getSingular() . '/{model_id}', function($app) {
|
||||
$app->put('/edit[/]', [get_class($this->getController()), 'editOne']);
|
||||
$app->delete('/remove[/]', [get_class($this->getController()), 'remove']);
|
||||
$app->get('[/]', [get_class($this->getController()), 'get']);
|
||||
});
|
||||
|
||||
$this->document();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user