Skip to content
Permalink
Browse files

Allow registering middleware as string with RouteCollection.

  • Loading branch information...
ADmad committed Nov 7, 2017
1 parent e6e3179 commit eaca9ce156fb5c52166de14161ae5e647c1d8533
@@ -970,7 +970,7 @@ public function fallbacks($routeClass = null)
* scope or any child scopes that share the same RouteCollection.
*
* @param string $name The name of the middleware. Used when applying middleware to a scope.
* @param callable $middleware The middleware object to register.
* @param callable|string $middleware The middleware callable or class name to register.
* @return $this
* @see \Cake\Routing\RouteCollection
*/
@@ -418,14 +418,11 @@ public function setExtensions(array $extensions, $merge = true)
* scope or any child scopes that share the same RouteCollection.
*
* @param string $name The name of the middleware. Used when applying middleware to a scope.
* @param callable $middleware The middleware object to register.
* @param callable|string $middleware The middleware callable or class name to register.
* @return $this
*/
public function registerMiddleware($name, callable $middleware)
public function registerMiddleware($name, $middleware)
{
if (is_string($middleware)) {
throw new RuntimeException("The '$name' middleware is not a callable object.");
}
$this->_middleware[$name] = $middleware;
return $this;
@@ -20,6 +20,7 @@
use TestApp\Application;
use Zend\Diactoros\Response;
use Zend\Diactoros\ServerRequestFactory;
use TestApp\Middleware\DumbMiddleware;
/**
* Test for RoutingMiddleware
@@ -247,8 +248,10 @@ public function testInvokeScopedMiddleware()
return $next($req, $res);
});
$routes->registerMiddleware('dumb', DumbMiddleware::class);
// Connect middleware in reverse to test ordering.
$routes->applyMiddleware('second', 'first');
$routes->applyMiddleware('second', 'first', 'dumb');
$routes->connect('/ping', ['controller' => 'Pings']);
});
@@ -871,19 +871,6 @@ public function testRegisterMiddleware()
$this->assertTrue($this->collection->middlewareExists('test'));
}
/**
* Test registering invalid middleware
*
* @expectedException \RuntimeException
* @expectedExceptionMessage The 'bad' middleware is not a callable object.
* @return void
*/
public function testRegisterMiddlewareString()
{
$routes = new RouteBuilder($this->collection, '/api');
$routes->registerMiddleware('bad', 'strlen');
}
/**
* Test middleware group
*
@@ -630,18 +630,6 @@ public function testSetExtensions()
$this->assertEquals(['csv'], $this->collection->getExtensions());
}
/**
* String methods are not acceptable.
*
* @expectedException \RuntimeException
* @expectedExceptionMessage The 'bad' middleware is not a callable object.
* @return void
*/
public function testRegisterMiddlewareNoCallableString()
{
$this->collection->registerMiddleware('bad', 'strlen');
}
/**
* Test adding middleware to the collection.
*
@@ -661,6 +649,8 @@ public function testRegisterMiddleware()
$this->assertTrue($this->collection->hasMiddleware('closure'));
$this->assertTrue($this->collection->hasMiddleware('callable'));
$this->collection->registerMiddleware('class', 'Dumb');
}
/**
@@ -17,7 +17,7 @@
/**
* Testing stub for middleware tests.
*/
class DumbMiddleWare
class DumbMiddleware
{
public function __invoke($request, $response, $next)
{

0 comments on commit eaca9ce

Please sign in to comment.
You can’t perform that action at this time.