| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152 |
- <?php
- declare(strict_types=1);
- namespace App\Service\Utils;
- use Symfony\Component\DependencyInjection\ContainerInterface;
- use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
- /**
- * Class Reflection : Gestion des réflection de class
- * @package App\Service\Utils
- */
- class Reflection
- {
- private ContainerInterface $container;
- public function __construct(ContainerInterface $container)
- {
- $this->container = $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
- * @throws \ReflectionException
- */
- public function dynamicInvokeServiceWithArgsAndMethod(string $serviceName, string $method, array $parameters = []) {
- $class = $this->container->get($serviceName);
- return $this->dynamicInvokeClassWithArgsAndMethod(get_class($class), $method, $parameters = []);
- }
- /**
- * 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 = []) {
- $reflection = new \ReflectionClass($className);
- $method = $reflection->getMethod($method);
- if($method->isStatic()){
- return $method->invoke(null, $parameters);
- }else{
- return $method->invoke($reflection, $parameters);
- }
- }
- }
|