- WIP on visual of API

This commit is contained in:
Dave Mc Nicoll 2025-04-10 17:54:04 +00:00
parent 3d81b71473
commit 21f315cc22
10 changed files with 230 additions and 32 deletions

View File

@ -35,4 +35,6 @@ return [
'Lean\\Api\\Controller' => implode(DIRECTORY_SEPARATOR, [ $path, "src", "Controller", "" ]),
],*/
],
\Lean\Api\Factory\MessageFactoryInterface::class => DI\autowire(\Lean\Lib\Message::class),
];

View File

@ -0,0 +1,13 @@
<?php
namespace Lean\Api\Factory;
interface MessageFactoryInterface
{
public static function generateSuccess(string $message, ? string $header = null, ? string $class = null) : self;
public static function generateError(string $message, ? string $header = null, ? string $class = null) : self;
public static function generateWarning(string $message, ? string $header = null, ? string $class = null) : self;
public static function generateInformation(string $message, ? string $header = null, ? string $class = null) : self;
public static function generateDebug(string $message, ? string $header = null, ? string $class = null) : self;
public static function generateTrace(string $message, ? string $header = null, ? string $class = null) : self;
}

View File

@ -6,11 +6,8 @@ use CSSLSJ\ExamenFga\Api\{Lib};
use Picea\Ui\Method\{FormContextInterface};
use Ulmus\Entity\EntityInterface;
class Delete implements \Picea\Ui\Method\FormInterface {
public function __construct(
public EntityInterface $entity,
) {}
abstract class Delete implements \Picea\Ui\Method\FormInterface {
use FormTrait;
public function getEntity() : EntityInterface
{
@ -22,7 +19,7 @@ class Delete implements \Picea\Ui\Method\FormInterface {
public function validate(FormContextInterface $context) : bool
{
if ( ! $this->entity->isLoaded() ) {
$context->pushMessage(Lib\Message::generateError(
$context->pushMessage($this->message::generateError(
$this->lang('lean.api.form.delete.error.entity')
));
}
@ -34,7 +31,7 @@ class Delete implements \Picea\Ui\Method\FormInterface {
{
try {
if ( $this->getEntity()::repository()->destroy($this->getEntity()) ) {
$context->pushMessage(Lib\Message::generateSuccess(
$context->pushMessage($this->message::generateSuccess(
$this->lang('lean.api.form.delete.success.save')
));
}

20
src/Form/FormTrait.php Normal file
View File

@ -0,0 +1,20 @@
<?php
namespace Lean\Api\Form;
use Lean\Api\Factory\MessageFactoryInterface;
use Lean\LanguageHandler;
trait FormTrait
{
public function __construct(
protected LanguageHandler $languageHandler,
protected MessageFactoryInterface $message,
# public EntityInterface $entity,
) {}
protected function lang(string $key, array $variables = [])
{
return $this->languageHandler->languageFromKey($key, $variables);
}
}

View File

@ -0,0 +1,13 @@
<?php
namespace Lean\Api\Form;
enum MessageTypeEnum
{
case Error;
case Success;
case Information;
case Warning;
case Debug;
case Trace;
}

View File

@ -2,24 +2,16 @@
namespace Lean\Api\Form;
use CSSLSJ\ExamenFga\Api\{Form\Location\SessionContext, Lib, Entity};
use Picea\Ui\Method\{ FormContextInterface, Message\ErrorMessage };
use Picea\Ui\Method\{ FormContextInterface, };
use Ulmus\Attribute\Property\Field;
use Lean\Api\Attribute\EntityField;
use Lean\LanguageHandler;
use Psr\Http\Message\ServerRequestInterface;
use Ulmus\Entity\EntityInterface;
use Ulmus\Entity\Field\Datetime;
use function CSSLSJ\ExamenFga\Api\View\{ lang };
abstract class Save implements \Picea\Ui\Method\FormInterface {
public function __construct(
protected LanguageHandler $languageHandler,
# public EntityInterface $entity,
) {}
use FormTrait;
public function getEntity() : EntityInterface
{
@ -51,7 +43,7 @@ abstract class Save implements \Picea\Ui\Method\FormInterface {
$this->assignContextToEntity($context);
if ( $entity::repository()->save($entity) ) {
$context->pushMessage(Lib\Message::generateSuccess(
$context->pushMessage($this->message::generateSuccess(
$this->lang('lean.api.form.save.success.entity')
));
}
@ -64,11 +56,6 @@ abstract class Save implements \Picea\Ui\Method\FormInterface {
}
}
protected function lang(string $key, array $variables = [])
{
return $this->languageHandler->languageFromKey($key, $variables);
}
protected function assignContextToEntity(FormContextInterface $context) : void
{
$entity = $this->getEntity();

132
src/Lib/Message.php Normal file
View File

@ -0,0 +1,132 @@
<?php declare(strict_types=1);
namespace Lean\Lib;
use Lean\Api\Factory\MessageFactoryInterface;
use Picea\Ui\Method\FormMessage;
class Message implements FormMessage, MessageFactoryInterface {
const MESSAGE_TYPE = [
'success' => [
'class' => 'success',
'header' => 'Succès !',
'message' => ''
],
'error' => [
'class' => 'danger',
'header' => 'Error !',
'message' => ''
],
'warning' => [
'class' => 'warning',
'header' => 'Attention !',
'message' => ''
],
'information' => [
'class' => 'information',
'header' => 'À savoir !',
'message' => ''
],
'debug' => [
'class' => 'debug',
'header' => 'Debug :',
'message' => ''
],
'trace' => [
'class' => 'trace',
'header' => 'Trace :',
'message' => ''
]
];
public string $type;
public string $message;
public string $class;
public ? string $header = null;
public function __construct(string $message, string $type = "error", ? string $header = null, ? string $class = null)
{
$this->message = $message;
$this->type = $type;
if ( $header !== null ) {
$this->header = $header;
}
else {
$this->header = static::MESSAGE_TYPE[$type]['header'];
}
if ( $class !== null ) {
$this->class = $class;
}
else {
$this->class = static::MESSAGE_TYPE[$type]['class'];
}
}
public function render() : string
{
return <<<HTML
<article class="message is-{$this->class}" role="alert">
<div class="message-body">
<strong>{$this->header}</strong>
<span>{$this->message}</span>
</div>
</article>
HTML;
}
public function renderJson() : array
{
return [
'name' => 'message',
'type' => $this->type,
'message' => $this->message,
];
}
public function isError() : bool
{
return $this->type === "error";
}
public static function generateSuccess(string $message, ? string $header = null, ? string $class = null) : self
{
return new static($message, 'success', $header, $class);
}
public static function generateError(string $message, ? string $header = null, ? string $class = null) : self
{
return new static($message, 'error', $header, $class);
}
public static function generateWarning(string $message, ? string $header = null, ? string $class = null) : self
{
return new static($message, 'warning', $header, $class);
}
public static function generateInformation(string $message, ? string $header = null, ? string $class = null) : self
{
return new static($message, 'information', $header, $class);
}
public static function generateDebug(string $message, ? string $header = null, ? string $class = null) : self
{
return new static($message, 'debug', $header, $class);
}
public static function generateTrace(string $message, ? string $header = null, ? string $class = null) : self
{
return new static($message, 'trace', $header, $class);
}
}

View File

@ -45,7 +45,8 @@ HTML;
'path' => $base.$path,
'cleaned' => $cleaned,
'description'=> $route->description,
'methods' =>implode(', ', (array)$route->method),
#'methods' =>implode(', ', (array)$route->method),
'methods' => (array) $route->method,
];
}
}

View File

@ -1,9 +1,14 @@
<ul>
<ul class="routes-wrapper">
{% foreach $routes as $route %}
<li>
<span><a href="{{ $route['route'] }}" title="{{ $route['path'] }}" style='font-family:monospace;font-size:.85em'>{{ $route['cleaned'] }}</a> - {{= $route['description'] }}</span>
<span style='color:#ac1b1b'>{{ $route['methods'] }}</span>
<small style="color:#374300">{{ $route['name'] }}</small>
{% foreach $route['methods'] as $method %}
<li class="method-{{ strtolower($method) }}">
<span class="route-method">{{ $method }}</span>
<span class="route-link">
<a href="{{ $route['route'] }}" title="{{ $route['path'] }}">{{ $route['cleaned'] }}</a>
<span>- {{= $route['description'] }}</span>
</span>
<small class="route-name">{{ $route['name'] }}</small>
</li>
{% endforeach %}
{% endforeach %}
</ul>

View File

@ -24,17 +24,45 @@
li.odd-even{border-top:1px solid #ccc;margin:10px 0;padding:15px 15px 10px 5px}
li.odd-even:first-child{border:0}
input, button {padding:5px; font-size:1em;margin-top:10px}
ul {background:#f4f4f4; list-style: none; padding-left:20px}
ul ul {margin: 0;border: 0;padding: 5px 30px;}
ul li + li {margin-top: 12px;}
ul ul > li:before {content:"↳"}
ol .title { display: flex;justify-content: space-between;background: #ffffffb2;padding: 9px 5px;border: 1px solid #fff;}
.field-desc > div {padding:5px;}
.forms ol {background: #ccdef2;}
.forms li {border-color: #859aae;}
.forms .form-name {background: #9cc5e6;color: #284168;font-size:110%}
.routes-wrapper {padding:8px}
.routes-wrapper li + li {margin-top: 5px}
.routes-wrapper li {display:flex;align-items:center;border: 1px solid #ccc;}
.routes-wrapper .route-method {line-height: 1.8rem;padding:6px 5px 0 5px;width:80px;text-align:center;font-weight:bold;color:#fff;}
.routes-wrapper .route-link {line-height: 1.8rem;padding:0 10px}
.routes-wrapper .route-link a {font-family:monospace;font-size:.85em}
.routes-wrapper .route-name {margin-left:auto;font-weight:bold;width: 200px;text-align: right;background:rgba(0, 0, 0, 0.02);line-height: 1.8rem;padding:6px 7px 0 5px;}
.routes-wrapper li.method-get {background:#e7eff7;border-color: #bfcfdd;}
.routes-wrapper li.method-get .route-method {background:#0f6ab4;}
.routes-wrapper li.method-get .route-link a, .routes-wrapper li.method-get .route-name {color: #0f6ab4;}
.routes-wrapper li.method-head {background:#f5e8f2;border-color:#ffc0e7;}
.routes-wrapper li.method-head .route-method {background:#ca6aa5;}
.routes-wrapper li.method-head .route-link a, .routes-wrapper li.method-head .route-name {color: #ca6aa5;}
.routes-wrapper li.method-post {background:#e6f5eb;border-color: #ace3c2;}
.routes-wrapper li.method-post .route-method {background:#10a44a;}
.routes-wrapper li.method-post .route-link a, .routes-wrapper li.method-post .route-name {color: #10a44a;}
.routes-wrapper li.method-patch {background:#fce9e3;border-color:#ffd4b3}
.routes-wrapper li.method-patch .route-method {background:#f4842f;}
.routes-wrapper li.method-patch .route-link a, .routes-wrapper li.method-patch .route-name {color: #f4842f;}
.routes-wrapper li.method-option {background:#eaeaea;border-color: #dbdbdb;}
.routes-wrapper li.method-option .route-method {background:#a4a4a4;}
.routes-wrapper li.method-option .route-link a, .routes-wrapper li.method-option .route-name {color: #717171;}
.routes-wrapper li.method-delete {background:#f5e8e8;border-color: #ffadaf;}
.routes-wrapper li.method-delete .route-method {background:#d73c41;}
.routes-wrapper li.method-delete .route-link a, .routes-wrapper li.method-delete .route-name {color: #d73c41;}
.routes-wrapper li.method-put {background:#fff5c2;border-color: #eadd9a;}
.routes-wrapper li.method-put .route-method {background:#d7bf3c;}
.routes-wrapper li.method-put .route-link a, .routes-wrapper li.method-put .route-name {color: #aa941c;}
.entity-wrapper .entity-name {background:#eaa1af;color: #682828;font-size:110%}
.entity-wrapper ol {background: #f0ddcd;}
.entity-wrapper li {border-color: #ae8585;}