This repository has been archived by the owner on Jul 31, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from tbreuss/develop
Cleaner & better middleware implementation
- Loading branch information
Showing
5 changed files
with
159 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?php | ||
|
||
namespace Tebe\Pvc\Middleware; | ||
|
||
use Psr\Http\Message\ResponseInterface; | ||
use Psr\Http\Message\ServerRequestInterface; | ||
use Psr\Http\Server\MiddlewareInterface; | ||
use Psr\Http\Server\RequestHandlerInterface; | ||
|
||
class MiddlewarePipe implements MiddlewareInterface | ||
{ | ||
/** | ||
* @var ServerMiddlewareInterface[] | ||
*/ | ||
private $middlewares = []; | ||
|
||
private function __construct() | ||
{ | ||
} | ||
|
||
/** | ||
* @param MiddlewareInterface[] $middlewares FIFO array of middlewares | ||
* @return self | ||
*/ | ||
public static function create(array $middlewares = []): self | ||
{ | ||
$pipe = new self(); | ||
foreach ($middlewares as $middleware) { | ||
$pipe = $pipe->add($middleware); | ||
} | ||
return $pipe; | ||
} | ||
|
||
/** | ||
* @param ServerRequestInterface $request | ||
* @param RequestHandlerInterface $requestHandler | ||
* @return ResponseInterface | ||
*/ | ||
public function process(ServerRequestInterface $request, RequestHandlerInterface $requestHandler): ResponseInterface | ||
{ | ||
if (empty($this->middlewares)) { | ||
return $requestHandler->handle($request); | ||
} | ||
$stack = MiddlewareStack::create($requestHandler, array_reverse($this->middlewares)); | ||
return $stack->handle($request); | ||
} | ||
|
||
/** | ||
* Creates a new pipe with the given middleware connected. | ||
* | ||
* @param MiddlewareInterface $middleware | ||
* @return self | ||
*/ | ||
public function add(MiddlewareInterface $middleware): self | ||
{ | ||
$pipe = clone $this; | ||
array_push($pipe->middlewares, $middleware); | ||
return $pipe; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
<?php | ||
|
||
namespace Tebe\Pvc\Middleware; | ||
|
||
use Psr\Http\Message\ResponseInterface; | ||
use Psr\Http\Message\ServerRequestInterface; | ||
use Psr\Http\Server\MiddlewareInterface; | ||
use Psr\Http\Server\RequestHandlerInterface; | ||
|
||
class MiddlewareStack implements RequestHandlerInterface | ||
{ | ||
/** | ||
* @var RequestHandlerInterface | ||
*/ | ||
private $requestHandler; | ||
|
||
/** | ||
* @var MiddlewareInterface[] | ||
*/ | ||
private $middlewares = []; | ||
|
||
/** | ||
* @param RequestHandlerInterface $requestHandler | ||
*/ | ||
private function __construct(RequestHandlerInterface $requestHandler) | ||
{ | ||
$this->requestHandler = $requestHandler; | ||
} | ||
|
||
/** | ||
* @param RequestHandlerInterface $requestHandler | ||
* @param MiddlewareInterface[] $middlewares LIFO array of middlewares | ||
* @return self | ||
*/ | ||
public static function create(RequestHandlerInterface $requestHandler, array $middlewares = []): self | ||
{ | ||
$stack = new self($requestHandler); | ||
foreach ($middlewares as $middleware) { | ||
$stack = $stack->push($middleware); | ||
} | ||
return $stack; | ||
} | ||
|
||
/** | ||
* @param ServerRequestInterface $request | ||
* @return ResponseInterface | ||
*/ | ||
public function handle(ServerRequestInterface $request): ResponseInterface | ||
{ | ||
$next = $this->peek(); | ||
if (null === $next) { | ||
return $this->requestHandler->handle($request); | ||
} | ||
return $next->process($request, $this->pop()); | ||
} | ||
|
||
/** | ||
* Creates a new stack with the given middleware pushed. | ||
* | ||
* @param MiddlewareInterface $middleware | ||
* @return self | ||
*/ | ||
public function push(MiddlewareInterface $middleware): self | ||
{ | ||
$stack = clone $this; | ||
array_unshift($stack->middlewares, $middleware); | ||
return $stack; | ||
} | ||
|
||
/** | ||
* @return MiddlewareInterface|null | ||
*/ | ||
private function peek() | ||
{ | ||
return reset($this->middlewares) ?: null; | ||
} | ||
|
||
/** | ||
* @return self | ||
*/ | ||
private function pop(): self | ||
{ | ||
$stack = clone $this; | ||
array_shift($stack->middlewares); | ||
return $stack; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters