diff --git a/.gitmodules b/.gitmodules deleted file mode 100644 index 21d57da..0000000 --- a/.gitmodules +++ /dev/null @@ -1,3 +0,0 @@ -[submodule "tools/phptools"] - path = tools/phptools - url = git://github.com/ralphschindler/PHPTools.git diff --git a/composer.json b/composer.json index ff0a37a..cd8d80a 100644 --- a/composer.json +++ b/composer.json @@ -1,6 +1,6 @@ { "name": "zendframework/zend-navigation", - "description": "Zend\\Navigation component", + "description": "provides support for managing trees of pointers to web pages", "license": "BSD-3-Clause", "keywords": [ "zf2", @@ -9,11 +9,11 @@ "homepage": "https://github.com/zendframework/zend-navigation", "autoload": { "psr-4": { - "Zend\\Navigation\\": "src/" + "Zend\\Navigation": "src/" } }, "require": { - "php": ">=5.3.23", + "php": ">=5.3.3", "zendframework/zend-stdlib": "self.version" }, "require-dev": { diff --git a/src/AbstractContainer.php b/src/AbstractContainer.php index 8eee228..91260d5 100644 --- a/src/AbstractContainer.php +++ b/src/AbstractContainer.php @@ -1,30 +1,21 @@ {$match[1]}($match[2], $arguments[0]); } diff --git a/src/Exception/BadMethodCallException.php b/src/Exception/BadMethodCallException.php index 3241176..ba63df1 100644 --- a/src/Exception/BadMethodCallException.php +++ b/src/Exception/BadMethodCallException.php @@ -1,22 +1,11 @@ parent) && $this->parent instanceof self ) { @@ -1116,7 +1103,7 @@ public function getCustomProperties() * * @return string a hash code value for this page */ - public final function hashCode() + final public function hashCode() { return spl_object_hash($this); } diff --git a/src/Page/Mvc.php b/src/Page/Mvc.php index b5dbefc..2691eda 100644 --- a/src/Page/Mvc.php +++ b/src/Page/Mvc.php @@ -1,29 +1,19 @@ 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' ); } @@ -200,10 +188,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(); @@ -372,49 +369,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: diff --git a/src/Page/Uri.php b/src/Page/Uri.php index 1dacd0b..b621e1c 100644 --- a/src/Page/Uri.php +++ b/src/Page/Uri.php @@ -1,22 +1,11 @@ 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) { @@ -42,15 +69,20 @@ 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; } + /** + * @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)) { @@ -62,9 +94,9 @@ protected function getPagesFromConfig($config = null) $config )); } - } else if ($config instanceof Config\Config) { + } elseif ($config instanceof Config\Config) { $config = $config->toArray(); - } else if (!is_array($config)) { + } elseif (!is_array($config)) { throw new Exception\InvalidArgumentException(' Invalid input, expected array, filename, or Zend\Config object' ); @@ -73,21 +105,27 @@ protected function getPagesFromConfig($config = null) return $config; } - protected function injectComponents($pages, RouteMatch $routeMatch, UrlHelper $urlHelper) + /** + * @param array $pages + * @param RouteMatch $routeMatch + * @param Router $router + * @return mixed + */ + 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'])) { + if (!isset($page['routeMatch']) && $routeMatch) { $page['routeMatch'] = $routeMatch; } - if (!isset($page['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; diff --git a/src/Service/ConstructedNavigationFactory.php b/src/Service/ConstructedNavigationFactory.php index cef6e0b..f276ed2 100644 --- a/src/Service/ConstructedNavigationFactory.php +++ b/src/Service/ConstructedNavigationFactory.php @@ -1,23 +1,47 @@ pages = $this->getPagesFromConfig($config); } + /** + * @param ServiceLocatorInterface $serviceLocator + * @return array|null|\Zend\Config\Config + */ public function getPages(ServiceLocatorInterface $serviceLocator) { return $this->pages; } + /** + * @return string + */ public function getName() { return 'constructed'; } -} \ No newline at end of file +} diff --git a/src/Service/DefaultNavigationFactory.php b/src/Service/DefaultNavigationFactory.php index b256fc1..da42d52 100644 --- a/src/Service/DefaultNavigationFactory.php +++ b/src/Service/DefaultNavigationFactory.php @@ -1,11 +1,28 @@ setFactory('navigation', function(HelperPluginManager $pm) { + $helper = new \Zend\View\Helper\Navigation; + $helper->setServiceLocator($pm->getServiceLocator()); + return $helper; + }); + } +} diff --git a/test/ContainerTest.php b/test/ContainerTest.php index 3b75b5e..2d6cb8f 100644 --- a/test/ContainerTest.php +++ b/test/ContainerTest.php @@ -1,29 +1,18 @@ _navigation = new \Zend\Navigation\Navigation(); } - + protected function tearDown() { $this->_navigation = null; @@ -55,7 +43,7 @@ protected function tearDown() /** * Testing that navigation order is done correctly - * + * * @group ZF-8337 * @group ZF-8313 */ @@ -78,5 +66,5 @@ public function testNavigationArraySortsCorrectly() $this->assertEquals('page1', $pages[1]['uri']); $this->assertEquals('page2', $pages[2]['uri']); } - + } diff --git a/test/Page/MvcTest.php b/test/Page/MvcTest.php index 9505056..8e31870 100644 --- a/test/Page/MvcTest.php +++ b/test/Page/MvcTest.php @@ -1,35 +1,23 @@ route = new RegexRoute( @@ -62,17 +44,13 @@ protected function setUp() $this->routeMatch = new RouteMatch(array()); $this->routeMatch->setMatchedRouteName('default'); - - $this->urlHelper = new UrlHelper(); - $this->urlHelper->setRouter($this->router); - $this->urlHelper->setRouteMatch($this->routeMatch); } protected function tearDown() { } - public function testHrefGeneratedByUrlHelperRequiresNoRoute() + public function testHrefGeneratedByRouterRequiresNoRoute() { $page = new Page\Mvc(array( 'label' => 'foo', @@ -80,7 +58,7 @@ public function testHrefGeneratedByUrlHelperRequiresNoRoute() 'controller' => 'index' )); $page->setRouteMatch($this->routeMatch); - $page->setUrlHelper($this->urlHelper); + $page->setRouter($this->router); $page->setAction('view'); $page->setController('news'); @@ -117,11 +95,7 @@ public function testHrefGeneratedIsRouteAware() 'page' => 1, )); - $urlHelper = new UrlHelper(); - $urlHelper->setRouter($router); - $urlHelper->setRouteMatch($routeMatch); - - $page->setUrlHelper($urlHelper); + $page->setRouter($router); $page->setRouteMatch($routeMatch); $this->assertEquals('/lolcat/myaction/1337', $page->getHref()); @@ -142,11 +116,7 @@ public function testIsActiveReturnsTrueWhenMatchingRoute() $routeMatch = new RouteMatch(array()); $routeMatch->setMatchedRouteName('lolfish'); - $urlHelper = new UrlHelper; - $urlHelper->setRouter($router); - $urlHelper->setRouteMatch($routeMatch); - - $page->setUrlHelper($urlHelper); + $page->setRouter($router); $page->setRouteMatch($routeMatch); $this->assertEquals(true, $page->isActive()); @@ -157,8 +127,6 @@ public function testIsActiveReturnsFalseWhenNoRouteAndNoMatchedRouteNameIsSet() $page = new Page\Mvc(); $routeMatch = new RouteMatch(array()); - $this->urlHelper->setRouteMatch($routeMatch); - $page->setRouteMatch($routeMatch); $this->assertFalse($page->isActive()); @@ -193,7 +161,7 @@ public function testGetHrefWithFragmentIdentifier() $this->routeMatch->setMatchedRouteName('myroute'); $page->setRouteMatch($this->routeMatch); - $page->setUrlHelper($this->urlHelper); + $page->setRouter($this->router); $this->assertEquals('/lolcat/myaction/1337#qux', $page->getHref()); } @@ -210,8 +178,6 @@ public function testIsActiveReturnsTrueOnIdenticalControllerAction() 'action' => 'index', )); - $this->urlHelper->setRouteMatch($routeMatch); - $page->setRouteMatch($routeMatch); $this->assertTrue($page->isActive()); @@ -229,8 +195,6 @@ public function testIsActiveReturnsFalseOnDifferentControllerAction() 'action' => 'index', )); - $this->urlHelper->setRouteMatch($routeMatch); - $page->setRouteMatch($routeMatch); $this->assertFalse($page->isActive()); @@ -253,8 +217,6 @@ public function testIsActiveReturnsTrueOnIdenticalIncludingPageParams() 'id' => '1337' )); - $this->urlHelper->setRouteMatch($routeMatch); - $page->setRouteMatch($routeMatch); $this->assertTrue($page->isActive()); @@ -274,8 +236,6 @@ public function testIsActiveReturnsTrueWhenRequestHasMoreParams() 'id' => '1337', )); - $this->urlHelper->setRouteMatch($routeMatch); - $page->setRouteMatch($routeMatch); $this->assertTrue($page->isActive()); @@ -298,8 +258,6 @@ public function testIsActiveReturnsFalseWhenRequestHasLessParams() 'id' => null )); - $this->urlHelper->setRouteMatch($routeMatch); - $page->setRouteMatch($routeMatch); $this->assertFalse($page->isActive()); @@ -435,55 +393,58 @@ public function testToArrayMethod() public function testSpecifyingAnotherUrlHelperToGenerateHrefs() { - $newHelper = new TestAsset\UrlHelper(); + $newRouter = new TestAsset\Router(); - $page = new Page\Mvc(); - $page->setUrlHelper($newHelper); + $page = new Page\Mvc(array( + 'route' => 'default' + )); + $page->setRouter($newRouter); - $expected = TestAsset\UrlHelper::RETURN_URL; + $expected = TestAsset\Router::RETURN_URL; $actual = $page->getHref(); $this->assertEquals($expected, $actual); } - public function testDefaultUrlHelperCanBeSetWithConstructor() + public function testDefaultRouterCanBeSetWithConstructor() { $page = new Page\Mvc(array( - 'label' => 'foo', - 'action' => 'index', - 'controller' => 'index', - 'defaultUrlHelper' => $this->urlHelper + 'label' => 'foo', + 'action' => 'index', + 'controller' => 'index', + 'defaultRouter' => $this->router )); - $this->assertEquals($this->urlHelper, $page->getDefaultUrlHelper()); - $page->setDefaultUrlHelper(null); + $this->assertEquals($this->router, $page->getDefaultRouter()); + $page->setDefaultRouter(null); } - public function testDefaultUrlHelperCanBeSetWithGetter() + public function testDefaultRouterCanBeSetWithGetter() { $page = new Page\Mvc(array( 'label' => 'foo', 'action' => 'index', 'controller' => 'index', )); - $page->setDefaultUrlHelper($this->urlHelper); + $page->setDefaultRouter($this->router); - $this->assertEquals($this->urlHelper, $page->getDefaultUrlHelper()); - $page->setDefaultUrlHelper(null); + $this->assertEquals($this->router, $page->getDefaultRouter()); + $page->setDefaultRouter(null); } - public function testNoExceptionForGetHrefIfDefaultUrlHelperIsSet() + public function testNoExceptionForGetHrefIfDefaultRouterIsSet() { $page = new Page\Mvc(array( 'label' => 'foo', 'action' => 'index', 'controller' => 'index', - 'defaultUrlHelper' => $this->urlHelper + 'route' => 'default', + 'defaultRouter' => $this->router )); - // If the default url helper is not used an exception will be thrown. + // If the default router is not used an exception will be thrown. // This method intentionally has no assertion. $page->getHref(); - $page->setDefaultUrlHelper(null); + $page->setDefaultRouter(null); } } diff --git a/test/Page/PageFactoryTest.php b/test/Page/PageFactoryTest.php index 24da6b5..a0db371 100644 --- a/test/Page/PageFactoryTest.php +++ b/test/Page/PageFactoryTest.php @@ -1,28 +1,17 @@ '#', 'fragment' => 'foo', )); - + $this->assertEquals('foo', $page->getFragment()); - + $page->setFragment('bar'); $this->assertEquals('bar', $page->getFragment()); - + $invalids = array(42, (object) null); foreach ($invalids as $invalid) { try { @@ -471,7 +458,7 @@ public function testSetResourceInterface() 'label' => 'hello' )); - $resource = new \Zend\Acl\Resource\GenericResource('bar'); + $resource = new \Zend\Permissions\Acl\Resource\GenericResource('bar'); $page->setResource($resource); $this->assertEquals($resource, $page->getResource()); @@ -729,7 +716,7 @@ public function testSetVisibleShouldJuggleValue() /** * ZF-10146 - * + * * @link http://framework.zend.com/issues/browse/ZF-10146 */ $page->setVisible('False'); diff --git a/test/Page/UriTest.php b/test/Page/UriTest.php index 917c164..bfd32f1 100644 --- a/test/Page/UriTest.php +++ b/test/Page/UriTest.php @@ -1,25 +1,15 @@ setUri($uri); $page->setFragment('bar'); - + $this->assertEquals($uri . '#bar', $page->getHref()); - + $page->setUri('#'); - + $this->assertEquals('#bar', $page->getHref()); } } diff --git a/test/ServiceFactoryTest.php b/test/ServiceFactoryTest.php index 74952ef..79107ff 100644 --- a/test/ServiceFactoryTest.php +++ b/test/ServiceFactoryTest.php @@ -1,29 +1,18 @@ false, 'cache_dir' => 'data/cache', 'module_paths' => array(), - ), - 'service_manager' => array( - 'factories' => array( - 'Configuration' => function() { - return array( - 'navigation' => array( - 'file' => __DIR__ . '/_files/navigation.xml', - 'default' => array( - array( - 'label' => 'Page 1', - 'uri' => 'page1.html' - ), - array( - 'label' => 'MVC Page', - 'route' => 'foo', - 'pages' => array( + 'extra_config' => array( + 'service_manager' => array( + 'factories' => array( + 'Config' => function() { + return array( + 'navigation' => array( + 'file' => __DIR__ . '/_files/navigation.xml', + 'default' => array( + array( + 'label' => 'Page 1', + 'uri' => 'page1.html' + ), + array( + 'label' => 'MVC Page', + 'route' => 'foo', + 'pages' => array( + array( + 'label' => 'Sub MVC Page', + 'route' => 'foo' + ) + ) + ), array( - 'label' => 'Sub MVC Page', - 'route' => 'foo' + 'label' => 'Page 3', + 'uri' => 'page3.html' ) ) - ), - array( - 'label' => 'Page 3', - 'uri' => 'page3.html' ) - ) - ) - ); - } + ); + } + ) + ), ) ), ); - $sm = $this->serviceManager = new ServiceManager(new ServiceManagerConfiguration($config['service_manager'])); - $sm->setService('ApplicationConfiguration', $config); + $sm = $this->serviceManager = new ServiceManager(new ServiceManagerConfig); + $sm->setService('ApplicationConfig', $config); $sm->get('ModuleManager')->loadModules(); $sm->get('Application')->bootstrap(); @@ -133,7 +122,7 @@ public function testMvcPagesGetInjectedWithComponents() $recursive = function($that, $pages) use (&$recursive) { foreach($pages as $page) { if ($page instanceof MvcPage) { - $that->assertInstanceOf('Zend\View\Helper\Url', $page->getUrlHelper()); + $that->assertInstanceOf('Zend\Mvc\Router\RouteStackInterface', $page->getRouter()); $that->assertInstanceOf('Zend\Mvc\Router\RouteMatch', $page->getRouteMatch()); } diff --git a/test/TestAsset/AbstractContainer.php b/test/TestAsset/AbstractContainer.php index 52634a0..ca24571 100644 --- a/test/TestAsset/AbstractContainer.php +++ b/test/TestAsset/AbstractContainer.php @@ -1,22 +1,11 @@