generated from yiisoft/package-template
-
-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Middleware attribute #93
Open
xepozz
wants to merge
18
commits into
master
Choose a base branch
from
middleware-attribute
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
9e72826
Test middleware attribute concept
xepozz 30c63c8
Move attribute
xepozz 910649a
Support middleware for both callable and callable-like arrays
xepozz 61d5ab4
Apply fixes from StyleCI
StyleCIBot db81418
Use getter instead of public property
xepozz 267bd7d
Replace map with for
xepozz a2bb149
Apply fixes from StyleCI
StyleCIBot 71b26a1
Check property before assign
xepozz 7e68181
Merge branch 'master' into middleware-attribute
xepozz 8acb333
Fix psalm
xepozz 054af31
Merge branch 'master' into middleware-attribute
xepozz 3801492
Fix psalm
xepozz 011c67d
Apply fixes from StyleCI
StyleCIBot 0e82a61
Apply Rector changes (CI)
xepozz 76541a8
Cover line
xepozz ffe8c2d
Format code
xepozz e83044b
Merge branch 'master' into middleware-attribute
xepozz 89a00da
Apply Rector changes (CI)
xepozz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\Middleware\Dispatcher\Attribute; | ||
|
||
use Attribute; | ||
|
||
#[Attribute(Attribute::TARGET_FUNCTION | Attribute::TARGET_METHOD | Attribute::IS_REPEATABLE)] | ||
final class Middleware | ||
{ | ||
private readonly mixed $definition; | ||
|
||
public function __construct( | ||
array|callable|string $definition | ||
) { | ||
$this->definition = $definition; | ||
} | ||
|
||
public function getDefinition(): mixed | ||
{ | ||
return $this->definition; | ||
} | ||
} |
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 |
---|---|---|
|
@@ -6,17 +6,18 @@ | |
|
||
use Closure; | ||
use Psr\Container\ContainerInterface; | ||
use Psr\EventDispatcher\EventDispatcherInterface; | ||
use Psr\Http\Message\ResponseInterface; | ||
use Psr\Http\Message\ServerRequestInterface; | ||
use Psr\Http\Server\MiddlewareInterface; | ||
use Psr\Http\Server\RequestHandlerInterface; | ||
use ReflectionClass; | ||
use ReflectionFunction; | ||
use ReflectionParameter; | ||
use Yiisoft\Definitions\ArrayDefinition; | ||
use Yiisoft\Definitions\Exception\InvalidConfigException; | ||
use Yiisoft\Definitions\Helpers\DefinitionValidator; | ||
use Yiisoft\Injector\Injector; | ||
use Yiisoft\Middleware\Dispatcher\Attribute\Middleware; | ||
|
||
use function in_array; | ||
use function is_array; | ||
|
@@ -29,15 +30,29 @@ | |
*/ | ||
final class MiddlewareFactory | ||
{ | ||
private ?EventDispatcherInterface $eventDispatcher = null; | ||
|
||
/** | ||
* @param ContainerInterface $container Container to use for resolving definitions. | ||
*/ | ||
public function __construct( | ||
private readonly ContainerInterface $container, | ||
private readonly ?ParametersResolverInterface $parametersResolver = null | ||
private ContainerInterface $container, | ||
private ?ParametersResolverInterface $parametersResolver = null | ||
Comment on lines
+39
to
+40
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why drop "readonly"? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It was written before the rector changes |
||
) { | ||
} | ||
|
||
public function withEventDispatcher(?EventDispatcherInterface $eventDispatcher): self | ||
{ | ||
$new = clone $this; | ||
$new->eventDispatcher = $eventDispatcher; | ||
return $new; | ||
} | ||
|
||
public function hasEventDispatcher(): bool | ||
{ | ||
return $this->eventDispatcher !== null; | ||
} | ||
|
||
/** | ||
* @param array|callable|string $middlewareDefinition Middleware definition in one of the following formats: | ||
* | ||
|
@@ -167,30 +182,42 @@ private function wrapCallable(array|callable $callable): MiddlewareInterface | |
return $this->createCallableWrapper($callable); | ||
} | ||
|
||
return $this->createActionWrapper($callable[0], $callable[1]); | ||
return $this->createCallableWrapper([$this->container->get($callable[0]), $callable[1]]); | ||
} | ||
|
||
private function createCallableWrapper(callable $callback): MiddlewareInterface | ||
{ | ||
return new class ($callback, $this->container, $this->parametersResolver) implements MiddlewareInterface { | ||
return new class ( | ||
$callback, | ||
$this->container, | ||
$this, | ||
$this->eventDispatcher, | ||
$this->parametersResolver, | ||
) implements MiddlewareInterface { | ||
/** @var callable */ | ||
private $callback; | ||
/** | ||
* @var ReflectionParameter[] | ||
* @psalm-var array<string,ReflectionParameter> | ||
*/ | ||
private array $callableParameters = []; | ||
public array $middlewares = []; | ||
|
||
public function __construct( | ||
callable $callback, | ||
private readonly ContainerInterface $container, | ||
private readonly MiddlewareFactory $middlewareFactory, | ||
private readonly ?EventDispatcherInterface $eventDispatcher, | ||
private readonly ?ParametersResolverInterface $parametersResolver | ||
) { | ||
$this->callback = $callback; | ||
$callback = Closure::fromCallable($callback); | ||
|
||
$callableParameters = (new ReflectionFunction($callback))->getParameters(); | ||
foreach ($callableParameters as $parameter) { | ||
$reflectionFunction = new ReflectionFunction(Closure::fromCallable($callback)); | ||
|
||
foreach ($reflectionFunction->getAttributes(Middleware::class) as $attribute) { | ||
$this->middlewares[] = $attribute->newInstance()->getDefinition(); | ||
} | ||
foreach ($reflectionFunction->getParameters() as $parameter) { | ||
$this->callableParameters[$parameter->getName()] = $parameter; | ||
} | ||
} | ||
|
@@ -207,8 +234,16 @@ public function process( | |
); | ||
} | ||
|
||
/** @var MiddlewareInterface|mixed|ResponseInterface $response */ | ||
$response = (new Injector($this->container))->invoke($this->callback, $parameters); | ||
if ($this->middlewares !== []) { | ||
$middlewares = [...$this->middlewares, fn(): mixed => ($this->callback)()]; | ||
$middlewareDispatcher = new MiddlewareDispatcher($this->middlewareFactory, $this->eventDispatcher); | ||
/** @psalm-suppress MixedArgumentTypeCoercion */ | ||
$middlewareDispatcher = $middlewareDispatcher->withMiddlewares($middlewares); | ||
$response = $middlewareDispatcher->dispatch($request, $handler); | ||
} else { | ||
/** @var MiddlewareInterface|mixed|ResponseInterface $response */ | ||
$response = (new Injector($this->container))->invoke($this->callback, $parameters); | ||
} | ||
if ($response instanceof ResponseInterface) { | ||
return $response; | ||
} | ||
|
@@ -221,66 +256,13 @@ public function process( | |
|
||
public function __debugInfo(): array | ||
{ | ||
return ['callback' => $this->callback]; | ||
} | ||
}; | ||
} | ||
|
||
/** | ||
* @param class-string $class | ||
* @param non-empty-string $method | ||
*/ | ||
private function createActionWrapper(string $class, string $method): MiddlewareInterface | ||
{ | ||
return new class ($this->container, $this->parametersResolver, $class, $method) implements MiddlewareInterface { | ||
/** | ||
* @var ReflectionParameter[] | ||
* @psalm-var array<string,ReflectionParameter> | ||
*/ | ||
private array $actionParameters = []; | ||
|
||
public function __construct( | ||
private readonly ContainerInterface $container, | ||
private readonly ?ParametersResolverInterface $parametersResolver, | ||
/** @var class-string */ | ||
private readonly string $class, | ||
/** @var non-empty-string */ | ||
private readonly string $method | ||
) { | ||
$actionParameters = (new ReflectionClass($this->class))->getMethod($this->method)->getParameters(); | ||
foreach ($actionParameters as $parameter) { | ||
$this->actionParameters[$parameter->getName()] = $parameter; | ||
if (is_array($this->callback) | ||
&& isset($this->callback[0], $this->callback[1]) | ||
&& is_object($this->callback[0]) | ||
) { | ||
return ['callback' => [$this->callback[0]::class, $this->callback[1]]]; | ||
} | ||
} | ||
|
||
public function process( | ||
ServerRequestInterface $request, | ||
RequestHandlerInterface $handler | ||
): ResponseInterface { | ||
/** @var mixed $controller */ | ||
$controller = $this->container->get($this->class); | ||
$parameters = [$request, $handler]; | ||
if ($this->parametersResolver !== null) { | ||
$parameters = array_merge( | ||
$parameters, | ||
$this->parametersResolver->resolve($this->actionParameters, $request) | ||
); | ||
} | ||
|
||
/** @var mixed|ResponseInterface $response */ | ||
$response = (new Injector($this->container))->invoke([$controller, $this->method], $parameters); | ||
if ($response instanceof ResponseInterface) { | ||
return $response; | ||
} | ||
|
||
throw new InvalidMiddlewareDefinitionException([$this->class, $this->method]); | ||
} | ||
|
||
public function __debugInfo() | ||
{ | ||
return [ | ||
'callback' => [$this->class, $this->method], | ||
]; | ||
return ['callback' => $this->callback]; | ||
} | ||
}; | ||
} | ||
|
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Suggest major refactoring...
1) MarkMiddlewareFactory
as internal2) Create
MiddlewareFactory
in constructor and replaceprivate MiddlewareFactory $middlewareFactory,
to:MiddlewareFactory
constructor.It's allow use
MiddlewareDispatcher::withMiddlewares()
intoMiddlewareFactory
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are no reasons to make
MiddlewareFactory
internalThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agree with internal. But 2 and 3 is actual.
MiddlewareFactory
methodswithEventDispatcher()
andhasEventDispatcher()
looks strange.