-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: add a KNP menu bridge to handle menu management
- Loading branch information
1 parent
eee9201
commit 34dc842
Showing
7 changed files
with
139 additions
and
150 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
namespace LAG\AdminBundle\Bridge\KnpMenu\Builder; | ||
|
||
use Knp\Menu\ItemInterface; | ||
use Knp\Menu\Provider\MenuProviderInterface; | ||
use LAG\AdminBundle\Event\MenuEvent; | ||
use LAG\AdminBundle\Event\MenuEvents; | ||
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface; | ||
|
||
final readonly class EventChainProvider implements MenuProviderInterface | ||
{ | ||
public function __construct( | ||
private MenuProviderInterface $decorated, | ||
private EventDispatcherInterface $eventDispatcher, | ||
) { | ||
} | ||
|
||
public function get(string $name, array $options = []): ItemInterface | ||
{ | ||
$menu = $this->decorated->get($name, $options); | ||
$event = new MenuEvent($menu); | ||
|
||
$this->eventDispatcher->dispatch($event, MenuEvents::MENU_CREATE); | ||
$this->eventDispatcher->dispatch($event, sprintf(MenuEvents::PRE_EVENT_PATTERN, $name)); | ||
|
||
return $menu; | ||
} | ||
|
||
public function has(string $name, array $options = []): bool | ||
{ | ||
return $this->decorated->has($name, $options); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
|
||
namespace LAG\AdminBundle\Bridge\KnpMenu\Extension; | ||
|
||
use Knp\Menu\Factory\ExtensionInterface; | ||
use Knp\Menu\ItemInterface; | ||
use LAG\AdminBundle\Resource\Registry\ResourceRegistryInterface; | ||
use LAG\AdminBundle\Routing\UrlGenerator\UrlGeneratorInterface; | ||
|
||
final readonly class ResourceExtension implements ExtensionInterface | ||
{ | ||
public function __construct( | ||
private ResourceRegistryInterface $registry, | ||
private UrlGeneratorInterface $urlGenerator, | ||
) { | ||
} | ||
|
||
public function buildOptions(array $options): array | ||
{ | ||
if (!isset($options['resource']) || !isset($options['operation'])) { | ||
return $options; | ||
} | ||
|
||
if (!$this->registry->has($options['resource'], $options['application'] ?? null)) { | ||
return $options; | ||
} | ||
$resource = $this->registry->get($options['resource'], $options['application'] ?? null); | ||
|
||
|
||
if (!$resource->hasOperation($options['operation'])) { | ||
return $options; | ||
} | ||
$operation = $resource->getOperation($options['operation']); | ||
$options['uri'] = $this->urlGenerator->generateOperationUrl($operation); | ||
$options['extras'] = []; | ||
|
||
if (empty($options['label'])) { | ||
$options['label'] = $operation->getTitle(); | ||
} | ||
|
||
return $options; | ||
} | ||
|
||
public function buildItem(ItemInterface $item, array $options): void | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
tests/phpunit/Bridge/KnpMenu/Builder/ContextualMenuBuilderTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
|
||
namespace LAG\AdminBundle\Tests\Bridge\KnpMenu\Builder; | ||
|
||
use Knp\Menu\FactoryInterface; | ||
use LAG\AdminBundle\Menu\Builder\ContextualMenuBuilder; | ||
use LAG\AdminBundle\Resource\Context\ResourceContextInterface; | ||
use LAG\AdminBundle\Resource\Registry\ResourceRegistryInterface; | ||
use LAG\AdminBundle\Routing\Route\RouteNameGeneratorInterface; | ||
use LAG\AdminBundle\Tests\TestCase; | ||
use PHPUnit\Framework\MockObject\MockObject; | ||
use Symfony\Component\HttpFoundation\RequestStack; | ||
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface; | ||
|
||
class ContextualMenuBuilderTest extends TestCase | ||
{ | ||
private ContextualMenuBuilder $builder; | ||
private MockObject $resourceContext; | ||
private MockObject $registry; | ||
private MockObject $requestStack; | ||
private MockObject $routeNameGenerator; | ||
private MockObject $factory; | ||
private MockObject $eventDispatcher; | ||
|
||
public function testBuildNonResourceMenu(): void | ||
{ | ||
$this->builder->build(); | ||
} | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->resourceContext = $this->createMock(ResourceContextInterface::class); | ||
$this->registry = $this->createMock(ResourceRegistryInterface::class); | ||
$this->requestStack = $this->createMock(RequestStack::class); | ||
$this->routeNameGenerator = $this->createMock(RouteNameGeneratorInterface::class); | ||
$this->factory = $this->createMock(FactoryInterface::class); | ||
$this->eventDispatcher = $this->createMock(EventDispatcherInterface::class); | ||
$this->builder = new ContextualMenuBuilder( | ||
$this->resourceContext, | ||
$this->registry, | ||
$this->requestStack, | ||
$this->routeNameGenerator, | ||
$this->factory, | ||
$this->eventDispatcher, | ||
); | ||
} | ||
} |