Skip to content

Commit

Permalink
fix: getSubscribedServices() can return SubscribedService[]
Browse files Browse the repository at this point in the history
  • Loading branch information
kbond committed Dec 22, 2022
1 parent 746a378 commit 5958c69
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions src/InvokableServiceCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Symfony\Component\Console\Style\StyleInterface;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Contracts\Service\Attribute\Required;
use Symfony\Contracts\Service\Attribute\SubscribedService;
use Symfony\Contracts\Service\ServiceSubscriberInterface;

/**
Expand Down Expand Up @@ -76,8 +77,7 @@ static function(\ReflectionParameter $parameter): ?string {
public function execute(InputInterface $input, OutputInterface $output): int
{
foreach (self::getSubscribedServices() as $serviceId) {
$optional = 0 === \mb_strpos($serviceId, '?');
$serviceId = \ltrim($serviceId, '?');
[$serviceId, $optional] = self::parseServiceId($serviceId);

try {
$value = $this->container()->get($serviceId);
Expand Down Expand Up @@ -122,4 +122,21 @@ private function container(): ContainerInterface

return $this->container;
}

/**
* @param string|SubscribedService $service
*
* @return array{0:string,1:bool}
*/
private static function parseServiceId($service): array
{
if ($service instanceof SubscribedService) {
return [(string) $service->type, $service->nullable];
}

return [
\ltrim($service, '?'),
\str_starts_with($service, '?'),
];
}
}

0 comments on commit 5958c69

Please sign in to comment.