Rename Router->addMiddleware to Router->add

This commit is contained in:
PJ Dietz 2018-06-29 16:29:46 -04:00
parent 677cdb4d7d
commit e676a17cac
5 changed files with 15 additions and 13 deletions

View File

@ -1,7 +1,7 @@
Changes from Version 3 Changes from Version 3
====================== ======================
If your project uses WellRESTed version 3, you can most likely upgrade to to version 4 without making any changes to your code. However, there are a few changes that may affect some users. If your project uses WellRESTed version 3, you can most likely upgrade to to version 4 without making any changes to your code. However, there are a few changes that may affect some users, particularly users who have subclassed ``WellRESTed\Server`` or used custom implementations of other ``WellRESTed`` classes.
Server Configuration Server Configuration
^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^

View File

@ -297,9 +297,9 @@ When a router is unable to match the route, it will delegate to the next middlew
Router-specific Middleware Router-specific Middleware
^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^
WellRESTed version 4 allows a Router to have a set of middleware to dispatch whenever it finds a route that matches. This middleware runs before the handler for the matched route, and only if a route matches. WellRESTed allows a Router to have a set of middleware to dispatch whenever it finds a route that matches. This middleware runs before the handler for the matched route, and only when a route matches.
This feature allows you to build a site where some sections use certain middleware and other do not. For example, suppose your site has a public section that does not require authentication and a section that does require authentication. We can use a different router for each section, and provide authentication middleware on only the router for the private area. This feature allows you to build a site where some sections use certain middleware and other do not. For example, suppose your site has a public section that does not require authentication and a private section that does. We can use a different router for each section, and provide authentication middleware on only the router for the private area.
.. code-block:: php .. code-block:: php
@ -317,7 +317,7 @@ This feature allows you to build a site where some sections use certain middlewa
$private = $server->createRouter(); $private = $server->createRouter();
// Authorizaiton middleware checks for an Authorization header and // Authorizaiton middleware checks for an Authorization header and
// responds 401 when the header is missing or invalid. // responds 401 when the header is missing or invalid.
$private->addMiddleware($authorizaitonMiddleware); $private->add($authorizaitonMiddleware);
$private->register('GET', '/secret', $secretHandler); $private->register('GET', '/secret', $secretHandler);
$private->register('GET', '/members-only', $otherHandler); $private->register('GET', '/members-only', $otherHandler);
$server->add($private); $server->add($private);

View File

@ -158,8 +158,10 @@ class Router
} }
/** /**
* Push a new middleware onto the stack. Middleware for a router runs only * Push a new middleware onto the stack.
* when the router has a route matching the request. *
* Middleware for a router runs before the middleware and handler for the
* matched route and runs only when a route matched.
* *
* $middleware may be: * $middleware may be:
* - An instance implementing MiddlewareInterface * - An instance implementing MiddlewareInterface
@ -172,7 +174,7 @@ class Router
* @param mixed $middleware Middleware to dispatch in sequence * @param mixed $middleware Middleware to dispatch in sequence
* @return static * @return static
*/ */
public function addMiddleware($middleware) public function add($middleware)
{ {
$this->stack[] = $middleware; $this->stack[] = $middleware;
return $this; return $this;

View File

@ -125,7 +125,7 @@ class RoutingTest extends TestCase
public function testDispatchesMiddlewareSpecificToRouter() public function testDispatchesMiddlewareSpecificToRouter()
{ {
$catRouter = $this->server->createRouter() $catRouter = $this->server->createRouter()
->addMiddleware(new HeaderAdderMiddleware( ->add(new HeaderAdderMiddleware(
'Content-type', 'application/cat')) 'Content-type', 'application/cat'))
->register('GET', '/molly', new StringHandler('Molly')) ->register('GET', '/molly', new StringHandler('Molly'))
->register('GET', '/oscar', new StringHandler('Oscar')) ->register('GET', '/oscar', new StringHandler('Oscar'))
@ -133,7 +133,7 @@ class RoutingTest extends TestCase
$this->server->add($catRouter); $this->server->add($catRouter);
$dogRouter = $this->server->createRouter() $dogRouter = $this->server->createRouter()
->addMiddleware(new HeaderAdderMiddleware( ->add(new HeaderAdderMiddleware(
'Content-type', 'application/dog')) 'Content-type', 'application/dog'))
->register('GET', '/bear', new StringHandler('Bear')); ->register('GET', '/bear', new StringHandler('Bear'));
$this->server->add($dogRouter); $this->server->add($dogRouter);
@ -152,7 +152,7 @@ class RoutingTest extends TestCase
public function testResponds404WhenNoRouteMatched() public function testResponds404WhenNoRouteMatched()
{ {
$catRouter = $this->server->createRouter() $catRouter = $this->server->createRouter()
->addMiddleware(new HeaderAdderMiddleware( ->add(new HeaderAdderMiddleware(
'Content-type', 'application/cat')) 'Content-type', 'application/cat'))
->register('GET', '/molly', new StringHandler('Molly')) ->register('GET', '/molly', new StringHandler('Molly'))
->register('GET', '/oscar', new StringHandler('Oscar')) ->register('GET', '/oscar', new StringHandler('Oscar'))
@ -160,7 +160,7 @@ class RoutingTest extends TestCase
$this->server->add($catRouter); $this->server->add($catRouter);
$dogRouter = $this->server->createRouter() $dogRouter = $this->server->createRouter()
->addMiddleware(new HeaderAdderMiddleware( ->add(new HeaderAdderMiddleware(
'Content-type', 'application/dog')) 'Content-type', 'application/dog'))
->register('GET', '/bear', new StringHandler('Bear')); ->register('GET', '/bear', new StringHandler('Bear'));
$this->server->add($dogRouter); $this->server->add($dogRouter);

View File

@ -401,7 +401,7 @@ class RouterTest extends TestCase
return $next($middlewareRequest, $middlewareResponse); return $next($middlewareRequest, $middlewareResponse);
}; };
$this->router->addMiddleware($middleware); $this->router->add($middleware);
$this->router->register("GET", "/", "Handler"); $this->router->register("GET", "/", "Handler");
$this->router->__invoke($this->request, $this->response, $this->next); $this->router->__invoke($this->request, $this->response, $this->next);
@ -429,7 +429,7 @@ class RouterTest extends TestCase
$this->request = $this->request->withRequestTarget("/no/match"); $this->request = $this->request->withRequestTarget("/no/match");
$this->router->addMiddleware($middleware); $this->router->add($middleware);
$this->router->register("GET", "/", "Handler"); $this->router->register("GET", "/", "Handler");
$this->router->__invoke($this->request, $this->response, $this->next); $this->router->__invoke($this->request, $this->response, $this->next);