forked from KnpLabs/KnpMenu
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduced factory extension to provide better hooks in the factory
- Loading branch information
Showing
11 changed files
with
323 additions
and
105 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?php | ||
|
||
namespace Knp\Menu\Factory; | ||
|
||
use Knp\Menu\ItemInterface; | ||
|
||
/** | ||
* core factory extension with the main logic | ||
*/ | ||
class CoreExtension implements ExtensionInterface | ||
{ | ||
/** | ||
* Builds the full option array used to configure the item. | ||
* | ||
* @param array $options | ||
* | ||
* @return array | ||
*/ | ||
public function buildOptions(array $options) | ||
{ | ||
return array_merge( | ||
array( | ||
'uri' => null, | ||
'label' => null, | ||
'attributes' => array(), | ||
'linkAttributes' => array(), | ||
'childrenAttributes' => array(), | ||
'labelAttributes' => array(), | ||
'extras' => array(), | ||
'current' => null, | ||
'display' => true, | ||
'displayChildren' => true, | ||
), | ||
$options | ||
); | ||
} | ||
|
||
/** | ||
* Configures the newly created item with the passed options | ||
* | ||
* @param ItemInterface $item | ||
* @param array $options | ||
*/ | ||
public function buildItem(ItemInterface $item, array $options) | ||
{ | ||
$item | ||
->setUri($options['uri']) | ||
->setLabel($options['label']) | ||
->setAttributes($options['attributes']) | ||
->setLinkAttributes($options['linkAttributes']) | ||
->setChildrenAttributes($options['childrenAttributes']) | ||
->setLabelAttributes($options['labelAttributes']) | ||
->setExtras($options['extras']) | ||
->setCurrent($options['current']) | ||
->setDisplay($options['display']) | ||
->setDisplayChildren($options['displayChildren']) | ||
; | ||
} | ||
} |
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,25 @@ | ||
<?php | ||
|
||
namespace Knp\Menu\Factory; | ||
|
||
use Knp\Menu\ItemInterface; | ||
|
||
interface ExtensionInterface | ||
{ | ||
/** | ||
* 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); | ||
|
||
/** | ||
* Configures the item with the passed options | ||
* | ||
* @param ItemInterface $item | ||
* @param array $options | ||
*/ | ||
public function buildItem(ItemInterface $item, array $options); | ||
} |
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,41 @@ | ||
<?php | ||
|
||
namespace Knp\Menu\Silex; | ||
|
||
use Knp\Menu\Factory\ExtensionInterface; | ||
use Knp\Menu\ItemInterface; | ||
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; | ||
|
||
/** | ||
* Factory able to use the Symfony2 Routing component to build the url | ||
*/ | ||
class RoutingExtension implements ExtensionInterface | ||
{ | ||
private $generator; | ||
|
||
public function __construct(UrlGeneratorInterface $generator) | ||
{ | ||
$this->generator = $generator; | ||
} | ||
|
||
public function buildOptions(array $options = array()) | ||
{ | ||
if (!empty($options['route'])) { | ||
$params = isset($options['routeParameters']) ? $options['routeParameters'] : array(); | ||
$absolute = isset($options['routeAbsolute']) ? $options['routeAbsolute'] : false; | ||
$options['uri'] = $this->generator->generate($options['route'], $params, $absolute); | ||
|
||
// adding the item route to the extras under the 'routes' key (for the Silex RouteVoter) | ||
$options['extras']['routes'][] = array( | ||
'route' => $options['route'], | ||
'parameters' => $params, | ||
); | ||
} | ||
|
||
return $options; | ||
} | ||
|
||
public function buildItem(ItemInterface $item, array $options) | ||
{ | ||
} | ||
} |
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
Oops, something went wrong.