Skip to content

Commit f9428ee

Browse files
authored
[feature] add InvokableServiceCommand::parameter() (#21)
1 parent 8f03882 commit f9428ee

File tree

5 files changed

+17
-4
lines changed

5 files changed

+17
-4
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,9 @@ class CreateUserCommand extends InvokableServiceCommand
123123
{
124124
public function __invoke(IO $io, UserRepository $repo, LoggerInterface $logger): void
125125
{
126+
// access container parameters
127+
$environment = $this->parameter('kernel.environment');
128+
126129
// ...
127130
}
128131
}

phpstan.neon

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,3 @@ parameters:
55
level: 8
66
paths:
77
- src
8-
- tests
9-
bootstrapFiles:
10-
- vendor/bin/.phpunit/phpunit/vendor/autoload.php

src/InvokableServiceCommand.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Symfony\Component\Console\Input\InputInterface;
99
use Symfony\Component\Console\Output\OutputInterface;
1010
use Symfony\Component\Console\Style\StyleInterface;
11+
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
1112
use Symfony\Contracts\Service\ServiceSubscriberInterface;
1213

1314
/**
@@ -26,7 +27,7 @@ abstract class InvokableServiceCommand extends Command implements ServiceSubscri
2627

2728
public static function getSubscribedServices(): array
2829
{
29-
return \array_values(
30+
$services = \array_values(
3031
\array_filter(
3132
\array_map(
3233
static function(\ReflectionParameter $parameter): ?string {
@@ -62,6 +63,8 @@ static function(\ReflectionParameter $parameter): ?string {
6263
)
6364
)
6465
);
66+
67+
return [...$services, ParameterBagInterface::class];
6568
}
6669

6770
public function execute(InputInterface $input, OutputInterface $output): int
@@ -96,6 +99,14 @@ public function setInvokeContainer(ContainerInterface $container): void
9699
$this->container = $container;
97100
}
98101

102+
/**
103+
* @return mixed
104+
*/
105+
final protected function parameter(string $name)
106+
{
107+
return $this->container()->get(ParameterBagInterface::class)->get($name);
108+
}
109+
99110
private function container(): ContainerInterface
100111
{
101112
if (!isset($this->container)) {

tests/Fixture/Command/ServiceCommand.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ public function __invoke(IO $io, InputInterface $input, OutputInterface $output,
2828
$io->comment(\sprintf('LoggerInterface: %s', get_debug_type($logger)));
2929
$io->comment(\sprintf('RouterInterface: %s', get_debug_type($router)));
3030
$io->comment(\sprintf('Table: %s', get_debug_type($optional)));
31+
$io->comment(\sprintf('Parameter environment: %s', $this->parameter('kernel.environment')));
3132

3233
$io->success('done!');
3334
}

tests/Integration/InvokableServiceCommandTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public function can_auto_inject_services_into_invoke(): void
3434
->assertOutputContains(\sprintf('LoggerInterface: %s', Logger::class))
3535
->assertOutputContains(\sprintf('RouterInterface: %s', Router::class))
3636
->assertOutputContains('Table: null')
37+
->assertOutputContains('Parameter environment: test')
3738
;
3839
}
3940

0 commit comments

Comments
 (0)