Skip to content

Instantly share code, notes, and snippets.

@kbond
Last active January 1, 2016 20:39
Show Gist options
  • Save kbond/8198512 to your computer and use it in GitHub Desktop.
Save kbond/8198512 to your computer and use it in GitHub Desktop.
ExpressionExtension for KnpMenu/KnpMenuBundle
<?php
namespace MyApp\Menu;
use Knp\Menu\Factory\ExtensionInterface;
use Knp\Menu\ItemInterface;
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
use Symfony\Component\Security\Core\Authentication\AuthenticationTrustResolverInterface;
use Symfony\Component\Security\Core\Role\RoleHierarchyInterface;
use Symfony\Component\Security\Core\SecurityContextInterface;
/**
* @author Kevin Bond <kevinbond@gmail.com>
*/
class ExpressionExtension implements ExtensionInterface
{
private $expressionLanguage;
private $securityContext;
private $roleHierarchy;
private $trustResolver;
private $variables;
public function __construct(
ExpressionLanguage $expressionLanguage,
SecurityContextInterface $securityContext,
RoleHierarchyInterface $roleHierarchy,
AuthenticationTrustResolverInterface $trustResolver
)
{
$this->expressionLanguage = $expressionLanguage;
$this->securityContext = $securityContext;
$this->roleHierarchy = $roleHierarchy;
$this->trustResolver = $trustResolver;
}
/**
* {@inheritDoc}
*/
public function buildOptions(array $options)
{
$variables = $this->getVariables();
if (!empty($options['display']) && !is_bool($options['display'])) {
$options['display'] = (bool) $this->expressionLanguage->evaluate($options['display'], $variables);
}
if (!empty($options['displayChildren']) && !is_bool($options['displayChildren'])) {
$options['displayChildren'] = (bool) $this->expressionLanguage->evaluate($options['displayChildren'], $variables);
}
if (!empty($options['extras']['expression_translation_params'])) {
foreach ($options['extras']['expression_translation_params'] as $key => $value) {
$options['extras']['translation_params'][$key] = $this->expressionLanguage->evaluate($value, $variables);
}
}
return $options;
}
/**
* {@inheritDoc}
*/
public function buildItem(ItemInterface $item, array $options)
{
}
// code should be sync with Sensio\Bundle\FrameworkExtraBundle\EventListener\SecurityListener::getVariables
private function getVariables()
{
if (is_array($this->variables)) {
return $this->variables;
}
$token = $this->securityContext->getToken();
$roles = $this->roleHierarchy->getReachableRoles($token->getRoles());
return $this->variables = array(
'token' => $token,
'user' => $token->getUser(),
'object' => null,
'roles' => array_map(function ($role) { return $role->getRole(); }, $roles),
'trust_resolver' => $this->trustResolver,
'security_context' => $this->securityContext,
);
}
}
myapp.menu.factory_extension.expression:
class: Hammond\Website\Menu\ExpressionExtension
arguments:
- @sensio_framework_extra.security.expression_language
- @security.context
- @security.role_hierarchy
- @security.authentication.trust_resolver
public: false
tags:
- { name: knp_menu.factory_extension, priority: -20 }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment