This repository has been archived by the owner on Sep 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 48
[POC] Provide a faster menu helper #241
Closed
Closed
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
2f48bfb
Very early sketch of a faster menu helper
wouterj a1130b8
Make code working
wouterj 46e1479
Register as services
wouterj ab949ab
Fix tests
wouterj 5f8c272
CI fixes
wouterj 6b0d289
Add content menu referrer support
wouterj 9065ce3
Fixed some bugs
wouterj File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,179 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony CMF package. | ||
* | ||
* (c) 2011-2015 Symfony CMF | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Cmf\Bundle\MenuBundle\Templating; | ||
|
||
use Knp\Menu\ItemInterface; | ||
use Knp\Menu\NodeInterface; | ||
use Knp\Menu\FactoryInterface; | ||
use Doctrine\Common\Persistence\ManagerRegistry; | ||
use Doctrine\Common\Collections\ArrayCollection; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\Templating\Helper\Helper; | ||
use Symfony\Cmf\Bundle\RoutingBundle\Routing\DynamicRouter; | ||
use Symfony\Cmf\Bundle\MenuBundle\Model\MenuNode; | ||
use Symfony\Cmf\Bundle\MenuBundle\Model\MenuNodeReferrersInterface; | ||
|
||
/** | ||
* A templating helper providing faster solutions to | ||
* the KnpMenu alternatives. | ||
* | ||
* @author Wouter de Jong <[email protected]> | ||
*/ | ||
class MenuHelper extends Helper | ||
{ | ||
/** | ||
* @var ManagerRegistry | ||
*/ | ||
private $managerRegistry; | ||
|
||
/** | ||
* @var FactoryInterface | ||
*/ | ||
private $menuFactory; | ||
private $managerName; | ||
private $contentObjectKey; | ||
private $routeNameKey; | ||
|
||
/** | ||
* @param ManagerRegistry $managerRegistry | ||
* @param FactoryInterface $menuFactory | ||
* @param string $contentObjectKey The name of the request attribute holding | ||
* the current content object | ||
* @param string $routeNameKey The name of the request attribute holding | ||
* the name of the current route | ||
*/ | ||
public function __construct(ManagerRegistry $managerRegistry, FactoryInterface $menuFactory, $contentObjectKey = DynamicRouter::CONTENT_KEY, $routeNameKey = DynamicRouter::ROUTE_KEY) | ||
{ | ||
$this->managerRegistry = $managerRegistry; | ||
$this->menuFactory = $menuFactory; | ||
$this->contentObjectKey = $contentObjectKey; | ||
$this->routeNameKey = $routeNameKey; | ||
} | ||
|
||
/** | ||
* Set the object manager name to use for this loader. If not set, the | ||
* default manager as decided by the manager registry will be used. | ||
* | ||
* @param string|null $managerName | ||
*/ | ||
public function setManagerName($managerName) | ||
{ | ||
$this->managerName = $managerName; | ||
} | ||
|
||
/** | ||
* Generates an array of breadcrumb items by traversing | ||
* up the tree from the current node. | ||
* | ||
* @param NodeInterface $node The current menu node (use {@link getCurrentNode} to get it) | ||
* @param bool $includeMenuRoot Whether to include the menu root as breadcrumb item | ||
* | ||
* @return array An array with breadcrumb items (each item has the following keys: label, uri, item) | ||
*/ | ||
public function getBreadcrumbsArray(NodeInterface $node, $includeMenuRoot = true) | ||
{ | ||
$item = $this->menuFactory->createItem($node->getName(), $node->getOptions()); | ||
|
||
$breadcrumbs = array( | ||
array( | ||
'label' => $item->getLabel(), | ||
'uri' => $item->getUri(), | ||
'item' => $item, | ||
), | ||
); | ||
|
||
$parent = $node->getParentObject(); | ||
if (!$parent instanceof MenuNode) { | ||
// We assume the root of the menu is reached | ||
return $includeMenuRoot ? $breadcrumbs : array(); | ||
} | ||
|
||
return array_merge($this->getBreadcrumbsArray($parent, $includeMenuRoot), $breadcrumbs); | ||
} | ||
|
||
/** | ||
* Tries to find the current item from the request. | ||
* | ||
* The returned item does *not* include the parent and children, | ||
* in order to minimalize the overhead. | ||
* | ||
* @param Request $request | ||
* | ||
* @return ItemInterface|null | ||
*/ | ||
public function getCurrentItem(Request $request) | ||
{ | ||
$node = $this->getCurrentNode($request); | ||
|
||
if (!$node instanceof NodeInterface) { | ||
return; | ||
} | ||
|
||
return $this->menuFactory->createItem($node->getName(), $node->getOptions()); | ||
} | ||
|
||
/** | ||
* Retrieves the current node based on a Request. | ||
* | ||
* It uses some special Request attributes that are managed by | ||
* the CmfRoutingBundle: | ||
* | ||
* * 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 | ||
*/ | ||
public function getCurrentNode(Request $request) | ||
{ | ||
$repository = $this->managerRegistry->getManager($this->managerName) | ||
->getRepository('Symfony\Cmf\Bundle\MenuBundle\Doctrine\Phpcr\MenuNode'); | ||
|
||
if ($request->attributes->has($this->contentObjectKey)) { | ||
$content = $request->attributes->get($this->contentObjectKey); | ||
|
||
if ($content instanceof MenuNodeReferrersInterface) { | ||
$node = $this->filterByLinkType(new ArrayCollection($content->getMenuNodes()), 'content'); | ||
|
||
if ($node) { | ||
return $node; | ||
} | ||
} | ||
} | ||
|
||
if ($request->attributes->has($this->routeNameKey)) { | ||
$route = $request->attributes->get($this->routeNameKey); | ||
|
||
return $this->filterByLinkType($repository->findBy(array('route' => $route)), 'route'); | ||
} | ||
} | ||
|
||
private function filterByLinkType(\Traversable $nodes, $type) | ||
{ | ||
if (1 === count($nodes)) { | ||
return $nodes->first(); | ||
} else { | ||
foreach ($nodes as $node) { | ||
if ($type === $node->getLinkType()) { | ||
return $node; | ||
} | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getName() | ||
{ | ||
return 'cmf_menu'; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
missing property declaration