Skip to content
Open
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
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,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 @@ -156,6 +156,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 @@ -205,7 +206,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 @@ -233,5 +234,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.2-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);
```
18 changes: 18 additions & 0 deletions doc/Migration/5.x-6.x.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# 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` and `Chubbyphp\Framework\Handler\RouteRequestHandler` as argument anymore.

**If you need a custom `Chubbyphp\Framework\Middleware\PipeMiddleware` for your use case:**

I suggest you:
- Create an own `Chubbyphp\Framework\Application` and `Chubbyphp\Framework\Handler\RouteRequestHandler` onces you read the code you'll see it's little to replace and to maintain yourself.
- Create an issue convincing me to find a better solution with you in collaboration

`Chubbyphp\Framework\Collection` gets dropped, cause i do not believe the additional runtime security for typing arrays was worth the performance hit. Use phpstan or something compatible to prevent mistakes in usage.
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: []
25 changes: 6 additions & 19 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,14 +15,9 @@

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

private MiddlewareDispatcherInterface $middlewareDispatcher;
private PipeMiddleware $pipeMiddleware;

private RequestHandlerInterface $requestHandler;
private RequestHandlerInterface $routeRequestHandler;

private EmitterInterface $emitter;

Expand All @@ -32,13 +26,10 @@ final class Application implements RequestHandlerInterface
*/
public function __construct(
array $middlewares,
?MiddlewareDispatcherInterface $middlewareDispatcher = null,
?RequestHandlerInterface $requestHandler = null,
?EmitterInterface $emitter = null
) {
$this->middlewares = (new Collection($middlewares, [MiddlewareInterface::class]))->toArray();
$this->middlewareDispatcher = $middlewareDispatcher ?? new MiddlewareDispatcher();
$this->requestHandler = $requestHandler ?? new RouteRequestHandler($this->middlewareDispatcher);
$this->pipeMiddleware = new PipeMiddleware($middlewares);
$this->routeRequestHandler = new RouteRequestHandler();
$this->emitter = $emitter ?? new Emitter();
}

Expand All @@ -49,11 +40,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());
}
}
5 changes: 2 additions & 3 deletions src/Router/Group.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Chubbyphp\Framework\Router;

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

final class Group implements GroupInterface
Expand All @@ -30,8 +29,8 @@ private function __construct(
array $middlewares = [],
private array $pathOptions = []
) {
$this->children = (new Collection($children, [GroupInterface::class, RouteInterface::class]))->toArray();
$this->middlewares = (new Collection($middlewares, [MiddlewareInterface::class]))->toArray();
$this->children = $children;
$this->middlewares = $middlewares;
}

/**
Expand Down
3 changes: 1 addition & 2 deletions src/Router/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Chubbyphp\Framework\Router;

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

Expand Down Expand Up @@ -32,7 +31,7 @@ private function __construct(
array $middlewares = [],
private array $pathOptions = []
) {
$this->middlewares = (new Collection($middlewares, [MiddlewareInterface::class]))->toArray();
$this->middlewares = $middlewares;
}

/**
Expand Down
11 changes: 0 additions & 11 deletions src/Router/RoutesByName.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,6 @@ final class RoutesByName implements RoutesByNameInterface
public function __construct(array $routes)
{
foreach ($routes as $i => $route) {
if (!$route instanceof RouteInterface) {
throw new \TypeError(
\sprintf(
'%s::__construct() expects parameter 1 at index %s to be %s, %s given',
self::class,
$i,
RouteInterface::class,
$route::class
)
);
}
$this->routes[$route->getName()] = $route;
}
}
Expand Down
Loading