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

Commit c70e610

Browse files
committed
One massive refactoring to PSR-7
1 parent 65ea6a4 commit c70e610

35 files changed

+231
-792
lines changed

src/Http/Chain.php

+6-9
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,13 @@
99
namespace Zend\Router\Http;
1010

1111
use ArrayObject;
12+
use Psr\Http\Message\ServerRequestInterface as Request;
1213
use Traversable;
1314
use Zend\Router\Exception;
1415
use Zend\Router\PriorityList;
1516
use Zend\Router\RoutePluginManager;
1617
use Zend\Stdlib\ArrayUtils;
17-
use Zend\Stdlib\RequestInterface as Request;
18+
use Zend\Router\RouteMatch as BaseRouteMatch;
1819

1920
/**
2021
* Chain route.
@@ -56,7 +57,7 @@ public function __construct(array $routes, RoutePluginManager $routePlugins, Arr
5657
* @see \Zend\Router\RouteInterface::factory()
5758
* @param mixed $options
5859
* @throws Exception\InvalidArgumentException
59-
* @return Part
60+
* @return Chain
6061
*/
6162
public static function factory($options = [])
6263
{
@@ -101,12 +102,8 @@ public static function factory($options = [])
101102
* @param array $options
102103
* @return RouteMatch|null
103104
*/
104-
public function match(Request $request, $pathOffset = null, array $options = [])
105+
public function match(Request $request, $pathOffset = null, array $options = []) : ?BaseRouteMatch
105106
{
106-
if (! method_exists($request, 'getUri')) {
107-
return;
108-
}
109-
110107
if ($pathOffset === null) {
111108
$mustTerminate = true;
112109
$pathOffset = 0;
@@ -127,15 +124,15 @@ public function match(Request $request, $pathOffset = null, array $options = [])
127124
$subMatch = $route->match($request, $pathOffset, $options);
128125

129126
if ($subMatch === null) {
130-
return;
127+
return null;
131128
}
132129

133130
$match->merge($subMatch);
134131
$pathOffset += $subMatch->getLength();
135132
}
136133

137134
if ($mustTerminate && $pathOffset !== $pathLength) {
138-
return;
135+
return null;
139136
}
140137

141138
return $match;

src/Http/Hostname.php

+7-10
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@
88

99
namespace Zend\Router\Http;
1010

11+
use Psr\Http\Message\ServerRequestInterface as Request;
1112
use Traversable;
1213
use Zend\Router\Exception;
1314
use Zend\Stdlib\ArrayUtils;
14-
use Zend\Stdlib\RequestInterface as Request;
15+
use Zend\Router\RouteMatch as BaseRouteMatch;
1516

1617
/**
1718
* Hostname route.
@@ -60,7 +61,7 @@ class Hostname implements RouteInterface
6061
* @param array $constraints
6162
* @param array $defaults
6263
*/
63-
public function __construct($route, array $constraints = [], array $defaults = [])
64+
public function __construct(string $route, array $constraints = [], array $defaults = [])
6465
{
6566
$this->defaults = $defaults;
6667
$this->parts = $this->parseRouteDefinition($route);
@@ -108,7 +109,7 @@ public static function factory($options = [])
108109
* @return array
109110
* @throws Exception\RuntimeException
110111
*/
111-
protected function parseRouteDefinition($def)
112+
protected function parseRouteDefinition(string $def)
112113
{
113114
$currentPos = 0;
114115
$length = strlen($def);
@@ -278,21 +279,17 @@ protected function buildHost(array $parts, array $mergedParams, $isOptional)
278279
*
279280
* @see \Zend\Router\RouteInterface::match()
280281
* @param Request $request
281-
* @return RouteMatch|null
282+
* @return BaseRouteMatch|null
282283
*/
283-
public function match(Request $request)
284+
public function match(Request $request) : ?BaseRouteMatch
284285
{
285-
if (! method_exists($request, 'getUri')) {
286-
return;
287-
}
288-
289286
$uri = $request->getUri();
290287
$host = $uri->getHost();
291288

292289
$result = preg_match('(^' . $this->regex . '$)', $host, $matches);
293290

294291
if (! $result) {
295-
return;
292+
return null;
296293
}
297294

298295
$params = [];

src/Http/Literal.php

+5-8
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@
88

99
namespace Zend\Router\Http;
1010

11+
use Psr\Http\Message\ServerRequestInterface as Request;
1112
use Traversable;
1213
use Zend\Router\Exception;
1314
use Zend\Stdlib\ArrayUtils;
14-
use Zend\Stdlib\RequestInterface as Request;
15+
use Zend\Router\RouteMatch as BaseRouteMatch;
1516

1617
/**
1718
* Literal route.
@@ -82,12 +83,8 @@ public static function factory($options = [])
8283
* @param integer|null $pathOffset
8384
* @return RouteMatch|null
8485
*/
85-
public function match(Request $request, $pathOffset = null)
86+
public function match(Request $request, $pathOffset = null) : ?BaseRouteMatch
8687
{
87-
if (! method_exists($request, 'getUri')) {
88-
return;
89-
}
90-
9188
$uri = $request->getUri();
9289
$path = $uri->getPath();
9390

@@ -98,14 +95,14 @@ public function match(Request $request, $pathOffset = null)
9895
}
9996
}
10097

101-
return;
98+
return null;
10299
}
103100

104101
if ($path === $this->route) {
105102
return new RouteMatch($this->defaults, strlen($this->route));
106103
}
107104

108-
return;
105+
return null;
109106
}
110107

111108
/**

src/Http/Method.php

+4-7
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@
88

99
namespace Zend\Router\Http;
1010

11+
use Psr\Http\Message\ServerRequestInterface as Request;
1112
use Traversable;
1213
use Zend\Router\Exception;
1314
use Zend\Stdlib\ArrayUtils;
14-
use Zend\Stdlib\RequestInterface as Request;
15+
use Zend\Router\RouteMatch as BaseRouteMatch;
1516

1617
/**
1718
* Method route.
@@ -81,12 +82,8 @@ public static function factory($options = [])
8182
* @param Request $request
8283
* @return RouteMatch|null
8384
*/
84-
public function match(Request $request)
85+
public function match(Request $request) : ?BaseRouteMatch
8586
{
86-
if (! method_exists($request, 'getMethod')) {
87-
return;
88-
}
89-
9087
$requestVerb = strtoupper($request->getMethod());
9188
$matchVerbs = explode(',', strtoupper($this->verb));
9289
$matchVerbs = array_map('trim', $matchVerbs);
@@ -95,7 +92,7 @@ public function match(Request $request)
9592
return new RouteMatch($this->defaults);
9693
}
9794

98-
return;
95+
return null;
9996
}
10097

10198
/**

src/Http/Part.php

+12-5
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,13 @@
99
namespace Zend\Router\Http;
1010

1111
use ArrayObject;
12+
use Psr\Http\Message\ServerRequestInterface as Request;
1213
use Traversable;
1314
use Zend\Router\Exception;
1415
use Zend\Router\PriorityList;
1516
use Zend\Router\RoutePluginManager;
1617
use Zend\Stdlib\ArrayUtils;
17-
use Zend\Stdlib\RequestInterface as Request;
18+
use Zend\Router\RouteMatch as BaseRouteMatch;
1819

1920
/**
2021
* Part route.
@@ -137,15 +138,19 @@ public static function factory($options = [])
137138
* @param array $options
138139
* @return RouteMatch|null
139140
*/
140-
public function match(Request $request, $pathOffset = null, array $options = [])
141+
public function match(Request $request, $pathOffset = null, array $options = []) : ?BaseRouteMatch
141142
{
142143
if ($pathOffset === null) {
143144
$pathOffset = 0;
144145
}
145146

147+
/**
148+
* @var RouteMatch $match
149+
*/
146150
$match = $this->route->match($request, $pathOffset, $options);
151+
// @TODO deal with RouteMatch that is not from TreeRouteStack. Exception?
147152
148-
if ($match !== null && method_exists($request, 'getUri')) {
153+
if ($match !== null) {
149154
if ($this->childRoutes !== null) {
150155
$this->addRoutes($this->childRoutes);
151156
$this->childRoutes = null;
@@ -170,13 +175,15 @@ public function match(Request $request, $pathOffset = null, array $options = [])
170175
foreach ($this->routes as $name => $route) {
171176
if (($subMatch = $route->match($request, $nextOffset, $options)) instanceof RouteMatch) {
172177
if ($match->getLength() + $subMatch->getLength() + $pathOffset === $pathLength) {
173-
return $match->merge($subMatch)->setMatchedRouteName($name);
178+
$match->merge($subMatch);
179+
$match->setMatchedRouteName($name);
180+
return $match;
174181
}
175182
}
176183
}
177184
}
178185

179-
return;
186+
return null;
180187
}
181188

182189
/**

src/Http/Regex.php

+4-7
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@
88

99
namespace Zend\Router\Http;
1010

11+
use Psr\Http\Message\ServerRequestInterface as Request;
1112
use Traversable;
1213
use Zend\Router\Exception;
1314
use Zend\Stdlib\ArrayUtils;
14-
use Zend\Stdlib\RequestInterface as Request;
15+
use Zend\Router\RouteMatch as BaseRouteMatch;
1516

1617
/**
1718
* Regex route.
@@ -103,12 +104,8 @@ public static function factory($options = [])
103104
* @param int $pathOffset
104105
* @return RouteMatch|null
105106
*/
106-
public function match(Request $request, $pathOffset = null)
107+
public function match(Request $request, $pathOffset = null) : ?BaseRouteMatch
107108
{
108-
if (! method_exists($request, 'getUri')) {
109-
return;
110-
}
111-
112109
$uri = $request->getUri();
113110
$path = $uri->getPath();
114111

@@ -119,7 +116,7 @@ public function match(Request $request, $pathOffset = null)
119116
}
120117

121118
if (! $result) {
122-
return;
119+
return null;
123120
}
124121

125122
$matchedLength = strlen($matches[0]);

src/Http/RouteMatch.php

+4-6
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class RouteMatch extends BaseRouteMatch
2828
* @param array $params
2929
* @param int $length
3030
*/
31-
public function __construct(array $params, $length = 0)
31+
public function __construct(array $params, int $length = 0)
3232
{
3333
parent::__construct($params);
3434

@@ -42,15 +42,13 @@ public function __construct(array $params, $length = 0)
4242
* @param string $name
4343
* @return RouteMatch
4444
*/
45-
public function setMatchedRouteName($name)
45+
public function setMatchedRouteName(string $name) : void
4646
{
4747
if ($this->matchedRouteName === null) {
4848
$this->matchedRouteName = $name;
4949
} else {
5050
$this->matchedRouteName = $name . '/' . $this->matchedRouteName;
5151
}
52-
53-
return $this;
5452
}
5553

5654
/**
@@ -59,7 +57,7 @@ public function setMatchedRouteName($name)
5957
* @param RouteMatch $match
6058
* @return RouteMatch
6159
*/
62-
public function merge(RouteMatch $match)
60+
public function merge(RouteMatch $match) : RouteMatch
6361
{
6462
$this->params = array_merge($this->params, $match->getParams());
6563
$this->length += $match->getLength();
@@ -74,7 +72,7 @@ public function merge(RouteMatch $match)
7472
*
7573
* @return int
7674
*/
77-
public function getLength()
75+
public function getLength() : int
7876
{
7977
return $this->length;
8078
}

src/Http/Scheme.php

+4-7
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@
88

99
namespace Zend\Router\Http;
1010

11+
use Psr\Http\Message\ServerRequestInterface as Request;
1112
use Traversable;
1213
use Zend\Router\Exception;
1314
use Zend\Stdlib\ArrayUtils;
14-
use Zend\Stdlib\RequestInterface as Request;
15+
use Zend\Router\RouteMatch as BaseRouteMatch;
1516

1617
/**
1718
* Scheme route.
@@ -81,17 +82,13 @@ public static function factory($options = [])
8182
* @param Request $request
8283
* @return RouteMatch|null
8384
*/
84-
public function match(Request $request)
85+
public function match(Request $request) : ?BaseRouteMatch
8586
{
86-
if (! method_exists($request, 'getUri')) {
87-
return;
88-
}
89-
9087
$uri = $request->getUri();
9188
$scheme = $uri->getScheme();
9289

9390
if ($scheme !== $this->scheme) {
94-
return;
91+
return null;
9592
}
9693

9794
return new RouteMatch($this->defaults);

src/Http/Segment.php

+5-9
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@
88

99
namespace Zend\Router\Http;
1010

11+
use Psr\Http\Message\ServerRequestInterface as Request;
1112
use Traversable;
1213
use Zend\I18n\Translator\TranslatorInterface as Translator;
1314
use Zend\Router\Exception;
1415
use Zend\Stdlib\ArrayUtils;
15-
use Zend\Stdlib\RequestInterface as Request;
16+
use Zend\Router\RouteMatch as BaseRouteMatch;
1617

1718
/**
1819
* Segment route.
@@ -357,14 +358,9 @@ protected function buildPath(array $parts, array $mergedParams, $isOptional, $ha
357358
* @return RouteMatch|null
358359
* @throws Exception\RuntimeException
359360
*/
360-
public function match(Request $request, $pathOffset = null, array $options = [])
361+
public function match(Request $request, $pathOffset = null, array $options = []) : ?BaseRouteMatch
361362
{
362-
if (! method_exists($request, 'getUri')) {
363-
return;
364-
}
365-
366-
$uri = $request->getUri();
367-
$path = $uri->getPath();
363+
$path = $request->getUri()->getPath();
368364

369365
$regex = $this->regex;
370366

@@ -389,7 +385,7 @@ public function match(Request $request, $pathOffset = null, array $options = [])
389385
}
390386

391387
if (! $result) {
392-
return;
388+
return null;
393389
}
394390

395391
$matchedLength = strlen($matches[0]);

0 commit comments

Comments
 (0)