Commit 01f1ab1
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" object1 file changed
+1
-1
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
4 | 4 | | |
5 | 5 | | |
6 | 6 | | |
7 | | - | |
| 7 | + | |
8 | 8 | | |
9 | 9 | | |
10 | 10 | | |
| |||
0 commit comments