-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2dc7a30
commit b8e8a42
Showing
5 changed files
with
69 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Patchlevel\EventSourcing\CommandBus\Handler; | ||
|
||
use Patchlevel\EventSourcing\Attribute\Inject; | ||
use Psr\Container\ContainerInterface; | ||
use ReflectionMethod; | ||
use ReflectionParameter; | ||
use Symfony\Component\TypeInfo\Type\ObjectType; | ||
use Symfony\Component\TypeInfo\TypeResolver\TypeResolver; | ||
|
||
final class DefaultParameterResolver implements ParameterResolver | ||
{ | ||
public function __construct( | ||
private readonly ContainerInterface|null $container = null, | ||
) { | ||
} | ||
|
||
/** @return iterable<mixed> */ | ||
public function resolve(ReflectionMethod $method, object $command): iterable | ||
{ | ||
foreach ($method->getParameters() as $index => $parameter) { | ||
if ($index === 0) { | ||
yield $command; // first parameter is always the command | ||
|
||
continue; | ||
} | ||
|
||
if (!$this->container) { | ||
throw ServiceNotResolvable::missingContainer(); | ||
} | ||
|
||
yield $this->container->get(self::serviceName($method, $parameter)); | ||
} | ||
} | ||
|
||
private function serviceName(ReflectionMethod $method, ReflectionParameter $parameter): string | ||
{ | ||
$attributes = $parameter->getAttributes(Inject::class); | ||
|
||
if ($attributes !== []) { | ||
return $attributes[0]->newInstance()->service; | ||
} | ||
|
||
$reflectionType = $parameter->getType(); | ||
|
||
if ($reflectionType === null) { | ||
throw ServiceNotResolvable::missingType($method->getDeclaringClass()->getName(), $parameter->getName()); | ||
} | ||
|
||
$type = TypeResolver::create()->resolve($reflectionType); | ||
|
||
if (!$type instanceof ObjectType) { | ||
throw ServiceNotResolvable::typeNotObject($method->getDeclaringClass()->getName(), $parameter->getName()); | ||
} | ||
|
||
return $type->getClassName(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters