Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/ServiceFactory/ConsoleRegistrationMethod.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace WonderNetwork\SlimKernel\ServiceFactory;

enum ConsoleRegistrationMethod: string {
case Deprecated = 'add';
case Latest = 'addCommand';
}
18 changes: 10 additions & 8 deletions src/ServiceFactory/SymfonyConsoleServiceFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,12 @@
use function DI\get;
use function WonderNetwork\SlimKernel\Collection\collection;

final class SymfonyConsoleServiceFactory implements ServiceFactory {
private string $path;
private string $name;

public function __construct(string $path = '/src/Cli/**/*Command.php', string $name = 'unknown') {
$this->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 {
Expand All @@ -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()),
Expand Down
15 changes: 15 additions & 0 deletions src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<T> $input
* @param callable(T,?(int|string)):F $values
* @param callable(T,?(int|string)):string $keys
* @return array<string,F>
*/
function indexmap($input, callable $values, callable $keys): array {
return collection($input)
->indexBy($keys)
->map($values)
->toArray();
}
8 changes: 5 additions & 3 deletions tests/ServiceFactory/SymfonyConsoleServiceFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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(),
Expand Down