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

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the NzoUrlEncryptorBundle package.
  4.  *
  5.  * (c) Ala Eddine Khefifi <alakfpro@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 $decryptor;
  17.     private $reader;
  18.     /**
  19.      * @param Reader|null $reader
  20.      */
  21.     public function __construct(Encryptor $decryptor$reader)
  22.     {
  23.         $this->decryptor $decryptor;
  24.         $this->reader $reader;
  25.     }
  26.     public function onKernelController(ControllerEvent $event)
  27.     {
  28.         if (is_array($controller $event->getController())) {
  29.             $objectController = new \ReflectionObject($controller[0]);
  30.             $method $objectController->getMethod($controller[1]);
  31.         } elseif (is_object($controller) && method_exists($controller'__invoke')) {
  32.             $objectController = new \ReflectionObject($controller);
  33.             $method $objectController->getMethod('__invoke');
  34.         } else {
  35.             return;
  36.         }
  37.         // handle php8 attribute
  38.         if (class_exists('ReflectionAttribute')) {
  39.             if ($this->hasAnnotation($method) && !$this->reader instanceof Reader) {
  40.                 throw new \InvalidArgumentException('NzoEncryptor:  Annotation service not loaded, PHP Attributes should be used instead.');
  41.             }
  42.             $annotations $this->getAnnotation($method);
  43.         } else {
  44.             $annotations $this->reader->getMethodAnnotations($method);
  45.         }
  46.         foreach ($annotations as $configuration) {
  47.             // handle php8 attribute
  48.             if (class_exists('ReflectionAttribute')) {
  49.                 $configuration $this->handleReflectionAttribute($configuration);
  50.             }
  51.             if ($configuration instanceof ParamEncryptor) {
  52.                 if (null !== $configuration->getParams()) {
  53.                     $request $event->getRequest();
  54.                     foreach ($configuration->getParams() as $param) {
  55.                         if ($request->attributes->has($param)) {
  56.                             $decrypted $this->decryptor->encrypt($request->attributes->get($param));
  57.                             $request->attributes->set($param$decrypted);
  58.                         } elseif ($request->request->has($param)) {
  59.                             $decrypted $this->decryptor->encrypt($request->request->get($param));
  60.                             $request->request->set($param$decrypted);
  61.                         }
  62.                     }
  63.                 }
  64.             } elseif ($configuration instanceof ParamDecryptor) {
  65.                 if (null !== $configuration->getParams()) {
  66.                     $request $event->getRequest();
  67.                     foreach ($configuration->getParams() as $param) {
  68.                         if ($request->attributes->has($param)) {
  69.                             $decrypted $this->decryptor->decrypt($request->attributes->get($param));
  70.                             $request->attributes->set($param$decrypted);
  71.                         } elseif ($request->request->has($param)) {
  72.                             $decrypted $this->decryptor->decrypt($request->request->get($param));
  73.                             $request->request->set($param$decrypted);
  74.                         }
  75.                     }
  76.                 }
  77.             }
  78.         }
  79.     }
  80.     /**
  81.      * @return mixed
  82.      */
  83.     private function handleReflectionAttribute($configuration)
  84.     {
  85.         if ($configuration instanceof \ReflectionAttribute
  86.             && \in_array($configuration->getName(), [ParamEncryptor::class, ParamDecryptor::class], true)) {
  87.             $class $configuration->getName();
  88.             $arguments $configuration->getArguments();
  89.             $params \is_array($arguments) && [] !== $arguments && \is_array($arguments[0]) ? $arguments[0] : [];
  90.             return new $class($params);
  91.         }
  92.         return $configuration;
  93.     }
  94.     /**
  95.      * @return array|mixed
  96.      */
  97.     private function getAnnotation($method)
  98.     {
  99.         return $this->reader instanceof Reader && !empty($this->reader->getMethodAnnotations($method))
  100.             ? $this->reader->getMethodAnnotations($method)
  101.             : $method->getAttributes();
  102.     }
  103.     /**
  104.      * @param \ReflectionMethod $method
  105.      *
  106.      * @return bool
  107.      */
  108.     private function hasAnnotation($method)
  109.     {
  110.         $docComment $method->getDocComment();
  111.         return false !== $docComment && (false !== strpos($docComment'@ParamEncryptor') || false !== strpos($docComment'@ParamDecryptor'));
  112.     }
  113. }