Skip to content

Commit 37eb136

Browse files
authored
Merge pull request #14 from WonderNetwork/feature/symfony-console-add-vs-addcommand
Feature: symfony console add vs addcommand
2 parents f45a35f + b6d5e13 commit 37eb136

4 files changed

Lines changed: 40 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()),

src/functions.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,18 @@ function map($input, callable ...$fn): array {
8585
function filter($input, callable $fn): array {
8686
return collection($input)->filter($fn)->toArray();
8787
}
88+
89+
/**
90+
* @template T of mixed
91+
* @template F of mixed
92+
* @param iterable<T> $input
93+
* @param callable(T,?(int|string)):F $values
94+
* @param callable(T,?(int|string)):string $keys
95+
* @return array<string,F>
96+
*/
97+
function indexmap($input, callable $values, callable $keys): array {
98+
return collection($input)
99+
->indexBy($keys)
100+
->map($values)
101+
->toArray();
102+
}

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)