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] With AsCommand attribute setAliases() in configure() don't create the aliases #52650

Open
aleblanc opened this issue Nov 20, 2023 · 15 comments
Labels
Console DX DX = Developer eXperience (anything that improves the experience of using Symfony) Good first issue Ideal for your first contribution! (some Symfony experience may be required) Help wanted Issues and PRs which are looking for volunteers to complete them.

Comments

@aleblanc
Copy link

aleblanc commented Nov 20, 2023

Symfony version(s) affected

6.3

Description

In a Command symfony, with "AsCommand" attribute it's no longer possible to create dynamic alias in the configure() method (for exemple for create alias which comes from a database).

How to reproduce

  • php bin/console make:command

  • name it "app:demo"

  • Change configure like this (alias app:dynamic) :

    protected function configure(): void
    {
        $this
            ->addArgument('arg1', InputArgument::OPTIONAL, 'Argument description')
            ->addOption('option1', null, InputOption::VALUE_NONE, 'Option description')
            ->setAliases(['app:dynamic'])
        ;
    }
  • php bin/console app:dynamic
    Error : Command "app:dynamic" is not defined.

Possible Solution

No response

Additional Context

No response

@stof
Copy link
Member

stof commented Nov 20, 2023

The whole point of moving that to an attribute was to support lazy-loading commands to load only the command that is being run instead of all registered commands. This is indeed not compatible with configure the command dynamically.
What is your actual use case for that ?

@aleblanc
Copy link
Author

This is for example to allow you to make aliases for several cities in the database, for example:
AsCommand('app:{city}:script1')
AsCommand('app:{city}:script2')
AsCommand('app:{city}:script3')

For example to filter Commands by city, permissions, etc.

@stof
Copy link
Member

stof commented Nov 20, 2023

I still don't understand the use case: if you have 2 cities in the database and you add 2 aliases app:city1:script1 and app:city2:script1, running the command will still run the same thing. So I don't see how this allows filtering by city.

@aleblanc
Copy link
Author

Depending on the city the script does a different treatment, for example city1:script load a configuration in database for city1 and city2 another configuration.

@stof
Copy link
Member

stof commented Nov 20, 2023

@aleblanc how do you detect that ?

@aleblanc
Copy link
Author

With $input->getArgument('command')

@stof
Copy link
Member

stof commented Nov 20, 2023

this won't be reliable. As Symfony supports shortening each part of the command name as much as you want as long as it is not ambiguous (meaning that there is only one command that has at least 1 name/alias that corresponds), you have no guarantee that the command argument will contain the full city name.
I would strongly suggest you to use a city argument instead.

@javaDeveloperKid
Copy link
Contributor

javaDeveloperKid commented Nov 21, 2023

@stof says:

I would strongly suggest you to use a city argument instead.

I fully agree. This is the right way for your use case.

@maxbeckers
Copy link
Contributor

Yes, I also agree with @stof. Make city an argument and run app:script1 city1 ... but of course you are right and if was working for you because you did not use the command shortening.
But just one thing I'm thinking about.

The whole point of moving that to an attribute was to support lazy-loading commands to load only the command that is being run instead of all registered commands. This is indeed not compatible with configure the command dynamically.
What is your actual use case for that ?

Does this mean we should remove the setAliases, because it's not working anymore this way? Or is it still removed / deprecate to use it this way?

@stof
Copy link
Member

stof commented Nov 22, 2023

Not all usages of symfony/console are using a container with lazy-loaded commands. This method is still useful in standalone usages.

@javaDeveloperKid
Copy link
Contributor

javaDeveloperKid commented Nov 22, 2023

I can see AsCommand attribute has $aliases parameter so I think OP should just use it instead of setAliases()

@stof
Copy link
Member

stof commented Nov 22, 2023

@javaDeveloperKid the problem is dynamic aliases.

@javaDeveloperKid
Copy link
Contributor

@javaDeveloperKid the problem is dynamic aliases.

Right. I missed it.

@javaDeveloperKid
Copy link
Contributor

javaDeveloperKid commented Nov 22, 2023

In Application.php I can see there is a command loader which is a service locator. Maybe it could be decorated and the logic would be:

  • if the locator has a command then return this
  • if not then look ask the database for dynamic alias and then get the actual command from the service locator

@chalasr
Copy link
Member

chalasr commented Nov 26, 2023

I agree the use case is better addressed otherwise, as mentioned above.
The DX looks unfortunate though: ideally setting aliases at runtime on a lazy command should rise an error explaining the limitation/incompatibility. PR welcome in that direction.

@chalasr chalasr added Good first issue Ideal for your first contribution! (some Symfony experience may be required) DX DX = Developer eXperience (anything that improves the experience of using Symfony) Help wanted Issues and PRs which are looking for volunteers to complete them. and removed Bug Status: Needs Review labels Nov 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Console DX DX = Developer eXperience (anything that improves the experience of using Symfony) Good first issue Ideal for your first contribution! (some Symfony experience may be required) Help wanted Issues and PRs which are looking for volunteers to complete them.
Projects
None yet
Development

No branches or pull requests

6 participants