166 lines
6.4 KiB
PHP
166 lines
6.4 KiB
PHP
<?php
|
|
|
|
namespace Lean\Api;
|
|
|
|
use DI\Attribute\Inject;
|
|
use League\CommonMark\CommonMarkConverter;
|
|
use Lean\Api\Lib\ApiSearchRequestInterface;
|
|
use Notes\Attribute\Ignore;
|
|
use Psr\Container\ContainerInterface;
|
|
use Psr\Http\Message\ResponseInterface;
|
|
use Psr\Http\Message\ServerRequestInterface;
|
|
use Ulmus\Entity\EntityInterface;
|
|
use Ulmus\EntityCollection;
|
|
use Ulmus\SearchRequest\SearchRequestInterface;
|
|
|
|
trait LeanApiTrait
|
|
{
|
|
use DescriptorTrait;
|
|
|
|
#[Inject]
|
|
protected ContainerInterface $container;
|
|
|
|
#[Ignore]
|
|
public function renderMarkdown(string $filepath, array $entities = [], array $forms = [], int $code = 200, array $headers = []) : ResponseInterface
|
|
{
|
|
if ( ! class_exists(CommonMarkConverter::class)) {
|
|
throw new \BadFunctionCallException("League\CommonMark seems to be missing, please install dependency before trying to render Markdown content");
|
|
}
|
|
|
|
$markdown = ( new CommonMarkConverter() )->convert(file_get_contents($filepath))->getContent();
|
|
|
|
if (str_contains($markdown, '{route:descriptor}'))
|
|
{
|
|
$markdown = str_replace('{route:descriptor}', $this->renderRawView('lean-api/route_descriptor', [
|
|
'routes' => (new RouteDescriptor($this, $this->picea->compiler->getExtensionFromToken('url'), $this->container))->getRoutes()
|
|
]), $markdown);
|
|
}
|
|
|
|
if (str_contains($markdown, '{request:debugger}'))
|
|
{
|
|
$markdown = str_replace('{request:debugger}', $this->renderRawView('lean-api/request_debugger', []), $markdown);
|
|
}
|
|
|
|
if (str_contains($markdown, '{entity:descriptor}'))
|
|
{
|
|
$markdown = str_replace('{entity:descriptor}', $this->renderRawView('lean-api/entity_descriptor', [
|
|
'entities' => (new EntityDescriptor())->getEntities(... $entities)
|
|
]), $markdown);
|
|
}
|
|
|
|
if (str_contains($markdown, '{form:descriptor}'))
|
|
{
|
|
$markdown = str_replace('{form:descriptor}', $this->renderRawView('lean-api/form_descriptor', [
|
|
'forms' => (new FormDescriptor())->getForms(... $forms)
|
|
]), $markdown);
|
|
}
|
|
|
|
return $this->renderView("lean/layout/docs", get_defined_vars());
|
|
}
|
|
|
|
#[Ignore]
|
|
protected function output(\JsonSerializable|array $data, int $count) : ResponseInterface
|
|
{
|
|
return $this->renderJson([
|
|
'data' => $data,
|
|
'count' => $count,
|
|
]);
|
|
}
|
|
|
|
#[Ignore]
|
|
protected function outputSearchResult(ApiSearchRequestInterface $apiSearchRequest) : ResponseInterface
|
|
{
|
|
return $this->output($apiSearchRequest->getResult(), $apiSearchRequest->getSearch()->count);
|
|
}
|
|
|
|
#[Ignore]
|
|
protected function searchEntityFromRequest(ServerRequestInterface $request, string $entityType, ? string $resultKey = null) : ServerRequestInterface
|
|
{
|
|
$search = $entityType::searchRequest()->fromRequest($request);
|
|
$entity = $entityType::repository()->filterServerRequest($search)->loadOne() ?? false;
|
|
|
|
if (! $entity ) {
|
|
throw new \InvalidArgumentException(sprintf("L'entré pour l'entité demandé (%s) est introuvable avec le ou les arguments fournis '%s'", $entityType, json_encode($request->getAttributes(), \JSON_PARTIAL_OUTPUT_ON_ERROR)));
|
|
}
|
|
|
|
if ($request->getAttribute("lean.searchRequest") === null) {
|
|
$request = $request->withAttribute("lean.searchRequest", $this->searchEntityResult());
|
|
}
|
|
|
|
$request->getAttribute("lean.searchRequest")[$resultKey ?: $entityType] = new class($search, $entity) implements ApiSearchRequestInterface {
|
|
public function __construct(
|
|
public readonly SearchRequestInterface $search,
|
|
public readonly EntityInterface $entity,
|
|
) {}
|
|
|
|
public function getSearch(): SearchRequestInterface
|
|
{
|
|
return $this->search;
|
|
}
|
|
|
|
public function getResult(): EntityCollection|EntityInterface
|
|
{
|
|
return $this->entity;
|
|
}
|
|
};
|
|
|
|
return $request;
|
|
}
|
|
|
|
#[Ignore]
|
|
protected function searchEntitiesFromRequest(ServerRequestInterface $request, string $entityType, ? string $resultKey = null) : ServerRequestInterface
|
|
{
|
|
$search = $entityType::searchRequest()->fromRequest($request);
|
|
$entity = $entityType::repository()->filterServerRequest($search)->loadAll();
|
|
|
|
if (! $entity ) {
|
|
throw new \InvalidArgumentException(sprintf("L'entré pour l'entité demandé (%s) est introuvable avec le ou les arguments fournis '%s'", $entityType, json_encode($request->getAttributes())));
|
|
}
|
|
|
|
if ($request->getAttribute("lean.searchRequest") === null) {
|
|
$request = $request->withAttribute("lean.searchRequest", $this->searchEntityResult());
|
|
}
|
|
|
|
$request->getAttribute("lean.searchRequest")[$resultKey ?: $entityType] = new class($search, $entity) implements ApiSearchRequestInterface {
|
|
public function __construct(
|
|
public readonly SearchRequestInterface $search,
|
|
public readonly EntityCollection $collection,
|
|
) {}
|
|
|
|
public function getSearch(): SearchRequestInterface
|
|
{
|
|
return $this->search;
|
|
}
|
|
|
|
public function getResult(): EntityCollection|EntityInterface
|
|
{
|
|
return $this->collection;
|
|
}
|
|
};
|
|
|
|
return $request;
|
|
}
|
|
|
|
#[Ignore]
|
|
protected function searchEntityResult() :\ArrayObject {
|
|
return new class()extends \ArrayObject implements ApiSearchRequestInterface {
|
|
|
|
public function getSearch(): SearchRequestInterface
|
|
{
|
|
if ($this->count() !== 1) {
|
|
throw new \LogicException("Search results contains more than one results. Impossible to continue without precising which one you needs.");
|
|
}
|
|
|
|
return $this[array_key_last($this->getArrayCopy())]->getSearch();
|
|
}
|
|
|
|
public function getResult() : EntityInterface|EntityCollection {
|
|
if ($this->count() !== 1) {
|
|
throw new \LogicException("Search results contains more than one results. Impossible to continue without precising which one you needs.");
|
|
}
|
|
|
|
return $this[array_key_last($this->getArrayCopy())]->getResult();
|
|
}
|
|
};
|
|
}
|
|
} |