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
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Moved most of the logic to a Factory extension
Refs commit: KnpLabs/KnpMenu@5056422 Again, this allows us to have less duplication.
- Loading branch information
Showing
6 changed files
with
134 additions
and
148 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
<?php | ||
|
||
namespace Symfony\Cmf\Bundle\MenuBundle\Extension; | ||
|
||
use Knp\Menu\Factory\ExtensionInterface; | ||
use Knp\Menu\ItemInterface; | ||
use Symfony\Component\Routing\Exception\RouteNotFoundException; | ||
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; | ||
|
||
/** | ||
* An extension of the MenuFactory to generate URLs from the Content object. | ||
* | ||
* It has to be registered with a priority higher than the CoreExtension and | ||
* RoutingExtension provided by the KnpMenuBundle. | ||
* | ||
* @author Wouter J <wouter@wouterj.nl> | ||
*/ | ||
class ContentExtension implements ExtensionInterface | ||
{ | ||
/** | ||
* @var UrlGeneratorInterface | ||
*/ | ||
private $contentRouter; | ||
|
||
/** | ||
* @param UrlGeneratorInterface $contentRouter A router to generate URLs based on the content object | ||
*/ | ||
public function __construct(UrlGeneratorInterface $contentRouter) | ||
{ | ||
$this->contentRouter = $contentRouter; | ||
} | ||
|
||
/** | ||
* Builds the full option array used to configure the item. | ||
* | ||
* @param array $options The options processed by the previous extensions | ||
* | ||
* @return array | ||
*/ | ||
public function buildOptions(array $options) | ||
{ | ||
$options = array_merge(array( | ||
'content' => null, | ||
'linkType' => null, | ||
'extras' => array(), | ||
), $options); | ||
|
||
if (null === $options['linkType']) { | ||
$options['linkType'] = $this->determineLinkType($options); | ||
} | ||
|
||
$this->validateLinkType($options['linkType']); | ||
|
||
if ('content' === $options['linkType']) { | ||
$options['uri'] = $this->contentRouter->generate( | ||
$options['content'], | ||
$options['routeParameters'], | ||
$options['routeAbsolute'] | ||
); | ||
} | ||
|
||
if (isset($options['route']) && 'route' !== $options['linkType']) { | ||
unset($options['route']); | ||
} | ||
|
||
$options['extras']['content'] = $options['content']; | ||
|
||
return $options; | ||
} | ||
|
||
/** | ||
* Configures the item with the passed options | ||
* | ||
* @param ItemInterface $item | ||
* @param array $options | ||
*/ | ||
public function buildItem(ItemInterface $item, array $options) | ||
{ | ||
} | ||
|
||
/** | ||
* If linkType not specified, we can determine it from | ||
* existing options | ||
*/ | ||
protected function determineLinkType($options) | ||
{ | ||
if (!empty($options['uri'])) { | ||
return 'uri'; | ||
} | ||
|
||
if (!empty($options['route'])) { | ||
return 'route'; | ||
} | ||
|
||
if (!empty($options['content'])) { | ||
return 'content'; | ||
} | ||
|
||
return 'uri'; | ||
} | ||
|
||
/** | ||
* Ensure that we have a valid link type. | ||
* | ||
* @param string $linkType | ||
* | ||
* @throws \InvalidArgumentException if $linkType is not one of the known | ||
* link types | ||
*/ | ||
protected function validateLinkType($linkType) | ||
{ | ||
$linkTypes = array('uri', 'route', 'content'); | ||
if (!in_array($linkType, $linkTypes)) { | ||
throw new \InvalidArgumentException(sprintf( | ||
'Invalid link type "%s", expected: "%s"', | ||
$linkType, | ||
implode(',', $linkTypes) | ||
)); | ||
} | ||
} | ||
} |
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