Skip to content
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

Fix #61: Add support callable middlewares like [$instance, 'method'] and $invokeable #69

Merged
merged 8 commits into from
Dec 23, 2022
Merged
Show file tree
Hide file tree
Changes from 5 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
33 changes: 19 additions & 14 deletions src/MiddlewareFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,9 @@ public function __construct(
* - A callable with
* `function(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface`
* signature.
* - A controller handler action in format `[TestController::class, 'index']`. `TestController` instance will
* be created and `index()` method will be executed.
* - Any callable.
* - A controller handler action in format `[TestController::class, 'index']` or `[new TestController(), 'index']`.
* `TestController` instance will be created if it is not an instance and `index()` method will be executed.
* - A function returning a middleware. The middleware returned will be executed.
*
* For handler action and callable
Expand Down Expand Up @@ -87,21 +88,21 @@ private function isMiddlewareClassDefinition(array|callable|string $definition):
}

/**
* @psalm-assert-if-true array{0:class-string, 1:non-empty-string}|Closure $definition
* @psalm-assert-if-true array{0:class-string|object, 1:non-empty-string}|callable-object|callable-string|Closure $definition
*/
private function isCallableDefinition(array|callable|string $definition): bool
{
if ($definition instanceof Closure) {
if (is_callable($definition)) {
return true;
}

return is_array($definition)
&& array_keys($definition) === [0, 1]
&& is_string($definition[0])
&& (is_string($definition[0]) || is_object($definition[0]))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not need, because is_callable above cover this case.

&& is_string($definition[1])
&& in_array(
$definition[1],
class_exists($definition[0]) ? get_class_methods($definition[0]) : [],
is_object($definition[0]) || class_exists($definition[0]) ? get_class_methods($definition[0]) : [],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not need, because is_callable above cover this case.

true
);
}
Expand All @@ -125,9 +126,9 @@ private function isArrayDefinition(array|callable|string $definition): bool
}

/**
* @param array{0:class-string, 1:non-empty-string}|Closure $callable
* @param array{0:class-string|object, 1:non-empty-string}|callable-object|callable-string|Closure $callable
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* @param array{0:class-string|object, 1:non-empty-string}|callable-object|callable-string|Closure $callable
* @param array{0:class-string, 1:non-empty-string}|callable|Closure $callable

*/
private function wrapCallable(array|Closure $callable): MiddlewareInterface
private function wrapCallable(array|callable $callable): MiddlewareInterface
{
if (is_array($callable)) {
return $this->createActionWrapper($callable[0], $callable[1]);
Expand Down Expand Up @@ -184,17 +185,17 @@ private function getCallableParameters(): array
}

/**
* @param class-string $class
* @param class-string|object $class
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not need, because is_callable above cover this case.

* @param non-empty-string $method
*/
private function createActionWrapper(string $class, string $method): MiddlewareInterface
private function createActionWrapper(string|object $class, string $method): MiddlewareInterface
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not need, because is_callable above cover this case.

{
return new class ($this->container, $this->parametersResolver, $class, $method) implements MiddlewareInterface {
public function __construct(
private ContainerInterface $container,
private ?ParametersResolverInterface $parametersResolver,
/** @var class-string */
private string $class,
/** @var class-string|object */
private string|object $class,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not need, because is_callable above cover this case.

/** @var non-empty-string */
private string $method
) {
Expand All @@ -204,8 +205,12 @@ public function process(
ServerRequestInterface $request,
RequestHandlerInterface $handler
): ResponseInterface {
/** @var mixed $controller */
$controller = $this->container->get($this->class);
if (is_string($this->class)) {
/** @var mixed $controller */
$controller = $this->container->get($this->class);
} else {
$controller = $this->class;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not need, because is_callable above cover this case.

$parameters = [$request, $handler];
if ($this->parametersResolver !== null) {
$parameters = array_merge(
Expand Down
48 changes: 40 additions & 8 deletions tests/MiddlewareFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Yiisoft\Middleware\Dispatcher\InvalidMiddlewareDefinitionException;
use Yiisoft\Middleware\Dispatcher\MiddlewareFactory;
use Yiisoft\Middleware\Dispatcher\ParametersResolverInterface;
use Yiisoft\Middleware\Dispatcher\Tests\Support\InvokeableAction;
use Yiisoft\Middleware\Dispatcher\Tests\Support\SimpleParametersResolver;
use Yiisoft\Middleware\Dispatcher\Tests\Support\UseParamsController;
use Yiisoft\Middleware\Dispatcher\Tests\Support\UseParamsMiddleware;
Expand Down Expand Up @@ -110,6 +111,45 @@ public function testCreateFromClosureWithResolver(): void
);
}

public function testCreateCallableFromArrayWithInstance(): void
{
$container = $this->getContainer();
$controller = new TestController();
$middleware = $this
->getMiddlewareFactory($container)
->create([$controller, 'index']);
self::assertSame(
'yii',
$middleware
->process(
$this->createMock(ServerRequestInterface::class),
$this->createMock(RequestHandlerInterface::class)
)
->getHeaderLine('test')
);
self::assertSame(
[$controller, 'index'],
$middleware->__debugInfo()['callback']
);
}

public function testCreateCallableObject(): void
{
$container = $this->getContainer();
$middleware = $this
->getMiddlewareFactory($container)
->create(new InvokeableAction());
self::assertSame(
'yii',
$middleware
->process(
$this->createMock(ServerRequestInterface::class),
$this->createMock(RequestHandlerInterface::class)
)
->getHeaderLine('test')
);
}

public function testCreateFromClosureMiddleware(): void
{
$container = $this->getContainer([TestController::class => new TestController()]);
Expand Down Expand Up @@ -259,14 +299,6 @@ public function testInvalidMiddlewareWithWrongArrayType(): void
->create(['class' => TestController::class, 'index']);
}

public function testInvalidMiddlewareWithWrongArrayWithInstance(): void
{
$this->expectException(InvalidMiddlewareDefinitionException::class);
$this
->getMiddlewareFactory()
->create([new TestController(), 'index']);
}

public function testInvalidMiddlewareWithWrongArrayWithIntItems(): void
{
$this->expectException(InvalidMiddlewareDefinitionException::class);
Expand Down
16 changes: 16 additions & 0 deletions tests/Support/InvokeableAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Middleware\Dispatcher\Tests\Support;

use Nyholm\Psr7\Response;
use Psr\Http\Message\ResponseInterface;

final class InvokeableAction
{
public function __invoke(): ResponseInterface
{
return new Response(200, ['test' => 'yii']);
}
}