2022-12-20

This commit is contained in:
2022-12-20 14:13:05 -03:00
parent 85fef16b27
commit 0f3febc00d
87 changed files with 2525 additions and 419 deletions

View File

@ -0,0 +1,60 @@
<?php
namespace Common\Service;
use function Safe\{json_encode, json_decode, file_get_contents, file_put_contents};
use ProVM\Concept\API\Route\Route;
use ProVM\Implement\API\Route\Route as RouteObj;
class Documenter
{
public function __construct(string $filename)
{
$this->setFilename($filename);
}
protected string $filename;
public function setFilename(string $filename): Documenter
{
$this->filename = $filename;
return $this;
}
public function getFilename(): string
{
return $this->filename;
}
protected array $routes;
public function setRoutes(array $routes): Documenter
{
foreach ($routes as $path => $route) {
$this->addRoute($path, $route);
}
return $this;
}
public function addRoute(string $path, Route $route): Documenter
{
$this->routes[$path] = $route;
return $this;
}
public function getRoutes(): array
{
return $this->routes;
}
protected function load(): mixed
{
$json = json_decode(trim(file_get_contents($this->getFilename())), JSON_OBJECT_AS_ARRAY);
foreach ($json['paths'] as $path => $data) {
$route = RouteObj::add($data);
$this->addRoute($path, $route);
}
return $json;
}
public function save(): void
{
$json = $this->load();
$json['paths'] = $this->getRoutes();
ksort($json['paths']);
file_put_contents($this->getFilename(), json_encode($json, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES));
}
}