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

Commit

Permalink
Fixed some bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
wouterj committed Jan 22, 2016
1 parent 6b0d289 commit 9065ce3
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 27 deletions.
8 changes: 4 additions & 4 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@

use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;
use Symfony\Cmf\Component\Routing\RouteObjectInterface;
use Symfony\Cmf\Bundle\RoutingBundle\Routing\DynamicRouter;

class Configuration implements ConfigurationInterface
{
public function getConfigTreeBuilder()
{
$cmfRoutingAvailable = interface_exists('Symfony\Cmf\Component\Routing\RouteObjectInterface');
$cmfRoutingAvailable = class_exists('Symfony\Cmf\Bundle\RoutingBundle\Routing\DynamicRouter');
$treeBuilder = new TreeBuilder();

$treeBuilder->root('cmf_menu')
Expand Down Expand Up @@ -53,8 +53,8 @@ public function getConfigTreeBuilder()

->scalarNode('content_url_generator')->defaultValue('router')->end()
->booleanNode('allow_empty_items')->defaultFalse()->end()
->scalarNode('content_key')->defaultValue($cmfRoutingAvailable ? RouteObjectInterface::CONTENT_OBJECT : '')->end()
->scalarNode('route_name_key')->defaultValue($cmfRoutingAvailable ? RouteObjectInterface::ROUTE_NAME : '')->end()
->scalarNode('content_key')->defaultValue($cmfRoutingAvailable ? DynamicRouter::CONTENT_KEY : '')->end()
->scalarNode('route_name_key')->defaultValue($cmfRoutingAvailable ? DynamicRouter::ROUTE_KEY : '')->end()

->arrayNode('voters')
->children()
Expand Down
18 changes: 11 additions & 7 deletions Templating/MenuHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Templating\Helper\Helper;
use Symfony\Cmf\Component\Routing\RouteObjectInterface;
use Symfony\Cmf\Bundle\RoutingBundle\Routing\DynamicRouter;
use Symfony\Cmf\Bundle\MenuBundle\Model\MenuNode;
use Symfony\Cmf\Bundle\MenuBundle\Model\MenuNodeReferrersInterface;

Expand Down Expand Up @@ -51,7 +51,7 @@ class MenuHelper extends Helper
* @param string $routeNameKey The name of the request attribute holding
* the name of the current route
*/
public function __construct(ManagerRegistry $managerRegistry, FactoryInterface $menuFactory, $contentObjectKey = RouteObjectInterface::CONTENT_OBJECT, $routeNameKey = RouteObjectInterface::ROUTE_NAME)
public function __construct(ManagerRegistry $managerRegistry, FactoryInterface $menuFactory, $contentObjectKey = DynamicRouter::CONTENT_KEY, $routeNameKey = DynamicRouter::ROUTE_KEY)
{
$this->managerRegistry = $managerRegistry;
$this->menuFactory = $menuFactory;
Expand Down Expand Up @@ -79,7 +79,7 @@ public function setManagerName($managerName)
*
* @return array An array with breadcrumb items (each item has the following keys: label, uri, item)
*/
public function getBreadcrumbArray(NodeInterface $node, $includeMenuRoot = true)
public function getBreadcrumbsArray(NodeInterface $node, $includeMenuRoot = true)
{
$item = $this->menuFactory->createItem($node->getName(), $node->getOptions());

Expand All @@ -97,7 +97,7 @@ public function getBreadcrumbArray(NodeInterface $node, $includeMenuRoot = true)
return $includeMenuRoot ? $breadcrumbs : array();
}

return array_merge($this->getBreadcrumbArray($parent, $includeMenuRoot), $breadcrumbs);
return array_merge($this->getBreadcrumbsArray($parent, $includeMenuRoot), $breadcrumbs);
}

/**
Expand Down Expand Up @@ -127,8 +127,8 @@ public function getCurrentItem(Request $request)
* It uses some special Request attributes that are managed by
* the CmfRoutingBundle:
*
* * RouteObjectInterface::CONTENT_OBJECT to match a menu node by the refering content
* * RouteObjectInterface::ROUTE_NAME to match a menu node by the refering route name
* * DynamicRouter::CONTENT_KEY to match a menu node by the refering content
* * DynamicRouter::ROUTE_KEY to match a menu node by the refering route name
*
* @return NodeInterface|null
*/
Expand All @@ -141,7 +141,11 @@ public function getCurrentNode(Request $request)
$content = $request->attributes->get($this->contentObjectKey);

if ($content instanceof MenuNodeReferrersInterface) {
return $this->filterByLinkType(new ArrayCollection($content->getMenuNodes()), 'content');
$node = $this->filterByLinkType(new ArrayCollection($content->getMenuNodes()), 'content');

if ($node) {
return $node;
}
}
}

Expand Down
32 changes: 16 additions & 16 deletions Tests/Functional/Templating/MenuHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

use Symfony\Cmf\Bundle\MenuBundle\Model\MenuNodeReferrersInterface;
use Symfony\Cmf\Bundle\MenuBundle\Templating\MenuHelper;
use Symfony\Cmf\Component\Routing\RouteObjectInterface;
use Symfony\Cmf\Bundle\RoutingBundle\Routing\DynamicRouter;
use Symfony\Cmf\Component\Testing\Functional\BaseTestCase;
use Knp\Menu\NodeInterface;

Expand All @@ -32,13 +32,13 @@ protected function setUp()
}

/**
* @dataProvider provideGetBreadcrumbArrayData
* @dataProvider provideGetBreadcrumbsArrayData
*/
public function testGetBreadcrumbArray($includeMenuRoot)
public function testGetBreadcrumbsArray($includeMenuRoot)
{
$currentNode = $this->db('PHPCR')->getOm()->find(null, '/test/menus/test-menu/item-2/sub-item-2');

$breadcrumbs = $this->helper->getBreadcrumbArray($currentNode, $includeMenuRoot);
$breadcrumbs = $this->helper->getBreadcrumbsArray($currentNode, $includeMenuRoot);

// simplify the returned breadcrumb array
$breadcrumbs = array_map(function ($breadcrumb) {
Expand All @@ -56,7 +56,7 @@ public function testGetBreadcrumbArray($includeMenuRoot)
$this->assertEquals($expectedBreadcrumbs, $breadcrumbs);
}

public function provideGetBreadcrumbArrayData()
public function provideGetBreadcrumbsArrayData()
{
return array('menu root included' => array(true), 'menu route excluded' => array(false));
}
Expand All @@ -67,9 +67,9 @@ public function provideGetBreadcrumbArrayData()
public function testGetCurrentNodeWithRoute($routeName, $nodeName)
{
$attributes = $this->prophesize('Symfony\Component\HttpFoundation\ParameterBag');
$attributes->has(RouteObjectInterface::CONTENT_OBJECT)->willReturn(false);
$attributes->has(RouteObjectInterface::ROUTE_NAME)->willReturn(true);
$attributes->get(RouteObjectInterface::ROUTE_NAME)->willReturn($routeName);
$attributes->has(DynamicRouter::CONTENT_KEY)->willReturn(false);
$attributes->has(DynamicRouter::ROUTE_KEY)->willReturn(true);
$attributes->get(DynamicRouter::ROUTE_KEY)->willReturn($routeName);

$request = $this->prophesize('Symfony\Component\HttpFoundation\Request');
$request->attributes = $attributes->reveal();
Expand All @@ -93,9 +93,9 @@ public function testGetCurrentNodeWithContent()
$content->addMenuNode($this->db('PHPCR')->getOm()->find(null, '/test/menus/test-menu/item-1'));

$attributes = $this->prophesize('Symfony\Component\HttpFoundation\ParameterBag');
$attributes->has(RouteObjectInterface::CONTENT_OBJECT)->willReturn(true);
$attributes->has(RouteObjectInterface::ROUTE_NAME)->willReturn(true);
$attributes->get(RouteObjectInterface::CONTENT_OBJECT)->willReturn($content);
$attributes->has(DynamicRouter::CONTENT_KEY)->willReturn(true);
$attributes->has(DynamicRouter::ROUTE_KEY)->willReturn(true);
$attributes->get(DynamicRouter::CONTENT_KEY)->willReturn($content);

$request = $this->prophesize('Symfony\Component\HttpFoundation\Request');
$request->attributes = $attributes->reveal();
Expand All @@ -108,8 +108,8 @@ public function testGetCurrentNodeWithContent()
public function testGetCurrentNodeWithoutMatch()
{
$attributes = $this->prophesize('Symfony\Component\HttpFoundation\ParameterBag');
$attributes->has(RouteObjectInterface::CONTENT_OBJECT)->willReturn(false);
$attributes->has(RouteObjectInterface::ROUTE_NAME)->willReturn(false);
$attributes->has(DynamicRouter::CONTENT_KEY)->willReturn(false);
$attributes->has(DynamicRouter::ROUTE_KEY)->willReturn(false);

$request = $this->prophesize('Symfony\Component\HttpFoundation\Request');
$request->attributes = $attributes->reveal();
Expand All @@ -120,9 +120,9 @@ public function testGetCurrentNodeWithoutMatch()
public function testGetCurrentItemWithMatch()
{
$attributes = $this->prophesize('Symfony\Component\HttpFoundation\ParameterBag');
$attributes->has(RouteObjectInterface::CONTENT_OBJECT)->willReturn(false);
$attributes->has(RouteObjectInterface::ROUTE_NAME)->willReturn(true);
$attributes->get(RouteObjectInterface::ROUTE_NAME)->willReturn('link_test_route_with_params');
$attributes->has(DynamicRouter::CONTENT_KEY)->willReturn(false);
$attributes->has(DynamicRouter::ROUTE_KEY)->willReturn(true);
$attributes->get(DynamicRouter::ROUTE_KEY)->willReturn('link_test_route_with_params');

$request = $this->prophesize('Symfony\Component\HttpFoundation\Request');
$request->attributes = $attributes->reveal();
Expand Down
1 change: 1 addition & 0 deletions Twig/MenuExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public function getFunctions()
return array(
new \Twig_SimpleFunction('cmf_menu_get_breadcrumbs_array', array($this->helper, 'getBreadcrumbsArray')),
new \Twig_SimpleFunction('cmf_menu_get_current_item', array($this->helper, 'getCurrentItem')),
new \Twig_SimpleFunction('cmf_menu_get_current_node', array($this->helper, 'getCurrentNode')),
);
}

Expand Down

0 comments on commit 9065ce3

Please sign in to comment.