-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New: Added psr-container integration for middlewares
- Loading branch information
Showing
6 changed files
with
52 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,7 +10,7 @@ | |
/** | ||
* @author merloxx <[email protected]> | ||
*/ | ||
interface DispatcherInterface extends MiddlewareAwareInterface, RequestHandlerInterface | ||
interface DispatcherInterface extends MiddlewareAwareInterface, ContainerAwareInterface, RequestHandlerInterface | ||
{ | ||
/** | ||
* @param RouteInterface $route | ||
|
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 |
---|---|---|
|
@@ -5,13 +5,16 @@ | |
namespace Zaphyr\Router\Traits; | ||
|
||
use Psr\Http\Server\MiddlewareInterface; | ||
use Throwable; | ||
use Zaphyr\Router\Exceptions\MiddlewareException; | ||
|
||
/** | ||
* @author merloxx <[email protected]> | ||
*/ | ||
trait MiddlewareAwareTrait | ||
{ | ||
use ContainerAwareTrait; | ||
|
||
/** | ||
* @var MiddlewareInterface[]|class-string[] | ||
*/ | ||
|
@@ -71,7 +74,13 @@ public function shiftMiddleware(): MiddlewareInterface | |
public function resolveMiddleware(MiddlewareInterface|string $middleware): MiddlewareInterface | ||
{ | ||
if (is_string($middleware) && class_exists($middleware)) { | ||
$middleware = new $middleware(); | ||
$container = $this->getContainer(); | ||
|
||
try { | ||
$middleware = $container !== null ? $container->get($middleware) : new $middleware(); | ||
} catch (Throwable $exception) { | ||
throw new MiddlewareException($exception->getMessage(), $exception->getCode(), $exception); | ||
} | ||
} | ||
|
||
if ($middleware instanceof MiddlewareInterface) { | ||
|
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,26 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Zaphyr\RouterTests\TestAssets; | ||
|
||
use Psr\Http\Message\ResponseInterface; | ||
use Psr\Http\Message\ServerRequestInterface; | ||
use Psr\Http\Server\MiddlewareInterface; | ||
use Psr\Http\Server\RequestHandlerInterface; | ||
use Zaphyr\HttpMessage\Response; | ||
|
||
class DIMiddleware implements MiddlewareInterface | ||
{ | ||
public function __construct(protected Foo $foo) | ||
{ | ||
} | ||
|
||
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface | ||
{ | ||
$response = new Response(); | ||
$response->getBody()->write($this->foo->greet()); | ||
|
||
return $response; | ||
} | ||
} |