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

Commit 9993386

Browse files
committed
Merge branch 'hotfix/router-abstraction'
Close #58
2 parents cbafd5f + cafd9a5 commit 9993386

File tree

5 files changed

+134
-24
lines changed

5 files changed

+134
-24
lines changed

CHANGELOG.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
All notable changes to this project will be documented in this file, in reverse chronological order by release.
44

5-
## 2.6.7 - TBD
5+
## 2.6.7 - 2016-04-18
66

77
### Added
88

@@ -18,7 +18,9 @@ All notable changes to this project will be documented in this file, in reverse
1818

1919
### Fixed
2020

21-
- Nothing.
21+
- [#58](https://github.com/zendframework/zend-view/pull/58) updates the `url()`
22+
helper so that it can work with either the zend-mvc v2 router subcomponent or
23+
zend-router.
2224

2325
## 2.6.6 - 2016-04-18
2426

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
"zendframework/zend-navigation": "^2.5",
3636
"zendframework/zend-paginator": "^2.5",
3737
"zendframework/zend-permissions-acl": "^2.6",
38+
"zendframework/zend-router": "^3.0.1",
3839
"zendframework/zend-serializer": "^2.6.1",
3940
"zendframework/zend-session": "^2.6.2",
4041
"zendframework/zend-servicemanager": "^2.7.5 || ^3.0.3",

composer.lock

Lines changed: 60 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Helper/Url.php

Lines changed: 51 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@
1111

1212
use Traversable;
1313
use Zend\Mvc\ModuleRouteListener;
14-
use Zend\Mvc\Router\RouteMatch;
15-
use Zend\Mvc\Router\RouteStackInterface;
14+
use Zend\Mvc\Router\RouteMatch as LegacyRouteMatch;
15+
use Zend\Mvc\Router\RouteStackInterface as LegacyRouteStackInterface;
16+
use Zend\Router\RouteMatch;
17+
use Zend\Router\RouteStackInterface;
1618
use Zend\View\Exception;
1719

1820
/**
@@ -21,32 +23,36 @@
2123
class Url extends AbstractHelper
2224
{
2325
/**
24-
* RouteStackInterface instance.
26+
* Router instance.
2527
*
26-
* @var RouteStackInterface
28+
* @var LegacyRouteStackInterface|RouteStackInterface
2729
*/
2830
protected $router;
2931

3032
/**
31-
* RouteInterface match returned by the router.
33+
* Route matches returned by the router.
3234
*
33-
* @var RouteMatch.
35+
* @var LegacyRouteMatch|RouteMatch.
3436
*/
3537
protected $routeMatch;
3638

3739
/**
3840
* Generates a url given the name of a route.
3941
*
40-
* @see Zend\Mvc\Router\RouteInterface::assemble()
41-
* @param string $name Name of the route
42-
* @param array $params Parameters for the link
43-
* @param array|Traversable $options Options for the route
44-
* @param bool $reuseMatchedParams Whether to reuse matched parameters
45-
* @return string Url For the link href attribute
46-
* @throws Exception\RuntimeException If no RouteStackInterface was provided
47-
* @throws Exception\RuntimeException If no RouteMatch was provided
48-
* @throws Exception\RuntimeException If RouteMatch didn't contain a matched route name
49-
* @throws Exception\InvalidArgumentException If the params object was not an array or \Traversable object
42+
* @see Zend\Mvc\Router\RouteInterface::assemble()
43+
* @see Zend\Router\RouteInterface::assemble()
44+
* @param string $name Name of the route
45+
* @param array $params Parameters for the link
46+
* @param array|Traversable $options Options for the route
47+
* @param bool $reuseMatchedParams Whether to reuse matched parameters
48+
* @return string Url For the link href attribute
49+
* @throws Exception\RuntimeException If no RouteStackInterface was
50+
* provided
51+
* @throws Exception\RuntimeException If no RouteMatch was provided
52+
* @throws Exception\RuntimeException If RouteMatch didn't contain a
53+
* matched route name
54+
* @throws Exception\InvalidArgumentException If the params object was not
55+
* an array or Traversable object.
5056
*/
5157
public function __invoke($name = null, $params = [], $options = [], $reuseMatchedParams = false)
5258
{
@@ -103,23 +109,48 @@ public function __invoke($name = null, $params = [], $options = [], $reuseMatche
103109
/**
104110
* Set the router to use for assembling.
105111
*
106-
* @param RouteStackInterface $router
112+
* @param LegacyRouteStackInterface|RouteStackInterface $router
107113
* @return Url
114+
* @throws Exception\InvalidArgumentException for invalid router types.
108115
*/
109-
public function setRouter(RouteStackInterface $router)
116+
public function setRouter($router)
110117
{
118+
if (! $router instanceof RouteStackInterface
119+
&& ! $router instanceof LegacyRouteStackInterface
120+
) {
121+
throw new Exception\InvalidArgumentException(sprintf(
122+
'%s expects a %s or %s instance; received %s',
123+
__METHOD__,
124+
RouteStackInterface::class,
125+
LegacyRouteStackInterface::class,
126+
(is_object($router) ? get_class($router) : gettype($router))
127+
));
128+
}
129+
111130
$this->router = $router;
112131
return $this;
113132
}
114133

115134
/**
116135
* Set route match returned by the router.
117136
*
118-
* @param RouteMatch $routeMatch
137+
* @param LegacyRouteMatch|RouteMatch $routeMatch
119138
* @return Url
120139
*/
121-
public function setRouteMatch(RouteMatch $routeMatch)
140+
public function setRouteMatch($routeMatch)
122141
{
142+
if (! $routeMatch instanceof RouteMatch
143+
&& ! $routeMatch instanceof LegacyRouteMatch
144+
) {
145+
throw new Exception\InvalidArgumentException(sprintf(
146+
'%s expects a %s or %s instance; received %s',
147+
__METHOD__,
148+
RouteMatch::class,
149+
LegacyRouteMatch::class,
150+
(is_object($routeMatch) ? get_class($routeMatch) : gettype($routeMatch))
151+
));
152+
}
153+
123154
$this->routeMatch = $routeMatch;
124155
return $this;
125156
}

test/Helper/UrlTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
use Zend\Mvc\ModuleRouteListener;
1515
use Zend\Mvc\Router\RouteMatch;
1616
use Zend\Mvc\Router\SimpleRouteStack as Router;
17+
use Zend\Router\RouteMatch as NextGenRouteMatch;
18+
use Zend\Router\SimpleRouteStack as NextGenRouter;
1719

1820
/**
1921
* Zend\View\Helper\Url Test
@@ -201,4 +203,20 @@ public function testRemovesModuleRouteListenerParamsWhenReusingMatchedParameters
201203
$url = $helper->__invoke('default/wildcard', ['Twenty' => 'Cooler'], true);
202204
$this->assertEquals('/Rainbow/Dash=Twenty%Cooler', $url);
203205
}
206+
207+
public function testAcceptsNextGenRouterToSetRouter()
208+
{
209+
$router = new NextGenRouter();
210+
$url = new UrlHelper();
211+
$url->setRouter($router);
212+
$this->assertAttributeSame($router, 'router', $url);
213+
}
214+
215+
public function testAcceptsNextGenRouteMatche()
216+
{
217+
$routeMatch = new NextGenRouteMatch([]);
218+
$url = new UrlHelper();
219+
$url->setRouteMatch($routeMatch);
220+
$this->assertAttributeSame($routeMatch, 'routeMatch', $url);
221+
}
204222
}

0 commit comments

Comments
 (0)