Source of file AbstractNavigationFactory.php
Size: 5,041 Bytes - Last Modified: 2014-03-12T23:21:18+01:00
/home/theseer/Downloads/ZendFramework-2.3.0/library/Zend/Navigation/Service/AbstractNavigationFactory.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153 | <?php /** * Zend Framework (http://framework.zend.com/) * * @link http://github.com/zendframework/zf2 for the canonical source repository * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */ 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; use Zend\Navigation\Navigation; use Zend\ServiceManager\FactoryInterface; use Zend\ServiceManager\ServiceLocatorInterface; /** * Abstract navigation factory */ abstract class AbstractNavigationFactory implements FactoryInterface { /** * @var array */ protected $pages; /** * @param ServiceLocatorInterface $serviceLocator * @return \Zend\Navigation\Navigation */ public function createService(ServiceLocatorInterface $serviceLocator) { $pages = $this->getPages($serviceLocator); return new Navigation($pages); } /** * @abstract * @return string */ abstract protected function getName(); /** * @param ServiceLocatorInterface $serviceLocator * @return array * @throws \Zend\Navigation\Exception\InvalidArgumentException */ protected function getPages(ServiceLocatorInterface $serviceLocator) { if (null === $this->pages) { $configuration = $serviceLocator->get('Config'); 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() )); } $pages = $this->getPagesFromConfig($configuration['navigation'][$this->getName()]); $this->pages = $this->preparePages($serviceLocator, $pages); } return $this->pages; } /** * @param ServiceLocatorInterface $serviceLocator * @param array|\Zend\Config\Config $pages * @throws \Zend\Navigation\Exception\InvalidArgumentException */ protected function preparePages(ServiceLocatorInterface $serviceLocator, $pages) { $application = $serviceLocator->get('Application'); $routeMatch = $application->getMvcEvent()->getRouteMatch(); $router = $application->getMvcEvent()->getRouter(); $request = $application->getMvcEvent()->getRequest(); // HTTP request is the only one that may be injected if (!$request instanceof Request) { $request = null; } return $this->injectComponents($pages, $routeMatch, $router, $request); } /** * @param string|\Zend\Config\Config|array $config * @return array|null|\Zend\Config\Config * @throws \Zend\Navigation\Exception\InvalidArgumentException */ 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 )); } } elseif ($config instanceof Config\Config) { $config = $config->toArray(); } elseif (!is_array($config)) { throw new Exception\InvalidArgumentException(' Invalid input, expected array, filename, or Zend\Config object' ); } return $config; } /** * @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, $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) { $page['routeMatch'] = $routeMatch; } 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, $request); } } return $pages; } } |