From d49b851921da73598d5be559b6a1b74ceafe2238 Mon Sep 17 00:00:00 2001 From: Tobias Gross-Vogt <tgros@tugraz.at> Date: Wed, 20 Apr 2022 14:13:06 +0200 Subject: [PATCH] localdata files moved to LocalData dir/namespace, added check for valid shortName attribute --- src/{Event => LocalData}/LocalDataAwareEvent.php | 4 +--- .../LocalDataAwareEventDispatcher.php | 15 ++++++--------- .../LocalDataAwareInterface.php | 2 +- src/{Entity => LocalData}/LocalDataAwareTrait.php | 2 +- 4 files changed, 9 insertions(+), 14 deletions(-) rename src/{Event => LocalData}/LocalDataAwareEvent.php (93%) rename src/{Service => LocalData}/LocalDataAwareEventDispatcher.php (90%) rename src/{Entity => LocalData}/LocalDataAwareInterface.php (92%) rename src/{Entity => LocalData}/LocalDataAwareTrait.php (96%) diff --git a/src/Event/LocalDataAwareEvent.php b/src/LocalData/LocalDataAwareEvent.php similarity index 93% rename from src/Event/LocalDataAwareEvent.php rename to src/LocalData/LocalDataAwareEvent.php index 1b5b474..3b13e38 100644 --- a/src/Event/LocalDataAwareEvent.php +++ b/src/LocalData/LocalDataAwareEvent.php @@ -2,11 +2,9 @@ declare(strict_types=1); -namespace Dbp\Relay\CoreBundle\Event; +namespace Dbp\Relay\CoreBundle\LocalData; -use Dbp\Relay\CoreBundle\Entity\LocalDataAwareInterface; use Dbp\Relay\CoreBundle\Exception\ApiError; -use Dbp\Relay\CoreBundle\Service\LocalDataAwareEventDispatcher; use Symfony\Contracts\EventDispatcher\Event; class LocalDataAwareEvent extends Event diff --git a/src/Service/LocalDataAwareEventDispatcher.php b/src/LocalData/LocalDataAwareEventDispatcher.php similarity index 90% rename from src/Service/LocalDataAwareEventDispatcher.php rename to src/LocalData/LocalDataAwareEventDispatcher.php index c4077d5..9882d20 100644 --- a/src/Service/LocalDataAwareEventDispatcher.php +++ b/src/LocalData/LocalDataAwareEventDispatcher.php @@ -2,11 +2,10 @@ declare(strict_types=1); -namespace Dbp\Relay\CoreBundle\Service; +namespace Dbp\Relay\CoreBundle\LocalData; use ApiPlatform\Core\Exception\ResourceClassNotFoundException; use ApiPlatform\Core\Metadata\Resource\Factory\AnnotationResourceMetadataFactory; -use Dbp\Relay\CoreBundle\Event\LocalDataAwareEvent; use Dbp\Relay\CoreBundle\Exception\ApiError; use Doctrine\Common\Annotations\AnnotationReader; use Symfony\Component\EventDispatcher\EventDispatcherInterface; @@ -23,14 +22,10 @@ class LocalDataAwareEventDispatcher /** @var EventDispatcherInterface */ private $eventDispatcher; - /** @var string */ - private $eventName; - - public function __construct(string $resourceClass, EventDispatcherInterface $eventDispatcher, string $eventName) + public function __construct(string $resourceClass, EventDispatcherInterface $eventDispatcher) { $this->uniqueEntityName = self::getUniqueEntityName($resourceClass); $this->eventDispatcher = $eventDispatcher; - $this->eventName = $eventName; } /** @@ -65,11 +60,11 @@ class LocalDataAwareEventDispatcher /** * Dispatches the given event. */ - public function dispatch(LocalDataAwareEvent $event): void + public function dispatch(LocalDataAwareEvent $event, string $eventName): void { $event->setRequestedAttributes($this->requestedAttributes); - $this->eventDispatcher->dispatch($event, $this->eventName); + $this->eventDispatcher->dispatch($event, $eventName); $remainingLocalDataAttributes = $event->getRemainingRequestedAttributes(); if (!empty($remainingLocalDataAttributes)) { @@ -95,6 +90,8 @@ class LocalDataAwareEventDispatcher $uniqueName = $resourceMetadata->getShortName() ?? ''; if (empty($uniqueName)) { throw new ApiError(500, sprintf("'shortName' attribute missing in ApiResource annotation of resource class '%s'", $resourceClass)); + } elseif (str_contains($uniqueName, '.') || str_contains($uniqueName, ',')) { + throw new ApiError(500, sprintf("'shortName' attribute of resource class '%s' must not contain '.' or ',' characters: '%s'", $resourceClass, $uniqueName)); } return $uniqueName; diff --git a/src/Entity/LocalDataAwareInterface.php b/src/LocalData/LocalDataAwareInterface.php similarity index 92% rename from src/Entity/LocalDataAwareInterface.php rename to src/LocalData/LocalDataAwareInterface.php index 5fc64a2..891b7e4 100644 --- a/src/Entity/LocalDataAwareInterface.php +++ b/src/LocalData/LocalDataAwareInterface.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace Dbp\Relay\CoreBundle\Entity; +namespace Dbp\Relay\CoreBundle\LocalData; interface LocalDataAwareInterface { diff --git a/src/Entity/LocalDataAwareTrait.php b/src/LocalData/LocalDataAwareTrait.php similarity index 96% rename from src/Entity/LocalDataAwareTrait.php rename to src/LocalData/LocalDataAwareTrait.php index edf94aa..d6dfe82 100644 --- a/src/Entity/LocalDataAwareTrait.php +++ b/src/LocalData/LocalDataAwareTrait.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace Dbp\Relay\CoreBundle\Entity; +namespace Dbp\Relay\CoreBundle\LocalData; use ApiPlatform\Core\Annotation\ApiProperty; use Symfony\Component\Serializer\Annotation\Groups; -- GitLab