-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
be86f91
commit ba08051
Showing
4 changed files
with
76 additions
and
18 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 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 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,55 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace SonsOfPHP\Component\HttpHandler; | ||
|
||
use Psr\Http\Server\MiddlewareInterface; | ||
|
||
class MiddlewareStack | ||
{ | ||
private array $middlewares = []; | ||
|
||
public function __construct($resolver) | ||
{ | ||
$this->resolver = $resolver; | ||
} | ||
|
||
public function add($middleware): self | ||
{ | ||
$this->middlewares[] = $middleware; | ||
|
||
return $this; | ||
} | ||
|
||
public function shift(): MiddlewareInterface | ||
{ | ||
$middleware = array_shift($this->middlewares); | ||
|
||
if ($middleware instanceof MiddlewareInterface) { | ||
return $middleware; | ||
} | ||
|
||
if ($middleware instanceof \Closure) { | ||
return new class($middleware) implements MiddlewareInterface { | ||
public function __construct(private \Closure $closure) {} | ||
|
||
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface | ||
{ | ||
return $this->closure($request, $handler); | ||
} | ||
}; | ||
} | ||
|
||
if (is_string($middleware)) { | ||
// use the resolver to figure out wtf this is | ||
} | ||
|
||
throw new \Exception('Unknown Middleware Type: ' . gettype($middleware)); | ||
} | ||
|
||
public function count(): int | ||
{ | ||
return count($this->middlewares); | ||
} | ||
} |
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