Skip to content

Commit

Permalink
Make middleware services lazy via ServiceSubscriber (#3)
Browse files Browse the repository at this point in the history
* Use service subscriber in Middlewares

* Add missing tests
  • Loading branch information
alexander-schranz authored Aug 11, 2022
1 parent b4450d1 commit d859d47
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 15 deletions.
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@
"doctrine/dbal": "^3.3",
"doctrine/doctrine-bundle": "^2.5",
"doctrine/orm": "^2.11",
"psr/container": "^1.0 || ^2.0",
"symfony/config": "^5.4 || ^6.0",
"symfony/dependency-injection": "^5.4 || ^6.0",
"symfony/doctrine-bridge": "^5.4 || ^6.0",
"symfony/framework-bundle": "^5.4 || ^6.0",
"symfony/http-kernel": "^5.4 || ^6.0",
"symfony/lock": "^5.4 || ^6.0",
"symfony/messenger": "^5.4 || ^6.0"
"symfony/messenger": "^5.4 || ^6.0",
"symfony/service-contracts": "^1.0 || ^2.0 || ^3.0"
},
"require-dev": {
"coduo/php-matcher": "^6.0",
Expand Down
4 changes: 2 additions & 2 deletions config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@

$services->set('sulu_messenger.doctrine_flush_middleware')
->class(DoctrineFlushMiddleware::class)
->args([service('doctrine.orm.entity_manager')]);
->tag('container.service_subscriber');

$services->set('sulu_messenger.unpack_exception_middleware')
->class(UnpackExceptionMiddleware::class);

$services->set('sulu_messenger.lock_middleware')
->class(LockMiddleware::class)
->args([service('lock.factory')]);
->tag('container.service_subscriber');
};
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,16 @@
namespace Sulu\Messenger\Infrastructure\Symfony\Messenger\FlushMiddleware;

use Doctrine\ORM\EntityManagerInterface;
use Psr\Container\ContainerInterface;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\Middleware\MiddlewareInterface;
use Symfony\Component\Messenger\Middleware\StackInterface;
use Symfony\Contracts\Service\ServiceSubscriberInterface;

class DoctrineFlushMiddleware implements MiddlewareInterface
class DoctrineFlushMiddleware implements MiddlewareInterface, ServiceSubscriberInterface
{
public function __construct(
private EntityManagerInterface $entityManager,
) {
public function __construct(private ContainerInterface $container)
{
}

public function handle(Envelope $envelope, StackInterface $stack): Envelope
Expand All @@ -22,9 +23,16 @@ public function handle(Envelope $envelope, StackInterface $stack): Envelope

// flush unit-of-work to the database after the root message was handled successfully
if (!empty($envelope->all(EnableFlushStamp::class))) {
$this->entityManager->flush();
$this->container->get('doctrine.orm.entity_manager')->flush();
}

return $envelope;
}

public static function getSubscribedServices(): array
{
return [
'doctrine.orm.entity_manager' => EntityManagerInterface::class,
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,17 @@

namespace Sulu\Messenger\Infrastructure\Symfony\Messenger\LockMiddleware;

use Psr\Container\ContainerInterface;
use Symfony\Component\Lock\LockFactory;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\Middleware\MiddlewareInterface;
use Symfony\Component\Messenger\Middleware\StackInterface;
use Symfony\Contracts\Service\ServiceSubscriberInterface;

class LockMiddleware implements MiddlewareInterface
class LockMiddleware implements MiddlewareInterface, ServiceSubscriberInterface
{
public function __construct(
private LockFactory $lockFactory,
) {
public function __construct(private ContainerInterface $container)
{
}

public function handle(Envelope $envelope, StackInterface $stack): Envelope
Expand All @@ -25,7 +26,7 @@ public function handle(Envelope $envelope, StackInterface $stack): Envelope
$lockStamps = $envelope->all(LockStamp::class);

foreach ($lockStamps as $lockStamp) {
$lock = $this->lockFactory->createLock(
$lock = $this->container->get('lock.factory')->createLock(
$lockStamp->getResource(),
$lockStamp->getTtl(),
$lockStamp->getAutoRelease(),
Expand All @@ -44,4 +45,11 @@ public function handle(Envelope $envelope, StackInterface $stack): Envelope

return $envelope;
}

public static function getSubscribedServices(): array
{
return [
'lock.factory' => LockFactory::class,
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use stdClass;
use Sulu\Messenger\Infrastructure\Symfony\Messenger\FlushMiddleware\DoctrineFlushMiddleware;
use Sulu\Messenger\Infrastructure\Symfony\Messenger\FlushMiddleware\EnableFlushStamp;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\Middleware\StackMiddleware;

Expand All @@ -31,8 +32,11 @@ class DoctrineFlushMiddlewareTest extends TestCase
protected function setUp(): void
{
$this->entityManager = $this->prophesize(EntityManagerInterface::class);
$container = new Container();
$container->set('doctrine.orm.entity_manager', $this->entityManager->reveal());

$this->middleware = new DoctrineFlushMiddleware(
$this->entityManager->reveal(),
$container,
);
}

Expand Down Expand Up @@ -65,6 +69,16 @@ public function testHandleWithStamp(): void
);
}

public function testGetSubscribedServices(): void
{
$this->assertSame(
[
'doctrine.orm.entity_manager' => EntityManagerInterface::class,
],
$this->middleware->getSubscribedServices(),
);
}

private function createEnvelope(): Envelope
{
return new Envelope(new stdClass());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use stdClass;
use Sulu\Messenger\Infrastructure\Symfony\Messenger\LockMiddleware\LockMiddleware;
use Sulu\Messenger\Infrastructure\Symfony\Messenger\LockMiddleware\LockStamp;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\Lock\LockFactory;
use Symfony\Component\Lock\LockInterface;
use Symfony\Component\Messenger\Envelope;
Expand All @@ -33,8 +34,11 @@ class LockMiddlewareTest extends TestCase
protected function setUp(): void
{
$this->lockFactory = $this->prophesize(LockFactory::class);
$container = new Container();
$container->set('lock.factory', $this->lockFactory->reveal());

$this->middleware = new LockMiddleware(
$this->lockFactory->reveal(),
$container,
);
}

Expand Down Expand Up @@ -74,6 +78,16 @@ public function testHandleWithStamp(): void
);
}

public function testGetSubscribedServices(): void
{
$this->assertSame(
[
'lock.factory' => LockFactory::class,
],
$this->middleware->getSubscribedServices(),
);
}

private function createEnvelope(): Envelope
{
return new Envelope(new stdClass());
Expand Down

0 comments on commit d859d47

Please sign in to comment.