Skip to content

Commit

Permalink
[HttpHandler] Component (#187)
Browse files Browse the repository at this point in the history
## Description



## Checklist
- [x] Updated CHANGELOG files
- [x] Updated Documentation
- [x] Unit Tests Created
- [x] php-cs-fixer
  • Loading branch information
JoshuaEstes authored Dec 7, 2023
1 parent 4842439 commit c81c9a3
Show file tree
Hide file tree
Showing 30 changed files with 672 additions and 11 deletions.
1 change: 1 addition & 0 deletions .github/CODEOWNERS
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ docs/ @JoshuaEstes
/src/SonsOfPHP/**/FeatureToggle @JoshuaEstes
/src/SonsOfPHP/**/Filesystem @JoshuaEstes
/src/SonsOfPHP/**/HttpFactory @JoshuaEstes
/src/SonsOfPHP/**/HttpHandler @JoshuaEstes
/src/SonsOfPHP/**/HttpMessage @JoshuaEstes
/src/SonsOfPHP/**/Json @JoshuaEstes
/src/SonsOfPHP/**/Link @JoshuaEstes
Expand Down
4 changes: 4 additions & 0 deletions .github/labeler.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ HttpFactory:
- docs/components/http-factory/*
- src/SonsOfPHP/**/HttpFactory/*

HttpHandler:
- docs/components/http-handler/*
- src/SonsOfPHP/**/HttpHandler/*

HttpMessage:
- docs/components/http-message/*
- src/SonsOfPHP/**/HttpMessage/*
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ To get the diff between two versions, go to https://github.com/SonsOfPHP/sonsofp
* [PR #173](https://github.com/SonsOfPHP/sonsofphp/pull/173) [Money] Twig Bridge
* [PR #181](https://github.com/SonsOfPHP/sonsofphp/pull/181) [Cookie] New Component and Contract
* [PR #182](https://github.com/SonsOfPHP/sonsofphp/pull/182) [Container] New Component (PSR-11)
* [PR #187](https://github.com/SonsOfPHP/sonsofphp/pull/187) [HttpHandler] New Component (PSR-15) and Contract

## [0.3.8]

Expand Down
6 changes: 6 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ test-cqrs: phpunit
test-http-factory: PHPUNIT_TESTSUITE=http-factory
test-http-factory: phpunit

test-http-handler: PHPUNIT_TESTSUITE=http-handler
test-http-handler: phpunit

test-link: PHPUNIT_TESTSUITE=link
test-link: phpunit

Expand Down Expand Up @@ -132,6 +135,9 @@ coverage-filesystem:
coverage-http-factory:
XDEBUG_MODE=coverage $(PHP) -dxdebug.mode=coverage $(PHPUNIT) --testsuite http-factory --coverage-html $(COVERAGE_DIR)

coverage-http-handler: PHPUNIT_TESTSUITE=http-handler
coverage-http-handler: coverage

coverage-http-message:
XDEBUG_MODE=coverage $(PHP) -dxdebug.mode=coverage $(PHPUNIT) --testsuite http-message --coverage-html $(COVERAGE_DIR)

Expand Down
8 changes: 8 additions & 0 deletions bard.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,14 @@
"path": "src/SonsOfPHP/Component/HttpFactory",
"repository": "git@github.com:SonsOfPHP/http-factory.git"
},
{
"path": "src/SonsOfPHP/Component/HttpHandler",
"repository": "git@github.com:SonsOfPHP/http-handler.git"
},
{
"path": "src/SonsOfPHP/Contract/HttpHandler",
"repository": "git@github.com:SonsOfPHP/http-handler-contract.git"
},
{
"path": "src/SonsOfPHP/Component/HttpMessage",
"repository": "git@github.com:SonsOfPHP/http-message.git"
Expand Down
21 changes: 17 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,11 @@
"sonsofphp/logger-implementation": "0.3.x-dev",
"sonsofphp/pager-implementation": "0.3.x-dev",
"psr/link-implementation": "^1.0 || ^2.0",
"sonsofphp/cookie-implementation": "0.3.x-dev"
"sonsofphp/cookie-implementation": "0.3.x-dev",
"psr/container-implementation": "^1.0 || ^2.0",
"psr/http-server-handler-implementation": "^1.0",
"psr/http-server-middleware-implementation": "^1.0",
"sonsofphp/http-handler-implementation": "0.3.x-dev"
},
"require": {
"php": ">=8.1",
Expand Down Expand Up @@ -77,7 +81,10 @@
"doctrine/collections": "^2",
"doctrine/orm": "^2",
"sonsofphp/cookie-contract": "0.3.x-dev",
"psr/container": "^1.0 || ^2.0"
"psr/container": "^1.0 || ^2.0",
"psr/http-server-handler": "^1.0",
"psr/http-server-middleware": "^1.0",
"sonsofphp/http-handler-contract": "0.3.x-dev"
},
"replace": {
"sonsofphp/bard": "self.version",
Expand Down Expand Up @@ -116,7 +123,9 @@
"sonsofphp/pager-doctrine-orm": "self.version",
"sonsofphp/cookie": "self.version",
"sonsofphp/cookie-contract": "self.version",
"sonsofphp/container": "self.version"
"sonsofphp/container": "self.version",
"sonsofphp/http-handler": "self.version",
"sonsofphp/http-handler-contract": "self.version"
},
"autoload": {
"psr-4": {
Expand All @@ -136,6 +145,8 @@
"SonsOfPHP\\Component\\FeatureToggle\\": "src/SonsOfPHP/Component/FeatureToggle",
"SonsOfPHP\\Component\\Filesystem\\": "src/SonsOfPHP/Component/Filesystem",
"SonsOfPHP\\Component\\HttpFactory\\": "src/SonsOfPHP/Component/HttpFactory",
"SonsOfPHP\\Component\\HttpHandler\\": "src/SonsOfPHP/Component/HttpHandler",
"SonsOfPHP\\Contract\\HttpHandler\\": "src/SonsOfPHP/Contract/HttpHandler",
"SonsOfPHP\\Component\\HttpMessage\\": "src/SonsOfPHP/Component/HttpMessage",
"SonsOfPHP\\Component\\Json\\": "src/SonsOfPHP/Component/Json",
"SonsOfPHP\\Component\\Link\\": "src/SonsOfPHP/Component/Link",
Expand Down Expand Up @@ -174,6 +185,7 @@
"src/SonsOfPHP/Component/FeatureToggle/Tests",
"src/SonsOfPHP/Component/Filesystem/Tests",
"src/SonsOfPHP/Component/HttpFactory/Tests",
"src/SonsOfPHP/Component/HttpHandler/Tests",
"src/SonsOfPHP/Component/HttpMessage/Tests",
"src/SonsOfPHP/Component/Json/Tests",
"src/SonsOfPHP/Component/Link/Tests",
Expand All @@ -199,7 +211,8 @@
"symfony/error-handler": "^6",
"symfony/messenger": "^5 || ^6",
"phpunit/phpunit": "^10.4",
"psr/simple-cache": "^1.0 || ^2.0 || ^3.0"
"psr/simple-cache": "^1.0 || ^2.0 || ^3.0",
"sonsofphp/http-message": "0.3.x-dev"
},
"autoload-dev": {
"psr-4": {
Expand Down
56 changes: 56 additions & 0 deletions docs/components/http-handler/index.md
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.
1 change: 1 addition & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ nav:
- components/filesystem/index.md
- Adapters: components/filesystem/adapters.md
- HttpFactory: components/http-factory/index.md
- HttpHandler: components/http-handler/index.md
- HttpMessage: components/http-message/index.md
- JSON: components/json/index.md
- Link: components/link/index.md
Expand Down
4 changes: 4 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@
<directory>src/SonsOfPHP/Component/HttpFactory/Tests</directory>
</testsuite>

<testsuite name="http-handler">
<directory>src/SonsOfPHP/Component/HttpHandler/Tests</directory>
</testsuite>

<testsuite name="http-message">
<directory>src/SonsOfPHP/Component/HttpMessage/Tests</directory>
</testsuite>
Expand Down
10 changes: 5 additions & 5 deletions src/SonsOfPHP/Component/Container/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Sons of PHP - Cookie
====================
Sons of PHP - Container
=======================

## Learn More

Expand All @@ -11,6 +11,6 @@ Sons of PHP - Cookie
[discussions]: https://github.com/orgs/SonsOfPHP/discussions
[mother-repo]: https://github.com/SonsOfPHP/sonsofphp
[contributing]: https://docs.sonsofphp.com/contributing/
[docs]: https://docs.sonsofphp.com/components/cookie/
[issues]: https://github.com/SonsOfPHP/sonsofphp/issues?q=is%3Aopen+is%3Aissue+label%3ACookie
[pull-requests]: https://github.com/SonsOfPHP/sonsofphp/pulls?q=is%3Aopen+is%3Apr+label%3ACookie
[docs]: https://docs.sonsofphp.com/components/container/
[issues]: https://github.com/SonsOfPHP/sonsofphp/issues?q=is%3Aopen+is%3Aissue+label%3AContainer
[pull-requests]: https://github.com/SonsOfPHP/sonsofphp/pulls?q=is%3Aopen+is%3Apr+label%3AContainer
4 changes: 2 additions & 2 deletions src/SonsOfPHP/Component/Container/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"psr/container": "^1.0 || ^2.0"
},
"provide": {
"psr/container": "2.0"
"psr/container-implementation": "^1.0 || ^2.0"
},
"extra": {
"sort-packages": true,
Expand All @@ -53,4 +53,4 @@
"url": "https://tidelift.com/subscription/pkg/packagist-sonsofphp-sonsofphp"
}
]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

/**
* @coversDefaultClass \SonsOfPHP\Component\EventSourcing\Test\CountEventsRaised
*
* @uses \SonsOfPHP\Component\EventSourcing\Test\CountEventsRaised
*/
final class CountEventsRaisedTest extends TestCase
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

/**
* @coversDefaultClass \SonsOfPHP\Component\EventSourcing\Test\EventRaised
*
* @uses \SonsOfPHP\Component\EventSourcing\Test\EventRaised
*/
final class EventRaisedTest extends TestCase
{
Expand Down
4 changes: 4 additions & 0 deletions src/SonsOfPHP/Component/HttpHandler/.gitattributes
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
3 changes: 3 additions & 0 deletions src/SonsOfPHP/Component/HttpHandler/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
composer.lock
phpunit.xml
vendor/
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 {}
31 changes: 31 additions & 0 deletions src/SonsOfPHP/Component/HttpHandler/HttpHandler.php
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);
}
}
19 changes: 19 additions & 0 deletions src/SonsOfPHP/Component/HttpHandler/LICENSE
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.
65 changes: 65 additions & 0 deletions src/SonsOfPHP/Component/HttpHandler/MiddlewareStack.php
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);
}
}
Loading

0 comments on commit c81c9a3

Please sign in to comment.