add callable controllers for nodes in node configuration

This commit is contained in:
Simon Vieille 2021-05-25 20:25:57 +02:00
parent cc368ac4b8
commit 0f78e3212d
7 changed files with 113 additions and 7 deletions

View File

@ -2,6 +2,8 @@ core:
site:
name: "Murph"
logo: "build/images/core/logo.svg"
# controllers:
# - {name: 'Foo', action: 'App\Controller\ExampleController::foo'}
pages:
App\Entity\Page\SimplePage:
name: 'Page simple'

View File

@ -20,6 +20,7 @@ use Symfony\Component\Form\FormError;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use App\Core\Site\ControllerLocator;
/**
* @Route("/admin/site/node")
@ -36,11 +37,13 @@ class NodeAdminController extends AdminController
EntityManager $entityManager,
NodeRepository $nodeRepository,
PageLocator $pageLocator,
ControllerLocator $controllerLocator,
Request $request
): Response {
$entity = $factory->create($node->getMenu());
$form = $this->createForm(EntityType::class, $entity, [
'pages' => $pageLocator->getPages(),
'controllers' => $controllerLocator->getControllers(),
'navigation' => $node->getMenu()->getNavigation(),
]);
@ -105,10 +108,12 @@ class NodeAdminController extends AdminController
EntityManager $entityManager,
PageFactory $pageFactory,
PageLocator $pageLocator,
ControllerLocator $controllerLocator,
Request $request
): Response {
$form = $this->createForm(EntityType::class, $entity, [
'pages' => $pageLocator->getPages(),
'controllers' => $controllerLocator->getControllers(),
'navigation' => $entity->getMenu()->getNavigation(),
]);

View File

@ -23,6 +23,18 @@ class Configuration implements ConfigurationInterface
->isRequired()
->cannotBeEmpty()
->end()
->arrayNode('controllers')
->prototype('array')
->children()
->scalarNode('name')
->cannotBeEmpty()
->end()
->scalarNode('action')
->cannotBeEmpty()
->end()
->end()
->end()
->end()
->arrayNode('pages')
->prototype('array')
->children()
@ -42,6 +54,7 @@ class Configuration implements ConfigurationInterface
->end()
->end()
->end()
->end()
->end()
->end()
->end()

View File

@ -89,15 +89,20 @@ class NodeType extends AbstractType
$builder->add(
'controller',
TextType::class,
ChoiceType::class,
[
'label' => 'Controller',
'required' => false,
'help' => 'Leave blank to use the default one. Example: App\\Controller\\FooController::barAction',
'attr' => [
],
'constraints' => [
],
'help' => 'Leave blank to use the default one',
'choices' => call_user_func(function () use ($options) {
$choices = [];
foreach ($options['controllers'] as $controller) {
$choices[$controller->getName()] = $controller->getAction();
}
return $choices;
}),
]
);
@ -259,6 +264,7 @@ class NodeType extends AbstractType
$resolver->setDefaults([
'data_class' => Node::class,
'pages' => [],
'controllers' => [],
'navigation' => null,
]);
}

View File

@ -0,0 +1,38 @@
<?php
namespace App\Core\Site;
/**
* class ControllerConfiguration.
*
* @author Simon Vieille <simon@deblan.fr>
*/
class ControllerConfiguration
{
protected string $name;
protected string $action;
public function setName(string $name): self
{
$this->name = $name;
return $this;
}
public function getName(): string
{
return $this->name;
}
public function setAction(string $action): self
{
$this->action = $action;
return $this;
}
public function getAction(): string
{
return $this->action;
}
}

View File

@ -0,0 +1,42 @@
<?php
namespace App\Core\Site;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
/**
* class ControllerLocator.
*
* @author Simon Vieille <simon@deblan.fr>
*/
class ControllerLocator
{
protected array $params;
protected array $controllers = [];
public function __construct(ParameterBagInterface $bag)
{
$this->params = $bag->get('core');
$this->loadControllers();
}
public function getControllers(): array
{
return $this->controllers;
}
protected function loadControllers(): void
{
$params = $this->params['site']['controllers'] ?? [];
foreach ($params as $conf) {
$controllerConfiguration = new ControllerConfiguration();
$controllerConfiguration
->setName($conf['name'])
->setAction($conf['action'])
;
$this->controllers[$conf['action']] = $controllerConfiguration;
}
}
}

View File

@ -12,7 +12,7 @@ use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
class PageLocator
{
protected array $params;
protected array $pages;
protected array $pages = [];
public function __construct(ParameterBagInterface $bag)
{