Skip to content
This repository has been archived by the owner on Sep 16, 2021. It is now read-only.

Commit

Permalink
Use KnpMenu's voters and removed BC voter mechanism
Browse files Browse the repository at this point in the history
  • Loading branch information
wouterj committed Nov 1, 2014
1 parent 71a7011 commit 1935eb0
Show file tree
Hide file tree
Showing 10 changed files with 14 additions and 224 deletions.
2 changes: 1 addition & 1 deletion CmfMenuBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class CmfMenuBundle extends Bundle
public function build(ContainerBuilder $container)
{
parent::build($container);
$container->addCompilerPass(new AddVotersPass());

if (class_exists('Doctrine\Bundle\PHPCRBundle\DependencyInjection\Compiler\DoctrinePhpcrMappingsPass')) {
$container->addCompilerPass(
DoctrinePhpcrMappingsPass::createXmlMappingDriver(
Expand Down
124 changes: 1 addition & 123 deletions ContentAwareFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,31 +44,6 @@ class ContentAwareFactory extends MenuFactory
*/
protected $contentRouter;

/**
* Valid link types values, e.g. route, uri, content
*/
protected $linkTypes = array();

/**
* List of priority => array of VoterInterface
*
* @var array
*/
private $voters = array();

/**
* @var LoggerInterface
*/
private $logger;

/**
* Whether to return null or a MenuItem without any URL if no URL can be
* found for a MenuNode.
*
* @var boolean
*/
private $allowEmptyItems;

/**
* @param UrlGeneratorInterface $generator for the parent class
* @param UrlGeneratorInterface $contentRouter to generate routes when
Expand All @@ -77,51 +52,10 @@ class ContentAwareFactory extends MenuFactory
* @param LoggerInterface $logger
*/
public function __construct(
UrlGeneratorInterface $generator,
UrlGeneratorInterface $contentRouter,
EventDispatcherInterface $dispatcher,
LoggerInterface $logger
UrlGeneratorInterface $contentRouter
)
{
$this->generator = $generator;
$this->contentRouter = $contentRouter;
$this->linkTypes = array('route', 'uri', 'content');
$this->dispatcher = $dispatcher;
$this->logger = $logger;
}

/**
* Return the linkTypes handled by this factory.
* e.g. array('uri', 'route', 'content').
*
* @return array
*/
public function getLinkTypes()
{
return $this->linkTypes;
}

/**
* Add a voter to decide on current item.
*
* @param VoterInterface $voter
* @param int $priority High numbers can vote first
*
* @see VoterInterface
*/
public function addCurrentItemVoter(VoterInterface $voter)
{
$this->voters[] = $voter;
}

/**
* Get the ordered list of all menu item voters.
*
* @return VoterInterface[]
*/
private function getVoters()
{
return $this->voters;
}

/**
Expand Down Expand Up @@ -181,60 +115,4 @@ public function addChildrenFromNode($nodes, ItemInterface $item)

return $item;
}

/**
* Create a MenuItem. This triggers the voters to decide if its the current
* item.
*
* You can add custom link types by overwriting this method and calling the
* parent - setting the URI option and the linkType to "uri".
*
* @param string $name the menu item name
* @param array $options options for the menu item, we care about
* 'content'
*
* @return MenuItem|null Returns null if no route can be built for this menu item,
*
* @throws \RuntimeException If the stored link type is not known.
*/
public function createItem($name, array $options = array())
{

$current = $this->isCurrentItem($item);

if ($current) {
$item->setCurrent(true);
}

return $item;
}

/**
* Cycle through all voters. If any votes true, this is the current item. If
* any votes false cycling stops. Continue cycling while we get null.
*
* @param ItemInterface $item the newly created menu item
*
* @return bool
*
* @see VoterInterface
*/
private function isCurrentItem(ItemInterface $item)
{
foreach ($this->getVoters() as $voter) {
try {
$vote = $voter->matchItem($item);
if (null ===$vote) {
continue;
}

return $vote;
} catch (\Exception $e) {
// ignore
$this->logger->error(sprintf('Current item voter failed with: "%s"', $e->getMessage()));
}
}

return false;
}
}
44 changes: 0 additions & 44 deletions DependencyInjection/Compiler/AddVotersPass.php

This file was deleted.

2 changes: 0 additions & 2 deletions Provider/PhpcrMenuProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,6 @@ public function get($name, array $options = array())
throw new \InvalidArgumentException("Menu at '$name' is misconfigured (f.e. the route might be incorrect) and could therefore not be instanciated");
}

$menuItem->setCurrentUri($this->request->getRequestUri());

return $menuItem;
}

Expand Down
7 changes: 3 additions & 4 deletions Resources/config/voters.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,13 @@
<services>

<service id="cmf_menu.current_item_voter.uri_prefix" class="%cmf_menu.current_item_voter.uri_prefix.class%">
<call method="setRequest"><argument type="service" id="request" on-invalid="null" strict="false"/></call>
<tag name="cmf_menu.voter"/>
<tag name="knp_menu.voter" request="true"/>
</service>

<service id="cmf_menu.current_item_voter.content_identity" class="%cmf_menu.current_item_voter.content_identity.class%">
<argument>%cmf_menu.content_key%</argument>
<call method="setRequest"><argument type="service" id="request" on-invalid="null" strict="false"/></call>
<tag name="cmf_menu.voter"/>

<tag name="knp_menu.voter" request="true"/>
</service>

</services>
Expand Down
4 changes: 2 additions & 2 deletions Tests/Resources/app/config/test-services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
class="Symfony\Cmf\Bundle\MenuBundle\Voter\RequestParentContentIdentityVoter">
<argument>contentDocument</argument>
<argument>Symfony\Cmf\Bundle\MenuBundle\Tests\Resources\Document\Post</argument>
<call method="setRequest"><argument type="service" id="request" on-invalid="null" strict="false"/></call>
<tag name="cmf_menu.voter"/>

<tag name="knp_menu.voter" request="true"/>
</service>


Expand Down
1 change: 1 addition & 0 deletions Voter/RequestContentIdentityVoter.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Cmf\Bundle\MenuBundle\Voter;

use Knp\Menu\ItemInterface;
use Knp\Menu\Matcher\Voter\VoterInterface;
use Symfony\Component\HttpFoundation\Request;

/**
Expand Down
5 changes: 3 additions & 2 deletions Voter/RequestParentContentIdentityVoter.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Cmf\Bundle\MenuBundle\Voter;

use Knp\Menu\ItemInterface;
use Knp\Menu\Matcher\Voter\VoterInterface;
use Symfony\Component\HttpFoundation\Request;

/**
Expand All @@ -20,7 +21,7 @@
* *getParentDocument()* is identical to the content item in the menu items extras.
*
* Note that there is no check, you have to make sure the $childClass does
* indeed have a getParent method.
* indeed have a getParentDocument method.
*
* @author David Buchmann <mail@davidbu.ch>
*/
Expand All @@ -46,7 +47,7 @@ class RequestParentContentIdentityVoter implements VoterInterface
* attributes
* @param string $childClass Fully qualified class name of the model class
* the content in the request must have to
* attempt calling getParent on it.
* attempt calling getParentDocument on it.
*/
public function __construct($requestKey, $childClass)
{
Expand Down
6 changes: 3 additions & 3 deletions Voter/UriPrefixVoter.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@

namespace Symfony\Cmf\Bundle\MenuBundle\Voter;

use Knp\Menu\ItemInterface;
use Knp\Menu\Matcher\Voter\VoterInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Route;

use Knp\Menu\ItemInterface;

/**
* This voter checks if the content entry in the menu item extras is a Symfony
* Route instance and if so compares its option "currentUriPrefix" with the
Expand Down Expand Up @@ -45,7 +45,7 @@ public function setRequest(Request $request = null)
*/
public function matchItem(ItemInterface $item)
{
if (! $this->request) {
if (!$this->request) {
return null;
}

Expand Down
43 changes: 0 additions & 43 deletions Voter/VoterInterface.php

This file was deleted.

0 comments on commit 1935eb0

Please sign in to comment.