Skip to content

Commit

Permalink
just playing around at this point
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshuaEstes committed Dec 6, 2023
1 parent be86f91 commit ba08051
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 18 deletions.
11 changes: 6 additions & 5 deletions docs/components/http-handler/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,21 @@ Simple PSR-15 Http Handler
composer require sonsofphp/http-handler
```

### Usage
## Usage

Usage is pretty simple.

```php
<?php

use SonsOfPHP\Component\HttpHandler\HttpHandler;
use SonsOfPHP\Component\HttpHandler\MiddlewareStack;

$middlewares = [];
$middlewares[] = new RouterMiddleware();
$middlewares[] = new CookieMiddleware();
$stack = new MiddlewareStack();
$stack->add(new RouterMiddleware());
$stack->add(new CookieMiddleware());
// ...

$app = new HttpHandler($middlewares);
$app = new HttpHandler($stack);
$response = $app->handle($request);
```
6 changes: 3 additions & 3 deletions src/SonsOfPHP/Component/HttpHandler/HttpHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,18 @@
// RequestHandler?
class HttpHandler implements RequestHandlerInterface
{
public function __construct(private array $queue = []) {}
public function __construct(private MiddlewareStack $stack) {}

/**
* {@inheritdoc}
*/
public function handle(ServerRequestInterface $request): ResponseInterface
{
if (0 === count($this->queue)) {
if (0 === $this->stack->count()) {
throw new \Exception('No Middleware in the queue.');
}

$middleware = array_shift($this->queue);
$middleware = $this->stack->shift();

return $middleware->process($request, $this);
}
Expand Down
55 changes: 55 additions & 0 deletions src/SonsOfPHP/Component/HttpHandler/MiddlewareStack.php
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);
}
}
22 changes: 12 additions & 10 deletions src/SonsOfPHP/Component/HttpHandler/Tests/HttpHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use PHPUnit\Framework\TestCase;
use SonsOfPHP\Component\HttpHandler\HttpHandler;
use SonsOfPHP\Component\HttpHandler\MiddlewareStack;
use Psr\Http\Server\RequestHandlerInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Message\ServerRequestInterface;
Expand All @@ -16,32 +17,27 @@
* @coversDefaultClass \SonsOfPHP\Component\HttpHandler\HttpHandler
*
* @uses \SonsOfPHP\Component\HttpHandler\HttpHandler
* @uses \SonsOfPHP\Component\HttpHandler\MiddlewareStack
*/
final class HttpHandlerTest extends TestCase
{
private $request;
private $response;
private array $middlewares = [];
private MiddlewareStack $stack;

protected function setUp(): void
{
$this->request = $this->createMock(ServerRequestInterface::class);
$this->response = $this->createMock(ResponseInterface::class);

$this->middlewares[] = new class implements MiddlewareInterface {
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
return new Response();
}
};
$this->stack = new MiddlewareStack();
}

/**
* @coversNothing
*/
public function testItHasTheCorrectInterface(): void
{
$handler = new HttpHandler();
$handler = new HttpHandler($this->stack);

$this->assertInstanceOf(RequestHandlerInterface::class, $handler);
}
Expand All @@ -51,7 +47,13 @@ public function testItHasTheCorrectInterface(): void
*/
public function testHandle(): void
{
$handler = new HttpHandler($this->middlewares);
$this->stack->add(new class implements MiddlewareInterface {
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
return new Response();
}
});
$handler = new HttpHandler($this->stack);

$this->assertNotNull($handler->handle($this->request));
}
Expand Down

0 comments on commit ba08051

Please sign in to comment.