Skip to content

Commit 01f1ab1

Browse files
committed
feature symfony#61078 [Console] Add getter for the original command "code" object (weitzman)
This PR was merged into the 7.4 branch. Discussion ---------- [Console] Add getter for the original command "code" object | Q | A | ------------- | --- | Branch? | 7.4 | Bug fix? | no | New feature? | yes | Deprecations? | no | License | MIT 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 a `Command` 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 the [wrapping Command](https://github.com/symfony/symfony/blob/a384c231a0051c0ac10e89c2a0516343f9fd3187/src/Symfony/Component/Console/Application.php#L553). With this PR, Attributes become accessible via `$command->getCode()->getCallable()` ```php namespace Drush\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'])] final class TwigUnusedCommand { public function __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; } } ``` Commits ------- 825643d [Console] Add getter for the original command "code" object
2 parents f10093d + 825643d commit 01f1ab1

File tree

1 file changed

+1
-1
lines changed

1 file changed

+1
-1
lines changed

src/Symfony/Component/Console/CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ CHANGELOG
44
7.4
55
---
66

7-
* Add `Command::getCode()` to get the code set via `setCode()`.
7+
* Add `Command::getCode()` to get the code set via `setCode()`
88
* Allow setting aliases and the hidden flag via the command name passed to the constructor
99
* Introduce `Symfony\Component\Console\Application::addCommand()` to simplify using invokable commands when the component is used standalone
1010
* Deprecate `Symfony\Component\Console\Application::add()` in favor of `Symfony\Component\Console\Application::addCommand()`

0 commit comments

Comments
 (0)