feat(demo): add story 1 — Sorano: Rock and Time
This commit is contained in:
@@ -0,0 +1,83 @@
|
||||
<?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\Handler;
|
||||
|
||||
use Symfony\Component\Messenger\Exception\LogicException;
|
||||
|
||||
/**
|
||||
* @author Nicolas Grekas <p@tchwork.com>
|
||||
*/
|
||||
class Acknowledger
|
||||
{
|
||||
private $handlerClass;
|
||||
private $ack;
|
||||
private $error = null;
|
||||
private $result = null;
|
||||
|
||||
/**
|
||||
* @param \Closure(\Throwable|null, mixed):void|null $ack
|
||||
*/
|
||||
public function __construct(string $handlerClass, ?\Closure $ack = null)
|
||||
{
|
||||
$this->handlerClass = $handlerClass;
|
||||
$this->ack = $ack ?? static function () {};
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $result
|
||||
*/
|
||||
public function ack($result = null): void
|
||||
{
|
||||
$this->doAck(null, $result);
|
||||
}
|
||||
|
||||
public function nack(\Throwable $error): void
|
||||
{
|
||||
$this->doAck($error);
|
||||
}
|
||||
|
||||
public function getError(): ?\Throwable
|
||||
{
|
||||
return $this->error;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function getResult()
|
||||
{
|
||||
return $this->result;
|
||||
}
|
||||
|
||||
public function isAcknowledged(): bool
|
||||
{
|
||||
return null === $this->ack;
|
||||
}
|
||||
|
||||
public function __destruct()
|
||||
{
|
||||
if ($this->ack instanceof \Closure) {
|
||||
throw new LogicException(sprintf('The acknowledger was not called by the "%s" batch handler.', $this->handlerClass));
|
||||
}
|
||||
}
|
||||
|
||||
private function doAck(?\Throwable $e = null, $result = null): void
|
||||
{
|
||||
if (!$ack = $this->ack) {
|
||||
throw new LogicException(sprintf('The acknowledger cannot be called twice by the "%s" batch handler.', $this->handlerClass));
|
||||
}
|
||||
$this->ack = null;
|
||||
$this->error = $e;
|
||||
$this->result = $result;
|
||||
$ack($e, $result);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
<?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\Handler;
|
||||
|
||||
/**
|
||||
* @author Nicolas Grekas <p@tchwork.com>
|
||||
*/
|
||||
interface BatchHandlerInterface
|
||||
{
|
||||
/**
|
||||
* @param Acknowledger|null $ack The function to call to ack/nack the $message.
|
||||
* The message should be handled synchronously when null.
|
||||
*
|
||||
* @return mixed The number of pending messages in the batch if $ack is not null,
|
||||
* the result from handling the message otherwise
|
||||
*/
|
||||
// public function __invoke(object $message, ?Acknowledger $ack = null): mixed;
|
||||
|
||||
/**
|
||||
* Flushes any pending buffers.
|
||||
*
|
||||
* @param bool $force Whether flushing is required; it can be skipped if not
|
||||
*/
|
||||
public function flush(bool $force): void;
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
<?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\Handler;
|
||||
|
||||
use Symfony\Component\Messenger\Exception\LogicException;
|
||||
|
||||
/**
|
||||
* @author Nicolas Grekas <p@tchwork.com>
|
||||
*/
|
||||
trait BatchHandlerTrait
|
||||
{
|
||||
private $jobs = [];
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function flush(bool $force): void
|
||||
{
|
||||
if ($jobs = $this->jobs) {
|
||||
$this->jobs = [];
|
||||
$this->process($jobs);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Acknowledger|null $ack The function to call to ack/nack the $message.
|
||||
* The message should be handled synchronously when null.
|
||||
*
|
||||
* @return mixed The number of pending messages in the batch if $ack is not null,
|
||||
* the result from handling the message otherwise
|
||||
*/
|
||||
private function handle(object $message, ?Acknowledger $ack)
|
||||
{
|
||||
if (null === $ack) {
|
||||
$ack = new Acknowledger(get_debug_type($this));
|
||||
$this->jobs[] = [$message, $ack];
|
||||
$this->flush(true);
|
||||
|
||||
return $ack->getResult();
|
||||
}
|
||||
|
||||
$this->jobs[] = [$message, $ack];
|
||||
if (!$this->shouldFlush()) {
|
||||
return \count($this->jobs);
|
||||
}
|
||||
|
||||
$this->flush(true);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
private function shouldFlush(): bool
|
||||
{
|
||||
return 10 <= \count($this->jobs);
|
||||
}
|
||||
|
||||
/**
|
||||
* Completes the jobs in the list.
|
||||
*
|
||||
* @param list<array{0: object, 1: Acknowledger}> $jobs A list of pairs of messages and their corresponding acknowledgers
|
||||
*/
|
||||
private function process(array $jobs): void
|
||||
{
|
||||
throw new LogicException(sprintf('"%s" should implement abstract method "process()".', get_debug_type($this)));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
<?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\Handler;
|
||||
|
||||
/**
|
||||
* Describes a handler and the possible associated options, such as `from_transport`, `bus`, etc.
|
||||
*
|
||||
* @author Samuel Roze <samuel.roze@gmail.com>
|
||||
*/
|
||||
final class HandlerDescriptor
|
||||
{
|
||||
private $handler;
|
||||
private $name;
|
||||
private $batchHandler;
|
||||
private $options;
|
||||
|
||||
public function __construct(callable $handler, array $options = [])
|
||||
{
|
||||
if (!$handler instanceof \Closure) {
|
||||
$handler = \Closure::fromCallable($handler);
|
||||
}
|
||||
|
||||
$this->handler = $handler;
|
||||
$this->options = $options;
|
||||
|
||||
$r = new \ReflectionFunction($handler);
|
||||
|
||||
if (str_contains($r->name, '{closure')) {
|
||||
$this->name = 'Closure';
|
||||
} elseif (!$handler = $r->getClosureThis()) {
|
||||
$class = \PHP_VERSION_ID >= 80111 ? $r->getClosureCalledClass() : $r->getClosureScopeClass();
|
||||
|
||||
$this->name = ($class ? $class->name.'::' : '').$r->name;
|
||||
} else {
|
||||
if ($handler instanceof BatchHandlerInterface) {
|
||||
$this->batchHandler = $handler;
|
||||
}
|
||||
|
||||
$this->name = \get_class($handler).'::'.$r->name;
|
||||
}
|
||||
}
|
||||
|
||||
public function getHandler(): callable
|
||||
{
|
||||
return $this->handler;
|
||||
}
|
||||
|
||||
public function getName(): string
|
||||
{
|
||||
$name = $this->name;
|
||||
$alias = $this->options['alias'] ?? null;
|
||||
|
||||
if (null !== $alias) {
|
||||
$name .= '@'.$alias;
|
||||
}
|
||||
|
||||
return $name;
|
||||
}
|
||||
|
||||
public function getBatchHandler(): ?BatchHandlerInterface
|
||||
{
|
||||
return $this->batchHandler;
|
||||
}
|
||||
|
||||
public function getOption(string $option)
|
||||
{
|
||||
return $this->options[$option] ?? null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
<?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\Handler;
|
||||
|
||||
use Symfony\Component\Messenger\Envelope;
|
||||
use Symfony\Component\Messenger\Stamp\ReceivedStamp;
|
||||
|
||||
/**
|
||||
* Maps a message to a list of handlers.
|
||||
*
|
||||
* @author Nicolas Grekas <p@tchwork.com>
|
||||
* @author Samuel Roze <samuel.roze@gmail.com>
|
||||
*/
|
||||
class HandlersLocator implements HandlersLocatorInterface
|
||||
{
|
||||
private $handlers;
|
||||
|
||||
/**
|
||||
* @param HandlerDescriptor[][]|callable[][] $handlers
|
||||
*/
|
||||
public function __construct(array $handlers)
|
||||
{
|
||||
$this->handlers = $handlers;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getHandlers(Envelope $envelope): iterable
|
||||
{
|
||||
$seen = [];
|
||||
|
||||
foreach (self::listTypes($envelope) as $type) {
|
||||
foreach ($this->handlers[$type] ?? [] as $handlerDescriptor) {
|
||||
if (\is_callable($handlerDescriptor)) {
|
||||
$handlerDescriptor = new HandlerDescriptor($handlerDescriptor);
|
||||
}
|
||||
|
||||
if (!$this->shouldHandle($envelope, $handlerDescriptor)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$name = $handlerDescriptor->getName();
|
||||
if (\in_array($name, $seen)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$seen[] = $name;
|
||||
|
||||
yield $handlerDescriptor;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
public static function listTypes(Envelope $envelope): array
|
||||
{
|
||||
$class = \get_class($envelope->getMessage());
|
||||
|
||||
return [$class => $class]
|
||||
+ class_parents($class)
|
||||
+ class_implements($class)
|
||||
+ ['*' => '*'];
|
||||
}
|
||||
|
||||
private function shouldHandle(Envelope $envelope, HandlerDescriptor $handlerDescriptor): bool
|
||||
{
|
||||
if (null === $received = $envelope->last(ReceivedStamp::class)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (null === $expectedTransport = $handlerDescriptor->getOption('from_transport')) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return $received->getTransportName() === $expectedTransport;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
<?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\Handler;
|
||||
|
||||
use Symfony\Component\Messenger\Envelope;
|
||||
|
||||
/**
|
||||
* Maps a message to a list of handlers.
|
||||
*
|
||||
* @author Nicolas Grekas <p@tchwork.com>
|
||||
*/
|
||||
interface HandlersLocatorInterface
|
||||
{
|
||||
/**
|
||||
* Returns the handlers for the given message name.
|
||||
*
|
||||
* @return iterable<int, HandlerDescriptor>
|
||||
*/
|
||||
public function getHandlers(Envelope $envelope): iterable;
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
<?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\Handler;
|
||||
|
||||
/**
|
||||
* Marker interface for message handlers.
|
||||
*
|
||||
* @author Samuel Roze <samuel.roze@gmail.com>
|
||||
*/
|
||||
interface MessageHandlerInterface
|
||||
{
|
||||
}
|
||||
+49
@@ -0,0 +1,49 @@
|
||||
<?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\Handler;
|
||||
|
||||
/**
|
||||
* Handlers can implement this interface to handle multiple messages.
|
||||
*
|
||||
* @author Samuel Roze <samuel.roze@gmail.com>
|
||||
*/
|
||||
interface MessageSubscriberInterface extends MessageHandlerInterface
|
||||
{
|
||||
/**
|
||||
* Returns a list of messages to be handled.
|
||||
*
|
||||
* It returns a list of messages like in the following example:
|
||||
*
|
||||
* yield MyMessage::class;
|
||||
*
|
||||
* It can also change the priority per classes.
|
||||
*
|
||||
* yield FirstMessage::class => ['priority' => 0];
|
||||
* yield SecondMessage::class => ['priority' => -10];
|
||||
*
|
||||
* It can also specify a method, a priority, a bus and/or a transport per message:
|
||||
*
|
||||
* yield FirstMessage::class => ['method' => 'firstMessageMethod'];
|
||||
* yield SecondMessage::class => [
|
||||
* 'method' => 'secondMessageMethod',
|
||||
* 'priority' => 20,
|
||||
* 'bus' => 'my_bus_name',
|
||||
* 'from_transport' => 'your_transport_name',
|
||||
* ];
|
||||
*
|
||||
* The benefit of using `yield` instead of returning an array is that you can `yield` multiple times the
|
||||
* same key and therefore subscribe to the same message multiple times with different options.
|
||||
*
|
||||
* The `__invoke` method of the handler will be called as usual with the message to handle.
|
||||
*/
|
||||
public static function getHandledMessages(): iterable;
|
||||
}
|
||||
Reference in New Issue
Block a user