Get a annotaion reader by Symfony. This way the annotations get cached, and it is much faster in production.

This commit is contained in:
Jan Böhmer 2019-03-21 13:09:42 +01:00
parent b06a4479a6
commit df40f712a6

View file

@ -31,7 +31,7 @@ namespace App\Security\EntityListeners;
use App\Security\Annotations\ColumnSecurity; use App\Security\Annotations\ColumnSecurity;
use App\Entity\DBElement; use App\Entity\DBElement;
use Doctrine\Common\Annotations\AnnotationReader; use Doctrine\Common\Annotations\Reader;
use Doctrine\Common\Persistence\Event\LifecycleEventArgs; use Doctrine\Common\Persistence\Event\LifecycleEventArgs;
use Doctrine\ORM\Event\PreUpdateEventArgs; use Doctrine\ORM\Event\PreUpdateEventArgs;
use Doctrine\ORM\Mapping\PostLoad; use Doctrine\ORM\Mapping\PostLoad;
@ -49,10 +49,12 @@ use Symfony\Component\Security\Core\Security;
class ElementPermissionListener class ElementPermissionListener
{ {
protected $security; protected $security;
protected $reader;
public function __construct(Security $security) public function __construct(Security $security, Reader $reader)
{ {
$this->security = $security; $this->security = $security;
$this->reader = $reader;
} }
/** /**
@ -67,13 +69,12 @@ class ElementPermissionListener
//Read Annotations and properties. //Read Annotations and properties.
$reflectionClass = new ReflectionClass($element); $reflectionClass = new ReflectionClass($element);
$properties = $reflectionClass->getProperties(); $properties = $reflectionClass->getProperties();
$reader = new AnnotationReader();
foreach ($properties as $property) { foreach ($properties as $property) {
/** /**
* @var ColumnSecurity * @var ColumnSecurity
*/ */
$annotation = $reader->getPropertyAnnotation($property, $annotation = $this->reader->getPropertyAnnotation($property,
ColumnSecurity::class); ColumnSecurity::class);
//Check if user is allowed to read info, otherwise apply placeholder //Check if user is allowed to read info, otherwise apply placeholder
@ -93,13 +94,12 @@ class ElementPermissionListener
{ {
$reflectionClass = new ReflectionClass($element); $reflectionClass = new ReflectionClass($element);
$properties = $reflectionClass->getProperties(); $properties = $reflectionClass->getProperties();
$reader = new AnnotationReader();
foreach ($properties as $property) { foreach ($properties as $property) {
/** /**
* @var ColumnSecurity * @var ColumnSecurity
*/ */
$annotation = $reader->getPropertyAnnotation($property, $annotation = $this->reader->getPropertyAnnotation($property,
ColumnSecurity::class); ColumnSecurity::class);
if (null !== $annotation) { if (null !== $annotation) {