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

Commit e4824a3

Browse files
committed
Splitting request attribute population into a private method
1 parent 16a77c3 commit e4824a3

File tree

1 file changed

+24
-7
lines changed

1 file changed

+24
-7
lines changed

src/Controller/MiddlewareController.php

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
namespace Zend\Mvc\Controller;
1111

1212
use Psr\Http\Message\ResponseInterface;
13+
use Psr\Http\Message\ServerRequestInterface;
1314
use Zend\EventManager\EventManager;
1415
use Zend\Http\Request;
1516
use Zend\Http\Response;
@@ -80,13 +81,10 @@ public function onDispatch(MvcEvent $e)
8081
}
8182

8283
$routeMatch = $e->getRouteMatch();
83-
$psr7Request = Psr7ServerRequest::fromZend($request)->withAttribute(RouteMatch::class, $routeMatch);
84-
85-
if ($routeMatch) {
86-
foreach ($routeMatch->getParams() as $key => $value) {
87-
$psr7Request = $psr7Request->withAttribute($key, $value);
88-
}
89-
}
84+
$psr7Request = $this->populateRequestParametersFromRoute(
85+
Psr7ServerRequest::fromZend($request)->withAttribute(RouteMatch::class, $routeMatch),
86+
$routeMatch
87+
);
9088

9189
$result = $this->pipe->process($psr7Request, new CallableDelegateDecorator(
9290
function () {
@@ -99,4 +97,23 @@ function () {
9997

10098
return $result;
10199
}
100+
101+
/**
102+
* @param ServerRequestInterface $request
103+
* @param RouteMatch|null $routeMatch
104+
*
105+
* @return ServerRequestInterface
106+
*/
107+
private function populateRequestParametersFromRoute(ServerRequestInterface $request, RouteMatch $routeMatch = null)
108+
{
109+
if (! $routeMatch) {
110+
return $request;
111+
}
112+
113+
foreach ($routeMatch->getParams() as $key => $value) {
114+
$request = $request->withAttribute($key, $value);
115+
}
116+
117+
return $request;
118+
}
102119
}

0 commit comments

Comments
 (0)