Skip to content

Commit

Permalink
allow to skip command in handler
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidBadura committed Jan 17, 2025
1 parent 3ec2c53 commit 3db9f20
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 9 deletions.
38 changes: 34 additions & 4 deletions src/CommandBus/SymfonyParameterResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,14 @@
namespace Patchlevel\EventSourcingBundle\CommandBus;

use Patchlevel\EventSourcing\CommandBus\Handler\ParameterResolver;
use Patchlevel\EventSourcing\CommandBus\Handler\ServiceNotResolvable;
use Psr\Container\ContainerInterface;
use ReflectionMethod;
use Symfony\Component\TypeInfo\Type\ObjectType;
use Symfony\Component\TypeInfo\TypeResolver\TypeResolver;

use function is_a;
use function sprintf;
use function strtolower;

final class SymfonyParameterResolver implements ParameterResolver
Expand All @@ -22,14 +27,39 @@ public function resolve(ReflectionMethod $method, object $command): iterable
{
$prefix = strtolower($method->getName()) . '.';

foreach ($method->getParameters() as $index => $parameter) {
if ($index === 0) {
yield $command; // first parameter is always the command
foreach ($method->getParameters() as $parameter) {
$serviceId = $prefix . $parameter->getName();

$this->container->has($serviceId);

if ($this->container->has($serviceId)) {
yield $this->container->get($serviceId);

continue;
}

$reflectionType = $parameter->getType();

if ($reflectionType === null) {
ServiceNotResolvable::missingType($method->getDeclaringClass()->getName(), $parameter->getName());
}

$type = TypeResolver::create()->resolve($reflectionType);

if ($type instanceof ObjectType && is_a($command, $type->getClassName(), true)) {
yield $command;

continue;
}

yield $this->container->get($prefix . $parameter->getName());
throw new ServiceNotResolvable(
sprintf(
'Missing service for parameter "%s" in "%s::%s"',
$parameter->getName(),
$method->getDeclaringClass()->getName(),
$method->getName(),
),
);
}
}
}
10 changes: 5 additions & 5 deletions src/DependencyInjection/HandlerServiceLocatorCompilerPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,7 @@ private function services(ReflectionMethod $method, ContainerInterface $containe
$services = [];
$prefix = strtolower($method->getName()) . '.';

foreach ($method->getParameters() as $index => $parameter) {
if ($index === 0) {
continue; // skip first parameter (command)
}

foreach ($method->getParameters() as $parameter) {
$key = $prefix . $parameter->getName();

$attributes = $parameter->getAttributes(Inject::class);
Expand Down Expand Up @@ -102,6 +98,10 @@ private function services(ReflectionMethod $method, ContainerInterface $containe
continue;
}

if (!$container->has($type->getClassName())) {
continue;
}

$services[$key] = new Reference($type->getClassName());
}

Expand Down

0 comments on commit 3db9f20

Please sign in to comment.