From 5a56a0216c992ef0335b09289907049f2c3d83a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20=C5=81ebkowski?= Date: Tue, 9 Dec 2025 15:15:53 +0100 Subject: [PATCH 1/3] add `indexmap` function --- src/functions.php | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/functions.php b/src/functions.php index 51325ca..fe63593 100644 --- a/src/functions.php +++ b/src/functions.php @@ -85,3 +85,19 @@ function map($input, callable ...$fn): array { function filter($input, callable $fn): array { return collection($input)->filter($fn)->toArray(); } + +/** + * @template T of mixed + * @template F of mixed + * @param iterable $input + * @param callable(T,?(int|string)):F $values + * @param callable(T,?(int|string)):string $keys + * @return array + */ +function indexmap($input, callable $values, callable $keys): array { + return collection($input) + ->indexBy($keys) + ->map($values) + ->toArray(); +} + From dbc6977abdb59b529eebb6253aaf0047546c6b4b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20=C5=81ebkowski?= Date: Tue, 9 Dec 2025 15:23:32 +0100 Subject: [PATCH 2/3] register commands using `addCommand` instead of `add` --- .../ConsoleRegistrationMethod.php | 10 ++++++++++ .../SymfonyConsoleServiceFactory.php | 18 ++++++++++-------- .../SymfonyConsoleServiceFactoryTest.php | 8 +++++--- 3 files changed, 25 insertions(+), 11 deletions(-) create mode 100644 src/ServiceFactory/ConsoleRegistrationMethod.php diff --git a/src/ServiceFactory/ConsoleRegistrationMethod.php b/src/ServiceFactory/ConsoleRegistrationMethod.php new file mode 100644 index 0000000..093bc5e --- /dev/null +++ b/src/ServiceFactory/ConsoleRegistrationMethod.php @@ -0,0 +1,10 @@ +path = $path; - $this->name = $name; +final readonly class SymfonyConsoleServiceFactory implements ServiceFactory { + public function __construct( + private string $path = '/src/Cli/**/*Command.php', + private string $name = 'unknown', + private ConsoleRegistrationMethod $registrationMethod = ConsoleRegistrationMethod::Deprecated, + ) { } public function __invoke(ServicesBuilder $builder): iterable { @@ -29,7 +28,10 @@ public function __invoke(ServicesBuilder $builder): iterable { yield Console\Application::class => collection($commands) ->keys() ->reduce( - static fn (CreateDefinitionHelper $def, string $command) => $def->method('add', get($command)), + fn (CreateDefinitionHelper $def, string $command) => $def->method( + $this->registrationMethod->value, + get($command), + ), autowire() ->constructor($this->name) ->method('setAutoExit', static fn (AutoExit $autoExit) => $autoExit->value()), diff --git a/tests/ServiceFactory/SymfonyConsoleServiceFactoryTest.php b/tests/ServiceFactory/SymfonyConsoleServiceFactoryTest.php index b459632..0e04606 100644 --- a/tests/ServiceFactory/SymfonyConsoleServiceFactoryTest.php +++ b/tests/ServiceFactory/SymfonyConsoleServiceFactoryTest.php @@ -17,7 +17,9 @@ final class SymfonyConsoleServiceFactoryTest extends TestCase { public function test(): void { $servicesBuilder = new ServicesBuilder(__DIR__.'/../Resources/Commands'); - $sut = new SymfonyConsoleServiceFactory(); + $sut = new SymfonyConsoleServiceFactory( + registrationMethod: ConsoleRegistrationMethod::Latest, + ); $actual = collection($sut($servicesBuilder))->realize(); $commands = $actual @@ -43,11 +45,11 @@ public function test(): void { self::assertEquals( map( $commands, - static fn (string $command) => new MethodInjection('add', [new Reference($command)]), + static fn (string $command) => new MethodInjection('addCommand', [new Reference($command)]), ), collection($definition->getMethodInjections()) ->filter( - static fn (MethodInjection $m) => $m->getMethodName() === 'add', + static fn (MethodInjection $m) => $m->getMethodName() === 'addCommand', ) ->values() ->toArray(), From b6d5e13ebd445644aedeb5b602dd197320210c7f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20=C5=81ebkowski?= Date: Tue, 9 Dec 2025 15:24:58 +0100 Subject: [PATCH 3/3] csfix --- src/functions.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/functions.php b/src/functions.php index fe63593..d298d56 100644 --- a/src/functions.php +++ b/src/functions.php @@ -100,4 +100,3 @@ function indexmap($input, callable $values, callable $keys): array { ->map($values) ->toArray(); } -