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

Commit 41f6194

Browse files
committed
Decorate callable double-pass middleware to fix deprecation errors
1 parent 7f752a7 commit 41f6194

File tree

1 file changed

+41
-8
lines changed

1 file changed

+41
-8
lines changed

src/MiddlewareListener.php

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,26 @@
11
<?php
22
/**
3-
* @link http://github.com/zendframework/zend-mvc for the canonical source repository
4-
* @copyright Copyright (c) 2005-2018 Zend Technologies USA Inc. (http://www.zend.com)
3+
* @see https://github.com/zendframework/zend-mvc for the canonical source repository
4+
* @copyright Copyright (c) 2018 Zend Technologies USA Inc. (http://www.zend.com)
55
* @license https://github.com/zendframework/zend-mvc/blob/master/LICENSE.md New BSD License
66
*/
77

88
namespace Zend\Mvc;
99

10+
use Closure;
1011
use Interop\Container\ContainerInterface;
12+
use Interop\Http\ServerMiddleware\DelegateInterface;
1113
use Interop\Http\ServerMiddleware\MiddlewareInterface;
12-
use Psr\Http\Message\ResponseInterface as PsrResponseInterface;
1314
use Psr\Http\Message\ResponseInterface;
14-
use Psr\Http\Message\ServerRequestInterface as PsrServerRequestInterface;
15+
use Psr\Http\Message\ResponseInterface as PsrResponseInterface;
16+
use ReflectionFunction;
17+
use ReflectionMethod;
1518
use Zend\EventManager\AbstractListenerAggregate;
1619
use Zend\EventManager\EventManagerInterface;
17-
use Zend\Mvc\Exception\InvalidMiddlewareException;
18-
use Zend\Mvc\Exception\ReachedFinalHandlerException;
1920
use Zend\Mvc\Controller\MiddlewareController;
21+
use Zend\Mvc\Exception\InvalidMiddlewareException;
2022
use Zend\Psr7Bridge\Psr7Response;
21-
use Zend\Router\RouteMatch;
22-
use Zend\Stratigility\Delegate\CallableDelegateDecorator;
23+
use Zend\Stratigility\Middleware\DoublePassMiddlewareDecorator;
2324
use Zend\Stratigility\MiddlewarePipe;
2425

2526
class MiddlewareListener extends AbstractListenerAggregate
@@ -146,6 +147,19 @@ private function createPipeFromSpec(
146147
throw InvalidMiddlewareException::fromMiddlewareName($middlewareName);
147148
}
148149

150+
// Decorate double-pass middleware
151+
if (is_callable($middlewareToBePiped)) {
152+
$r = $this->getReflectionFunction($middlewareToBePiped);
153+
$paramsCount = $r->getNumberOfParameters();
154+
155+
$params = $r->getParameters();
156+
$type = isset($params[1]) ? $params[1]->getClass() : null;
157+
158+
if ($paramsCount !== 2 || ! $type || ! is_a($type->getName(), DelegateInterface::class, true)) {
159+
$middlewareToBePiped = new DoublePassMiddlewareDecorator($middlewareToBePiped);
160+
}
161+
}
162+
149163
$pipe->pipe($middlewareToBePiped);
150164
}
151165
return $pipe;
@@ -184,4 +198,23 @@ protected function marshalInvalidMiddleware(
184198
}
185199
return $return;
186200
}
201+
202+
/**
203+
* @param callable $middleware
204+
* @return \ReflectionFunctionAbstract
205+
*/
206+
private function getReflectionFunction(callable $middleware)
207+
{
208+
if (is_array($middleware)) {
209+
$class = array_shift($middleware);
210+
$method = array_shift($middleware);
211+
return new ReflectionMethod($class, $method);
212+
}
213+
214+
if ($middleware instanceof Closure || ! is_object($middleware)) {
215+
return new ReflectionFunction($middleware);
216+
}
217+
218+
return new ReflectionMethod($middleware, '__invoke');
219+
}
187220
}

0 commit comments

Comments
 (0)