[Messenger] Move data collector & command into the component
This commit is contained in:
parent
fb172cae86
commit
f9c9ca0514
@ -69,7 +69,7 @@
|
|||||||
<tag name="console.command" command="debug:event-dispatcher" />
|
<tag name="console.command" command="debug:event-dispatcher" />
|
||||||
</service>
|
</service>
|
||||||
|
|
||||||
<service id="console.command.messenger_consume_messages" class="Symfony\Bundle\FrameworkBundle\Command\MessengerConsumeMessagesCommand">
|
<service id="console.command.messenger_consume_messages" class="Symfony\Component\Messenger\Command\ConsumeMessagesCommand">
|
||||||
<argument type="service" id="message_bus" />
|
<argument type="service" id="message_bus" />
|
||||||
<argument type="service" id="messenger.receiver_locator" />
|
<argument type="service" id="messenger.receiver_locator" />
|
||||||
|
|
||||||
|
@ -58,7 +58,7 @@
|
|||||||
<tag name="monolog.logger" channel="messenger" />
|
<tag name="monolog.logger" channel="messenger" />
|
||||||
</service>
|
</service>
|
||||||
|
|
||||||
<service id="data_collector.messenger" class="Symfony\Bundle\FrameworkBundle\DataCollector\MessengerDataCollector">
|
<service id="data_collector.messenger" class="Symfony\Component\Messenger\DataCollector\MessengerDataCollector">
|
||||||
<tag name="data_collector" template="@WebProfiler/Collector/messenger.html.twig" id="messenger" priority="100" />
|
<tag name="data_collector" template="@WebProfiler/Collector/messenger.html.twig" id="messenger" priority="100" />
|
||||||
<tag name="message_bus_middleware" />
|
<tag name="message_bus_middleware" />
|
||||||
</service>
|
</service>
|
||||||
|
@ -9,7 +9,7 @@
|
|||||||
* file that was distributed with this source code.
|
* file that was distributed with this source code.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
namespace Symfony\Bundle\FrameworkBundle\Command;
|
namespace Symfony\Component\Messenger\Command;
|
||||||
|
|
||||||
use Psr\Container\ContainerInterface;
|
use Psr\Container\ContainerInterface;
|
||||||
use Symfony\Component\Console\Command\Command;
|
use Symfony\Component\Console\Command\Command;
|
||||||
@ -24,8 +24,10 @@ use Symfony\Component\Messenger\Worker;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Samuel Roze <samuel.roze@gmail.com>
|
* @author Samuel Roze <samuel.roze@gmail.com>
|
||||||
|
*
|
||||||
|
* @experimental in 4.1
|
||||||
*/
|
*/
|
||||||
class MessengerConsumeMessagesCommand extends Command
|
class ConsumeMessagesCommand extends Command
|
||||||
{
|
{
|
||||||
protected static $defaultName = 'messenger:consume-messages';
|
protected static $defaultName = 'messenger:consume-messages';
|
||||||
|
|
@ -9,7 +9,7 @@
|
|||||||
* file that was distributed with this source code.
|
* file that was distributed with this source code.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
namespace Symfony\Bundle\FrameworkBundle\DataCollector;
|
namespace Symfony\Component\Messenger\DataCollector;
|
||||||
|
|
||||||
use Symfony\Component\HttpFoundation\Request;
|
use Symfony\Component\HttpFoundation\Request;
|
||||||
use Symfony\Component\HttpFoundation\Response;
|
use Symfony\Component\HttpFoundation\Response;
|
||||||
@ -18,6 +18,8 @@ use Symfony\Component\Messenger\MiddlewareInterface;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Samuel Roze <samuel.roze@gmail.com>
|
* @author Samuel Roze <samuel.roze@gmail.com>
|
||||||
|
*
|
||||||
|
* @experimental in 4.1
|
||||||
*/
|
*/
|
||||||
class MessengerDataCollector extends DataCollector implements MiddlewareInterface
|
class MessengerDataCollector extends DataCollector implements MiddlewareInterface
|
||||||
{
|
{
|
||||||
@ -26,7 +28,7 @@ class MessengerDataCollector extends DataCollector implements MiddlewareInterfac
|
|||||||
*/
|
*/
|
||||||
public function collect(Request $request, Response $response, \Exception $exception = null)
|
public function collect(Request $request, Response $response, \Exception $exception = null)
|
||||||
{
|
{
|
||||||
return $this->data;
|
// noop
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -61,25 +63,25 @@ class MessengerDataCollector extends DataCollector implements MiddlewareInterfac
|
|||||||
try {
|
try {
|
||||||
$result = $next($message);
|
$result = $next($message);
|
||||||
|
|
||||||
if (is_object($result)) {
|
if (\is_object($result)) {
|
||||||
$debugRepresentation['result'] = array(
|
$debugRepresentation['result'] = array(
|
||||||
'type' => get_class($result),
|
'type' => \get_class($result),
|
||||||
'object' => $this->cloneVar($result),
|
'object' => $this->cloneVar($result),
|
||||||
);
|
);
|
||||||
} elseif (is_array($result)) {
|
} elseif (\is_array($result)) {
|
||||||
$debugRepresentation['result'] = array(
|
$debugRepresentation['result'] = array(
|
||||||
'type' => 'array',
|
'type' => 'array',
|
||||||
'object' => $this->cloneVar($result),
|
'object' => $this->cloneVar($result),
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
$debugRepresentation['result'] = array(
|
$debugRepresentation['result'] = array(
|
||||||
'type' => gettype($result),
|
'type' => \gettype($result),
|
||||||
'value' => $result,
|
'value' => $result,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
} catch (\Throwable $exception) {
|
} catch (\Throwable $exception) {
|
||||||
$debugRepresentation['exception'] = array(
|
$debugRepresentation['exception'] = array(
|
||||||
'type' => get_class($exception),
|
'type' => \get_class($exception),
|
||||||
'message' => $exception->getMessage(),
|
'message' => $exception->getMessage(),
|
||||||
);
|
);
|
||||||
}
|
}
|
@ -0,0 +1,130 @@
|
|||||||
|
<?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\DataCollector;
|
||||||
|
|
||||||
|
use PHPUnit\Framework\TestCase;
|
||||||
|
use Symfony\Component\Messenger\DataCollector\MessengerDataCollector;
|
||||||
|
use Symfony\Component\Messenger\Tests\Fixtures\DummyMessage;
|
||||||
|
use Symfony\Component\VarDumper\Test\VarDumperTestTrait;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Maxime Steinhausser <maxime.steinhausser@gmail.com>
|
||||||
|
*/
|
||||||
|
class MessengerDataCollectorTest extends TestCase
|
||||||
|
{
|
||||||
|
use VarDumperTestTrait;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dataProvider getHandleTestData
|
||||||
|
*/
|
||||||
|
public function testHandle($returnedValue, $expected)
|
||||||
|
{
|
||||||
|
$collector = new MessengerDataCollector();
|
||||||
|
$message = new DummyMessage('dummy message');
|
||||||
|
|
||||||
|
$next = $this->createPartialMock(\stdClass::class, array('__invoke'));
|
||||||
|
$next->expects($this->once())->method('__invoke')->with($message)->willReturn($returnedValue);
|
||||||
|
|
||||||
|
$this->assertSame($returnedValue, $collector->handle($message, $next));
|
||||||
|
|
||||||
|
$messages = $collector->getMessages();
|
||||||
|
$this->assertCount(1, $messages);
|
||||||
|
|
||||||
|
$this->assertDumpMatchesFormat($expected, $messages[0]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getHandleTestData()
|
||||||
|
{
|
||||||
|
$messageDump = <<<DUMP
|
||||||
|
"message" => array:2 [
|
||||||
|
"type" => "Symfony\Component\Messenger\Tests\Fixtures\DummyMessage"
|
||||||
|
"object" => Symfony\Component\VarDumper\Cloner\Data {%A
|
||||||
|
%A+class: "Symfony\Component\Messenger\Tests\Fixtures\DummyMessage"%A
|
||||||
|
}
|
||||||
|
]
|
||||||
|
DUMP;
|
||||||
|
|
||||||
|
yield 'no returned value' => array(
|
||||||
|
null,
|
||||||
|
<<<DUMP
|
||||||
|
array:2 [
|
||||||
|
$messageDump
|
||||||
|
"result" => array:2 [
|
||||||
|
"type" => "NULL"
|
||||||
|
"value" => null
|
||||||
|
]
|
||||||
|
]
|
||||||
|
DUMP
|
||||||
|
);
|
||||||
|
|
||||||
|
yield 'scalar returned value' => array(
|
||||||
|
'returned value',
|
||||||
|
<<<DUMP
|
||||||
|
array:2 [
|
||||||
|
$messageDump
|
||||||
|
"result" => array:2 [
|
||||||
|
"type" => "string"
|
||||||
|
"value" => "returned value"
|
||||||
|
]
|
||||||
|
]
|
||||||
|
DUMP
|
||||||
|
);
|
||||||
|
|
||||||
|
yield 'array returned value' => array(
|
||||||
|
array('returned value'),
|
||||||
|
<<<DUMP
|
||||||
|
array:2 [
|
||||||
|
$messageDump
|
||||||
|
"result" => array:2 [
|
||||||
|
"type" => "array"
|
||||||
|
"object" => Symfony\Component\VarDumper\Cloner\Data {%A
|
||||||
|
]
|
||||||
|
]
|
||||||
|
DUMP
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testHandleWithException()
|
||||||
|
{
|
||||||
|
$collector = new MessengerDataCollector();
|
||||||
|
$message = new DummyMessage('dummy message');
|
||||||
|
|
||||||
|
$expectedException = new \RuntimeException('foo');
|
||||||
|
$next = $this->createPartialMock(\stdClass::class, array('__invoke'));
|
||||||
|
$next->expects($this->once())->method('__invoke')->with($message)->willThrowException($expectedException);
|
||||||
|
|
||||||
|
try {
|
||||||
|
$collector->handle($message, $next);
|
||||||
|
} catch (\Throwable $actualException) {
|
||||||
|
$this->assertSame($expectedException, $actualException);
|
||||||
|
}
|
||||||
|
|
||||||
|
$messages = $collector->getMessages();
|
||||||
|
$this->assertCount(1, $messages);
|
||||||
|
|
||||||
|
$this->assertDumpMatchesFormat(<<<DUMP
|
||||||
|
array:2 [
|
||||||
|
"message" => array:2 [
|
||||||
|
"type" => "Symfony\Component\Messenger\Tests\Fixtures\DummyMessage"
|
||||||
|
"object" => Symfony\Component\VarDumper\Cloner\Data {%A
|
||||||
|
%A+class: "Symfony\Component\Messenger\Tests\Fixtures\DummyMessage"%A
|
||||||
|
}
|
||||||
|
]
|
||||||
|
"exception" => array:2 [
|
||||||
|
"type" => "RuntimeException"
|
||||||
|
"message" => "foo"
|
||||||
|
]
|
||||||
|
]
|
||||||
|
DUMP
|
||||||
|
, $messages[0]);
|
||||||
|
}
|
||||||
|
}
|
@ -21,7 +21,9 @@
|
|||||||
"require-dev": {
|
"require-dev": {
|
||||||
"symfony/serializer": "~3.4|~4.0",
|
"symfony/serializer": "~3.4|~4.0",
|
||||||
"symfony/dependency-injection": "~3.4.6|~4.0",
|
"symfony/dependency-injection": "~3.4.6|~4.0",
|
||||||
"symfony/property-access": "~3.4|~4.0"
|
"symfony/http-kernel": "~3.4|~4.0",
|
||||||
|
"symfony/property-access": "~3.4|~4.0",
|
||||||
|
"symfony/var-dumper": "~3.4|~4.0"
|
||||||
},
|
},
|
||||||
"suggest": {
|
"suggest": {
|
||||||
"sroze/enqueue-bridge": "For using the php-enqueue library as an adapter."
|
"sroze/enqueue-bridge": "For using the php-enqueue library as an adapter."
|
||||||
|
Reference in New Issue
Block a user