Gephart 0.6
Verze 0.6 se nese ve znamení úprav designu administrace a refactoringu typů položek v generátoru.
Jednotlivé typy jako třeba "text", "bool", "textarea" jsou reprezentovány samostatnými objekty a lze pomocí eventu "Admin\Generator\Service::HOOK_EVENT" přidávat další dle osobních potřeb.
Nejprve tedy vytvoříte vlastní typ. Například:
<?php # /src/App/My/MyType.php
namespace App\My;
use Admin\Generator\Type\TypeInterface;
final class MyType implements TypeInterface
{
public function getName(): string
{
return "my-custom-type";
}
public function getEntityProperty(): string
{
return <<<EOL
/**
* @var string
*
* @ORM\Type TEXT
* @ORM\Column {{ item.slug }}
* @ORM\Translatable
*/
private \${{ item.slug }} = "";
EOL;
}
public function getEntityMethods(): string
{
return <<<EOL
/**
* @return string
*/
public function get{{ item.slugInCamel }}(): string
{
return \$this->{{ item.slug }};
}
/**
* @param string \${{ item.slug }}
*/
public function set{{ item.slugInCamel }}(string \${{ item.slug }})
{
\$this->{{ item.slug }} = \${{ item.slug }};
}
EOL;
}
public function getSet(): string
{
return <<<EOL
\${{ module.slugSingular }}->set{{ item.slugInCamel }}(\$data["{{ item.slug }}"]);
EOL;
}
public function getForm(): string
{
return <<<EOL
<div class="form-group">
<label for="form_edit_{{ item.slug }}">{{ item.name }}</label>
<textarea class="form-control" name="{{ item.slug }}" rows="10" id="form_edit_{{ item.slug }}">{{ "{{" }} {{ module.slugSingular }}.{{ item.slugInCamel }} {{ "}}" }}</textarea>
</div>
EOL;
}
public function getShow(): string
{
return <<<EOL
{{ "{{" }} {{ module.slugSingular }}.{{ item.slugInCamel }} {{ "}}" }}
EOL;
}
public function getPriority(): int
{
return 500;
}
}
Následně odchytit event a vlastní typ zaregistrovat:
<?php # /src/App/My/MyListener.php
namespace App\My;
use Admin\Generator\Service\Types;
use Gephart\EventManager\Event;
use Gephart\EventManager\EventManager;
class MyListener
{
public function __construct(EventManager $event_manager)
{
$event_manager->attach(Types::HOOK_EVENT, [$this, "registerMyType"]);
}
public function registerMyType(Event $event)
{
$types = $event->getParam("types");
$types[] = MyType::class;
$event->setParams(["types" => $types]);
}
}
V poslední řadě nelze zapomenout listener odchytávají funkci zaregistrovat:
<?php # /web/index.php
use Gephart\Framework\Kernel;
use Gephart\Http\RequestFactory;
include_once __DIR__ . "/../vendor/autoload.php";
$request = (new RequestFactory())->createFromGlobals();
$kernel = new Kernel($request);
$kernel->registerServices([
\App\My\MyListener::class
]);
$response = $kernel->run();
echo $kernel->render($response);