Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[Console] Add getter for the original command "code" object#61078

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
weitzman wants to merge12 commits intosymfony:7.4
base:7.4
Choose a base branch
Loading
fromweitzman:feature/get-invokable-command-attributes

Conversation

weitzman
Copy link
Contributor

@weitzmanweitzman commentedJul 9, 2025
edited
Loading

QA
Branch?7.4
Bug fix?no
New feature?yes
Deprecations?no
LicenseMIT

I'm playing with Invokable Commands and am seeing a limitation which is bothersome. Attributes attached to the invokable class are lost by the time we have aCommand added to the Application. Consider an invokable class like below. The #[CLI\FieldLabels] and #[CLI\DefaultFields] Attributes are not retrievable from the Command. That is, there is no easy way to access the actual InvokableCommand object instead of thewrapping Command.

With this PR, Attributes become accessible via$command->getCode()->getCallable()

namespaceDrush\Commands\core;#[AsCommand(    name:'twig:unused',    description:'Find potentially unused Twig templates.',    aliases: ['twu'],)]#[CLI\FieldLabels(labels: ['template' =>'Template','compiled' =>'Compiled'])]#[CLI\DefaultFields(fields: ['template','compiled'])]finalclass TwigUnusedCommand{publicfunction__invoke(        #[Argument(description:'A comma delimited list of paths to recursively search')]string$searchpaths,InputInterface$input,OutputInterface$output    ):int    {$data =$this->doExecute($searchpaths);$this->writeFormattedOutput($input,$output,$data);return Command::SUCCESS;    }}

@carsonbot
Copy link

Hey!

Thanks for your PR. You are targeting branch "7.4" but it seems your PR description refers to branch "7.4 for features".
Could you update the PR description or change target branch? This helps core maintainers a lot.

Cheers!

Carsonbot

@derrabus
Copy link
Member

Attributes attached to the invokable class are lost by the time we have aCommand added to the Application.

Can you elaborate on this? Why are they lost?

Also: Pleasealways provide tests that cover your change.

@weitzman
Copy link
ContributorAuthor

@derrabus - I added a hyperlink to the OP explaining whats meant by "wrapping command". Let me know if its still unclear.

If anyone can suggest an existing test to modify, I will do so.

@GromNaN
Copy link
Member

GromNaN commentedJul 9, 2025
edited
Loading

I don't understand whyyou need to access attributes of the wrapped invokable commandfrom the actualCommand class. Attributes should not be read at runtime (for performance). You can create an autoconfiguration mechanism to read the attributes during container compilation and inject them into a service.

dkarlovi reacted with thumbs up emoji

@chalasr
Copy link
Member

Jérôme has a good point about manipulating the command at compile time instead of runtime. When accessing the service you get the real instance .

@derrabus
Copy link
Member

Let me know if its still unclear.

It is. Your link is pointing to some line in our codebase, but that line does not explain your use-case. It neither tells me what you're trying to achieve, nor why you want to do that, nor how you tried to do that.

If anyone can suggest an existing test to modify, I will do so.

This is a new feature, which means we need new tests. You can have a look at the existing test suite for inspiration, but we won't write those tests for you. And we won't merge a new feature without any tests.

@OskarStarkOskarStark changed the titleMake Attributes accessible on Invokable CommandsMake attributes accessible on invokable commandsJul 9, 2025
@weitzman
Copy link
ContributorAuthor

Apologies for not including my use case originally. I'm no Symfony expert, so your expertise here is much appreciated.

I'm the maintainer ofDrush, the CLI for Drupal. Drush is a happy user of thestandalone Console component.

I am exploring using Invokable Commands in our next version. Symfony made this possible in May in#60394. Drush has no command services at this time, so the suggestion to read Attributes at compile time won't work. We need a runtime solution, and so will other standalone Console users. We could read the Attributes as we add commands to the Application, but then we would have to store them separate from the Command object. Or we would have to extend Command and add a method to get the callable with its Attributes. That feels awkward enough that we wouldn't support Invokable Commands which is a shame IMO - they are great.

GromNaN and kbond reacted with thumbs up emoji

@GromNaN
Copy link
Member

Thanks for the context.

I see that Drush is built withconsolidation/annotated-command. Looking atits documentation for theFieldLabels andDefaultFields attributes, it looks very close to what we built with invokable commands. I do think that the two attribute systems (Symfony and Consolidation) are mutually exclusive to configure the command.

@weitzman
Copy link
ContributorAuthor

weitzman commentedJul 9, 2025
edited
Loading

We are working to remove the dependency on consolidation/annotated-command in favor native Symfony Console conventions. Seedrush-ops/drush#6135. This Console PR is part of the effort.

Symfony Console supports a few Attributes like AsCommand and Argument and Option - we need a lot more in Drush. We can add declare Attribute classes and add them to our Invokable Commands but we can't cleanly read the Attributes - thus this PR.

FYI, I removed the return type hint to an Internal class.

@weitzman
Copy link
ContributorAuthor

weitzman commentedJul 10, 2025
edited
Loading

I have added tests to the PR. I'm getting a failure because I removed the return type hint to an internal class. How can we best resolve this?

So, do we think this is solvable for us and other standalone Console users? I hope so.

Copy link
Member

@GromNaNGromNaN left a comment

Choose a reason for hiding this comment

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

Here is a solution so keep theInvokableCommand internal.

Do you want to read the Consolidation attributes for backward compatibility? I still don't get how you will proceed.

weitzman reacted with thumbs up emoji
weitzmanand others added3 commitsJuly 10, 2025 06:19
Co-authored-by: Jérôme Tamarelle <jerome@tamarelle.net>
Co-authored-by: Jérôme Tamarelle <jerome@tamarelle.net>
@carsonbotcarsonbot changed the titleMake attributes accessible on invokable commands[Console] Make attributes accessible on invokable commandsJul 10, 2025
Co-authored-by: Jérôme Tamarelle <jerome@tamarelle.net>
@weitzman
Copy link
ContributorAuthor

I think I've addressed all feedback. The one test failure is unrelated to this PR.

@GromNaNGromNaN changed the title[Console] Make attributes accessible on invokable commands[Console] add getter for the original command "code" objectJul 14, 2025
@GromNaNGromNaN changed the title[Console] add getter for the original command "code" object[Console] Add getter for the original command "code" objectJul 14, 2025
Copy link
Member

@GromNaNGromNaN left a comment

Choose a reason for hiding this comment

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

Please add an entry in the changelog of the console component.

@weitzman
Copy link
ContributorAuthor

Changelog entry added.

Co-authored-by: Jérôme Tamarelle <jerome@tamarelle.net>
@weitzman
Copy link
ContributorAuthor

Test failure looks unrelated.

@antonkomarev
Copy link

Getter namegetCode looks like status code. Those were my initial thoughts when I read the PR title.

@GromNaN
Copy link
Member

GromNaN commentedJul 17, 2025
edited
Loading

Getter namegetCode looks like status code. Those were my initial thoughts when I read the PR title.

This name matches the existingsetCode. If it was returning the status code, we would name itgetStatusCode.

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@stofstofstof left review comments

@GromNaNGromNaNGromNaN approved these changes

@chalasrchalasrAwaiting requested review from chalasrchalasr is a code owner

Assignees
No one assigned
Projects
None yet
Milestone
7.4
Development

Successfully merging this pull request may close these issues.

8 participants
@weitzman@carsonbot@derrabus@GromNaN@chalasr@antonkomarev@stof@OskarStark

[8]ページ先頭

©2009-2025 Movatter.jp