Skip to content

Handle 'hidden' param from AsCommand attribute #41547

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

Conversation

yoannrenard
Copy link
Contributor

@yoannrenard yoannrenard commented Jun 4, 2021

Q A
Branch? 5.4
Bug fix? yes
New feature? no
Deprecations? no
Tickets
License MIT
Doc PR

The recently introduced AsCommand attribute works like a charm (as much as the whole 5.3 release 👌). Thanks a lot for that!

Though, it seems that the hidden property has been forgotten, whether one set it or not the command only checks the $this->hidden value set from the configure() method.

For example:

#[AsCommand(
    name: 'app:do:something',
    description: 'Will do something very cool.',
    aliases: [],
    hidden: true
)]
class DoSomethingCommand extends Command

When I run bash bin/console list app I get

Available commands for the "app" namespace:
  app:do:something  Do something very cool.

I implemented a fix inspired by the $name/$description properties.

@yoannrenard yoannrenard changed the title Handle 'hidden' param from AsCommand attribute [Console] Handle 'hidden' param from AsCommand attribute Jun 4, 2021
Copy link
Contributor

@OskarStark OskarStark left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me except my comment

@carsonbot carsonbot changed the title [Console] Handle 'hidden' param from AsCommand attribute Handle 'hidden' param from AsCommand attribute Jun 5, 2021
@nicolas-grekas
Copy link
Member

This patch doesn't fix the root issue. If you inspect the dumped container for example, you'll see that the command is correctly registered as hidden.
Yet, for some reason I didn't spot yet, the list command doesn't see this flag as set.
This needs to be investigated.

$isHidden = $attribute[0]->newInstance()->hidden;
}

return $this->hidden || $isHidden;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For reviewers (and for the author): this patch cannot be correct, because it makes the attribute a mandatory piece of configuration that cannot be ignored. This is against the root principles of attributes, which should be purely declarative.

@nicolas-grekas nicolas-grekas added this to the 5.3 milestone Jun 5, 2021
@nicolas-grekas
Copy link
Member

nicolas-grekas commented Jun 5, 2021

Note that past some issue with registering commands, there is another one: when the Command class is registered without the DI component (without the AddConsoleCommandPass), then the aliases and hidden flags are ignored, and the name is not parsed (explode('|')). I don't know how common it is to use Command without DI. A fix would involve parsing the name and using it when hidden+aliases are undefined.

@yoannrenard
Copy link
Contributor Author

As said @nicolas-grekas without DI, the command name is not parsed when the hidden param is set in the AsCommand attribute.
I've created a quick bug reproducer here to show you.

@yoannrenard
Copy link
Contributor Author

After more tests this morning, I've discovered that AsCommand attribute works fine with CI. I just didn't know that this attribute needed to clear the cache to work. My bad.

I'll close this PR as it is wrong and push my fix for the usage without CI in another one.

@stof
Copy link
Member

stof commented Jun 7, 2021

@yoannrenard the need to clear cache when changing attributes is caused by #39988

@yoannrenard
Copy link
Contributor Author

Thanks for the information @stof

nicolas-grekas added a commit that referenced this pull request Jun 12, 2021
This PR was merged into the 5.3 branch.

Discussion
----------

[Console] Fix using #[AsCommand] without DI

| Q             | A
| ------------- | ---
| Branch?       | 5.3
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | Fix #41547
| License       | MIT
| Doc PR        | -

Commits
-------

c4357ea [Console] Fix using #[AsCommand] without DI
@yoannrenard yoannrenard deleted the handle_hidden_param_from_AsCommand_attribute branch June 13, 2021 05:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants