Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ Any Router which implements `Chubbyphp\Framework\Router\RouteMatcherInterface` c
Through [Composer](http://getcomposer.org) as [chubbyphp/chubbyphp-framework][60].

```bash
composer require chubbyphp/chubbyphp-framework "^5.3" \
composer require chubbyphp/chubbyphp-framework "^6.0" \
chubbyphp/chubbyphp-framework-router-fastroute "^2.3" \
slim/psr7 "^1.7"
```
Expand Down Expand Up @@ -124,7 +124,7 @@ $app->emit($app->handle((new ServerRequestFactory())->createFromGlobals()));
* [CallbackMiddleware][70]
* [ExceptionMiddleware][71]
* [LazyMiddleware][72]
* [MiddlewareDispatcher][73]
* [PipeMiddleware][73]
* [RouteMatcherMiddleware][74]
* [SlimCallbackMiddleware][75]
* [SlimLazyMiddleware][76]
Expand Down Expand Up @@ -158,6 +158,7 @@ $app->emit($app->handle((new ServerRequestFactory())->createFromGlobals()));

## Migration

* [5.x to 6.x][214]
* [4.x to 5.x][213]
* [3.x to 4.x][212]
* [2.x to 3.x][211]
Expand Down Expand Up @@ -208,7 +209,7 @@ $app->emit($app->handle((new ServerRequestFactory())->createFromGlobals()));
[70]: doc/Middleware/CallbackMiddleware.md
[71]: doc/Middleware/ExceptionMiddleware.md
[72]: doc/Middleware/LazyMiddleware.md
[73]: doc/Middleware/MiddlewareDispatcher.md
[73]: doc/Middleware/PipeMiddleware.md
[74]: doc/Middleware/RouteMatcherMiddleware.md
[75]: doc/Middleware/SlimCallbackMiddleware.md
[76]: doc/Middleware/SlimLazyMiddleware.md
Expand Down Expand Up @@ -236,5 +237,6 @@ $app->emit($app->handle((new ServerRequestFactory())->createFromGlobals()));
[211]: doc/Migration/2.x-3.x.md
[212]: doc/Migration/3.x-4.x.md
[213]: doc/Migration/4.x-5.x.md
[214]: doc/Migration/5.x-6.x.md

[219]: doc/Migration/Slim-Chubbyphp.md
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
},
"extra": {
"branch-alias": {
"dev-master": "5.3-dev"
"dev-master": "6.0-dev"
}
},
"scripts": {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# MiddlewareDispatcher
# PipeMiddleware

## Methods

Expand All @@ -7,7 +7,7 @@
```php
<?php

use Chubbyphp\Framework\Middleware\MiddlewareDispatcher;
use Chubbyphp\Framework\Middleware\PipeMiddleware;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
Expand Down Expand Up @@ -37,7 +37,7 @@ $handler = new class() implements RequestHandlerInterface {
}
};

$middlewareDispatcher = new MiddlewareDispatcher();
$pipeMiddleware = new PipeMiddleware([$middleware1, $middleware2]);

$response = $middlewareDispatcher->dispatch([$middleware1, $middleware2], $handler, $request);
$response = $pipeMiddleware->dispatch($request, $handler);
```
12 changes: 12 additions & 0 deletions doc/Migration/5.x-6.x.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# 5.x to 6.x

`Chubbyphp\Framework\Middleware\MiddlewareDispatcher` gets dropped and replaced by `Chubbyphp\Framework\Middleware\PipeMiddleware`.
`Chubbyphp\Framework\Middleware\MiddlewareDispatcher` as an implements of `Chubbyphp\Framework\Middleware\MiddlewareDispatcherInterface` which is gone as well.

This makes the framework as bit less flexible for special use cases, but easier in use for the majority.

Resulting changes:
- `Chubbyphp\Framework\Handler\RouteRequestHandler::__construct` does not take a `Chubbyphp\Framework\Middleware\MiddlewareDispatcherInterface` as argument anymore.
- `Chubbyphp\Framework\Application::__construct` does not take a `Chubbyphp\Framework\Middleware\MiddlewareDispatcherInterface` as argument anymore.

`Chubbyphp\Framework\Collection` is gone, use phpstan if you want to make sure you providing the correct types on array of.
7 changes: 2 additions & 5 deletions doc/RequestHandler/RouteRequestHandler.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
```php
<?php

use Chubbyphp\Framework\Middleware\MiddlewareDispatcher;
use Chubbyphp\Framework\RequestHandler\RouteRequestHandler;
use Psr\Http\Message\ServerRequestInterface;
use Some\Psr7\Response;
Expand All @@ -16,9 +15,7 @@ use Some\Psr7\ServerRequest;
$request = new ServerRequest();
$response = new Response();

$middlewareDispatcher = new MiddlewareDispatcher();
$handler = new RouteRequestHandler();

$callbackHandler = new RouteRequestHandler($middlewareDispatcher);

$response = $callbackHandler->handle($request);
$response = $handler->handle($request);
```
5 changes: 1 addition & 4 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,5 +1,2 @@
parameters:
ignoreErrors:
-
message: '/Instanceof between Chubbyphp\\Framework\\Router\\RouteInterface and Chubbyphp\\Framework\\Router\\RouteInterface will always evaluate to true./'
path: %currentWorkingDirectory%/src/Router/RoutesByName.php
ignoreErrors: []
30 changes: 6 additions & 24 deletions src/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@

use Chubbyphp\Framework\Emitter\Emitter;
use Chubbyphp\Framework\Emitter\EmitterInterface;
use Chubbyphp\Framework\Middleware\MiddlewareDispatcher;
use Chubbyphp\Framework\Middleware\MiddlewareDispatcherInterface;
use Chubbyphp\Framework\Middleware\PipeMiddleware;
use Chubbyphp\Framework\RequestHandler\RouteRequestHandler;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
Expand All @@ -16,30 +15,17 @@

final class Application implements RequestHandlerInterface
{
/**
* @var array<MiddlewareInterface>
*/
private array $middlewares;

private MiddlewareDispatcherInterface $middlewareDispatcher;

private RequestHandlerInterface $requestHandler;

private EmitterInterface $emitter;
private PipeMiddleware $pipeMiddleware;

/**
* @param array<MiddlewareInterface> $middlewares
*/
public function __construct(
array $middlewares,
?MiddlewareDispatcherInterface $middlewareDispatcher = null,
?RequestHandlerInterface $requestHandler = null,
?EmitterInterface $emitter = null
private RequestHandlerInterface $routeRequestHandler = new RouteRequestHandler(),
private EmitterInterface $emitter = new Emitter()
) {
$this->middlewares = (new Collection($middlewares, [MiddlewareInterface::class]))->toArray();
$this->middlewareDispatcher = $middlewareDispatcher ?? new MiddlewareDispatcher();
$this->requestHandler = $requestHandler ?? new RouteRequestHandler($this->middlewareDispatcher);
$this->emitter = $emitter ?? new Emitter();
$this->pipeMiddleware = new PipeMiddleware($middlewares);
}

public function __invoke(ServerRequestInterface $request): ResponseInterface
Expand All @@ -49,11 +35,7 @@ public function __invoke(ServerRequestInterface $request): ResponseInterface

public function handle(ServerRequestInterface $request): ResponseInterface
{
return $this->middlewareDispatcher->dispatch(
$this->middlewares,
$this->requestHandler,
$request
);
return $this->pipeMiddleware->process($request, $this->routeRequestHandler);
}

public function emit(ResponseInterface $response): void
Expand Down
51 changes: 0 additions & 51 deletions src/Collection.php

This file was deleted.

28 changes: 0 additions & 28 deletions src/Middleware/MiddlewareDispatcher.php

This file was deleted.

22 changes: 0 additions & 22 deletions src/Middleware/MiddlewareDispatcherInterface.php

This file was deleted.

36 changes: 36 additions & 0 deletions src/Middleware/PipeMiddleware.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

namespace Chubbyphp\Framework\Middleware;

use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;

final class PipeMiddleware implements MiddlewareInterface
{
/**
* @var array<MiddlewareInterface>
*/
private array $middlewares;

/**
* @param array<MiddlewareInterface> $middlewares
*/
public function __construct(array $middlewares)
{
$this->middlewares = array_reverse($middlewares);
}

public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
$reducedHandler = $handler;
foreach ($this->middlewares as $middleware) {
$reducedHandler = new MiddlewareRequestHandler($middleware, $reducedHandler);
}

return $reducedHandler->handle($request);
}
}
10 changes: 2 additions & 8 deletions src/RequestHandler/RouteRequestHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Chubbyphp\Framework\RequestHandler;

use Chubbyphp\Framework\Middleware\MiddlewareDispatcherInterface;
use Chubbyphp\Framework\Middleware\PipeMiddleware;
use Chubbyphp\Framework\Router\Exceptions\MissingRouteAttributeOnRequestException;
use Chubbyphp\Framework\Router\RouteInterface;
use Psr\Http\Message\ResponseInterface;
Expand All @@ -13,8 +13,6 @@

final class RouteRequestHandler implements RequestHandlerInterface
{
public function __construct(private MiddlewareDispatcherInterface $middlewareDispatcher) {}

public function handle(ServerRequestInterface $request): ResponseInterface
{
$route = $request->getAttribute('route');
Expand All @@ -23,10 +21,6 @@ public function handle(ServerRequestInterface $request): ResponseInterface
throw MissingRouteAttributeOnRequestException::create($route);
}

return $this->middlewareDispatcher->dispatch(
$route->getMiddlewares(),
$route->getRequestHandler(),
$request
);
return (new PipeMiddleware($route->getMiddlewares()))->process($request, $route->getRequestHandler());
}
}
20 changes: 3 additions & 17 deletions src/Router/Group.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,21 @@

namespace Chubbyphp\Framework\Router;

use Chubbyphp\Framework\Collection;
use Psr\Http\Server\MiddlewareInterface;

final class Group implements GroupInterface
{
/**
* @var array<MiddlewareInterface>
*/
private array $middlewares;

/**
* @var array<GroupInterface|RouteInterface>
*/
private array $children;

/**
* @param array<GroupInterface|RouteInterface> $children
* @param array<MiddlewareInterface> $middlewares
* @param array<string, mixed> $pathOptions
*/
private function __construct(
private string $path,
array $children = [],
array $middlewares = [],
private array $children = [],
private array $middlewares = [],
private array $pathOptions = []
) {
$this->children = (new Collection($children, [GroupInterface::class, RouteInterface::class]))->toArray();
$this->middlewares = (new Collection($middlewares, [MiddlewareInterface::class]))->toArray();
}
) {}

/**
* @param array<GroupInterface|RouteInterface> $children
Expand Down
Loading