This commit is contained in:
Kevin Frantz 2018-09-06 09:44:29 +02:00
parent f86b9a0f75
commit 36b9020c01
11 changed files with 301 additions and 3 deletions

View File

@ -5,6 +5,7 @@
"php": "^7.1.3",
"ext-ctype": "*",
"ext-iconv": "*",
"knplabs/knp-menu-bundle": "^2.0",
"sensio/framework-extra-bundle": "^5.1",
"symfony/asset": "*",
"symfony/console": "*",

View File

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "be855819d48e2927e7f5ba7a11b794c5",
"content-hash": "46e513d742140ba387e272208c8a9dc9",
"packages": [
{
"name": "doctrine/annotations",
@ -1331,6 +1331,129 @@
],
"time": "2014-01-12T16:20:24+00:00"
},
{
"name": "knplabs/knp-menu",
"version": "2.3.0",
"source": {
"type": "git",
"url": "https://github.com/KnpLabs/KnpMenu.git",
"reference": "655630a1db0b72108262d1a844de3b1ba0885be5"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/KnpLabs/KnpMenu/zipball/655630a1db0b72108262d1a844de3b1ba0885be5",
"reference": "655630a1db0b72108262d1a844de3b1ba0885be5",
"shasum": ""
},
"require": {
"php": ">=5.6.0"
},
"require-dev": {
"psr/container": "^1.0",
"symfony/http-foundation": "~2.4|~3.0|^4.0",
"symfony/phpunit-bridge": "~3.3|^4.0",
"symfony/routing": "~2.3|~3.0|^4.0",
"twig/twig": "~1.16|~2.0"
},
"suggest": {
"twig/twig": "for the TwigRenderer and the integration with your templates"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "2.3-dev"
}
},
"autoload": {
"psr-4": {
"Knp\\Menu\\": "src/Knp/Menu"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Christophe Coevoet",
"email": "stof@notk.org"
},
{
"name": "Symfony Community",
"homepage": "https://github.com/KnpLabs/KnpMenu/contributors"
},
{
"name": "KnpLabs",
"homepage": "https://knplabs.com"
}
],
"description": "An object oriented menu library",
"homepage": "https://knplabs.com",
"keywords": [
"menu",
"tree"
],
"time": "2017-11-18T20:49:26+00:00"
},
{
"name": "knplabs/knp-menu-bundle",
"version": "v2.2.1",
"source": {
"type": "git",
"url": "https://github.com/KnpLabs/KnpMenuBundle.git",
"reference": "6bea43eb84fc67c43ab2b43709194efffa8a8ac0"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/KnpLabs/KnpMenuBundle/zipball/6bea43eb84fc67c43ab2b43709194efffa8a8ac0",
"reference": "6bea43eb84fc67c43ab2b43709194efffa8a8ac0",
"shasum": ""
},
"require": {
"knplabs/knp-menu": "~2.3",
"php": "^5.6 || ^7",
"symfony/framework-bundle": "~2.7|~3.0 | ^4.0"
},
"require-dev": {
"symfony/expression-language": "~2.7|~3.0 | ^4.0",
"symfony/phpunit-bridge": "^3.3 | ^4.0",
"symfony/templating": "~2.7|~3.0 | ^4.0"
},
"type": "symfony-bundle",
"extra": {
"branch-alias": {
"dev-master": "2.2.x-dev"
}
},
"autoload": {
"psr-4": {
"Knp\\Bundle\\MenuBundle\\": "src"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Christophe Coevoet",
"email": "stof@notk.org"
},
{
"name": "Knplabs",
"homepage": "http://knplabs.com"
},
{
"name": "Symfony Community",
"homepage": "https://github.com/KnpLabs/KnpMenuBundle/contributors"
}
],
"description": "This bundle provides an integration of the KnpMenu library",
"keywords": [
"menu"
],
"time": "2017-12-24T16:32:39+00:00"
},
{
"name": "monolog/monolog",
"version": "1.23.0",

View File

@ -14,4 +14,5 @@ return [
Symfony\Bundle\DebugBundle\DebugBundle::class => ['dev' => true, 'test' => true],
Symfony\Bundle\MakerBundle\MakerBundle::class => ['dev' => true],
Symfony\Bundle\WebServerBundle\WebServerBundle::class => ['dev' => true],
Knp\Bundle\MenuBundle\KnpMenuBundle::class => ['all' => true],
];

View File

@ -14,7 +14,13 @@ services:
public: false # Allows optimizing the container by removing unused services; this also means
# fetching services directly from the container via $container->get() won't work.
# The best practice is to be explicit about your dependencies anyway.
app.menu_builder:
class: App\Menu\Menu
app.menu.usertopbar:
class: Knp\Menu\MenuItem
factory: ['@app.menu_builder', 'userTopbar']
tags:
- { name: knp_menu.menu, alias: userTopbar }
# makes classes in src/ available to be used as services
# this creates a service per class whose id is the fully-qualified class name
App\:

View File

@ -0,0 +1,59 @@
<?php
namespace App\Event\Menu\Topbar;
use Knp\Menu\FactoryInterface;
use Knp\Menu\ItemInterface;
use Symfony\Component\EventDispatcher\Event;
use Symfony\Component\HttpFoundation\RequestStack;
class UserMenuEvent extends Event
{
public const EVENT = 'app.menu.topbar.user';
/**
* @var FactoryInterface
*/
private $factory;
/**
* @var ItemInterface
*/
private $item;
/**
* @var RequestStack
*/
private $request;
public function __construct(FactoryInterface $factory, ItemInterface $item, RequestStack $request)
{
$this->factory = $factory;
$this->item = $item;
$this->request = $request;
}
/**
* @return FactoryInterface
*/
public function getFactory(): FactoryInterface
{
return $this->factory;
}
/**
* @return ItemInterface
*/
public function getItem(): ItemInterface
{
return $this->item;
}
/**
* @return RequestStack
*/
public function getRequest(): RequestStack
{
return $this->request;
}
}

View File

@ -0,0 +1,42 @@
<?php
// src/Menu/Menu.php
namespace App\Menu;
use App\Event\Menu\Topbar\UserMenuEvent;
use Knp\Menu\FactoryInterface;
use Knp\Menu\ItemInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpFoundation\RequestStack;
class Menu
{
/**
* @var EventDispatcherInterface
*/
private $dispatcher;
/**
* @var FactoryInterface
*/
private $factory;
public function __construct(FactoryInterface $factory, EventDispatcherInterface $dispatcher)
{
$this->dispatcher = $dispatcher;
$this->factory = $factory;
}
public function userTopbar(RequestStack $request): ItemInterface
{
$menu = $this->factory->createItem('root');
$this->dispatcher->dispatch(
UserMenuEvent::EVENT,
new UserMenuEvent($this->factory, $menu, $request)
);
return $menu;
}
}

View File

@ -0,0 +1,54 @@
<?php
// src/Subscriber/UserMenuSubscriber.php
namespace App\Subscriber;
use App\Entity\User;
use App\Event\Menu\Topbar\UserMenuEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Translation\TranslatorInterface;
class UserMenuSubscriber implements EventSubscriberInterface
{
/**
* @var TokenStorageInterface
*/
private $tokenStorage;
/**
* @var TranslatorInterface
*/
private $translator;
public function __construct(TokenStorageInterface $tokenStorage, TranslatorInterface $translator)
{
$this->tokenStorage = $tokenStorage;
$this->translator = $translator;
}
public function onUserMenuConfigure(UserMenuEvent $event)
{
$menu = $event->getItem();
/** @var User $user */
$user = $this->tokenStorage->getToken()->getUser();
$dropdown = $menu->addChild(
$this->translator->trans('Hello %username%', ['%username%' => 'Noname'], 'usermenu'),
['dropdown' => true]
);
$dropdown->addChild(
$this->translator->trans('Login', [], 'usermenu'),
['route' => 'user_login']
);
}
public static function getSubscribedEvents(): array
{
return [
UserMenuEvent::EVENT => 'onUserMenuConfigure',
];
}
}

View File

@ -83,6 +83,12 @@
"jdorn/sql-formatter": {
"version": "v1.2.17"
},
"knplabs/knp-menu": {
"version": "2.3.0"
},
"knplabs/knp-menu-bundle": {
"version": "v2.2.1"
},
"monolog/monolog": {
"version": "1.23.0"
},

View File

@ -1,9 +1,10 @@
{% extends "base.html.twig" %}
{% block body %}
{% include 'frames/structure/navbar.html.twig' with menu_items %}
{% include 'topbar.html.twig' %}
<br />
<div class="container">
{% block content %}
{% endblock %}
</div>
{% endblock %}
{% endblock %}

View File

@ -0,0 +1,3 @@
{# templates/topbar.htm.twig #}
{{ knp_menu_render('userTopbar') }}

2
composer.json Normal file
View File

@ -0,0 +1,2 @@
{
}