Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix issues with using FQCN service id for tree builder #42

Merged
merged 1 commit into from
Jul 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/DAMAMenuBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -12,5 +13,6 @@ public function build(ContainerBuilder $container): void
{
parent::build($container);
$container->addCompilerPass(new NodeVisitorCompilerPass());
$container->addCompilerPass(new MenuCompilerPass());
}
}
66 changes: 66 additions & 0 deletions src/DependencyInjection/CompilerPass/MenuCompilerPass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

namespace DAMA\MenuBundle\DependencyInjection\CompilerPass;

use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;

final class MenuCompilerPass implements CompilerPassInterface
{
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 process(ContainerBuilder $container): void
{
/** @var array{
* node_factory: string,
* twig_template: string,
* menues?: array<string, array{
* tree_builder: string,
* twig_template: string
* }>
* } $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);
}
}
45 changes: 2 additions & 43 deletions src/DependencyInjection/DAMAMenuExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
6 changes: 3 additions & 3 deletions src/Node/Node.php
Original file line number Diff line number Diff line change
Expand Up @@ -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++;
Expand All @@ -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;

Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/Node/NodeFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/Node/NodeFactoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@

interface NodeFactoryInterface
{
public function create(string $label = null): Node;
public function create(?string $label = null): Node;
}
33 changes: 2 additions & 31 deletions tests/Functional/FunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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);
}
}
Loading