From 70f39050f5e8d897d3a7664c54eb75b94631a940 Mon Sep 17 00:00:00 2001 From: David Maicher Date: Mon, 29 Jul 2024 22:11:07 +0200 Subject: [PATCH] fix issues with using FQCN service id for tree builder --- src/DAMAMenuBundle.php | 2 + .../CompilerPass/MenuCompilerPass.php | 66 +++++++++++++++++++ src/DependencyInjection/DAMAMenuExtension.php | 45 +------------ src/Node/Node.php | 6 +- src/Node/NodeFactory.php | 2 +- src/Node/NodeFactoryInterface.php | 2 +- tests/Functional/FunctionalTest.php | 33 +--------- 7 files changed, 77 insertions(+), 79 deletions(-) create mode 100644 src/DependencyInjection/CompilerPass/MenuCompilerPass.php diff --git a/src/DAMAMenuBundle.php b/src/DAMAMenuBundle.php index aecf556..539f108 100644 --- a/src/DAMAMenuBundle.php +++ b/src/DAMAMenuBundle.php @@ -2,6 +2,7 @@ namespace DAMA\MenuBundle; +use DAMA\MenuBundle\DependencyInjection\CompilerPass\MenuCompilerPass; use DAMA\MenuBundle\DependencyInjection\CompilerPass\NodeVisitorCompilerPass; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\HttpKernel\Bundle\Bundle; @@ -12,5 +13,6 @@ public function build(ContainerBuilder $container): void { parent::build($container); $container->addCompilerPass(new NodeVisitorCompilerPass()); + $container->addCompilerPass(new MenuCompilerPass()); } } diff --git a/src/DependencyInjection/CompilerPass/MenuCompilerPass.php b/src/DependencyInjection/CompilerPass/MenuCompilerPass.php new file mode 100644 index 0000000..cef84d0 --- /dev/null +++ b/src/DependencyInjection/CompilerPass/MenuCompilerPass.php @@ -0,0 +1,66 @@ + + * } $processedConfig + */ + $processedConfig = $container->getParameter('dama.menu.config'); + + $globalConfig = [ + 'node_factory' => $processedConfig['node_factory'], + 'twig_template' => $processedConfig['twig_template'], + ]; + + if (isset($processedConfig['menues'])) { + $menuConfigProviderDef = $container->getDefinition('dama_menu.menu_config_provider'); + + foreach ($processedConfig['menues'] as $name => $menuConfig) { + $menuConfig = array_merge($globalConfig, $menuConfig); + + $menuConfig['tree_builder'] = $this->getReferenceFromConfigValue( + $container, + self::TREE_BUILDER_SERVICE_ID_PREFIX, + $menuConfig['tree_builder'] + ); + + $menuConfig['node_factory'] = $this->getReferenceFromConfigValue( + $container, + self::NODE_FACTORY_SERVICE_ID_PREFIX, + $menuConfig['node_factory'] + ); + + $menuConfigProviderDef->addMethodCall('addMenuConfig', [$name, $menuConfig]); + } + } + } + + protected function getReferenceFromConfigValue(ContainerBuilder $container, string $prefix, string $value): Reference + { + if (class_exists($value) && !$container->has($value)) { + $id = $prefix.md5($value); + $container->register($id, $value); + + return new Reference($id); + } + + return new Reference($value); + } +} diff --git a/src/DependencyInjection/DAMAMenuExtension.php b/src/DependencyInjection/DAMAMenuExtension.php index 763b8bc..a1ce5e2 100644 --- a/src/DependencyInjection/DAMAMenuExtension.php +++ b/src/DependencyInjection/DAMAMenuExtension.php @@ -4,60 +4,19 @@ use Symfony\Component\Config\FileLocator; use Symfony\Component\DependencyInjection\ContainerBuilder; +use Symfony\Component\DependencyInjection\Extension\Extension; use Symfony\Component\DependencyInjection\Loader; -use Symfony\Component\DependencyInjection\Reference; -use Symfony\Component\HttpKernel\DependencyInjection\Extension; final class DAMAMenuExtension extends Extension { - public const NODE_FACTORY_SERVICE_ID_PREFIX = 'dama_menu.node_factory.custom.'; - public const TREE_BUILDER_SERVICE_ID_PREFIX = 'dama_menu.tree_builder.custom.'; - public function load(array $configs, ContainerBuilder $container): void { $configuration = new Configuration(); $processedConfig = $this->processConfiguration($configuration, $configs); - $globalConfig = [ - 'node_factory' => $processedConfig['node_factory'], - 'twig_template' => $processedConfig['twig_template'], - ]; + $container->setParameter('dama.menu.config', $processedConfig); $loader = new Loader\PhpFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); $loader->load('services.php'); - - if (isset($processedConfig['menues'])) { - $menuConfigProviderDef = $container->getDefinition('dama_menu.menu_config_provider'); - - foreach ($processedConfig['menues'] as $name => $menuConfig) { - $menuConfig = array_merge($globalConfig, $menuConfig); - - $menuConfig['tree_builder'] = $this->getReferenceFromConfigValue( - $container, - self::TREE_BUILDER_SERVICE_ID_PREFIX, - $menuConfig['tree_builder'] - ); - - $menuConfig['node_factory'] = $this->getReferenceFromConfigValue( - $container, - self::NODE_FACTORY_SERVICE_ID_PREFIX, - $menuConfig['node_factory'] - ); - - $menuConfigProviderDef->addMethodCall('addMenuConfig', [$name, $menuConfig]); - } - } - } - - protected function getReferenceFromConfigValue(ContainerBuilder $container, string $prefix, string $value): Reference - { - if (class_exists($value) && !$container->has($value)) { - $id = $prefix.md5($value); - $container->register($id, $value); - - return new Reference($id); - } else { - return new Reference($value); - } } } diff --git a/src/Node/Node.php b/src/Node/Node.php index 2634a19..a28809e 100644 --- a/src/Node/Node.php +++ b/src/Node/Node.php @@ -81,7 +81,7 @@ class Node */ protected $removeIfNoChildren = false; - public function __construct(string $label = null) + public function __construct(?string $label = null) { $this->label = $label; $this->id = self::$counter++; @@ -90,7 +90,7 @@ public function __construct(string $label = null) /** * @return $this */ - public function setParent(Node $parent = null): self + public function setParent(?Node $parent = null): self { $this->parent = $parent; @@ -134,7 +134,7 @@ public function endIf(): self /** * @throws \BadMethodCallException */ - public function child(string $label = null): self + public function child(?string $label = null): self { if (!$this->nodeFactory) { throw new \BadMethodCallException('nodeFactory needs to be set on this node to be able diff --git a/src/Node/NodeFactory.php b/src/Node/NodeFactory.php index e43f2f8..8549921 100644 --- a/src/Node/NodeFactory.php +++ b/src/Node/NodeFactory.php @@ -4,7 +4,7 @@ final class NodeFactory implements NodeFactoryInterface { - public function create(string $label = null): Node + public function create(?string $label = null): Node { $node = new Node($label); $node->setNodeFactory($this); diff --git a/src/Node/NodeFactoryInterface.php b/src/Node/NodeFactoryInterface.php index 206d486..ee7252f 100644 --- a/src/Node/NodeFactoryInterface.php +++ b/src/Node/NodeFactoryInterface.php @@ -4,5 +4,5 @@ interface NodeFactoryInterface { - public function create(string $label = null): Node; + public function create(?string $label = null): Node; } diff --git a/tests/Functional/FunctionalTest.php b/tests/Functional/FunctionalTest.php index a6cbf21..111ebca 100644 --- a/tests/Functional/FunctionalTest.php +++ b/tests/Functional/FunctionalTest.php @@ -4,10 +4,7 @@ use Symfony\Bundle\FrameworkBundle\KernelBrowser; use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; -use Symfony\Component\BrowserKit\Cookie; -use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken; use Symfony\Component\Security\Core\User\InMemoryUser; -use Symfony\Component\Security\Core\User\User; class FunctionalTest extends WebTestCase { @@ -51,33 +48,7 @@ public function testRenderMenuWithUserBar(): void private function loginUser(KernelBrowser $client, string $username, string $password, array $roles): void { - if (class_exists(InMemoryUser::class)) { - $user = new InMemoryUser($username, $password, $roles); - } else { - $user = new User($username, $password, $roles); - } - - if (method_exists($client, 'loginUser')) { - $client->loginUser($user); - - return; - } - - // TODO: cleanup once Symfony 4.4 support is dropped - $token = new UsernamePasswordToken($user, $user->getPassword(), 'main', $user->getRoles()); - - $container = self::$kernel->getContainer()->get('test.service_container'); - $container->get('security.untracked_token_storage')->setToken($token); - - if (!$container->has('session') && !$container->has('session_factory')) { - return; - } - - $session = $container->get($container->has('session') ? 'session' : 'session_factory'); - $session->set('_security_main', serialize($token)); - $session->save(); - - $cookie = new Cookie($session->getName(), $session->getId()); - $client->getCookieJar()->set($cookie); + $user = new InMemoryUser($username, $password, $roles); + $client->loginUser($user); } }