Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2824e9a

Browse files
committed
Various valid changes based on reviews
1 parent 923324b commit 2824e9a

File tree

11 files changed

+64
-45
lines changed

11 files changed

+64
-45
lines changed

src/Symfony/Bundle/FrameworkBundle/Command/MessageConsumeCommand.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
use Symfony\Component\DependencyInjection\ContainerInterface;
2020
use Symfony\Component\Message\Asynchronous\Transport\ReceivedMessage;
2121
use Symfony\Component\Message\MessageBusInterface;
22-
use Symfony\Component\Message\MessageConsumerInterface;
22+
use Symfony\Component\Message\Transport\ReceiverInterface;
2323

2424
/**
2525
* @author Samuel Roze <[email protected]>
@@ -35,7 +35,7 @@ protected function configure()
3535
{
3636
$this
3737
->setDefinition(array(
38-
new InputArgument('consumer', InputArgument::REQUIRED, 'Name of the consumer'),
38+
new InputArgument('receiver', InputArgument::REQUIRED, 'Name of the receiver'),
3939
new InputOption('bus', 'b', InputOption::VALUE_REQUIRED, 'Name of the bus to dispatch the messages to', 'message_bus'),
4040
))
4141
->setDescription('Consume a message')
@@ -57,10 +57,10 @@ protected function execute(InputInterface $input, OutputInterface $output)
5757
/** @var ContainerInterface $container */
5858
$container = $this->getApplication()->getKernel()->getContainer();
5959

60-
if (!$container->has($consumerName = $input->getArgument('consumer'))) {
61-
throw new \RuntimeException(sprintf('Consumer "%s" do not exists', $consumerName));
62-
} elseif (!($consumer = $container->get($consumerName)) instanceof MessageConsumerInterface) {
63-
throw new \RuntimeException(sprintf('Consumer "%s" is not a valid message consumer. It should implement the interface "%s"', $consumerName, MessageConsumerInterface::class));
60+
if (!$container->has($receiverName = $input->getArgument('receiver'))) {
61+
throw new \RuntimeException(sprintf('Receiver "%s" do not exists', $receiverName));
62+
} elseif (!($receiver = $container->get($receiverName)) instanceof ReceiverInterface) {
63+
throw new \RuntimeException(sprintf('Receiver "%s" is not a valid message consumer. It should implement the interface "%s"', $receiverName, ReceiverInterface::class));
6464
}
6565

6666
if (!$container->has($busName = $input->getOption('bus'))) {
@@ -69,7 +69,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
6969
throw new \RuntimeException(sprintf('Bus "%s" is not a valid message bus. It should implement the interface "%s"', $busName, MessageBusInterface::class));
7070
}
7171

72-
foreach ($consumer->consume() as $message) {
72+
foreach ($receiver->receive() as $message) {
7373
if (!$message instanceof ReceivedMessage) {
7474
$message = new ReceivedMessage($message);
7575
}

src/Symfony/Bundle/FrameworkBundle/Resources/config/console.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,11 @@
6464
<tag name="console.command" command="debug:event-dispatcher" />
6565
</service>
6666

67-
<service id="Symfony\Bundle\FrameworkBundle\Command\MessageConsumeCommand">
67+
<service id="console.command.message_consume" class="Symfony\Bundle\FrameworkBundle\Command\MessageConsumeCommand">
6868
<tag name="console.command" command="message:consume" />
6969
</service>
7070

71-
<service id="Symfony\Bundle\FrameworkBundle\Command\RouterDebugCommand">
71+
<service id="console.command.router_debug" class="Symfony\Bundle\FrameworkBundle\Command\RouterDebugCommand">
7272
<argument type="service" id="router" />
7373
<tag name="console.command" command="debug:router" />
7474
</service>

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/ConfigurationTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,10 @@ class_exists(SemaphoreStore::class) && SemaphoreStore::isSupported() ? 'semaphor
249249
),
250250
),
251251
),
252+
'message' => array(
253+
'enabled' => !class_exists(FullStack::class),
254+
'routing' => array(),
255+
),
252256
);
253257
}
254258
}

src/Symfony/Component/Message/Asynchronous/Middleware/SendMessageMiddleware.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,6 @@
2020
*/
2121
class SendMessageMiddleware implements MiddlewareInterface
2222
{
23-
/**
24-
* @var SenderLocatorInterface
25-
*/
2623
private $senderLocator;
2724

2825
public function __construct(SenderLocatorInterface $senderLocator)

src/Symfony/Component/Message/Asynchronous/Transport/WrapIntoReceivedMessage.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public function __construct(ReceiverInterface $decoratedConsumer)
2828
$this->decoratedReceiver = $decoratedConsumer;
2929
}
3030

31-
public function receive(): \Generator
31+
public function receive(): \iterable
3232
{
3333
foreach ($this->decoratedReceiver->receive() as $message) {
3434
yield new ReceivedMessage($message);

src/Symfony/Component/Message/Debug/LoggingMiddleware.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,23 +34,26 @@ public function __construct(LoggerInterface $logger)
3434
*/
3535
public function handle($message, callable $next)
3636
{
37-
$this->logger->debug('Starting processing message', array(
37+
$this->logger->debug('Starting processing message {class}', array(
3838
'message' => $message,
39+
'class' => get_class($message),
3940
));
4041

4142
try {
4243
$result = $next($message);
4344
} catch (\Throwable $e) {
44-
$this->logger->warning('Something went wrong while processing message', array(
45+
$this->logger->warning('An exception occurred while processing message {class}', array(
4546
'message' => $message,
4647
'exception' => $e,
48+
'class' => get_class($message),
4749
));
4850

4951
throw $e;
5052
}
5153

52-
$this->logger->debug('Finished processing message', array(
54+
$this->logger->debug('Finished processing message {class}', array(
5355
'message' => $message,
56+
'class' => get_class($message),
5457
));
5558

5659
return $result;

src/Symfony/Component/Message/DependencyInjection/MessagePass.php

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,17 @@ class MessagePass implements CompilerPassInterface
2424
{
2525
use PriorityTaggedServiceTrait;
2626

27-
private $messageHandlerResolverService;
28-
private $handlerTag;
2927
private $messageBusService;
3028
private $middlewareTag;
29+
private $messageHandlerResolverService;
30+
private $handlerTag;
3131

3232
public function __construct(string $messageBusService = 'message_bus', string $middlewareTag = 'message_middleware', string $messageHandlerResolverService = 'message.handler_resolver', string $handlerTag = 'message_handler')
3333
{
34-
$this->messageHandlerResolverService = $messageHandlerResolverService;
35-
$this->handlerTag = $handlerTag;
3634
$this->messageBusService = $messageBusService;
3735
$this->middlewareTag = $middlewareTag;
36+
$this->messageHandlerResolverService = $messageHandlerResolverService;
37+
$this->handlerTag = $handlerTag;
3838
}
3939

4040
/**
@@ -63,24 +63,9 @@ private function findHandlers(ContainerBuilder $container): array
6363

6464
foreach ($container->findTaggedServiceIds($this->handlerTag, true) as $serviceId => $tags) {
6565
foreach ($tags as $tag) {
66-
$reflection = new \ReflectionClass($container->getDefinition($serviceId)->getClass());
67-
68-
try {
69-
$method = $reflection->getMethod('__invoke');
70-
} catch (\ReflectionException $e) {
71-
throw new RuntimeException(sprintf('Service "%s" should have an `__invoke` function', $serviceId));
72-
}
73-
74-
$parameters = $method->getParameters();
75-
if (1 !== count($parameters)) {
76-
throw new RuntimeException(sprintf('`__invoke` function of service "%s" must have exactly one parameter', $serviceId));
77-
}
66+
$handles = isset($tag['handles']) ? $tag['handles'] : $this->guessHandledClass($container, $serviceId);
7867

79-
$parameter = $parameters[0];
80-
if (null === $parameter->getClass()) {
81-
throw new RuntimeException(sprintf('The parameter of `__invoke` function of service "%s" must type hint the Message class it handles', $serviceId));
82-
}
83-
if (!class_exists($handles = $parameter->getClass()->getName())) {
68+
if (!class_exists($handles)) {
8469
throw new RuntimeException(sprintf('The message class "%s" declared in `__invoke` function of service "%s" does not exist', $handles, $serviceId));
8570
}
8671

@@ -96,4 +81,26 @@ private function findHandlers(ContainerBuilder $container): array
9681

9782
return $handlersByMessage;
9883
}
84+
85+
private function guessHandledClass(ContainerBuilder $container, string $serviceId): string
86+
{
87+
$reflection = new \ReflectionClass($container->getDefinition($serviceId)->getClass());
88+
try {
89+
$method = $reflection->getMethod('__invoke');
90+
} catch (\ReflectionException $e) {
91+
throw new RuntimeException(sprintf('Service "%s" should have an `__invoke` function', $serviceId));
92+
}
93+
94+
$parameters = $method->getParameters();
95+
if (1 !== count($parameters)) {
96+
throw new RuntimeException(sprintf('`__invoke` function of service "%s" must have exactly one parameter', $serviceId));
97+
}
98+
99+
$parameter = $parameters[0];
100+
if (null === $parameter->getClass()) {
101+
throw new RuntimeException(sprintf('The parameter of `__invoke` function of service "%s" must type hint the Message class it handles', $serviceId));
102+
}
103+
104+
return $parameter->getClass()->getName();
105+
}
99106
}

src/Symfony/Component/Message/Handler/MessageHandlerCollection.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ public function __construct(array $handlers)
3737

3838
public function __invoke($message)
3939
{
40-
return array_map(function ($handler) use ($message) {
41-
return $handler($message);
42-
}, $this->handlers);
40+
foreach ($this->handlers as $handler) {
41+
yield $handler($message);
42+
}
4343
}
4444
}

src/Symfony/Component/Message/README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ class MyMessageHandler
7676
</service>
7777
```
7878

79+
**Note:** If the message cannot be guessed from the handler's type-hint, use the `handles` attribute on the tag.
80+
7981
### Asynchronous messages
8082

8183
Using the Message Component is useful to decouple your application but it also very useful when you want to do some
@@ -126,7 +128,7 @@ framework:
126128
#### Same bus received and sender
127129

128130
To allow us to receive and send messages on the same bus and prevent a loop, the message bus is equipped with the
129-
`WrappedIntoReceivedMessage` received. It will wraps the received messages into `ReceivedMessage` objects and the
131+
`WrapIntoReceivedMessage` received. It will wrap the received messages into `ReceivedMessage` objects and the
130132
`SendMessageMiddleware` middleware will know it should not send these messages.
131133

132134
### Your own sender

src/Symfony/Component/Message/Transport/ReceiverInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,5 @@
1616
*/
1717
interface ReceiverInterface
1818
{
19-
public function receive(): \Generator;
19+
public function receive(): \iterable;
2020
}

src/Symfony/Component/Message/Transport/Serialization/SymfonySerialization.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,15 @@ class SymfonySerialization implements DecoderInterface, EncoderInterface
2323
*/
2424
private $serializer;
2525

26-
public function __construct(SerializerInterface $serializer)
26+
/**
27+
* @var string
28+
*/
29+
private $format;
30+
31+
public function __construct(SerializerInterface $serializer, string $format = 'json')
2732
{
2833
$this->serializer = $serializer;
34+
$this->format = $format;
2935
}
3036

3137
/**
@@ -39,7 +45,7 @@ public function decode(array $encodedMessage)
3945
throw new \InvalidArgumentException('Encoded message do not have a `type` header');
4046
}
4147

42-
return $this->serializer->deserialize($encodedMessage['body'], $encodedMessage['headers']['type'], 'json');
48+
return $this->serializer->deserialize($encodedMessage['body'], $encodedMessage['headers']['type'], $this->format);
4349
}
4450

4551
/**
@@ -48,7 +54,7 @@ public function decode(array $encodedMessage)
4854
public function encode($message): array
4955
{
5056
return array(
51-
'body' => $this->serializer->serialize($message, 'json'),
57+
'body' => $this->serializer->serialize($message, $this->format),
5258
'headers' => array(
5359
'type' => get_class($message),
5460
),

0 commit comments

Comments
 (0)