vendor/league/tactician-bundle/src/DependencyInjection/HandlerMapping/TypeHintMapping.php line 61

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace League\Tactician\Bundle\DependencyInjection\HandlerMapping;
  4. use Symfony\Component\DependencyInjection\ContainerBuilder;
  5. use Symfony\Component\DependencyInjection\Definition;
  6. use ReflectionClass;
  7. use function method_exists;
  8. /**
  9.  * Routes commands based on typehints in the handler.
  10.  *
  11.  * If your handler has a public method with a single, non-scalar, no-interface type hinted
  12.  * parameter, we'll assume that typehint is a command and route it to this
  13.  * service definition as the handler.
  14.  *
  15.  * So, a class like this:
  16.  *
  17.  * class MyHandler
  18.  * {
  19.  *     public function handle(RegisterUser $command) {...}
  20.  *     private function foobar(SomeObject $obj) {...}
  21.  *     public function checkThings(OtherObject $obj, WhatObject $obj2)
  22.  *     public function setADependency(ManagerInterface $interface) {...}
  23.  * }
  24.  *
  25.  * would have RegisterUser routed to it, but not SomeObject (because it's
  26.  * used in a private method), not OtherObject or WhatObject (because they
  27.  * don't appear as the only parameter) and not setADependency (because it
  28.  * has an interface type hinted parameter).
  29.  */
  30. final class TypeHintMapping extends TagBasedMapping
  31. {
  32.     protected function isSupported(ContainerBuilder $containerDefinition $definition, array $tagAttributes): bool
  33.     {
  34.         return isset($tagAttributes['typehints']) && $tagAttributes['typehints'] === true;
  35.     }
  36.     protected function findCommandsForService(ContainerBuilder $containerDefinition $definition, array $tagAttributes): array
  37.     {
  38.         $results = [];
  39.         $reflClass = new ReflectionClass($container->getParameterBag()->resolveValue($definition->getClass()));
  40.         foreach ($reflClass->getMethods() as $method) {
  41.             if (!$method->isPublic()
  42.                 || $method->isConstructor()
  43.                 || $method->isStatic()
  44.                 || $method->isAbstract()
  45.                 || $method->isVariadic()
  46.                 || $method->getNumberOfParameters() !== 1
  47.             ) {
  48.                 continue;
  49.             }
  50.             $parameter $method->getParameters()[0];
  51.             if (!$parameter->hasType()
  52.                 || $parameter->getType()->isBuiltin()
  53.                 || $parameter->getClass()->isInterface()
  54.             ) {
  55.                 continue;
  56.             }
  57.             $type $parameter->getType();
  58.             if (version_compare(PHP_VERSION'7.1.0') >= 0) {
  59.                 $results[] = $type->getName();
  60.             } else {
  61.                 $results[] = (string)$type;
  62.             }
  63.         }
  64.         return $results;
  65.     }
  66. }