src/RequestMapper/RequestMapper.php line 24

Open in your IDE?
  1. <?php
  2. namespace App\RequestMapper;
  3. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  4. use Symfony\Component\HttpKernel\Event\ControllerArgumentsEvent;
  5. use Symfony\Component\HttpKernel\KernelEvents;
  6. /**
  7.  * @author Azad KAYA <info@azadkaya.com>
  8.  */
  9. class RequestMapper implements EventSubscriberInterface
  10. {
  11.     public static function getSubscribedEvents()
  12.     {
  13.         return [
  14.             KernelEvents::CONTROLLER_ARGUMENTS=>[
  15.                 ['onControllerEvent'10]
  16.             ]
  17.         ];
  18.     }
  19.     public function onControllerEvent(ControllerArgumentsEvent $controllerArgumentsEvent)
  20.     {
  21.         $this->setParametersToObject($controllerArgumentsEvent);
  22.     }
  23.     /**
  24.      * @throws \ReflectionException
  25.      * @throws \Exception
  26.      */
  27.     public function setParametersToObject(ControllerArgumentsEvent $event)
  28.     {
  29.         $event->getRequest()->isXmlHttpRequest();
  30.         $getParameters =  $event->getRequest()->query->all();
  31.         $postParameters $event->getRequest()->request->all();;
  32.         $arguments $event->getArguments();
  33.         foreach ($arguments as $argument){
  34.             if(is_object($argument)){
  35.                 $argReflection = new \ReflectionClass($argument);
  36.                 if(count($argReflection->getAttributes()) > 0){
  37.                     foreach($argReflection->getAttributes() as $attr){
  38.                         if($attr->getName() == 'App\RequestMapper\Annotations\DtoAnnotation'){
  39.                             if($attr->getArguments()){
  40.                                 foreach ($attr->getArguments()['methods'] as $method){
  41.                                     if($method == 'POST')
  42.                                         $this->setParameters($postParameters$argReflection$argument);
  43.                                     else if($method == 'GET')
  44.                                         $this->setParameters($getParameters$argReflection$argument);
  45.                                     else
  46.                                         throw new \Exception(sprintf("Annotation methods parameter must be only POST, GET, PUT, DELETE. %s is not supported"$method));
  47.                                 }
  48.                             }else{
  49.                                 $this->setParameters($postParameters$argReflection$argument);
  50.                                 $this->setParameters($getParameters$argReflection$argument);
  51.                             }
  52.                         }
  53.                     }
  54.                 }
  55.             }
  56.         }
  57.     }
  58.     public function setParameters($parameters\ReflectionClass $argReflection$argument)
  59.     {
  60.         foreach ($parameters as $key => $value){
  61.             if($argReflection->hasProperty($key)){
  62.                 $argReflection->getProperty($key)->setAccessible(true);
  63.                 $argReflection->getProperty($key)->setValue($argument,$value);
  64.             }
  65.         }
  66.     }
  67. }