Skip to content

Commit

Permalink
fix: autowiring non-service arguments (#60)
Browse files Browse the repository at this point in the history
  • Loading branch information
kbond authored Feb 14, 2024
1 parent 625ff6c commit 70e4b6a
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 3 deletions.
10 changes: 9 additions & 1 deletion src/InvokableServiceCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ static function(\ReflectionParameter $parameter) use ($supportsAttributes) {
return null;
}

if (!$type instanceof \ReflectionNamedType || $type->isBuiltin()) {
if (!$type instanceof \ReflectionNamedType) {
return null;
}

Expand All @@ -67,12 +67,20 @@ static function(\ReflectionParameter $parameter) use ($supportsAttributes) {
return null;
}

if (!$supportsAttributes && $type->isBuiltin()) {
return null;
}

if (!$supportsAttributes) {
return $type->allowsNull() ? '?'.$name : $name;
}

$attributes = \array_map(static fn(\ReflectionAttribute $a) => $a->newInstance(), $parameter->getAttributes());

if (!$attributes && $type->isBuiltin()) {
return null; // an attribute (ie Autowire) is required for built-in types
}

return new SubscribedService('invoke:'.$parameter->name, $name, $type->allowsNull(), $attributes); // @phpstan-ignore-line
},
self::invokeParameters(),
Expand Down
8 changes: 6 additions & 2 deletions tests/Fixture/Command/WithAttributesServiceCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,14 @@ public function __invoke(
AnInterface $imp,

#[Autowire('%kernel.environment%')]
string $env,
string $environment,

#[Autowire('%kernel.debug%')]
bool $debug,
): void {
$io->comment('Imp1: '.$imp1->get());
$io->comment('Imp2: '.$imp->get());
$io->comment('Env: '.$env);
$io->comment('Env: '.$environment);
$io->comment('Debug: '.\var_export($debug, true));
}
}
1 change: 1 addition & 0 deletions tests/Integration/WithAttributesServiceCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public function services_injected(): void
->assertOutputContains('Imp1: implementation1')
->assertOutputContains('Imp2: implementation2')
->assertOutputContains('Env: test')
->assertOutputContains('Debug: true')
;
}
}

0 comments on commit 70e4b6a

Please sign in to comment.