-
-
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.
## Description ## Checklist - [x] Updated CHANGELOG files - [x] Updated Documentation - [x] Unit Tests Created - [x] php-cs-fixer
- Loading branch information
1 parent
4842439
commit c81c9a3
Showing
30 changed files
with
672 additions
and
11 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
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
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,56 @@ | ||
--- | ||
title: HttpHandler | ||
--- | ||
|
||
Simple PSR-15 Http Handler | ||
|
||
## Installation | ||
|
||
```shell | ||
composer require sonsofphp/http-handler | ||
``` | ||
|
||
## Usage | ||
|
||
Usage is pretty simple. | ||
|
||
```php | ||
<?php | ||
|
||
use SonsOfPHP\Component\HttpHandler\HttpHandler; | ||
use SonsOfPHP\Component\HttpHandler\MiddlewareStack; | ||
|
||
$stack = new MiddlewareStack(); | ||
$stack->add(new RouterMiddleware()); | ||
$stack->add(new CookieMiddleware()); | ||
$stack->add(function ($request, $handler) { | ||
// ... | ||
}); | ||
// ... | ||
|
||
$app = new HttpHandler($stack); | ||
$response = $app->handle($request); | ||
``` | ||
|
||
The `MiddlewareStack` accepts objects that implement `Psr\Http\Server\MiddlewareInterface` | ||
and anonymous functions. | ||
|
||
### Middleware Priorities | ||
|
||
An optional second argument may be passed to the `MiddlewareStack` which is for | ||
the priority of the middleware. Priorities are ordered in ascending order. | ||
|
||
```php | ||
<?php | ||
|
||
use SonsOfPHP\Component\HttpHandler\MiddlewareStack; | ||
|
||
$stack = new MiddlewareStack(); | ||
$stack->add(new NotFoundMiddleware(), 1025); | ||
$stack->add(new RouterMiddleware(), 255); | ||
$stack->add(new CookieMiddleware(), -255); | ||
$stack->add(new DefaultMiddleware()); | ||
``` | ||
|
||
In the above example, the `CookieMiddleware` will be processed first and | ||
`NotFoundMiddleware` will be processed last. |
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
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
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,4 @@ | ||
/Tests export-ignore | ||
/phpunit.xml.dist export-ignore | ||
/.gitattributes export-ignore | ||
/.gitignore export-ignore |
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,3 @@ | ||
composer.lock | ||
phpunit.xml | ||
vendor/ |
12 changes: 12 additions & 0 deletions
12
src/SonsOfPHP/Component/HttpHandler/Exception/HttpHandlerException.php
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,12 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace SonsOfPHP\Component\HttpHandler\Exception; | ||
|
||
use SonsOfPHP\Contract\HttpHandler\HttpHandlerExceptionInterface; | ||
|
||
/** | ||
* @author Joshua Estes <joshua@sonsofphp.com> | ||
*/ | ||
class HttpHandlerException extends \Exception implements HttpHandlerExceptionInterface {} |
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,31 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace SonsOfPHP\Component\HttpHandler; | ||
|
||
use Psr\Http\Message\ResponseInterface; | ||
use Psr\Http\Message\ServerRequestInterface; | ||
use Psr\Http\Server\RequestHandlerInterface; | ||
|
||
/** | ||
* @author Joshua Estes <joshua@sonsofphp.com> | ||
*/ | ||
class HttpHandler implements RequestHandlerInterface | ||
{ | ||
public function __construct(private MiddlewareStack $stack) {} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function handle(ServerRequestInterface $request): ResponseInterface | ||
{ | ||
if (0 === $this->stack->count()) { | ||
throw new \Exception('No Middleware in the queue.'); | ||
} | ||
|
||
$middleware = $this->stack->next(); | ||
|
||
return $middleware->process($request, $this); | ||
} | ||
} |
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,19 @@ | ||
Copyright 2022 to Present Joshua Estes | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy of | ||
this software and associated documentation files (the "Software"), to deal in | ||
the Software without restriction, including without limitation the rights to | ||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies | ||
of the Software, and to permit persons to whom the Software is furnished to do | ||
so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
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,65 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace SonsOfPHP\Component\HttpHandler; | ||
|
||
use Psr\Http\Message\ResponseInterface; | ||
use Psr\Http\Message\ServerRequestInterface; | ||
use Psr\Http\Server\MiddlewareInterface; | ||
use Psr\Http\Server\RequestHandlerInterface; | ||
use SonsOfPHP\Contract\HttpHandler\MiddlewareStackInterface; | ||
|
||
/** | ||
* @author Joshua Estes <joshua@sonsofphp.com> | ||
*/ | ||
class MiddlewareStack implements MiddlewareStackInterface | ||
{ | ||
private array $middlewares = []; | ||
//private $resolver; | ||
|
||
//public function __construct($resolver) | ||
//{ | ||
// $this->resolver = $resolver; | ||
//} | ||
|
||
/** | ||
* Adds a new middleware to the stack. Middlewares can be prioritized and | ||
* will be ordered from the lowest number to the highest number (ascending | ||
* order). | ||
*/ | ||
public function add(MiddlewareInterface|\Closure $middleware, int $priority = 0): self | ||
{ | ||
$this->middlewares[$priority][] = $middleware; | ||
ksort($this->middlewares); | ||
|
||
return $this; | ||
} | ||
|
||
public function next(): MiddlewareInterface | ||
{ | ||
$priorityStack = array_shift($this->middlewares); | ||
$middleware = array_shift($priorityStack); | ||
if (0 !== count($priorityStack)) { | ||
array_unshift($this->middlewares, $priorityStack); | ||
} | ||
|
||
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); | ||
} | ||
}; | ||
} | ||
|
||
return $middleware; | ||
} | ||
|
||
public function count(): int | ||
{ | ||
return count($this->middlewares); | ||
} | ||
} |
Oops, something went wrong.