Skip to content

Commit

Permalink
Global Configuration Append & PHP8 Compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
cesurapp committed Feb 7, 2021
1 parent 31820fd commit eeb950e
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 30 deletions.
2 changes: 1 addition & 1 deletion Builder/ItemProcess.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ protected function recursiveProcess(ItemInterface $menu, $options): bool

// Sort Item
usort($childs, static function ($a, $b) {
return $a->getOrder() > $b->getOrder();
return $a->getOrder() > $b->getOrder() ? 1 : ($a->getOrder() === $b->getOrder() ? 0 : -1);
});

// Set Childs
Expand Down
36 changes: 36 additions & 0 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

/**
* This file is part of the pd-admin pd-user package.
*
* @package pd-user
* @license LICENSE
* @author Ramazan APAYDIN <[email protected]>
* @link https://github.com/appaydin/pd-user
*/

namespace Pd\MenuBundle\DependencyInjection;

use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;

class Configuration implements ConfigurationInterface
{
public function getConfigTreeBuilder()
{
$treeBuilder = new TreeBuilder('pd_menu');
$rootNode = $treeBuilder->getRootNode();

// Set Configuration
$rootNode
->children()
->scalarNode('template')->defaultValue('@PdMenu/Default/menu.html.twig')->end()
->scalarNode('depth')->defaultValue(null)->end()
->scalarNode('currentClass')->defaultValue('active')->end()
->scalarNode('trans_domain')->defaultValue(null)->end()
->scalarNode('iconTemplate')->defaultValue('<i class="material-icons">&text</i>')->end()
->end();

return $treeBuilder;
}
}
7 changes: 7 additions & 0 deletions DependencyInjection/PdMenuExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ class PdMenuExtension extends Extension
*/
public function load(array $configs, ContainerBuilder $container)
{
// Load Configuration
$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);

// Set Configuration
$container->setParameter('pd_menu', $config);

// Load Services
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('services.yml');
Expand Down
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Open a command console, enter your project directory and execute the
following command to download the latest stable version of this bundle:

```console
$ composer require appaydin/pd-menu
composer require appaydin/pd-menu
```

This command requires you to have Composer installed globally, as explained
Expand Down Expand Up @@ -202,6 +202,19 @@ You can change the default options.
}) }}
```

You can change the default options globally.

```twig
config/packages/pd_menu.yaml
pd_menu:
template: '@PdMenu/Default/menu.html.twig',
depth: null,
currentClass: active
trans_domain: admin,
iconTemplate: '<i class="fa&text"></i>'
```

Create Menu Event & Event Listener
---
#### Step 1: Create Menu Event
Expand Down
7 changes: 4 additions & 3 deletions Resources/views/Default/menu.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@

{# Create Menu Item #}
{% block menu_item %}
<li {{ arrayToAttr(menu.listAttr, [], options) }}>
{% set dropdown = menu.child|length > 0 %}
<li {{ arrayToAttr(menu.listAttr, {'class': dropdown ? 'dropdown' : ''}, options) }}>
{# Create Link #}
{% if menu.link %}
<a href="{{ menu.link }}" {{ arrayToAttr(menu.linkAttr, [], options) }}>
{% if menu.extra('label_icon') %}
{{ options.iconTemplate|replace({ 'itext': menu.extra('label_icon') })|raw }}
{{ options.iconTemplate|replace({ '&text': menu.extra('label_icon') })|raw }}
{% endif %}
<span {{ arrayToAttr(menu.labelAttr, [], options) }}>
{{ menu.extra('label_translate', true) ? menu.label|trans([], options.trans_domain)|raw : menu.label|raw }}
Expand All @@ -27,7 +28,7 @@
{{ menu.linkAfterHtml|raw }}
{% else %}
{% if menu.extra('label_icon') %}
{{ options.iconTemplate|replace({ 'itext': menu.extra('label_icon') })|raw }}
{{ options.iconTemplate|replace({ '&text': menu.extra('label_icon') })|raw }}
{% endif %}
<span {{ arrayToAttr(menu.labelAttr, [], options) }}>
{{ menu.extra('label_translate', true) ? menu.label|trans([], options.trans_domain)|raw : menu.label|raw }}
Expand Down
43 changes: 19 additions & 24 deletions Twig/MenuExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Pd\MenuBundle\Builder\MenuInterface;
use Pd\MenuBundle\Render\RenderInterface;
use Psr\Container\ContainerInterface;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;
Expand All @@ -31,35 +32,28 @@ class MenuExtension extends AbstractExtension
* @var RenderInterface
*/
private $engine;

/**
* @var ItemProcessInterface
*/
private $itemProcess;

/**
* @var TranslatorInterface
*/
private $translator;

/**
* Default Menu Options.
*
* @var array
* @var ParameterBagInterface
*/
private $defaultOptions = [
'template' => '@PdMenu/Default/menu.html.twig',
'depth' => null,
'currentClass' => 'active',
'trans_domain' => null,
'iconTemplate' => '<i class="material-icons">itext</i>',
];

public function __construct(RenderInterface $engine, ItemProcessInterface $itemProcess, TranslatorInterface $translator)
private ParameterBagInterface $parameterBag;

public function __construct(RenderInterface $engine, ItemProcessInterface $itemProcess,
TranslatorInterface $translator,
ParameterBagInterface $parameterBag
)
{
$this->engine = $engine;
$this->itemProcess = $itemProcess;
$this->translator = $translator;
$this->parameterBag = $parameterBag;
}

/**
Expand All @@ -82,12 +76,12 @@ public function getFunctions()
* @param string $menuClass
* @param array $options
*
* @return string
* @return string|null
*/
public function renderMenu(string $menuClass = '', $options = []): string
public function renderMenu(string $menuClass = '', $options = []): ?string
{
// Merge Options
$options = array_merge($this->defaultOptions, $options);
$options = array_merge($this->parameterBag->get('pd_menu'), $options);

// Get Menu
$menu = new $menuClass();
Expand All @@ -100,7 +94,7 @@ public function renderMenu(string $menuClass = '', $options = []): string
return $this->engine->render($menu, $options);
}

return false;
return null;
}

/**
Expand All @@ -109,12 +103,12 @@ public function renderMenu(string $menuClass = '', $options = []): string
* @param string $menuClass
* @param array $options
*
* @return ItemInterface|bool
* @return ItemInterface|null
*/
public function getMenu(string $menuClass, $options = [])
public function getMenu(string $menuClass, $options = []): ?ItemInterface
{
// Merge Options
$options = array_merge($this->defaultOptions, $options);
$options = array_merge($this->parameterBag->get('pd_menu'), $options);

// Get Menu
$menu = new $menuClass();
Expand All @@ -124,7 +118,7 @@ public function getMenu(string $menuClass, $options = [])
return $this->itemProcess->processMenu($menu->createMenu($options), $options);
}

return false;
return null;
}

/**
Expand All @@ -148,7 +142,8 @@ public function arrayToAttr(array $array = [], array $append = [], array $option

if ('title' === mb_strtolower($key)) {
if (!isset($array['title_translate'])) {
$value = $this->translator->trans($value, [], $options['trans_domain'] ?? null);
$value = $this->translator->trans($value, [],
$options['trans_domain'] ?? $this->parameterBag->get('pd_menu')['trans_domain']);
}
}

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
}
],
"require": {
"php": "^7.3.0",
"php": "^7.4.0",
"twig/twig": "*",
"symfony/event-dispatcher": "*"
},
Expand Down

0 comments on commit eeb950e

Please sign in to comment.