| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152 |
- <?php
- declare(strict_types=1);
- namespace App\Service\Utils;
- use Symfony\Component\DependencyInjection\ContainerInterface;
- /**
- * Class Reflection : Gestion des réflection de class
- * @package App\Service\Utils
- */
- class Reflection
- {
- public function __construct(private ContainerInterface $container)
- { }
- /**
- * Appel une fonction avec ses paramètres depuis le nom d'un service
- * @param string $serviceName
- * @param string $method
- * @param array $parameters
- * @return mixed
- */
- public function dynamicInvokeServiceWithArgsAndMethod(string $serviceName, string $method, array $parameters = []): mixed
- {
- $class = $this->container->get($serviceName);
- //verif méthode exist
- if(method_exists($class, $method))
- return $class->{$method}(...$parameters);
- throw new \LogicException("method_not_exist", 400);
- }
- /**
- * Appel une fonction avec ses paramètres depuis le nom d'une classe
- * @param string $serviceName
- * @param string $method
- * @param array $parameters
- * @return mixed
- * @throws \ReflectionException
- */
- public function dynamicInvokeClassWithArgsAndMethod(string $className, string $method, array $parameters = []): mixed
- {
- $reflection = new \ReflectionClass($className);
- $method = $reflection->getMethod($method);
- if($method->isStatic()){
- return $method->invoke(null, $parameters);
- }else{
- return $method->invoke($reflection, $parameters);
- }
- }
- }
|