Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Adding a middleware to allow you to "hook into" the stamping process #30646

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all 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
Adding a middleware to allow you to "hook into" the stamping process
  • Loading branch information
weaverryan committed Mar 22, 2019
commit aa0e880f87304d9cbee1c5ac15a91bfa8f6fe399
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
use Symfony\Component\Messenger\Handler\MessageHandlerInterface;
use Symfony\Component\Messenger\MessageBus;
use Symfony\Component\Messenger\MessageBusInterface;
use Symfony\Component\Messenger\Middleware\Stamper\EnvelopeStamperInterface;
use Symfony\Component\Messenger\Transport\Serialization\SerializerInterface;
use Symfony\Component\Messenger\Transport\TransportFactoryInterface;
use Symfony\Component\Messenger\Transport\TransportInterface;
Expand Down Expand Up @@ -398,6 +399,8 @@ public function load(array $configs, ContainerBuilder $container)
->addTag('messenger.message_handler');
$container->registerForAutoconfiguration(TransportFactoryInterface::class)
->addTag('messenger.transport_factory');
$container->registerForAutoconfiguration(EnvelopeStamperInterface::class)
->addTag('messenger.envelope_stamper');
$container->registerForAutoconfiguration(MimeTypeGuesserInterface::class)
->addTag('mime.mime_type_guesser');
$container->registerForAutoconfiguration(LoggerAwareInterface::class)
Expand Down Expand Up @@ -1616,8 +1619,14 @@ private function registerMessengerConfiguration(array $config, ContainerBuilder
}

$defaultMiddleware = [
'before' => [['id' => 'dispatch_after_current_bus']],
'after' => [['id' => 'send_message'], ['id' => 'handle_message']],
'before' => [
['id' => 'envelope_stamper_middleware'],
['id' => 'dispatch_after_current_bus'],
],
'after' => [
['id' => 'send_message'],
['id' => 'handle_message'],
],
];
foreach ($config['buses'] as $busId => $bus) {
$middleware = $bus['middleware'];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@

<service id="messenger.middleware.dispatch_after_current_bus" class="Symfony\Component\Messenger\Middleware\DispatchAfterCurrentBusMiddleware" />

<service id="messenger.middleware.envelope_stamper_middleware" class="Symfony\Component\Messenger\Middleware\EnvelopeStamperMiddleware">
<argument type="tagged" tag="messenger.envelope_stamper" />
</service>

<service id="messenger.middleware.validation" class="Symfony\Component\Messenger\Middleware\ValidationMiddleware">
<argument type="service" id="validator" />
</service>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Messenger\Middleware;

use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\Middleware\Stamper\EnvelopeStamperInterface;

/**
* @author Ryan Weaver <ryan@symfonycasts.com>
*/
class EnvelopeStamperMiddleware implements MiddlewareInterface
{
private $envelopeStampers;

/**
* @param EnvelopeStamperInterface[] $envelopeStampers
*/
public function __construct(iterable $envelopeStampers)
{
$this->envelopeStampers = $envelopeStampers;
}

public function handle(Envelope $envelope, StackInterface $stack): Envelope
{
foreach ($this->envelopeStampers as $envelopeStamper) {
$envelope = $envelopeStamper->stampEnvelope($envelope);
}

return $stack->next()->handle($envelope, $stack);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Messenger\Middleware\Stamper;

use Symfony\Component\Messenger\Envelope;

/**
* Classes used by EnvelopeStamperMiddleware that can add stamps to envelope.
*
* @author Ryan Weaver <ryan@symfonycasts.com>
*/
interface EnvelopeStamperInterface
{
/**
* Apply new stamps and return the new envelope.
*/
public function stampEnvelope(Envelope $envelope): Envelope;
Copy link
Contributor

@OskarStark OskarStark Mar 22, 2019

Choose a reason for hiding this comment

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

I would propose stamp(...) because stamp and envelope are already mentioned in the interface name

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Messenger\Tests\Middleware;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\MessageBus;
use Symfony\Component\Messenger\Middleware\EnvelopeStamperMiddleware;
use Symfony\Component\Messenger\Middleware\Stamper\EnvelopeStamperInterface;

class EnvelopeStamperMiddlewareTest extends TestCase
{
public function testHandleCallsStampers()
{
$stamper1 = $this->createMock(EnvelopeStamperInterface::class);
$stamper2 = $this->createMock(EnvelopeStamperInterface::class);

$envelopeOriginal = new Envelope(new \stdClass());
$envelopeAfterStamper1 = new Envelope(new \stdClass());
$envelopeAfterStamper2 = new Envelope(new \stdClass());

$stamper1->expects($this->once())
->method('stampEnvelope')
->with($envelopeOriginal)
->willReturn($envelopeAfterStamper1);

$stamper2->expects($this->once())
->method('stampEnvelope')
->with($envelopeAfterStamper1)
->willReturn($envelopeAfterStamper2);

$stamperMiddleware = new EnvelopeStamperMiddleware([$stamper1, $stamper2]);
$bus = new MessageBus([$stamperMiddleware]);
$actualFinalEnvelope = $bus->dispatch($envelopeOriginal);
$this->assertSame($envelopeAfterStamper2, $actualFinalEnvelope);
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.