Skip to content
This repository has been archived by the owner on Jan 30, 2020. It is now read-only.

Commit

Permalink
Merge branch 'feature/4348' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
weierophinney committed May 22, 2013
151 parents 7aff06a + e6b286e + d5c10b6 + 0eabc04 + fed2583 + af2d845 + e8e4ce2 + f4be937 + 9323faa + 65deb5a + d5a4f17 + 94b36e8 + 62824a2 + 6378cbe + a10281c + cd0ff39 + 02e8122 + a667dd4 + 02ccc35 + 7d61487 + ab2f663 + a27ca10 + 75af0e5 + 24f498b + d2dd157 + 6c759d8 + 51b0320 + aabe1c0 + e76b67c + 5d85a78 + d285b08 + c5df6b0 + ff58ff4 + 5675884 + a55ead5 + 47eb5a8 + a36e34a + 3e520ad + 7b0d374 + 461c28b + 3890e69 + 316384e + 1431343 + debad5a + 1680741 + 4497a00 + bb6633a + 6b7ed03 + 35fcae3 + 0737e9d + c31bf4c + 918649e + ecbe771 + b4124fc + f4e1da0 + a8c70a8 + 50d3c5d + 79180fa + f6f9386 + 8ed6590 + d79a4a4 + 3958052 + eae6146 + 1cc7a46 + 1d9542f + a22d6e2 + 3496b67 + 9398e77 + 687d980 + 900dc7d + 21f6363 + 0eeb6ff + 8c442a3 + adf1f8e + c2ceb18 + 9b17a1c + d5dab4a + 346a62a + 9808633 + bdeec54 + 1a2506a + f5f3d02 + b623064 + 4338bf4 + bdababf + 4054000 + 4449c16 + d3d8b33 + 8427cf8 + 8d162ec + a988e9a + 3e9d597 + ee93c08 + 9d3ce53 + f04a59c + 0c3f2de + a654383 + 502d937 + 93350ca + 23a0ccc + 15dc071 + 6ca79e9 + 7645850 + 7b0843e + b9f38cc + 53d4205 + 0ba91ba + 7ad75c4 + 35376fd + e6713be + b15c871 + 4121ed2 + 905626d + 529973a + e28e80a + b64275a + 2f43075 + 05dea5f + b0f56ea + a0ae2ac + 8b01bd8 + fc7acfc + 87b1ffc + c00475b + 6d05c34 + 2f2787b + 229c668 + c164e00 + e5c1ff6 + 5023ed3 + 3c689b8 + 789b92c + ae927c2 + 7741eac + ab3bdcd + 6c02989 + 0d17da2 + 1035d13 + 361c1cf + b31816b + 0102405 + 84330e4 + 50b28a7 + 196892e + 55ecf0a + cdf2159 + 97a5f0f + 2951d73 + bbfc15c + 2ec87c8 + fc4d8b7 commit 5559cd9
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 3 deletions.
55 changes: 55 additions & 0 deletions src/Page/Uri.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

namespace Zend\Navigation\Page;

use Zend\Http\Request;
use Zend\Navigation\Exception;

/**
Expand All @@ -23,6 +24,13 @@ class Uri extends AbstractPage
*/
protected $uri = null;

/**
* Request object used to determine uri path
*
* @var string
*/
protected $request;

/**
* Sets page URI
*
Expand Down Expand Up @@ -76,6 +84,53 @@ public function getHref()
return $uri;
}

/**
* Returns whether page should be considered active or not
*
* This method will compare the page properties against the request uri.
*
* @param bool $recursive
* [optional] whether page should be considered
* active if any child pages are active. Default is
* false.
* @return bool whether page should be considered active or not
*/
public function isActive($recursive = false)
{
if (!$this->active) {
if ($this->getRequest() instanceof Request) {
if ($this->getRequest()->getUri()->getPath() == $this->getUri()) {
$this->active = true;
return true;
}
}
}

return parent::isActive($recursive);
}

/**
* Get the request
*
* @return Request
*/
public function getRequest()
{
return $this->request;
}

/**
* Sets request for assembling URLs
*
* @param Request $request
* @return Fluent interface, returns self
*/
public function setRequest(Request $request = null)
{
$this->request = $request;
return $this;
}

/**
* Returns an array representation of the page
*
Expand Down
20 changes: 17 additions & 3 deletions src/Service/AbstractNavigationFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
namespace Zend\Navigation\Service;

use Zend\Config;
use Zend\Http\Request;
use Zend\Mvc\Router\RouteMatch;
use Zend\Mvc\Router\RouteStackInterface as Router;
use Zend\Navigation\Exception;
Expand Down Expand Up @@ -79,8 +80,14 @@ protected function preparePages(ServiceLocatorInterface $serviceLocator, $pages)
$application = $serviceLocator->get('Application');
$routeMatch = $application->getMvcEvent()->getRouteMatch();
$router = $application->getMvcEvent()->getRouter();
$request = $application->getMvcEvent()->getRequest();

return $this->injectComponents($pages, $routeMatch, $router);
// HTTP request is the only one that may be injected
if (!$request instanceof Request) {
$request = null;
}

return $this->injectComponents($pages, $routeMatch, $router, $request);
}

/**
Expand Down Expand Up @@ -114,11 +121,13 @@ protected function getPagesFromConfig($config = null)
* @param array $pages
* @param RouteMatch $routeMatch
* @param Router $router
* @param null|Request $request
* @return mixed
*/
protected function injectComponents(array $pages, RouteMatch $routeMatch = null, Router $router = null)
protected function injectComponents(array $pages, RouteMatch $routeMatch = null, Router $router = null, $request = null)
{
foreach ($pages as &$page) {
$hasUri = isset($page['uri']);
$hasMvc = isset($page['action']) || isset($page['controller']) || isset($page['route']);
if ($hasMvc) {
if (!isset($page['routeMatch']) && $routeMatch) {
Expand All @@ -127,12 +136,17 @@ protected function injectComponents(array $pages, RouteMatch $routeMatch = null,
if (!isset($page['router'])) {
$page['router'] = $router;
}
} elseif ($hasUri) {
if (!isset($page['request'])) {
$page['request'] = $request;
}
}

if (isset($page['pages'])) {
$page['pages'] = $this->injectComponents($page['pages'], $routeMatch, $router);
$page['pages'] = $this->injectComponents($page['pages'], $routeMatch, $router, $request);
}
}
return $pages;
}

}
35 changes: 35 additions & 0 deletions test/Page/UriTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

use Zend\Navigation\Page;
use Zend\Navigation;
use Zend\Http\Request;

/**
* Tests the class Zend_Navigation_Page_Uri
Expand Down Expand Up @@ -90,6 +91,40 @@ public function testGetHref()
$this->assertEquals($uri, $page->getHref());
}

public function testIsActiveReturnsTrueWhenHasMatchingRequestUri()
{
$page = new Page\Uri(array(
'label' => 'foo',
'uri' => '/bar'
));

$request = new Request();
$request->setUri('/bar');
$request->setMethod('GET');

$page->setRequest($request);

$this->assertInstanceOf('Zend\Http\Request', $page->getRequest());

$this->assertTrue($page->isActive());
}

public function testIsActiveReturnsFalseOnNonMatchingRequestUri()
{
$page = new Page\Uri(array(
'label' => 'foo',
'uri' => '/bar'
));

$request = new Request();
$request->setUri('/baz');
$request->setMethod('GET');

$page->setRequest($request);

$this->assertFalse($page->isActive());
}

/**
* @group ZF-8922
*/
Expand Down

0 comments on commit 5559cd9

Please sign in to comment.