Skip to content

Commit dbc6977

Browse files
committed
register commands using addCommand instead of add
1 parent 5a56a02 commit dbc6977

3 files changed

Lines changed: 25 additions & 11 deletions

File tree

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace WonderNetwork\SlimKernel\ServiceFactory;
6+
7+
enum ConsoleRegistrationMethod: string {
8+
case Deprecated = 'add';
9+
case Latest = 'addCommand';
10+
}

src/ServiceFactory/SymfonyConsoleServiceFactory.php

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,12 @@
1313
use function DI\get;
1414
use function WonderNetwork\SlimKernel\Collection\collection;
1515

16-
final class SymfonyConsoleServiceFactory implements ServiceFactory {
17-
private string $path;
18-
private string $name;
19-
20-
public function __construct(string $path = '/src/Cli/**/*Command.php', string $name = 'unknown') {
21-
$this->path = $path;
22-
$this->name = $name;
16+
final readonly class SymfonyConsoleServiceFactory implements ServiceFactory {
17+
public function __construct(
18+
private string $path = '/src/Cli/**/*Command.php',
19+
private string $name = 'unknown',
20+
private ConsoleRegistrationMethod $registrationMethod = ConsoleRegistrationMethod::Deprecated,
21+
) {
2322
}
2423

2524
public function __invoke(ServicesBuilder $builder): iterable {
@@ -29,7 +28,10 @@ public function __invoke(ServicesBuilder $builder): iterable {
2928
yield Console\Application::class => collection($commands)
3029
->keys()
3130
->reduce(
32-
static fn (CreateDefinitionHelper $def, string $command) => $def->method('add', get($command)),
31+
fn (CreateDefinitionHelper $def, string $command) => $def->method(
32+
$this->registrationMethod->value,
33+
get($command),
34+
),
3335
autowire()
3436
->constructor($this->name)
3537
->method('setAutoExit', static fn (AutoExit $autoExit) => $autoExit->value()),

tests/ServiceFactory/SymfonyConsoleServiceFactoryTest.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717
final class SymfonyConsoleServiceFactoryTest extends TestCase {
1818
public function test(): void {
1919
$servicesBuilder = new ServicesBuilder(__DIR__.'/../Resources/Commands');
20-
$sut = new SymfonyConsoleServiceFactory();
20+
$sut = new SymfonyConsoleServiceFactory(
21+
registrationMethod: ConsoleRegistrationMethod::Latest,
22+
);
2123
$actual = collection($sut($servicesBuilder))->realize();
2224

2325
$commands = $actual
@@ -43,11 +45,11 @@ public function test(): void {
4345
self::assertEquals(
4446
map(
4547
$commands,
46-
static fn (string $command) => new MethodInjection('add', [new Reference($command)]),
48+
static fn (string $command) => new MethodInjection('addCommand', [new Reference($command)]),
4749
),
4850
collection($definition->getMethodInjections())
4951
->filter(
50-
static fn (MethodInjection $m) => $m->getMethodName() === 'add',
52+
static fn (MethodInjection $m) => $m->getMethodName() === 'addCommand',
5153
)
5254
->values()
5355
->toArray(),

0 commit comments

Comments
 (0)