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

Commit

Permalink
Merge remote-tracking branch 'SpiffyJr/feature/navigation-service-fac…
Browse files Browse the repository at this point in the history
…tory'
  • Loading branch information
Show file tree
Hide file tree
Showing 12 changed files with 419 additions and 20 deletions.
20 changes: 10 additions & 10 deletions src/Container.php → src/AbstractContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@
/**
* Zend_Navigation_Container
*
* Container class for Zend\Navigation\Page classes.
* AbstractContainer class for Zend\Navigation\Page classes.
*
* @category Zend
* @package Zend_Navigation
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
abstract class Container implements RecursiveIterator, Countable
abstract class AbstractContainer implements RecursiveIterator, Countable
{
/**
* Contains sub pages
Expand Down Expand Up @@ -109,7 +109,7 @@ public function notifyOrderUpdated()
* calling {@link Page\AbstractPage::setParent()}.
*
* @param Page\AbstractPage|array|Traversable $page page to add
* @return Container fluent interface, returns self
* @return AbstractContainer fluent interface, returns self
* @throws Exception\InvalidArgumentException if page is invalid
*/
public function addPage($page)
Expand Down Expand Up @@ -151,26 +151,26 @@ public function addPage($page)
/**
* Adds several pages at once
*
* @param array|Traversable|Container $pages pages to add
* @return Container fluent interface, returns self
* @param array|Traversable|AbstractContainer $pages pages to add
* @return AbstractContainer fluent interface, returns self
* @throws Exception\InvalidArgumentException if $pages is not array,
* Traversable or Container
* Traversable or AbstractContainer
*/
public function addPages($pages)
{
if (!is_array($pages) && !$pages instanceof Traversable) {
throw new Exception\InvalidArgumentException(
'Invalid argument: $pages must be an array, an '
. 'instance of Traversable or an instance of '
. 'Zend\Navigation\Container'
. 'Zend\Navigation\AbstractContainer'
);
}

// Because adding a page to a container removes it from the original
// (see {@link Page\AbstractPage::setParent()}), iteration of the
// original container will break. As such, we need to iterate the
// container into an array first.
if ($pages instanceof Container) {
if ($pages instanceof AbstractContainer) {
$pages = iterator_to_array($pages);
}

Expand All @@ -185,7 +185,7 @@ public function addPages($pages)
* Sets pages this container should have, removing existing pages
*
* @param array $pages pages to set
* @return Container fluent interface, returns self
* @return AbstractContainer fluent interface, returns self
*/
public function setPages(array $pages)
{
Expand Down Expand Up @@ -236,7 +236,7 @@ public function removePage($page)
/**
* Removes all pages in container
*
* @return Container fluent interface, returns self
* @return AbstractContainer fluent interface, returns self
*/
public function removePages()
{
Expand Down
2 changes: 1 addition & 1 deletion src/Navigation.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class Navigation extends Container
class Navigation extends AbstractContainer
{
/**
* Creates a new navigation container
Expand Down
12 changes: 6 additions & 6 deletions src/Page/AbstractPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

use Traversable,
Zend\Acl\Resource\ResourceInterface as AclResource,
Zend\Navigation\Container,
Zend\Navigation\AbstractContainer,
Zend\Navigation\Exception,
Zend\Stdlib\ArrayUtils;

Expand All @@ -36,7 +36,7 @@
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
abstract class AbstractPage extends Container
abstract class AbstractPage extends AbstractContainer
{
/**
* Page label
Expand Down Expand Up @@ -143,7 +143,7 @@ abstract class AbstractPage extends Container
/**
* Parent container
*
* @var \Zend\Navigation\Container|null
* @var \Zend\Navigation\AbstractContainer|null
*/
protected $parent;

Expand Down Expand Up @@ -822,11 +822,11 @@ public function getVisible($recursive = false)
/**
* Sets parent container
*
* @param Container $parent [optional] new parent to set.
* @param AbstractContainer $parent [optional] new parent to set.
* Default is null which will set no parent.
* @return AbstractPage fluent interface, returns self
*/
public function setParent(Container $parent = null)
public function setParent(AbstractContainer $parent = null)
{
if ($parent === $this) {
throw new Exception\InvalidArgumentException(
Expand Down Expand Up @@ -858,7 +858,7 @@ public function setParent(Container $parent = null)
/**
* Returns parent container
*
* @return Container|null parent container or null
* @return AbstractContainer|null parent container or null
*/
public function getParent()
{
Expand Down
20 changes: 20 additions & 0 deletions src/Page/Mvc.php
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,16 @@ public function getRoute()
return $this->route;
}

/**
* Get the route match.
*
* @return \Zend\Mvc\Router\RouteMatch
*/
public function getRouteMatch()
{
return $this->routeMatch;
}

/**
* Set route match object from which parameters will be retrieved
*
Expand All @@ -361,6 +371,16 @@ public function setRouteMatch(RouteMatch $matches)
return $this;
}

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

/**
* Sets action helper for assembling URLs
*
Expand Down
95 changes: 95 additions & 0 deletions src/Service/AbstractNavigationFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?php

namespace Zend\Navigation\Service;

use Zend\Config;
use Zend\Navigation\Exception;
use Zend\Navigation\Navigation;
use Zend\Navigation\Page\Mvc as MvcPage;
use Zend\Mvc\Router\RouteMatch;
use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
use Zend\View\Helper\Url as UrlHelper;

abstract class AbstractNavigationFactory implements FactoryInterface
{
/**
* @var array
*/
protected $pages;

public function createService(ServiceLocatorInterface $serviceLocator)
{
$pages = $this->getPages($serviceLocator);
return new Navigation($pages);
}

abstract protected function getName();

protected function getPages(ServiceLocatorInterface $serviceLocator)
{
if (null === $this->pages) {
$configuration = $serviceLocator->get('Configuration');

if (!isset($configuration['navigation'])) {
throw new Exception\InvalidArgumentException('Could not find navigation configuration key');
}
if (!isset($configuration['navigation'][$this->getName()])) {
throw new Exception\InvalidArgumentException(sprintf(
'Failed to find a navigation container by the name "%s"',
$this->getName()
));
}

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

$this->pages = $this->injectComponents($pages, $routeMatch, $urlHelper);
}
return $this->pages;
}

protected function getPagesFromConfig($config = null)
{
if (is_string($config)) {
if (file_exists($config)) {
$config = Config\Factory::fromFile($config);
} else {
throw new Exception\InvalidArgumentException(sprintf(
'Config was a string but file "%s" does not exist',
$config
));
}
} else if ($config instanceof Config\Config) {
$config = $config->toArray();
} else if (!is_array($config)) {
throw new Exception\InvalidArgumentException('
Invalid input, expected array, filename, or Zend\Config object'
);
}

return $config;
}

protected function injectComponents($pages, RouteMatch $routeMatch, UrlHelper $urlHelper)
{
foreach($pages as &$page) {
$hasMvc = isset($page['action']) || isset($page['controller']) || isset($page['route']);
if ($hasMvc) {
if (!isset($page['routeMatch'])) {
$page['routeMatch'] = $routeMatch;
}
if (!isset($page['urlHelper'])) {
$page['urlHelper'] = $urlHelper;
}
}

if (isset($page['pages'])) {
$page['pages'] = $this->injectComponents($page['pages'], $routeMatch, $urlHelper);
}
}
return $pages;
}
}
23 changes: 23 additions & 0 deletions src/Service/ConstructedNavigationFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Zend\Navigation\Service;

use Zend\ServiceManager\ServiceLocatorInterface;

class ConstructedNavigationFactory extends AbstractNavigationFactory
{
public function __construct($config)
{
$this->pages = $this->getPagesFromConfig($config);
}

public function getPages(ServiceLocatorInterface $serviceLocator)
{
return $this->pages;
}

public function getName()
{
return 'constructed';
}
}
11 changes: 11 additions & 0 deletions src/Service/DefaultNavigationFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Zend\Navigation\Service;

class DefaultNavigationFactory extends AbstractNavigationFactory
{
protected function getName()
{
return 'default';
}
}
4 changes: 2 additions & 2 deletions test/ContainerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1008,7 +1008,7 @@ public function testCurrent()

public function testCurrentShouldThrowExceptionIfIndexIsInvalid()
{
$container = new \ZendTest\Navigation\TestAsset\Container(array(
$container = new \ZendTest\Navigation\TestAsset\AbstractContainer(array(
array(
'label' => 'Page 2',
'type' => 'uri'
Expand All @@ -1022,7 +1022,7 @@ public function testCurrentShouldThrowExceptionIfIndexIsInvalid()

try {
$page = $container->current();
$this->fail('Container index is invalid, ' .
$this->fail('AbstractContainer index is invalid, ' .
'but a Zend\Navigation\Exception\InvalidArgumentException was ' .
'not thrown');
} catch (Navigation\Exception\OutOfBoundsException $e) {
Expand Down
Loading

0 comments on commit a55ead5

Please sign in to comment.