Skip to content

Instantly share code, notes, and snippets.

@Hounddog
Created September 11, 2012 11:09
Show Gist options
  • Save Hounddog/3697642 to your computer and use it in GitHub Desktop.
Save Hounddog/3697642 to your computer and use it in GitHub Desktop.
NavigationFactory
<?php
namespace DysAdmin\Navigation\Service;
use ZfcAdmin\Navigation\Service\AdminNavigationFactory as ZfcAdminNavigationFactory;
use Zend\ServiceManager\ServiceLocatorInterface;
use DysAdmin\Navigation\Navigation;
class AdminNavigationFactory extends ZfcAdminNavigationFactory
{
public function createService(ServiceLocatorInterface $serviceLocator)
{
$pages = $this->getPages($serviceLocator);
return new Navigation($pages);
}
}
<?php
/**
* V5
* LICENSE
*
* Insert License here
*
*/
namespace DysAdmin;
use DysBase\Module\AbstractModule;
use Zend\EventManager\EventInterface;
use Zend\Mvc\MvcEvent;
/**
* Base Module for Site
* @category Site
* @package Module
* @copyright Copyright (c) 2012 Doyousoft
* @license $license_information
* @version $Id
*/
class Module extends AbstractModule
{
public function getDoctrineConfig()
{
return array();
}
public function getDir()
{
return __DIR__;
}
public function getNamespace()
{
return __NAMESPACE__;
}
/**
* @{inheritdoc}
*/
public function getServiceConfig()
{
return array(
'factories' => array(
'admin_json_navigation' => 'DysAdmin\Navigation\Service\AdminNavigationFactory',
),
);
}
}
<?php
namespace DysAdmin\Navigation\Page;
use Zend\Navigation\Page\Mvc;
use Zend\Mvc\Router\RouteStackInterface;
class Json extends Mvc
{
/**
* Returns an array representation of the page
*
* @return array associative array containing all page properties
*/
public function toArray()
{
return array_merge($this->getCustomProperties(), array(
'label' => $this->getLabel(),
'id' => $this->getId(),
'class' => $this->getClass(),
'title' => $this->getTitle(),
'target' => $this->getHref(),
'order' => $this->getOrder(),
'privilege' => $this->getPrivilege(),
'active' => $this->isActive(),
'visible' => $this->isVisible(),
'type' => get_called_class(),
'pages' => parent::toArray(),
));
}
}
<?php
namespace DysAdmin\Navigation;
use Zend\Navigation\Navigation as ZendNavigation;
class Navigation extends ZendNavigation
{
/**
* Adds several pages at once
*
* @param array|Traversable|AbstractContainer $pages pages to add
* @return AbstractContainer fluent interface, returns self
* @throws Exception\InvalidArgumentException if $pages is not array,
* 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\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 AbstractContainer) {
$pages = iterator_to_array($pages);
}
foreach ($pages as $page) {
$page['type'] = 'DysAdmin\Navigation\Page\Json';
$this->addPage($page);
}
return $this;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment