diff --git a/README.md b/README.md index 5c11189f..836bc494 100644 --- a/README.md +++ b/README.md @@ -166,7 +166,7 @@ By default, Yii Framework uses [yiisoft/yii-console](https://github.com/yiisoft/ See [Console commands](docs/guide/en/console-commands.md) for more details. -Producers use `Yiisoft\Queue\QueueProducerInterface` (`push()`, `status()`, `getName()`); consumers use `Yiisoft\Queue\QueueConsumerInterface` (`run()`, `listen()`). See [capability configuration](docs/guide/en/queue-capabilities.md) for the strict role map used when named queues are configured. +Producers use `Yiisoft\Queue\QueueProducerInterface` (`push()`, `status()`, `getQueueName()`); consumers use `Yiisoft\Queue\QueueConsumerInterface` (`run()`, `listen()`). See [capability configuration](docs/guide/en/queue-capabilities.md) for the strict role map used when named queues are configured. > In case you're running the queue in synchronous mode (no adapter), `queue:listen` logs an info message and exits. The messages are processed immediately when pushed. diff --git a/docs/guide/en/console-commands.md b/docs/guide/en/console-commands.md index 041bf0c1..9e214e9c 100644 --- a/docs/guide/en/console-commands.md +++ b/docs/guide/en/console-commands.md @@ -6,7 +6,7 @@ If you are using [yiisoft/config](https://github.com/yiisoft/config) and [yiisof If you are using [symfony/console](https://github.com/symfony/console) directly, you should register the commands manually. -> **Note:** `queue:run` and `queue:listen-all` use `QueueConsumerProviderInterface::getConsumerNames()` when no queue names are passed. Explicitly passed names are resolved with `getConsumer()` and must have a consumer role. +> **Note:** `queue:run` and `queue:listen-all` use `QueueConsumerProviderInterface::getConsumerQueueNames()` when no queue names are passed. Explicitly passed names are resolved with `getConsumer()` and must have a consumer role. In [yiisoft/app](https://github.com/yiisoft/app) the `yii` console binary is provided out of the box. If you are using [yiisoft/yii-console](https://github.com/yiisoft/yii-console) or `symfony/console` without that template, invoke these commands the same way you invoke other console commands in your application. diff --git a/docs/guide/en/queue-capabilities.md b/docs/guide/en/queue-capabilities.md index 5ee4ac8d..085e8b8c 100644 --- a/docs/guide/en/queue-capabilities.md +++ b/docs/guide/en/queue-capabilities.md @@ -2,7 +2,7 @@ A logical queue name can independently expose a producer, a consumer, or both. Inject `QueueProducerInterface` to push/status messages and `QueueConsumerInterface` to run/listen. Console commands use only `QueueConsumerProviderInterface`; retry middleware uses a direct `QueueProducerInterface` or `QueueProducerProviderInterface`. -Named providers use a strict nested role map. `getProducerNames()` and `getConsumerNames()` return only names with that role. Role definitions are created lazily and cached per name and role; failed lazy creation is cached and repeated lookups rethrow the same configuration error. +Named providers use a strict nested role map. `getProducerQueueNames()` and `getConsumerQueueNames()` return only names with that role. Role definitions are created lazily and cached per name and role; failed lazy creation is cached and repeated lookups rethrow the same configuration error. ```php use Yiisoft\Queue\QueueConsumer; diff --git a/docs/guide/en/queue-names-advanced.md b/docs/guide/en/queue-names-advanced.md index 0734118e..a605815f 100644 --- a/docs/guide/en/queue-names-advanced.md +++ b/docs/guide/en/queue-names-advanced.md @@ -8,9 +8,9 @@ Most applications configure names through [`yiisoft/queue.queues`](queue-names.m Providers translate a queue name into the capability the caller needs: -- `QueueProducerProviderInterface::getProducer($name)` returns a `QueueProducerInterface` for pushing messages and obtaining their status. -- `QueueConsumerProviderInterface::getConsumer($name)` returns a `QueueConsumerInterface` for running or listening for messages. -- `hasProducer()` / `hasConsumer()` check whether a name exposes a role. `getProducerNames()` / `getConsumerNames()` list names for only that role. +- `QueueProducerProviderInterface::getProducer($queueName)` returns a `QueueProducerInterface` for pushing messages and obtaining their status. +- `QueueConsumerProviderInterface::getConsumer($queueName)` returns a `QueueConsumerInterface` for running or listening for messages. +- `hasProducer()` / `hasConsumer()` check whether a name exposes a role. `getProducerQueueNames()` / `getConsumerQueueNames()` list names for only that role. Both lookup methods accept a string or `BackedEnum`. They throw `QueueNotFoundException` when the name is unknown or does not have the requested role. This separation prevents a producer-only queue from accidentally being used by a worker, and vice versa. diff --git a/docs/guide/en/queue-names.md b/docs/guide/en/queue-names.md index 1895c3bc..bee1d1bc 100644 --- a/docs/guide/en/queue-names.md +++ b/docs/guide/en/queue-names.md @@ -78,7 +78,7 @@ final readonly class SendTransactionalEmail } ``` -Both typed providers accept strings and `BackedEnum` values. Use `getProducerNames()` or `getConsumerNames()` when enumerating only that role. +Both typed providers accept strings and `BackedEnum` values. Use `getProducerQueueNames()` or `getConsumerQueueNames()` when enumerating only that role. ## Running workers diff --git a/src/AsyncQueueProducer.php b/src/AsyncQueueProducer.php index 38c8a3c0..7d63c389 100644 --- a/src/AsyncQueueProducer.php +++ b/src/AsyncQueueProducer.php @@ -18,7 +18,7 @@ */ final class AsyncQueueProducer implements QueueProducerInterface { - private string $name; + private string $queueName; private PushMiddlewareDispatcher $dispatcher; /** @@ -28,10 +28,10 @@ public function __construct( private readonly LoggerInterface $logger, PushMiddlewareConfig $middlewareConfig, private readonly AdapterInterface $adapter, - string|BackedEnum $name = DefaultQueue::NAME, + string|BackedEnum $queueName = DefaultQueue::NAME, array $middlewareDefinitions = [], ) { - $this->name = StringNormalizer::normalize($name); + $this->queueName = StringNormalizer::normalize($queueName); $this->dispatcher = new PushMiddlewareDispatcher( middlewareFactory: $middlewareConfig->middlewareFactory, middlewareDefinitions: [...$middlewareConfig->commonMiddlewareDefinitions, ...$middlewareDefinitions], @@ -39,9 +39,9 @@ public function __construct( ); } - public function getName(): string + public function getQueueName(): string { - return $this->name; + return $this->queueName; } public function push(MessageInterface $message): MessageInterface diff --git a/src/Command/ListenAllCommand.php b/src/Command/ListenAllCommand.php index da3d53bd..5ef5d75f 100644 --- a/src/Command/ListenAllCommand.php +++ b/src/Command/ListenAllCommand.php @@ -64,16 +64,16 @@ protected function execute(InputInterface $input, OutputInterface $output): int /** @var string[] $queueNames */ $queueNames = $input->getArgument('queue'); if ($queueNames === []) { - $queueNames = $this->queueProvider->getConsumerNames(); + $queueNames = $this->queueProvider->getConsumerQueueNames(); } - $queues = []; - /** @var string $queue */ - foreach ($queueNames as $queue) { - $queues[] = $this->queueProvider->getConsumer($queue); + $consumers = []; + /** @var string $queueName */ + foreach ($queueNames as $queueName) { + $consumers[] = $this->queueProvider->getConsumer($queueName); } - if ($queues === []) { + if ($consumers === []) { $output->writeln('No consumers are configured.'); return Command::SUCCESS; @@ -86,8 +86,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int while ($this->loop->canContinue()) { $hasMessages = false; - foreach ($queues as $queue) { - $hasMessages = $queue->run((int) $input->getOption('limit')) > 0 || $hasMessages; + foreach ($consumers as $consumer) { + $hasMessages = $consumer->run((int) $input->getOption('limit')) > 0 || $hasMessages; } if (!$hasMessages) { diff --git a/src/Command/RunCommand.php b/src/Command/RunCommand.php index 71f4c3c1..c6f9a422 100644 --- a/src/Command/RunCommand.php +++ b/src/Command/RunCommand.php @@ -47,14 +47,14 @@ protected function execute(InputInterface $input, OutputInterface $output): int /** @var string[] $queueNames */ $queueNames = $input->getArgument('queue'); if ($queueNames === []) { - $queueNames = $this->queueProvider->getConsumerNames(); + $queueNames = $this->queueProvider->getConsumerQueueNames(); } - /** @var string $queue */ - foreach ($queueNames as $queue) { - $queueConsumer = $this->queueProvider->getConsumer($queue); + /** @var string $queueName */ + foreach ($queueNames as $queueName) { + $queueConsumer = $this->queueProvider->getConsumer($queueName); - $output->write("Processing queue $queue... "); + $output->write("Processing queue $queueName... "); $count = $queueConsumer->run((int) $input->getOption('limit')); $output->writeln("Messages processed: $count."); diff --git a/src/Debug/QueueConsumerProviderProxy.php b/src/Debug/QueueConsumerProviderProxy.php index c842c5ad..ab6a6e28 100644 --- a/src/Debug/QueueConsumerProviderProxy.php +++ b/src/Debug/QueueConsumerProviderProxy.php @@ -12,18 +12,18 @@ final class QueueConsumerProviderProxy implements QueueConsumerProviderInterface { public function __construct(private readonly QueueConsumerProviderInterface $provider, private readonly QueueCollector $collector) {} - public function getConsumer(string|BackedEnum $name): QueueConsumerInterface + public function getConsumer(string|BackedEnum $queueName): QueueConsumerInterface { - return new QueueConsumerDecorator($this->provider->getConsumer($name), $this->collector); + return new QueueConsumerDecorator($this->provider->getConsumer($queueName), $this->collector); } - public function hasConsumer(string|BackedEnum $name): bool + public function hasConsumer(string|BackedEnum $queueName): bool { - return $this->provider->hasConsumer($name); + return $this->provider->hasConsumer($queueName); } - public function getConsumerNames(): array + public function getConsumerQueueNames(): array { - return $this->provider->getConsumerNames(); + return $this->provider->getConsumerQueueNames(); } } diff --git a/src/Debug/QueueProducerDecorator.php b/src/Debug/QueueProducerDecorator.php index f973c48d..589ba1c1 100644 --- a/src/Debug/QueueProducerDecorator.php +++ b/src/Debug/QueueProducerDecorator.php @@ -22,12 +22,12 @@ public function status(string|int $id): MessageStatus public function push(MessageInterface $message): MessageInterface { /** @psalm-var array{file: string, line: int} $stack */ $stack = debug_backtrace()[0]; $message = $this->queue->push($message); - $this->collector->collectPush($this->queue->getName(), $message, $stack['file'] . ':' . $stack['line']); + $this->collector->collectPush($this->queue->getQueueName(), $message, $stack['file'] . ':' . $stack['line']); return $message; } - public function getName(): string + public function getQueueName(): string { - return $this->queue->getName(); + return $this->queue->getQueueName(); } } diff --git a/src/Debug/QueueProducerProviderProxy.php b/src/Debug/QueueProducerProviderProxy.php index ed929f6d..a71f846a 100644 --- a/src/Debug/QueueProducerProviderProxy.php +++ b/src/Debug/QueueProducerProviderProxy.php @@ -12,18 +12,18 @@ final class QueueProducerProviderProxy implements QueueProducerProviderInterface { public function __construct(private readonly QueueProducerProviderInterface $provider, private readonly QueueCollector $collector) {} - public function getProducer(string|BackedEnum $name): QueueProducerInterface + public function getProducer(string|BackedEnum $queueName): QueueProducerInterface { - return new QueueProducerDecorator($this->provider->getProducer($name), $this->collector); + return new QueueProducerDecorator($this->provider->getProducer($queueName), $this->collector); } - public function hasProducer(string|BackedEnum $name): bool + public function hasProducer(string|BackedEnum $queueName): bool { - return $this->provider->hasProducer($name); + return $this->provider->hasProducer($queueName); } - public function getProducerNames(): array + public function getProducerQueueNames(): array { - return $this->provider->getProducerNames(); + return $this->provider->getProducerQueueNames(); } } diff --git a/src/Middleware/Push/SynchronousPushHandler.php b/src/Middleware/Push/SynchronousPushHandler.php index f35c1009..db7b9cfc 100644 --- a/src/Middleware/Push/SynchronousPushHandler.php +++ b/src/Middleware/Push/SynchronousPushHandler.php @@ -20,7 +20,7 @@ public function __construct( public function handlePush(MessageInterface $message): MessageInterface { - $this->worker->process($message, $this->queue->getName(), $this->queue); + $this->worker->process($message, $this->queue->getQueueName(), $this->queue); return $message; } diff --git a/src/Provider/CompositeQueueProvider.php b/src/Provider/CompositeQueueProvider.php index 6218da60..7a7abbe8 100644 --- a/src/Provider/CompositeQueueProvider.php +++ b/src/Provider/CompositeQueueProvider.php @@ -29,65 +29,65 @@ public function __construct(QueueProducerProviderInterface|QueueConsumerProvider } } - public function getProducer(string|BackedEnum $name): QueueProducerInterface + public function getProducer(string|BackedEnum $queueName): QueueProducerInterface { foreach ($this->producerProviders as $provider) { - if ($provider->hasProducer($name)) { - return $provider->getProducer($name); + if ($provider->hasProducer($queueName)) { + return $provider->getProducer($queueName); } } - throw new QueueNotFoundException(StringNormalizer::normalize($name)); + throw new QueueNotFoundException(StringNormalizer::normalize($queueName)); } - public function hasProducer(string|BackedEnum $name): bool + public function hasProducer(string|BackedEnum $queueName): bool { foreach ($this->producerProviders as $p) { - if ($p->hasProducer($name)) { + if ($p->hasProducer($queueName)) { return true; } } return false; } /** @return list */ - public function getProducerNames(): array + public function getProducerQueueNames(): array { $result = []; foreach ($this->producerProviders as $provider) { - foreach ($provider->getProducerNames() as $name) { - if (!in_array($name, $result, true)) { - $result[] = $name; + foreach ($provider->getProducerQueueNames() as $queueName) { + if (!in_array($queueName, $result, true)) { + $result[] = $queueName; } } } return $result; } - public function getConsumer(string|BackedEnum $name): QueueConsumerInterface + public function getConsumer(string|BackedEnum $queueName): QueueConsumerInterface { foreach ($this->consumerProviders as $provider) { - if ($provider->hasConsumer($name)) { - return $provider->getConsumer($name); + if ($provider->hasConsumer($queueName)) { + return $provider->getConsumer($queueName); } } - throw new QueueNotFoundException(StringNormalizer::normalize($name)); + throw new QueueNotFoundException(StringNormalizer::normalize($queueName)); } - public function hasConsumer(string|BackedEnum $name): bool + public function hasConsumer(string|BackedEnum $queueName): bool { foreach ($this->consumerProviders as $p) { - if ($p->hasConsumer($name)) { + if ($p->hasConsumer($queueName)) { return true; } } return false; } /** @return list */ - public function getConsumerNames(): array + public function getConsumerQueueNames(): array { $result = []; foreach ($this->consumerProviders as $provider) { - foreach ($provider->getConsumerNames() as $name) { - if (!in_array($name, $result, true)) { - $result[] = $name; + foreach ($provider->getConsumerQueueNames() as $queueName) { + if (!in_array($queueName, $result, true)) { + $result[] = $queueName; } } } return $result; diff --git a/src/Provider/PredefinedQueueProvider.php b/src/Provider/PredefinedQueueProvider.php index 7bb7335a..7b3d6df2 100644 --- a/src/Provider/PredefinedQueueProvider.php +++ b/src/Provider/PredefinedQueueProvider.php @@ -24,91 +24,91 @@ final class PredefinedQueueProvider implements QueueProducerProviderInterface, Q /** @var array> */ private array $queues = []; /** @var list */ - private array $producerNames = []; + private array $producerQueueNames = []; /** @var list */ - private array $consumerNames = []; + private array $consumerQueueNames = []; /** @param array $queues */ public function __construct(array $queues) { - foreach ($queues as $name => $roles) { + foreach ($queues as $queueName => $roles) { if (!is_array($roles) || $roles === []) { - throw new InvalidQueueConfigException(sprintf('Queue "%s" must be a non-empty role map containing ready "producer" and/or "consumer" instances.', $name)); + throw new InvalidQueueConfigException(sprintf('Queue "%s" must be a non-empty role map containing ready "producer" and/or "consumer" instances.', $queueName)); } $unknown = array_diff(array_keys($roles), ['producer', 'consumer']); if ($unknown !== []) { - throw new InvalidQueueConfigException(sprintf('Queue "%s" has unknown role key(s) "%s". Only "producer" and "consumer" are allowed.', $name, implode('", "', $unknown))); + throw new InvalidQueueConfigException(sprintf('Queue "%s" has unknown role key(s) "%s". Only "producer" and "consumer" are allowed.', $queueName, implode('", "', $unknown))); } - foreach ($roles as $role => $queue) { + foreach ($roles as $role => $instance) { $expected = $role === 'producer' ? QueueProducerInterface::class : QueueConsumerInterface::class; - if (!$queue instanceof $expected) { - $hint = is_array($queue) || is_string($queue) ? ' Use QueueFactoryProvider for factory definitions.' : ''; + if (!$instance instanceof $expected) { + $hint = is_array($instance) || is_string($instance) ? ' Use QueueFactoryProvider for factory definitions.' : ''; throw new InvalidQueueConfigException(sprintf( 'Queue "%s" role "%s" must be a ready instance of "%s"; got "%s" (configuration path queues.%s.%s).%s', - $name, + $queueName, $role, $expected, - get_debug_type($queue), - $name, + get_debug_type($instance), + $queueName, $role, $hint, )); } } /** @var array $roles */ - $this->queues[$name] = $roles; + $this->queues[$queueName] = $roles; if (array_key_exists('producer', $roles)) { - $this->producerNames[] = $name; + $this->producerQueueNames[] = $queueName; } if (array_key_exists('consumer', $roles)) { - $this->consumerNames[] = $name; + $this->consumerQueueNames[] = $queueName; } } } - public function getProducer(string|BackedEnum $name): QueueProducerInterface + public function getProducer(string|BackedEnum $queueName): QueueProducerInterface { - $queue = $this->get($name, 'producer'); - assert($queue instanceof QueueProducerInterface); - return $queue; + $instance = $this->get($queueName, 'producer'); + assert($instance instanceof QueueProducerInterface); + return $instance; } - public function hasProducer(string|BackedEnum $name): bool + public function hasProducer(string|BackedEnum $queueName): bool { - return array_key_exists('producer', $this->queues[StringNormalizer::normalize($name)] ?? []); + return array_key_exists('producer', $this->queues[StringNormalizer::normalize($queueName)] ?? []); } - public function getProducerNames(): array + public function getProducerQueueNames(): array { - return $this->producerNames; + return $this->producerQueueNames; } - public function getConsumer(string|BackedEnum $name): QueueConsumerInterface + public function getConsumer(string|BackedEnum $queueName): QueueConsumerInterface { - $queue = $this->get($name, 'consumer'); - assert($queue instanceof QueueConsumerInterface); - return $queue; + $instance = $this->get($queueName, 'consumer'); + assert($instance instanceof QueueConsumerInterface); + return $instance; } - public function hasConsumer(string|BackedEnum $name): bool + public function hasConsumer(string|BackedEnum $queueName): bool { - return array_key_exists('consumer', $this->queues[StringNormalizer::normalize($name)] ?? []); + return array_key_exists('consumer', $this->queues[StringNormalizer::normalize($queueName)] ?? []); } - public function getConsumerNames(): array + public function getConsumerQueueNames(): array { - return $this->consumerNames; + return $this->consumerQueueNames; } - private function get(string|BackedEnum $name, string $role): QueueProducerInterface|QueueConsumerInterface + private function get(string|BackedEnum $queueName, string $role): QueueProducerInterface|QueueConsumerInterface { - $name = StringNormalizer::normalize($name); - if (!array_key_exists($name, $this->queues)) { - throw new QueueNotFoundException($name); + $queueName = StringNormalizer::normalize($queueName); + if (!array_key_exists($queueName, $this->queues)) { + throw new QueueNotFoundException($queueName); } - if (!array_key_exists($role, $this->queues[$name])) { - throw new QueueNotFoundException(sprintf('Queue "%s" does not have the "%s" capability.', $name, $role)); + if (!array_key_exists($role, $this->queues[$queueName])) { + throw new QueueNotFoundException(sprintf('Queue "%s" does not have the "%s" capability.', $queueName, $role)); } - return $this->queues[$name][$role]; + return $this->queues[$queueName][$role]; } } diff --git a/src/Provider/QueueConsumerProviderInterface.php b/src/Provider/QueueConsumerProviderInterface.php index 56aed416..e114f68d 100644 --- a/src/Provider/QueueConsumerProviderInterface.php +++ b/src/Provider/QueueConsumerProviderInterface.php @@ -11,11 +11,11 @@ interface QueueConsumerProviderInterface { /** @throws InvalidQueueConfigException|QueueNotFoundException|QueueProviderException */ - public function getConsumer(string|BackedEnum $name): QueueConsumerInterface; + public function getConsumer(string|BackedEnum $queueName): QueueConsumerInterface; - /** Whether this name has a configured consumer role. */ - public function hasConsumer(string|BackedEnum $name): bool; + /** Whether this queue name has a configured consumer role. */ + public function hasConsumer(string|BackedEnum $queueName): bool; - /** @return list Names which have a configured consumer role. */ - public function getConsumerNames(): array; + /** @return list Queue names which have a configured consumer role. */ + public function getConsumerQueueNames(): array; } diff --git a/src/Provider/QueueFactoryProvider.php b/src/Provider/QueueFactoryProvider.php index ed414653..5957815a 100644 --- a/src/Provider/QueueFactoryProvider.php +++ b/src/Provider/QueueFactoryProvider.php @@ -29,9 +29,9 @@ final class QueueFactoryProvider implements QueueProducerProviderInterface, Queu /** @var array> */ private array $resolved = []; /** @var list */ - private array $producerNames = []; + private array $producerQueueNames = []; /** @var list */ - private array $consumerNames = []; + private array $consumerQueueNames = []; /** @param array $definitions */ public function __construct( @@ -42,98 +42,98 @@ public function __construct( /** @var array> $validatedDefinitions */ $validatedDefinitions = $this->validateRoleMaps($definitions); $this->definitions = $validatedDefinitions; - foreach ($this->definitions as $name => $roles) { + foreach ($this->definitions as $queueName => $roles) { if (array_key_exists('producer', $roles)) { - $this->producerNames[] = $name; + $this->producerQueueNames[] = $queueName; } if (array_key_exists('consumer', $roles)) { - $this->consumerNames[] = $name; + $this->consumerQueueNames[] = $queueName; } } } - public function getProducer(string|BackedEnum $name): QueueProducerInterface + public function getProducer(string|BackedEnum $queueName): QueueProducerInterface { - $producer = $this->get($name, 'producer', QueueProducerInterface::class); + $producer = $this->get($queueName, 'producer', QueueProducerInterface::class); assert($producer instanceof QueueProducerInterface); return $producer; } - public function hasProducer(string|BackedEnum $name): bool + public function hasProducer(string|BackedEnum $queueName): bool { - return array_key_exists('producer', $this->definitions[StringNormalizer::normalize($name)] ?? []); + return array_key_exists('producer', $this->definitions[StringNormalizer::normalize($queueName)] ?? []); } - public function getProducerNames(): array + public function getProducerQueueNames(): array { - return $this->producerNames; + return $this->producerQueueNames; } - public function getConsumer(string|BackedEnum $name): QueueConsumerInterface + public function getConsumer(string|BackedEnum $queueName): QueueConsumerInterface { - $consumer = $this->get($name, 'consumer', QueueConsumerInterface::class); + $consumer = $this->get($queueName, 'consumer', QueueConsumerInterface::class); assert($consumer instanceof QueueConsumerInterface); return $consumer; } - public function hasConsumer(string|BackedEnum $name): bool + public function hasConsumer(string|BackedEnum $queueName): bool { - return array_key_exists('consumer', $this->definitions[StringNormalizer::normalize($name)] ?? []); + return array_key_exists('consumer', $this->definitions[StringNormalizer::normalize($queueName)] ?? []); } - public function getConsumerNames(): array + public function getConsumerQueueNames(): array { - return $this->consumerNames; + return $this->consumerQueueNames; } /** @template T of QueueProducerInterface|QueueConsumerInterface @param class-string $expected @return T */ - private function get(string|BackedEnum $name, string $role, string $expected): QueueProducerInterface|QueueConsumerInterface + private function get(string|BackedEnum $queueName, string $role, string $expected): QueueProducerInterface|QueueConsumerInterface { - $name = StringNormalizer::normalize($name); - if (!array_key_exists($name, $this->definitions)) { - throw new QueueNotFoundException($name); + $queueName = StringNormalizer::normalize($queueName); + if (!array_key_exists($queueName, $this->definitions)) { + throw new QueueNotFoundException($queueName); } - if (!array_key_exists($role, $this->definitions[$name])) { - throw new QueueNotFoundException(sprintf('Queue "%s" does not have the "%s" capability.', $name, $role)); + if (!array_key_exists($role, $this->definitions[$queueName])) { + throw new QueueNotFoundException(sprintf('Queue "%s" does not have the "%s" capability.', $queueName, $role)); } - if (isset($this->resolved[$name][$role])) { - $result = $this->resolved[$name][$role]; + if (isset($this->resolved[$queueName][$role])) { + $result = $this->resolved[$queueName][$role]; if ($result instanceof Throwable) { throw $result; } return $result; } try { - $key = $name . ':' . $role; - $factory = new StrictFactory([$key => $this->definitions[$name][$role]], $this->container, $this->validate); + $key = $queueName . ':' . $role; + $factory = new StrictFactory([$key => $this->definitions[$queueName][$role]], $this->container, $this->validate); $result = $factory->create($key); if (!$result instanceof $expected) { throw new InvalidQueueConfigException(sprintf( 'Queue "%s" role "%s" must implement "%s"; got "%s" (configuration path queues.%s.%s).', - $name, + $queueName, $role, $expected, get_debug_type($result), - $name, + $queueName, $role, )); } assert($result instanceof QueueProducerInterface || $result instanceof QueueConsumerInterface); - $this->resolved[$name][$role] = $result; + $this->resolved[$queueName][$role] = $result; return $result; } catch (InvalidQueueConfigException $exception) { - $this->resolved[$name][$role] = $exception; + $this->resolved[$queueName][$role] = $exception; throw $exception; } catch (InvalidConfigException $exception) { $wrapped = new InvalidQueueConfigException(sprintf( 'Invalid queue "%s" role "%s" definition (configuration path queues.%s.%s): %s', - $name, + $queueName, $role, - $name, + $queueName, $role, $exception->getMessage(), ), previous: $exception); - $this->resolved[$name][$role] = $wrapped; + $this->resolved[$queueName][$role] = $wrapped; throw $wrapped; } } @@ -143,20 +143,20 @@ private function validateRoleMaps(array $definitions): array { /** @var array> $result */ $result = []; - foreach ($definitions as $name => $roles) { + foreach ($definitions as $queueName => $roles) { if (!is_array($roles)) { - throw new InvalidQueueConfigException(sprintf('Queue "%s" must be a role map containing "producer" and/or "consumer"; got "%s".', $name, get_debug_type($roles))); + throw new InvalidQueueConfigException(sprintf('Queue "%s" must be a role map containing "producer" and/or "consumer"; got "%s".', $queueName, get_debug_type($roles))); } $keys = array_keys($roles); $unknown = array_diff($keys, ['producer', 'consumer']); if ($unknown !== []) { - throw new InvalidQueueConfigException(sprintf('Queue "%s" has unknown role key(s) "%s". Only "producer" and "consumer" are allowed.', $name, implode('", "', $unknown))); + throw new InvalidQueueConfigException(sprintf('Queue "%s" has unknown role key(s) "%s". Only "producer" and "consumer" are allowed.', $queueName, implode('", "', $unknown))); } if ($roles === []) { - throw new InvalidQueueConfigException(sprintf('Queue "%s" role map must contain "producer" and/or "consumer".', $name)); + throw new InvalidQueueConfigException(sprintf('Queue "%s" role map must contain "producer" and/or "consumer".', $queueName)); } /** @var array $roles */ - $result[$name] = $roles; + $result[$queueName] = $roles; } return $result; } diff --git a/src/Provider/QueueNotFoundException.php b/src/Provider/QueueNotFoundException.php index 08acbe40..5f36327c 100644 --- a/src/Provider/QueueNotFoundException.php +++ b/src/Provider/QueueNotFoundException.php @@ -16,10 +16,10 @@ */ final class QueueNotFoundException extends LogicException implements QueueProviderException { - public function __construct(string|BackedEnum $name, int $code = 0, ?Throwable $previous = null) + public function __construct(string|BackedEnum $queueName, int $code = 0, ?Throwable $previous = null) { parent::__construct( - sprintf('Queue with name "%s" not found.', StringNormalizer::normalize($name)), + sprintf('Queue "%s" not found.', StringNormalizer::normalize($queueName)), $code, $previous, ); diff --git a/src/Provider/QueueProducerProviderInterface.php b/src/Provider/QueueProducerProviderInterface.php index a83ab001..8d8073ea 100644 --- a/src/Provider/QueueProducerProviderInterface.php +++ b/src/Provider/QueueProducerProviderInterface.php @@ -11,11 +11,11 @@ interface QueueProducerProviderInterface { /** @throws InvalidQueueConfigException|QueueNotFoundException|QueueProviderException */ - public function getProducer(string|BackedEnum $name): QueueProducerInterface; + public function getProducer(string|BackedEnum $queueName): QueueProducerInterface; - /** Whether this name has a configured producer role. */ - public function hasProducer(string|BackedEnum $name): bool; + /** Whether this queue name has a configured producer role. */ + public function hasProducer(string|BackedEnum $queueName): bool; - /** @return list Names which have a configured producer role. */ - public function getProducerNames(): array; + /** @return list Queue names which have a configured producer role. */ + public function getProducerQueueNames(): array; } diff --git a/src/QueueConsumer.php b/src/QueueConsumer.php index eddb0d9a..ffcfbe7b 100644 --- a/src/QueueConsumer.php +++ b/src/QueueConsumer.php @@ -14,16 +14,16 @@ /** Consumes messages for one logical queue. */ final class QueueConsumer implements QueueConsumerInterface { - private string $name; + private string $queueName; public function __construct( private readonly WorkerInterface $worker, private readonly LoopInterface $loop, private readonly LoggerInterface $logger, private readonly ?AdapterInterface $adapter = null, - string|BackedEnum $name = DefaultQueue::NAME, + string|BackedEnum $queueName = DefaultQueue::NAME, ) { - $this->name = StringNormalizer::normalize($name); + $this->queueName = StringNormalizer::normalize($queueName); } public function run(int $max = 0): int @@ -58,7 +58,7 @@ public function listen(): void private function handle(MessageInterface $message): bool { - $this->worker->process($message, $this->name); + $this->worker->process($message, $this->queueName); return $this->loop->canContinue(); } } diff --git a/src/QueueProducerInterface.php b/src/QueueProducerInterface.php index 21906bc2..5613b195 100644 --- a/src/QueueProducerInterface.php +++ b/src/QueueProducerInterface.php @@ -16,5 +16,5 @@ public function push(MessageInterface $message): MessageInterface; public function status(string|int $id): MessageStatus; /** Returns the logical queue name. */ - public function getName(): string; + public function getQueueName(): string; } diff --git a/src/SyncQueueProducer.php b/src/SyncQueueProducer.php index 53315f9a..555c21fd 100644 --- a/src/SyncQueueProducer.php +++ b/src/SyncQueueProducer.php @@ -17,7 +17,7 @@ */ final class SyncQueueProducer implements QueueProducerInterface { - private string $name; + private string $queueName; private PushMiddlewareDispatcher $dispatcher; /** @@ -27,10 +27,10 @@ public function __construct( private readonly LoggerInterface $logger, PushMiddlewareConfig $middlewareConfig, WorkerInterface $worker, - string|BackedEnum $name = DefaultQueue::NAME, + string|BackedEnum $queueName = DefaultQueue::NAME, array $middlewareDefinitions = [], ) { - $this->name = StringNormalizer::normalize($name); + $this->queueName = StringNormalizer::normalize($queueName); $this->dispatcher = new PushMiddlewareDispatcher( middlewareFactory: $middlewareConfig->middlewareFactory, middlewareDefinitions: [...$middlewareConfig->commonMiddlewareDefinitions, ...$middlewareDefinitions], @@ -38,9 +38,9 @@ public function __construct( ); } - public function getName(): string + public function getQueueName(): string { - return $this->name; + return $this->queueName; } public function push(MessageInterface $message): MessageInterface diff --git a/stubs/StubQueueProducer.php b/stubs/StubQueueProducer.php index bac0547e..4525747b 100644 --- a/stubs/StubQueueProducer.php +++ b/stubs/StubQueueProducer.php @@ -10,7 +10,7 @@ final class StubQueueProducer implements QueueProducerInterface { - public function __construct(private string $name = 'default') {} + public function __construct(private string $queueName = 'default') {} public function push(MessageInterface $message): MessageInterface { @@ -22,8 +22,8 @@ public function status(string|int $id): MessageStatus return MessageStatus::DONE; } - public function getName(): string + public function getQueueName(): string { - return $this->name; + return $this->queueName; } } diff --git a/tests/Integration/MiddlewareTest.php b/tests/Integration/MiddlewareTest.php index 3ef03f6f..9118d0e5 100644 --- a/tests/Integration/MiddlewareTest.php +++ b/tests/Integration/MiddlewareTest.php @@ -131,7 +131,7 @@ public function testFullStackFailure(): void $callableFactory = new CallableFactory($container); $queue->expects(self::exactly(7))->method('push')->willReturnCallback($queueCallback); - $queue->method('getName')->willReturn('simple'); + $queue->method('getQueueName')->willReturn('simple'); $middlewares = [ 'test-queue' => [ diff --git a/tests/Integration/QueueProviderTest.php b/tests/Integration/QueueProviderTest.php index c2af9af7..cd9e5e31 100644 --- a/tests/Integration/QueueProviderTest.php +++ b/tests/Integration/QueueProviderTest.php @@ -33,7 +33,7 @@ public function testFactoryRoleMapsResolveThroughContainerAndKeepCapabilitiesSep 'both' => [ 'producer' => [ 'class' => StubQueueProducer::class, - '__construct()' => ['name' => Reference::to('producer-name')], + '__construct()' => ['queueName' => Reference::to('producer-name')], ], 'consumer' => StubQueueConsumer::class, ], @@ -41,9 +41,9 @@ public function testFactoryRoleMapsResolveThroughContainerAndKeepCapabilitiesSep 'consumer-only' => ['consumer' => StubQueueConsumer::class], ], $container); - self::assertSame(['both', 'producer-only'], $provider->getProducerNames()); - self::assertSame(['both', 'consumer-only'], $provider->getConsumerNames()); - self::assertSame('factory-both', $provider->getProducer('both')->getName()); + self::assertSame(['both', 'producer-only'], $provider->getProducerQueueNames()); + self::assertSame(['both', 'consumer-only'], $provider->getConsumerQueueNames()); + self::assertSame('factory-both', $provider->getProducer('both')->getQueueName()); self::assertInstanceOf(StubQueueConsumer::class, $provider->getConsumer('both')); self::assertInstanceOf(StubQueueProducer::class, $provider->getProducer('producer-only')); self::assertInstanceOf(StubQueueConsumer::class, $provider->getConsumer('consumer-only')); @@ -64,8 +64,8 @@ public function testPredefinedRoleMapsAndListenCommandUseConsumerOnlyService(): 'consumer-only' => ['consumer' => $consumer], ]); - self::assertSame(['both', 'producer-only'], $provider->getProducerNames()); - self::assertSame(['both', 'consumer-only'], $provider->getConsumerNames()); + self::assertSame(['both', 'producer-only'], $provider->getProducerQueueNames()); + self::assertSame(['both', 'consumer-only'], $provider->getConsumerQueueNames()); self::assertInstanceOf(QueueProducerInterface::class, $provider->getProducer('both')); self::assertInstanceOf(QueueConsumerInterface::class, $provider->getConsumer('both')); self::assertFalse($provider->hasConsumer('producer-only')); @@ -98,8 +98,8 @@ public function testDebugProxiesPreserveSeparatedProviderRoles(): void self::assertInstanceOf(QueueProducerDecorator::class, $producer); self::assertInstanceOf(QueueConsumerDecorator::class, $consumer); - self::assertSame(['mixed-name'], $producerProvider->getProducerNames()); - self::assertSame(['consumer-only'], $consumerProvider->getConsumerNames()); + self::assertSame(['mixed-name'], $producerProvider->getProducerQueueNames()); + self::assertSame(['consumer-only'], $consumerProvider->getConsumerQueueNames()); self::assertSame(1, $collector->getSummary()['countPushes']); } } diff --git a/tests/TestCase.php b/tests/TestCase.php index 8f597663..5ed6f752 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -93,20 +93,20 @@ protected function getContainer(): ContainerInterface protected function createQueue( ?AdapterInterface $adapter = null, - string|BackedEnum $name = DefaultQueue::NAME, + string|BackedEnum $queueName = DefaultQueue::NAME, ): QueueProducerInterface { return $adapter === null ? new SyncQueueProducer( new NullLogger(), $this->getPushMiddlewareConfig(), $this->getWorker(), - $name, + $queueName, ) : new AsyncQueueProducer( new NullLogger(), $this->getPushMiddlewareConfig(), $adapter, - $name, + $queueName, ); } diff --git a/tests/Unit/Debug/QueueDecoratorTest.php b/tests/Unit/Debug/QueueDecoratorTest.php index c408726d..48913476 100644 --- a/tests/Unit/Debug/QueueDecoratorTest.php +++ b/tests/Unit/Debug/QueueDecoratorTest.php @@ -19,7 +19,7 @@ public function testProducerDecoratorDelegatesAndCollects(): void { $message = new GenericMessage('test', null); $producer = $this->createMock(QueueProducerInterface::class); - $producer->method('getName')->willReturn('queue'); + $producer->method('getQueueName')->willReturn('queue'); $producer->expects($this->once())->method('push')->with($message)->willReturn($message); $producer->expects($this->once())->method('status')->with('1')->willReturn(MessageStatus::WAITING); $collector = new QueueCollector(); diff --git a/tests/Unit/Debug/QueueProviderInterfaceProxyTest.php b/tests/Unit/Debug/QueueProviderInterfaceProxyTest.php index cf6325d3..f9e5fc48 100644 --- a/tests/Unit/Debug/QueueProviderInterfaceProxyTest.php +++ b/tests/Unit/Debug/QueueProviderInterfaceProxyTest.php @@ -32,10 +32,10 @@ public function testConsumerProxyDelegatesOnlyConsumerRole(): void $provider = $this->createMock(QueueConsumerProviderInterface::class); $provider->method('getConsumer')->willReturn($consumer); $provider->method('hasConsumer')->with('queue')->willReturn(true); - $provider->method('getConsumerNames')->willReturn(['queue']); + $provider->method('getConsumerQueueNames')->willReturn(['queue']); $proxy = new QueueConsumerProviderProxy($provider, new QueueCollector()); self::assertInstanceOf(QueueConsumerDecorator::class, $proxy->getConsumer('queue')); self::assertTrue($proxy->hasConsumer('queue')); - self::assertSame(['queue'], $proxy->getConsumerNames()); + self::assertSame(['queue'], $proxy->getConsumerQueueNames()); } } diff --git a/tests/Unit/Provider/CompositeQueueProviderTest.php b/tests/Unit/Provider/CompositeQueueProviderTest.php index d4526b95..2a1a2db1 100644 --- a/tests/Unit/Provider/CompositeQueueProviderTest.php +++ b/tests/Unit/Provider/CompositeQueueProviderTest.php @@ -22,8 +22,8 @@ public function testCombinesRolesAndPreservesPrecedence(): void ); self::assertSame($firstProducer, $provider->getProducer('queue')); self::assertInstanceOf(StubQueueConsumer::class, $provider->getConsumer('queue')); - self::assertSame(['queue'], $provider->getProducerNames()); - self::assertSame(['queue'], $provider->getConsumerNames()); + self::assertSame(['queue'], $provider->getProducerQueueNames()); + self::assertSame(['queue'], $provider->getConsumerQueueNames()); } public function testMissingCapabilityThrows(): void diff --git a/tests/Unit/Provider/PredefinedQueueProviderTest.php b/tests/Unit/Provider/PredefinedQueueProviderTest.php index 982abc27..bf751209 100644 --- a/tests/Unit/Provider/PredefinedQueueProviderTest.php +++ b/tests/Unit/Provider/PredefinedQueueProviderTest.php @@ -22,8 +22,8 @@ public function testProvidesIndependentRoles(): void self::assertSame($producer, $provider->getProducer('queue1')); self::assertSame($consumer, $provider->getConsumer('queue1')); - self::assertSame(['queue1'], $provider->getProducerNames()); - self::assertSame(['queue1'], $provider->getConsumerNames()); + self::assertSame(['queue1'], $provider->getProducerQueueNames()); + self::assertSame(['queue1'], $provider->getConsumerQueueNames()); } public function testCapabilityIsolationAndEnumNames(): void diff --git a/tests/Unit/Provider/QueueFactoryProviderTest.php b/tests/Unit/Provider/QueueFactoryProviderTest.php index 902c1053..51f2a826 100644 --- a/tests/Unit/Provider/QueueFactoryProviderTest.php +++ b/tests/Unit/Provider/QueueFactoryProviderTest.php @@ -20,8 +20,8 @@ public function testLazilyCreatesRolesIndependently(): void self::assertInstanceOf(StubQueueProducer::class, $provider->getProducer('queue')); self::assertSame($provider->getProducer('queue'), $provider->getProducer('queue')); self::assertInstanceOf(StubQueueConsumer::class, $provider->getConsumer('queue')); - self::assertSame(['queue'], $provider->getProducerNames()); - self::assertSame(['queue'], $provider->getConsumerNames()); + self::assertSame(['queue'], $provider->getProducerQueueNames()); + self::assertSame(['queue'], $provider->getConsumerQueueNames()); } public function testCapabilityIsolation(): void diff --git a/tests/Unit/QueueTest.php b/tests/Unit/QueueTest.php index cc0a5502..4923edca 100644 --- a/tests/Unit/QueueTest.php +++ b/tests/Unit/QueueTest.php @@ -69,7 +69,7 @@ public function testSynchronousConsumerIsNoOp(): void public function testProducerNameSupportsEnum(): void { - self::assertSame('high-priority', $this->createQueue(name: TestQueue::HIGH_PRIORITY)->getName()); + self::assertSame('high-priority', $this->createQueue(queueName: TestQueue::HIGH_PRIORITY)->getQueueName()); } public function testConsumerStopsAtLimit(): void diff --git a/tests/Unit/WorkerTest.php b/tests/Unit/WorkerTest.php index 786f7577..6840f828 100644 --- a/tests/Unit/WorkerTest.php +++ b/tests/Unit/WorkerTest.php @@ -39,10 +39,10 @@ public function testMessageHandled(mixed $handler, array $containerServices): vo $container = new SimpleContainer($containerServices); $handlers = ['simple' => $handler]; - $queue = 'test-queue'; + $queueName = 'test-queue'; $worker = $this->createWorkerByParams($handlers, $container, $logger); - $worker->process($message, $queue); + $worker->process($message, $queueName); $processedMessages = FakeHandler::$processedMessages; FakeHandler::$processedMessages = []; @@ -93,10 +93,10 @@ public function testMessageFailWithDefinitionUndefinedMethodHandler(): void $container = new SimpleContainer([FakeHandler::class => $handler]); $handlers = ['simple' => [FakeHandler::class, 'undefinedMethod']]; - $queue = 'test-queue'; + $queueName = 'test-queue'; $worker = $this->createWorkerByParams($handlers, $container); - $worker->process($message, $queue); + $worker->process($message, $queueName); } public function testMessageFailWithDefinitionUndefinedClassHandler(): void @@ -109,10 +109,10 @@ public function testMessageFailWithDefinitionUndefinedClassHandler(): void $container = new SimpleContainer([FakeHandler::class => $handler]); $handlers = ['simple' => ['UndefinedClass', 'handle']]; - $queue = 'test-queue'; + $queueName = 'test-queue'; $worker = $this->createWorkerByParams($handlers, $container, $logger); - $worker->process($message, $queue); + $worker->process($message, $queueName); } public function testMessageFailWithDefinitionClassNotFoundInContainerHandler(): void @@ -122,10 +122,10 @@ public function testMessageFailWithDefinitionClassNotFoundInContainerHandler(): $container = new SimpleContainer(); $handlers = ['simple' => [FakeHandler::class, 'handle']]; - $queue = 'test-queue'; + $queueName = 'test-queue'; $worker = $this->createWorkerByParams($handlers, $container); - $worker->process($message, $queue); + $worker->process($message, $queueName); } public function testMessageFailWithDefinitionHandlerException(): void @@ -136,11 +136,11 @@ public function testMessageFailWithDefinitionHandlerException(): void $container = new SimpleContainer([FakeHandler::class => $handler]); $handlers = ['simple' => [FakeHandler::class, 'handleWithException']]; - $queue = 'test-queue'; + $queueName = 'test-queue'; $worker = $this->createWorkerByParams($handlers, $container, $logger); try { - $worker->process($message, $queue); + $worker->process($message, $queueName); } catch (MessageFailureException $exception) { self::assertSame($exception::class, MessageFailureException::class); self::assertSame($exception->getMessage(), "Processing of message without ID is stopped because of an exception:\nTest exception."); @@ -161,12 +161,12 @@ public function testHandlerNotFoundInContainer(): void $container = new SimpleContainer(); $handlers = []; - $queue = 'test-queue'; + $queueName = 'test-queue'; $worker = $this->createWorkerByParams($handlers, $container); $this->expectException(RuntimeException::class); $this->expectExceptionMessage('Queue handler for message type "nonexistent" does not exist'); - $worker->process($message, $queue); + $worker->process($message, $queueName); } public function testHandlerInContainerNotImplementingInterface(): void @@ -179,18 +179,18 @@ public function handle(): void {} ]); $handlers = []; - $queue = 'test-queue'; + $queueName = 'test-queue'; $worker = $this->createWorkerByParams($handlers, $container); $this->expectException(RuntimeException::class); $this->expectExceptionMessage('Queue handler for message type "invalid" does not exist'); - $worker->process($message, $queue); + $worker->process($message, $queueName); } public function testMessageFailureIsHandledSuccessfully(): void { $message = new GenericMessage('simple', null); - $queue = 'test-queue'; + $queueName = 'test-queue'; $originalException = new RuntimeException('Consume failed'); /** @var ConsumeMiddlewareInterface&MockObject $consumeMiddleware */ @@ -205,7 +205,7 @@ public function testMessageFailureIsHandledSuccessfully(): void $finalMessage = new GenericMessage('final', null); /** @var FailureMiddlewareInterface&MockObject $failureMiddleware */ $failureMiddleware = $this->createMock(FailureMiddlewareInterface::class); - $failureMiddleware->method('processFailure')->willReturn(new FailureHandlingRequest($finalMessage, $originalException, $queue)); + $failureMiddleware->method('processFailure')->willReturn(new FailureHandlingRequest($finalMessage, $originalException, $queueName)); /** @var FailureMiddlewareFactoryInterface&MockObject $failureMiddlewareFactory */ $failureMiddlewareFactory = $this->createMock(FailureMiddlewareFactoryInterface::class); @@ -223,7 +223,7 @@ public function testMessageFailureIsHandledSuccessfully(): void new CallableFactory($container), ); - $result = $worker->process($message, $queue); + $result = $worker->process($message, $queueName); self::assertSame($finalMessage, $result); } @@ -236,11 +236,11 @@ public function testStaticMethodHandler(): void 'static-handler' => StaticMessageHandler::handle(...), ]; - $queue = 'test-queue'; + $queueName = 'test-queue'; $worker = $this->createWorkerByParams($handlers, $container); StaticMessageHandler::$wasHandled = false; - $worker->process($message, $queue); + $worker->process($message, $queueName); $this->assertTrue(StaticMessageHandler::$wasHandled); }