Reflection.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Service\Utils;
  4. use Symfony\Component\DependencyInjection\ContainerInterface;
  5. use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
  6. /**
  7. * Class Reflection : Gestion des réflection de class
  8. * @package App\Service\Utils
  9. */
  10. class Reflection
  11. {
  12. private ContainerInterface $container;
  13. public function __construct(ContainerInterface $container)
  14. {
  15. $this->container = $container;
  16. }
  17. /**
  18. * Appel une fonction avec ses paramètres depuis le nom d'un service
  19. * @param string $serviceName
  20. * @param string $method
  21. * @param array $parameters
  22. * @return mixed
  23. * @throws \ReflectionException
  24. */
  25. public function dynamicInvokeServiceWithArgsAndMethod(string $serviceName, string $method, array $parameters = []) {
  26. $class = $this->container->get($serviceName);
  27. return $this->dynamicInvokeClassWithArgsAndMethod(get_class($class), $method, $parameters = []);
  28. }
  29. /**
  30. * Appel une fonction avec ses paramètres depuis le nom d'une classe
  31. * @param string $serviceName
  32. * @param string $method
  33. * @param array $parameters
  34. * @return mixed
  35. * @throws \ReflectionException
  36. */
  37. public function dynamicInvokeClassWithArgsAndMethod(string $className, string $method, array $parameters = []) {
  38. $reflection = new \ReflectionClass($className);
  39. $method = $reflection->getMethod($method);
  40. if($method->isStatic()){
  41. return $method->invoke(null, $parameters);
  42. }else{
  43. return $method->invoke($reflection, $parameters);
  44. }
  45. }
  46. }