Skip to content

Commit

Permalink
Merge remote-tracking branch 'juriansluiman/feature/navigation-mvc-ro…
Browse files Browse the repository at this point in the history
…uter2'
  • Loading branch information
akrabat committed Jun 29, 2012
2 parents 8c9c45a + 6d3c8b5 commit 8361e9e
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 112 deletions.
83 changes: 46 additions & 37 deletions library/Zend/Navigation/Page/Mvc.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@

namespace Zend\Navigation\Page;

use Zend\Mvc\Router\RouteMatch,
Zend\Navigation\Exception,
Zend\View\Helper\Url as UrlHelper;
use Zend\Mvc\Router\RouteMatch;
use Zend\Mvc\Router\RouteStackInterface;
use Zend\Navigation\Exception;

/**
* Represents a page that is defined using controller, action, route
Expand Down Expand Up @@ -86,21 +86,21 @@ class Mvc extends AbstractPage
protected $routeMatch;

/**
* View helper for assembling URLs
* Router for assembling URLs
*
* @see getHref()
* @var UrlHelper
* @var RouteStackInterface
*/
protected $urlHelper = null;
protected $router = null;

/**
* Default urlHelper to be used if urlHelper is not given.
* Default router to be used if router is not given.
*
* @see getHref()
*
* @var UrlHelper
* @var RouteStackInterface
*/
protected static $defaultUrlHelper = null;
protected static $defaultRouter= null;

// Accessors:

Expand Down Expand Up @@ -165,28 +165,28 @@ public function isActive($recursive = false)
/**
* Returns href for this page
*
* This method uses {@link UrlHelper} to assemble
* This method uses {@link RouteStackInterface} to assemble
* the href based on the page's properties.
*
* @see UrlHelper
* @see RouteStackInterface
* @return string page href
* @throws Exception\DomainException if no UrlHelper is set
* @throws Exception\DomainException if no router is set
*/
public function getHref()
{
if ($this->hrefCache) {
return $this->hrefCache;
}

$helper = $this->urlHelper;
if (null === $helper) {
$helper = self::$defaultUrlHelper;
$router = $this->router;
if (null === $router) {
$router = self::$defaultRouter;
}

if (!$helper instanceof UrlHelper) {
if (!$router instanceof RouteStackInterface) {
throw new Exception\DomainException(
__METHOD__
. ' cannot execute as no Zend\View\Helper\Url instance is composed'
. ' cannot execute as no Zend\Mvc\Router\RouteStackInterface instance is composed'
);
}

Expand All @@ -200,10 +200,19 @@ public function getHref()
$params['action'] = $param;
}

$url = $helper(
$this->getRoute(),
$params
);
switch (true) {
case ($this->getRoute() !== null):
$name = $this->getRoute();
break;
case ($this->getRouteMatch() !== null):
$name = $this->getRouteMatch()->getMatchedRouteName();
break;
default:
throw new Exception\DomainException('No route name could be found');
}

$options = array('name' => $name);
$url = $router->assemble($params, $options);

// Add the fragment identifier if it is set
$fragment = $this->getFragment();
Expand Down Expand Up @@ -372,49 +381,49 @@ public function setRouteMatch(RouteMatch $matches)
}

/**
* Get the url helper.
* Get the router.
*
* @return null|\Zend\View\Helper\Url
* @return null|RouteStackInterface
*/
public function getUrlHelper()
public function getRouter()
{
return $this->urlHelper;
return $this->router;
}

/**
* Sets action helper for assembling URLs
* Sets router for assembling URLs
*
* @see getHref()
*
* @param UrlHelper $helper URL helper plugin
* @param RouteStackInterface $router Router
* @return Mvc fluent interface, returns self
*/
public function setUrlHelper(UrlHelper $helper)
public function setRouter(RouteStackInterface $router)
{
$this->urlHelper = $helper;
$this->router = $router;
return $this;
}

/**
* Sets the default view helper for assembling URLs.
* Sets the default router for assembling URLs.
*
* @see getHref()
* @param null|UrlHelper $helper URL helper
* @param RouteStackInterface $router Router
* @return void
*/
public static function setDefaultUrlHelper($helper)
public static function setDefaultRouter($router)
{
self::$defaultUrlHelper = $helper;
self::$defaultRouter = $router;
}

/**
* Gets the default view helper for assembling URLs.
* Gets the default router for assembling URLs.
*
* @return UrlHelper
* @return RouteStackInterface
*/
public static function getDefaultUrlHelper()
public static function getDefaultRouter()
{
return self::$defaultUrlHelper;
return self::$defaultRouter;
}

// Public methods:
Expand Down
14 changes: 7 additions & 7 deletions library/Zend/Navigation/Service/AbstractNavigationFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@
use Zend\Navigation\Navigation;
use Zend\Navigation\Page\Mvc as MvcPage;
use Zend\Mvc\Router\RouteMatch;
use Zend\Mvc\Router\RouteStackInterface as Router;
use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
use Zend\View\Helper\Url as UrlHelper;

/**
* Abstract navigation factory
Expand Down Expand Up @@ -82,11 +82,11 @@ protected function getPages(ServiceLocatorInterface $serviceLocator)
}

$application = $serviceLocator->get('Application');
$urlHelper = $serviceLocator->get('ViewHelperManager')->get('url');
$routeMatch = $application->getMvcEvent()->getRouteMatch();
$router = $application->getMvcEvent()->getRouter();
$pages = $this->getPagesFromConfig($configuration['navigation'][$this->getName()]);

$this->pages = $this->injectComponents($pages, $routeMatch, $urlHelper);
$this->pages = $this->injectComponents($pages, $routeMatch, $router);
}
return $this->pages;
}
Expand Down Expand Up @@ -124,21 +124,21 @@ protected function getPagesFromConfig($config = null)
* @param UrlHelper $urlHelper
* @return mixed
*/
protected function injectComponents(array $pages, RouteMatch $routeMatch = null, UrlHelper $urlHelper = null)
protected function injectComponents(array $pages, RouteMatch $routeMatch = null, Router $router = null)
{
foreach($pages as &$page) {
$hasMvc = isset($page['action']) || isset($page['controller']) || isset($page['route']);
if ($hasMvc) {
if (!isset($page['routeMatch']) && $routeMatch) {
$page['routeMatch'] = $routeMatch;
}
if (!isset($page['urlHelper']) && $urlHelper) {
$page['urlHelper'] = $urlHelper;
if (!isset($page['router'])) {
$page['router'] = $router;
}
}

if (isset($page['pages'])) {
$page['pages'] = $this->injectComponents($page['pages'], $routeMatch, $urlHelper);
$page['pages'] = $this->injectComponents($page['pages'], $routeMatch, $router);
}
}
return $pages;
Expand Down
8 changes: 4 additions & 4 deletions tests/Zend/Navigation/NavigationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,13 @@ class NavigationTest extends \PHPUnit_Framework_TestCase
* @var Zend_Navigation
*/
private $_navigation;

protected function setUp()
{
parent::setUp();
$this->_navigation = new \Zend\Navigation\Navigation();
}

protected function tearDown()
{
$this->_navigation = null;
Expand All @@ -55,7 +55,7 @@ protected function tearDown()

/**
* Testing that navigation order is done correctly
*
*
* @group ZF-8337
* @group ZF-8313
*/
Expand All @@ -78,5 +78,5 @@ public function testNavigationArraySortsCorrectly()
$this->assertEquals('page1', $pages[1]['uri']);
$this->assertEquals('page2', $pages[2]['uri']);
}

}
Loading

0 comments on commit 8361e9e

Please sign in to comment.