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/src/functions.php b/src/functions.php index 51325ca..d298d56 100644 --- a/src/functions.php +++ b/src/functions.php @@ -85,3 +85,18 @@ 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(); +} 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(),