vendor/nzo/url-encryptor-bundle/Annotations/AnnotationResolver.php line 29

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the NzoUrlEncryptorBundle package.
  4.  *
  5.  * (c) Ala Eddine Khefifi <alakhefifi@gmail.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Nzo\UrlEncryptorBundle\Annotations;
  11. use Doctrine\Common\Annotations\Reader;
  12. use Nzo\UrlEncryptorBundle\Encryptor\Encryptor;
  13. use Symfony\Component\HttpKernel\Event\ControllerEvent;
  14. class AnnotationResolver
  15. {
  16.     private $reader;
  17.     private $decryptor;
  18.     public function __construct(Reader $readerEncryptor $decryptor)
  19.     {
  20.         $this->reader $reader;
  21.         $this->decryptor $decryptor;
  22.     }
  23.     public function onKernelController(ControllerEvent $event)
  24.     {
  25.         if (is_array($controller $event->getController())) {
  26.             $objectController = new \ReflectionObject($controller[0]);
  27.             $method $objectController->getMethod($controller[1]);
  28.         } elseif (is_object($controller) && method_exists($controller'__invoke')) {
  29.             $objectController = new \ReflectionObject($controller);
  30.             $method $objectController->getMethod('__invoke');
  31.         } else {
  32.             return;
  33.         }
  34.         // handle php8 annotation
  35.         if (class_exists('ReflectionAttribute')) {
  36.             $annotations $this->getAnnotation($method);
  37.         } else {
  38.             $annotations $this->reader->getMethodAnnotations($method);
  39.         }
  40.         foreach ($annotations as $configuration) {
  41.             // handle php8 annotation
  42.             if (class_exists('ReflectionAttribute')) {
  43.                 $configuration $this->handleReflectionAttribute($configuration);
  44.             }
  45.             if ($configuration instanceof ParamEncryptor) {
  46.                 if (isset($configuration->params)) {
  47.                     $request $event->getRequest();
  48.                     foreach ($configuration->params as $param) {
  49.                         if ($request->attributes->has($param)) {
  50.                             $decrypted $this->decryptor->encrypt($request->attributes->get($param));
  51.                             $request->attributes->set($param$decrypted);
  52.                         } elseif ($request->request->has($param)) {
  53.                             $decrypted $this->decryptor->encrypt($request->request->get($param));
  54.                             $request->request->set($param$decrypted);
  55.                         }
  56.                     }
  57.                 }
  58.             } elseif ($configuration instanceof ParamDecryptor) {
  59.                 if (isset($configuration->params)) {
  60.                     $request $event->getRequest();
  61.                     foreach ($configuration->params as $param) {
  62.                         if ($request->attributes->has($param)) {
  63.                             $decrypted $this->decryptor->decrypt($request->attributes->get($param));
  64.                             $request->attributes->set($param$decrypted);
  65.                         } elseif ($request->request->has($param)) {
  66.                             $decrypted $this->decryptor->decrypt($request->request->get($param));
  67.                             $request->request->set($param$decrypted);
  68.                         }
  69.                     }
  70.                 }
  71.             }
  72.         }
  73.     }
  74.     /**
  75.      * @return mixed
  76.      */
  77.     private function handleReflectionAttribute($configuration)
  78.     {
  79.         if ($configuration instanceof \ReflectionAttribute
  80.             && \in_array($configuration->getName(), [ParamEncryptor::class, ParamDecryptor::class])) {
  81.             $class $configuration->getName();
  82.             $arguments $configuration->getArguments();
  83.             $customConfiguration = new $class();
  84.             $customConfiguration->params \is_array($arguments) && [] !== $arguments && \is_array($arguments[0])
  85.                 ? $arguments[0]
  86.                 : [];
  87.             return $customConfiguration;
  88.         }
  89.         return $configuration;
  90.     }
  91.     /**
  92.      * @return array|mixed
  93.      */
  94.     private function getAnnotation($method)
  95.     {
  96.         return !empty($this->reader->getMethodAnnotations($method))
  97.             ? $this->reader->getMethodAnnotations($method)
  98.             : $method->getAttributes();
  99.     }
  100. }