Skip to content

Commit d3560c4

Browse files
authored
Add Symfony Console 8 support (#233)
1 parent 369c394 commit d3560c4

File tree

4 files changed

+35
-9
lines changed

4 files changed

+35
-9
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
## 2.4.2 under development
44

5-
- no changes in this release.
5+
- Enh #233: Add Symfony Console 8 support (@vjik)
66

77
## 2.4.1 December 02, 2025
88

composer.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,12 @@
3131
"psr/container": "^1.0 || ^2.0",
3232
"psr/event-dispatcher": "^1.0",
3333
"psr/log": "^1.0 || ^2.0 || ^3.0",
34-
"symfony/console": "^5.4 || ^6.0 || ^7.0",
34+
"symfony/console": "^5.4 || ^6.0 || ^7.0 || ^8.0",
3535
"symfony/event-dispatcher-contracts": "^2.2 || ^3.0",
3636
"yiisoft/friendly-exception": "^1.0"
3737
},
3838
"require-dev": {
3939
"bamarni/composer-bin-plugin": "^1.8.3",
40-
"maglnet/composer-require-checker": "^4.4",
4140
"phpunit/phpunit": "^9.6.22",
4241
"rector/rector": "^2.0.11",
4342
"yiisoft/config": "^1.5",

src/CommandLoader.php

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
namespace Yiisoft\Yii\Console;
66

77
use Psr\Container\ContainerInterface;
8+
use ReflectionClass;
89
use RuntimeException;
10+
use Symfony\Component\Console\Attribute\AsCommand;
911
use Symfony\Component\Console\Command\Command;
1012
use Symfony\Component\Console\Command\LazyCommand;
1113
use Symfony\Component\Console\CommandLoader\CommandLoaderInterface;
@@ -31,6 +33,8 @@ final class CommandLoader implements CommandLoaderInterface
3133
*/
3234
private array $commandNames;
3335

36+
private ?bool $symfonyConsoleVersionLess8 = null;
37+
3438
/**
3539
* @param array $commandMap An array with command names as keys and service ids as values.
3640
*
@@ -52,12 +56,7 @@ public function get(string $name): Command
5256
$commandClass = $this->commandMap[$name]['class'];
5357
$commandHidden = $this->commandMap[$name]['hidden'];
5458

55-
/**
56-
* @see https://github.com/yiisoft/yii-console/issues/229
57-
* @psalm-suppress DeprecatedMethod
58-
*/
59-
$description = $commandClass::getDefaultDescription();
60-
59+
$description = $this->getCommandDescription($commandClass);
6160
if ($description === null) {
6261
return $this->getCommandInstance($name);
6362
}
@@ -154,4 +153,27 @@ private function validateAliases(array $aliases): void
154153
}
155154
}
156155
}
156+
157+
/**
158+
* @psalm-param class-string<Command> $commandClass
159+
*/
160+
private function getCommandDescription(string $commandClass): ?string
161+
{
162+
$this->symfonyConsoleVersionLess8 ??= method_exists(Command::class, 'getDefaultDescription');
163+
164+
if ($this->symfonyConsoleVersionLess8) {
165+
/**
166+
* @see https://github.com/yiisoft/yii-console/issues/229
167+
* @psalm-suppress DeprecatedMethod, UndefinedMethod
168+
* @var string|null
169+
*/
170+
return $commandClass::getDefaultDescription();
171+
}
172+
173+
if ($attribute = (new ReflectionClass($commandClass))->getAttributes(AsCommand::class)) {
174+
return $attribute[0]->newInstance()->description;
175+
}
176+
177+
return null;
178+
}
157179
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"require-dev": {
3+
"maglnet/composer-require-checker": "^4.4"
4+
}
5+
}

0 commit comments

Comments
 (0)