Skip to content

Commit 531ce4e

Browse files
Roman3349f3l1x
authored andcommitted
Composer: allow Symfony 8.x
Signed-off-by: Roman Ondráček <[email protected]>
1 parent f6ca8e0 commit 531ce4e

File tree

2 files changed

+28
-12
lines changed

2 files changed

+28
-12
lines changed

composer.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,15 @@
1818
"require": {
1919
"php": ">=8.2",
2020
"nette/di": "^3.1.8",
21-
"symfony/console": "^6.4.2 || ^7.0.2"
21+
"symfony/console": "^6.4.2 || ^7.0.2 || ^8.0.0"
2222
},
2323
"require-dev": {
2424
"nette/http": "^3.2.3",
2525
"contributte/qa": "^0.4",
2626
"contributte/tester": "^0.4",
27-
"contributte/phpstan": "^0.1",
28-
"mockery/mockery": "^1.6.7",
29-
"symfony/event-dispatcher": "^6.4.2 || ^7.0.2"
27+
"contributte/phpstan": "^0.2",
28+
"mockery/mockery": "^1.6.12",
29+
"symfony/event-dispatcher": "^6.4.2 || ^7.0.2 || ^8.0.0"
3030
},
3131
"autoload": {
3232
"psr-4": {

src/DI/ConsoleExtension.php

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@
1414
use Nette\Schema\Expect;
1515
use Nette\Schema\Schema;
1616
use Nette\Utils\Arrays;
17+
use ReflectionClass;
18+
use ReflectionProperty;
1719
use stdClass;
20+
use Symfony\Component\Console\Attribute\AsCommand;
1821
use Symfony\Component\Console\Command\Command;
1922
use Symfony\Component\Console\CommandLoader\CommandLoaderInterface;
2023
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
@@ -25,11 +28,10 @@
2528
class ConsoleExtension extends CompilerExtension
2629
{
2730

28-
private bool $cliMode;
29-
30-
public function __construct(bool $cliMode = false)
31+
public function __construct(
32+
private readonly bool $cliMode = false,
33+
)
3134
{
32-
$this->cliMode = $cliMode;
3335
}
3436

3537
public function getConfigSchema(): Schema
@@ -158,17 +160,31 @@ public function beforeCompile(): void
158160
}
159161

160162
$aliases = [];
161-
// Try to detect command name from Command::getDefaultName()
162-
if ($commandName === null) {
163-
$commandName = call_user_func([$service->getType(), 'getDefaultName']); // @phpstan-ignore-line
164-
if ($commandName === null) {
163+
// Try to detect command name from Command::getDefaultName() or Command::defaultName property
164+
if (!is_string($commandName) || $commandName === '') {
165+
/** @var class-string $className */
166+
$className = $service->getType();
167+
$reflection = new ReflectionClass($className);
168+
$attributes = $reflection->getAttributes(AsCommand::class);
169+
170+
if ($attributes !== []) {
171+
$commandName = $attributes[0]->newInstance()->name;
172+
} elseif (method_exists($className, 'getDefaultName')) {
173+
$commandName = call_user_func([$service->getType(), 'getDefaultName']); // @phpstan-ignore-line
174+
} elseif (property_exists($className, 'defaultName')) {
175+
$rp = new ReflectionProperty($className, 'defaultName');
176+
$commandName = $rp->getValue();
177+
}
178+
179+
if (!is_string($commandName) || $commandName === '') {
165180
throw new ServiceCreationException(
166181
sprintf(
167182
'Command "%s" missing #[AsCommand] attribute',
168183
$service->getType(),
169184
)
170185
);
171186
}
187+
172188
$aliases = explode('|', $commandName);
173189
$commandName = array_shift($aliases);
174190
if ($commandName === '') {

0 commit comments

Comments
 (0)