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.             return;
  27.         }
  28.         $objectController = new \ReflectionObject($controller[0]);
  29.         $method $objectController->getMethod($controller[1]);
  30.         // handle php8 annotation
  31.         if (class_exists('ReflectionAttribute')) {
  32.             $annotations $this->getAnnotation($method);
  33.         } else {
  34.             $annotations $this->reader->getMethodAnnotations($method);
  35.         }
  36.         foreach ($annotations as $configuration) {
  37.             // handle php8 annotation
  38.             if (class_exists('ReflectionAttribute')) {
  39.                 $configuration $this->handleReflectionAttribute($configuration);
  40.             }
  41.             if ($configuration instanceof ParamEncryptor) {
  42.                 if (isset($configuration->params)) {
  43.                     $request $event->getRequest();
  44.                     foreach ($configuration->params as $param) {
  45.                         if ($request->attributes->has($param)) {
  46.                             $decrypted $this->decryptor->encrypt($request->attributes->get($param));
  47.                             $request->attributes->set($param$decrypted);
  48.                         } elseif ($request->request->has($param)) {
  49.                             $decrypted $this->decryptor->encrypt($request->request->get($param));
  50.                             $request->request->set($param$decrypted);
  51.                         }
  52.                     }
  53.                 }
  54.             } elseif ($configuration instanceof ParamDecryptor) {
  55.                 if (isset($configuration->params)) {
  56.                     $request $event->getRequest();
  57.                     foreach ($configuration->params as $param) {
  58.                         if ($request->attributes->has($param)) {
  59.                             $decrypted $this->decryptor->decrypt($request->attributes->get($param));
  60.                             $request->attributes->set($param$decrypted);
  61.                         } elseif ($request->request->has($param)) {
  62.                             $decrypted $this->decryptor->decrypt($request->request->get($param));
  63.                             $request->request->set($param$decrypted);
  64.                         }
  65.                     }
  66.                 }
  67.             }
  68.         }
  69.     }
  70.     /**
  71.      * @return mixed
  72.      */
  73.     private function handleReflectionAttribute($configuration)
  74.     {
  75.         if ($configuration instanceof \ReflectionAttribute
  76.             && \in_array($configuration->getName(), [ParamEncryptor::class, ParamDecryptor::class])) {
  77.             $class $configuration->getName();
  78.             $arguments $configuration->getArguments();
  79.             $customConfiguration = new $class();
  80.             $customConfiguration->params = \is_array($arguments) && [] !== $arguments && \is_array($arguments[0])
  81.                 ? $arguments[0]
  82.                 : [];
  83.             return $customConfiguration;
  84.         }
  85.         return $configuration;
  86.     }
  87.     /**
  88.      * @return array|mixed
  89.      */
  90.     private function getAnnotation($method)
  91.     {
  92.         return !empty($this->reader->getMethodAnnotations($method))
  93.             ? $this->reader->getMethodAnnotations($method)
  94.             : $method->getAttributes();
  95.     }
  96. }