Skip to content
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

[Console] Cannot change input arguments on ConsoleCommandEvent #52415

Closed
fschmtt opened this issue Nov 2, 2023 · 5 comments
Closed

[Console] Cannot change input arguments on ConsoleCommandEvent #52415

fschmtt opened this issue Nov 2, 2023 · 5 comments

Comments

@fschmtt
Copy link
Contributor

fschmtt commented Nov 2, 2023

Symfony version(s) affected

  • 6.3
  • 5.4

Description

When subscribing to the console.command and modifying the input arguments, the command receives the original, unmodified arguments.

How to reproduce

Create a subscriber changing the input arguments, e.g.:

class ConsumeMessagesSubscriber implements EventSubscriberInterface
{
    public const LOW_PRIORITY_QUEUE = 'low_priority';

    public static function getSubscribedEvents(): array
    {
        return [
            ConsoleEvents::COMMAND => 'onMessengerConsume',
        ];
    }

    public function onMessengerConsume(ConsoleCommandEvent $event): void
    {
        $command = $event->getCommand();

        if ($command === null) {
            return;
        }

        if ($command->getName() !== 'messenger:consume') {
            return;
        }

        $receivers = $event->getInput()->getArgument('receivers');

        if (!\in_array(self::LOW_PRIORITY_QUEUE, $receivers, true)) {
            $receivers[] = self::LOW_PRIORITY_QUEUE;
            $event->getInput()->setArgument('receivers', $receivers);
        }
    }
}

Run bin/console messenger:consume async. The subscriber should add low_priority as an additional receiver. The command receives the unmodified input argument, though:

❯ php bin/console messenger:consume async

 [OK] Consuming messages from transport "async".

Possible Solution

Possibly the input arguments get bound too early. They are bound in both

Additional Context

No response

@OskarStark
Copy link
Contributor

Can you confirm this bug is also present in 5.4?

@fschmtt
Copy link
Contributor Author

fschmtt commented Nov 2, 2023

Yes, just tested this, can confirm this bug exists in 5.4, too.

@chalasr
Copy link
Member

chalasr commented Nov 2, 2023

This is a known limitation. PR welcome if one wants to give it a try, but I'm going to close here since we already went back & forth without success on this one and concluded it was a won't fix.

@shyim
Copy link
Contributor

shyim commented Nov 2, 2023

Does it make sense then to mention this in the event class?

@chalasr
Copy link
Member

chalasr commented Nov 4, 2023

Looks sensible to me yes

fschmtt added a commit to fschmtt/symfony that referenced this issue Nov 17, 2023
fabpot added a commit that referenced this issue Nov 18, 2023
This PR was submitted for the 7.1 branch but it was merged into the 5.4 branch instead.

Discussion
----------

Hint that changing input arguments has no effect

| Q             | A
| ------------- | ---
| Branch?       | 7.1
| Bug fix?      | no
| New feature?  | no
| Deprecations? | no
| Issues        | #52415
| License       | MIT

Adding a hint to `\Symfony\Component\Console\Event\ConsoleCommandEvent` that changing the input arguments has no effect.

Commits
-------

9ec9ead Add hint that changing input arguments has no effect
@fabpot fabpot closed this as completed Nov 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants