Skip to content

Commit 376c265

Browse files
Middleware factory improving (yiisoft#134)
1 parent 2506620 commit 376c265

File tree

10 files changed

+85
-9
lines changed

10 files changed

+85
-9
lines changed

src/Middleware/Consume/MiddlewareFactoryConsume.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@
77
use Psr\Container\ContainerExceptionInterface;
88
use Psr\Container\ContainerInterface;
99
use Psr\Container\NotFoundExceptionInterface;
10+
use Yiisoft\Definitions\Exception\InvalidConfigException;
11+
use Yiisoft\Definitions\Exception\NotInstantiableClassException;
12+
use Yiisoft\Definitions\Exception\NotInstantiableException;
13+
use Yiisoft\Factory\Factory;
14+
use Yiisoft\Factory\NotFoundException;
1015
use Yiisoft\Injector\Injector;
1116
use Yiisoft\Yii\Queue\Middleware\CallableFactory;
1217
use Yiisoft\Yii\Queue\Middleware\InvalidMiddlewareDefinitionException;
@@ -23,6 +28,7 @@ final class MiddlewareFactoryConsume implements MiddlewareFactoryConsumeInterfac
2328
*/
2429
public function __construct(
2530
private ContainerInterface $container,
31+
private Factory $factory,
2632
private CallableFactory $callableFactory,
2733
) {
2834
}
@@ -61,6 +67,14 @@ public function createConsumeMiddleware(
6167
return $this->container->get($middlewareDefinition);
6268
}
6369

70+
try {
71+
$result = $this->factory->create($middlewareDefinition);
72+
if ($result instanceof MiddlewareConsumeInterface) {
73+
return $result;
74+
}
75+
} catch (NotFoundException|NotInstantiableClassException|NotInstantiableException|InvalidConfigException) {
76+
}
77+
6478
$callable = $this->callableFactory->create($middlewareDefinition);
6579

6680
return $this->wrapCallable($callable);

src/Middleware/Push/MiddlewareFactoryPush.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@
77
use Psr\Container\ContainerExceptionInterface;
88
use Psr\Container\ContainerInterface;
99
use Psr\Container\NotFoundExceptionInterface;
10+
use Yiisoft\Definitions\Exception\InvalidConfigException;
11+
use Yiisoft\Definitions\Exception\NotInstantiableClassException;
12+
use Yiisoft\Definitions\Exception\NotInstantiableException;
13+
use Yiisoft\Factory\Factory;
14+
use Yiisoft\Factory\NotFoundException;
1015
use Yiisoft\Injector\Injector;
1116
use Yiisoft\Yii\Queue\Middleware\CallableFactory;
1217
use Yiisoft\Yii\Queue\Middleware\InvalidMiddlewareDefinitionException;
@@ -23,6 +28,7 @@ final class MiddlewareFactoryPush implements MiddlewareFactoryPushInterface
2328
*/
2429
public function __construct(
2530
private ContainerInterface $container,
31+
private Factory $factory,
2632
private CallableFactory $callableFactory,
2733
) {
2834
}
@@ -61,6 +67,14 @@ public function createPushMiddleware(
6167
return $this->container->get($middlewareDefinition);
6268
}
6369

70+
try {
71+
$result = $this->factory->create($middlewareDefinition);
72+
if ($result instanceof MiddlewarePushInterface) {
73+
return $result;
74+
}
75+
} catch (NotFoundException|NotInstantiableClassException|NotInstantiableException|InvalidConfigException) {
76+
}
77+
6478
$callable = $this->callableFactory->create($middlewareDefinition);
6579

6680
return $this->wrapCallable($callable);

tests/Integration/MiddlewareTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use PHPUnit\Framework\TestCase;
88
use Psr\Container\ContainerInterface;
99
use Psr\Log\LoggerInterface;
10+
use Yiisoft\Factory\Factory;
1011
use Yiisoft\Injector\Injector;
1112
use Yiisoft\Test\Support\Container\SimpleContainer;
1213
use Yiisoft\Test\Support\Log\SimpleLogger;
@@ -42,6 +43,7 @@ public function testFullStackPush(): void
4243
$pushMiddlewareDispatcher = new PushMiddlewareDispatcher(
4344
new MiddlewareFactoryPush(
4445
$this->createMock(ContainerInterface::class),
46+
new Factory($this->createMock(ContainerInterface::class)),
4547
new CallableFactory(
4648
$this->createMock(ContainerInterface::class)
4749
),
@@ -85,6 +87,7 @@ public function testFullStackConsume(): void
8587
$consumeMiddlewareDispatcher = new ConsumeMiddlewareDispatcher(
8688
new MiddlewareFactoryConsume(
8789
$this->createMock(ContainerInterface::class),
90+
new Factory($this->createMock(ContainerInterface::class)),
8891
new CallableFactory(
8992
$this->createMock(ContainerInterface::class)
9093
),

tests/TestCase.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Psr\Container\ContainerInterface;
1010
use Psr\Log\NullLogger;
1111
use RuntimeException;
12+
use Yiisoft\Factory\Factory;
1213
use Yiisoft\Injector\Injector;
1314
use Yiisoft\Test\Support\Container\SimpleContainer;
1415
use Yiisoft\Yii\Queue\Adapter\AdapterInterface;
@@ -180,6 +181,7 @@ protected function getPushMiddlewareDispatcher()
180181
return new PushMiddlewareDispatcher(
181182
new MiddlewareFactoryPush(
182183
$this->getContainer(),
184+
new Factory($this->getContainer()),
183185
new CallableFactory($this->getContainer()),
184186
),
185187
);
@@ -190,6 +192,7 @@ protected function getConsumeMiddlewareDispatcher()
190192
return new ConsumeMiddlewareDispatcher(
191193
new MiddlewareFactoryConsume(
192194
$this->getContainer(),
195+
new Factory($this->getContainer()),
193196
new CallableFactory($this->getContainer()),
194197
),
195198
);

tests/Unit/Middleware/Consume/MiddlewareDispatcherTest.php

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use PHPUnit\Framework\TestCase;
88
use Psr\Container\ContainerInterface;
9+
use Yiisoft\Factory\Factory;
910
use Yiisoft\Test\Support\Container\SimpleContainer;
1011
use Yiisoft\Yii\Queue\Adapter\AdapterInterface;
1112
use Yiisoft\Yii\Queue\Message\Message;
@@ -37,7 +38,7 @@ static function (ConsumeRequest $request): ConsumeRequest {
3738
$this->assertSame('New closure test data', $request->getMessage()->getData());
3839
}
3940

40-
public function testArrayMiddlewareCall(): void
41+
public function testArrayMiddlewareCallableDefinition(): void
4142
{
4243
$request = $this->getConsumeRequest();
4344
$container = $this->createContainer(
@@ -50,6 +51,19 @@ public function testArrayMiddlewareCall(): void
5051
$this->assertSame('New test data', $request->getMessage()->getData());
5152
}
5253

54+
public function testFactoryArrayDefinition(): void
55+
{
56+
$request = $this->getConsumeRequest();
57+
$container = $this->createContainer();
58+
$definition = [
59+
'class' => TestMiddleware::class,
60+
'__construct()' => ['message' => 'New test data from the definition'],
61+
];
62+
$dispatcher = $this->createDispatcher($container)->withMiddlewares([$definition]);
63+
$request = $dispatcher->dispatch($request, $this->getRequestHandler());
64+
$this->assertSame('New test data from the definition', $request->getMessage()->getData());
65+
}
66+
5367
public function testMiddlewareFullStackCalled(): void
5468
{
5569
$request = $this->getConsumeRequest();
@@ -149,10 +163,11 @@ private function createDispatcher(
149163
ContainerInterface $container = null,
150164
): ConsumeMiddlewareDispatcher {
151165
$container = $container ?? $this->createContainer([AdapterInterface::class => new FakeAdapter()]);
166+
$factory = new Factory($container);
152167
$callableFactory = new CallableFactory($container);
153168

154169
return new ConsumeMiddlewareDispatcher(
155-
new MiddlewareFactoryConsume($container, $callableFactory),
170+
new MiddlewareFactoryConsume($container, $factory, $callableFactory),
156171
);
157172
}
158173

tests/Unit/Middleware/Consume/MiddlewareFactoryTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use PHPUnit\Framework\TestCase;
88
use Psr\Container\ContainerInterface;
9+
use Yiisoft\Factory\Factory;
910
use Yiisoft\Test\Support\Container\SimpleContainer;
1011
use Yiisoft\Yii\Queue\Adapter\AdapterInterface;
1112
use Yiisoft\Yii\Queue\Message\Message;
@@ -184,8 +185,9 @@ public function testInvalidMiddlewareWithWrongArrayWithIntItems(): void
184185
private function getMiddlewareFactory(ContainerInterface $container = null): MiddlewareFactoryConsumeInterface
185186
{
186187
$container = $container ?? $this->getContainer([AdapterInterface::class => new FakeAdapter()]);
188+
$factory = new Factory($container);
187189

188-
return new MiddlewareFactoryConsume($container, new CallableFactory($container));
190+
return new MiddlewareFactoryConsume($container, $factory, new CallableFactory($container));
189191
}
190192

191193
private function getContainer(array $instances = []): ContainerInterface

tests/Unit/Middleware/Consume/Support/TestMiddleware.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,12 @@
1111

1212
final class TestMiddleware implements MiddlewareConsumeInterface
1313
{
14+
public function __construct(private string $message = 'New middleware test data')
15+
{
16+
}
17+
1418
public function processConsume(ConsumeRequest $request, MessageHandlerConsumeInterface $handler): ConsumeRequest
1519
{
16-
return $request->withMessage(new Message('test', 'New middleware test data'));
20+
return $request->withMessage(new Message('test', $this->message));
1721
}
1822
}

tests/Unit/Middleware/Push/MiddlewareDispatcherTest.php

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use PHPUnit\Framework\TestCase;
88
use Psr\Container\ContainerInterface;
9+
use Yiisoft\Factory\Factory;
910
use Yiisoft\Test\Support\Container\SimpleContainer;
1011
use Yiisoft\Yii\Queue\Adapter\AdapterInterface;
1112
use Yiisoft\Yii\Queue\Message\Message;
@@ -43,7 +44,7 @@ static function (PushRequest $request, AdapterInterface $adapter): PushRequest {
4344
$this->assertSame('closure-channel', $request->getAdapter()->channel);
4445
}
4546

46-
public function testArrayMiddlewareCall(): void
47+
public function testArrayMiddlewareCallableDefinition(): void
4748
{
4849
$request = $this->getPushRequest();
4950
$container = $this->createContainer(
@@ -56,6 +57,19 @@ public function testArrayMiddlewareCall(): void
5657
$this->assertSame('New test data', $request->getMessage()->getData());
5758
}
5859

60+
public function testFactoryArrayDefinition(): void
61+
{
62+
$request = $this->getPushRequest();
63+
$container = $this->createContainer();
64+
$definition = [
65+
'class' => TestMiddleware::class,
66+
'__construct()' => ['message' => 'New test data from the definition'],
67+
];
68+
$dispatcher = $this->createDispatcher($container)->withMiddlewares([$definition]);
69+
$request = $dispatcher->dispatch($request, $this->getRequestHandler());
70+
$this->assertSame('New test data from the definition', $request->getMessage()->getData());
71+
}
72+
5973
public function testMiddlewareFullStackCalled(): void
6074
{
6175
$request = $this->getPushRequest();
@@ -163,10 +177,11 @@ private function createDispatcher(
163177
ContainerInterface $container = null,
164178
): PushMiddlewareDispatcher {
165179
$container = $container ?? $this->createContainer([AdapterInterface::class => new FakeAdapter()]);
180+
$factory = new Factory($container);
166181
$callableFactory = new CallableFactory($container);
167182

168183
return new PushMiddlewareDispatcher(
169-
new MiddlewareFactoryPush($container, $callableFactory),
184+
new MiddlewareFactoryPush($container, $factory, $callableFactory),
170185
);
171186
}
172187

tests/Unit/Middleware/Push/MiddlewareFactoryTest.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use PHPUnit\Framework\TestCase;
88
use Psr\Container\ContainerInterface;
9+
use Yiisoft\Factory\Factory;
910
use Yiisoft\Yii\Queue\Adapter\AdapterInterface;
1011
use Yiisoft\Yii\Queue\Message\Message;
1112
use Yiisoft\Yii\Queue\Middleware\CallableFactory;
@@ -31,7 +32,7 @@ public function testCreateFromString(): void
3132
self::assertInstanceOf(TestMiddleware::class, $middleware);
3233
}
3334

34-
public function testCreateFromArray(): void
35+
public function testCreateCallableFromArray(): void
3536
{
3637
$container = $this->getContainer([TestCallableMiddleware::class => new TestCallableMiddleware()]);
3738
$middleware = $this->getMiddlewareFactory($container)->createPushMiddleware([TestCallableMiddleware::class, 'index']);
@@ -174,8 +175,9 @@ public function testInvalidMiddlewareWithWrongArrayWithIntItems(): void
174175
private function getMiddlewareFactory(ContainerInterface $container = null): MiddlewareFactoryPushInterface
175176
{
176177
$container = $container ?? $this->getContainer([AdapterInterface::class => new FakeAdapter()]);
178+
$factory = new Factory($container);
177179

178-
return new MiddlewareFactoryPush($container, new CallableFactory($container));
180+
return new MiddlewareFactoryPush($container, $factory, new CallableFactory($container));
179181
}
180182

181183
private function getContainer(array $instances = []): ContainerInterface

tests/Unit/Middleware/Push/Support/TestMiddleware.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,12 @@
1111

1212
final class TestMiddleware implements MiddlewarePushInterface
1313
{
14+
public function __construct(private string $message = 'New middleware test data')
15+
{
16+
}
17+
1418
public function processPush(PushRequest $request, MessageHandlerPushInterface $handler): PushRequest
1519
{
16-
return $request->withMessage(new Message('test', 'New middleware test data'));
20+
return $request->withMessage(new Message('test', $this->message));
1721
}
1822
}

0 commit comments

Comments
 (0)