Skip to content

Commit

Permalink
fix: don't autowire Option|Attribute (#61)
Browse files Browse the repository at this point in the history
  • Loading branch information
kbond committed Feb 14, 2024
1 parent b88e355 commit 4053813
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
6 changes: 6 additions & 0 deletions src/InvokableServiceCommand.php
Expand Up @@ -22,6 +22,8 @@
use Symfony\Contracts\Service\Attribute\Required;
use Symfony\Contracts\Service\Attribute\SubscribedService;
use Symfony\Contracts\Service\ServiceSubscriberInterface;
use Zenstruck\Console\Attribute\Argument;
use Zenstruck\Console\Attribute\Option;

/**
* All the benefits of {@see Invokable} but also allows for auto-injection of
Expand Down Expand Up @@ -75,6 +77,10 @@ static function(\ReflectionParameter $parameter) use ($supportsAttributes) {
return $type->allowsNull() ? '?'.$name : $name;
}

if ($parameter->getAttributes(Option::class) || $parameter->getAttributes(Argument::class)) {
return null; // don't auto-inject options/arguments
}

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

if (!$attributes && $type->isBuiltin()) {
Expand Down
12 changes: 12 additions & 0 deletions tests/Fixture/Command/WithAttributesServiceCommand.php
Expand Up @@ -14,6 +14,8 @@
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
use Symfony\Component\DependencyInjection\Attribute\Target;
use Zenstruck\Console\Attribute\Argument;
use Zenstruck\Console\ConfigureWithAttributes;
use Zenstruck\Console\InvokableServiceCommand;
use Zenstruck\Console\IO;
use Zenstruck\Console\Tests\Fixture\Service\AnInterface;
Expand All @@ -24,6 +26,8 @@
#[AsCommand('with-attributes-service-command')]
final class WithAttributesServiceCommand extends InvokableServiceCommand
{
use ConfigureWithAttributes;

public function __invoke(
IO $io,

Expand All @@ -38,10 +42,18 @@ public function __invoke(

#[Autowire('%kernel.debug%')]
bool $debug,

#[Argument]
string $arg1,

#[Argument]
string $arg2,
): void {
$io->comment('Imp1: '.$imp1->get());
$io->comment('Imp2: '.$imp->get());
$io->comment('Env: '.$environment);
$io->comment('Debug: '.\var_export($debug, true));
$io->comment('Arg1: '.$arg1);
$io->comment('Arg2: '.$arg2);
}
}
4 changes: 3 additions & 1 deletion tests/Integration/WithAttributesServiceCommandTest.php
Expand Up @@ -34,12 +34,14 @@ protected function setUp(): void
*/
public function services_injected(): void
{
$this->executeConsoleCommand('with-attributes-service-command')
$this->executeConsoleCommand('with-attributes-service-command foo bar')
->assertSuccessful()
->assertOutputContains('Imp1: implementation1')
->assertOutputContains('Imp2: implementation2')
->assertOutputContains('Env: test')
->assertOutputContains('Debug: true')
->assertOutputContains('Arg1: foo')
->assertOutputContains('Arg2: bar')
;
}
}

0 comments on commit 4053813

Please sign in to comment.