Skip to content

Commit

Permalink
Restructure metadata and add collection endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
mattamon committed Nov 28, 2024
1 parent 5464233 commit fe02194
Show file tree
Hide file tree
Showing 17 changed files with 350 additions and 43 deletions.
15 changes: 1 addition & 14 deletions config/assets.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@ services:
Pimcore\Bundle\StudioBackendBundle\Asset\Hydrator\CustomSettingsHydratorInterface:
class: Pimcore\Bundle\StudioBackendBundle\Asset\Hydrator\CustomSettingsHydrator

Pimcore\Bundle\StudioBackendBundle\Asset\Hydrator\CustomMetadataHydratorInterface:
class: Pimcore\Bundle\StudioBackendBundle\Asset\Hydrator\CustomMetadataHydrator

# Encoder
Pimcore\Bundle\StudioBackendBundle\Asset\Encoder\TextEncoderInterface:
class: Pimcore\Bundle\StudioBackendBundle\Asset\Encoder\TextEncoder
Expand All @@ -29,8 +26,7 @@ services:
Pimcore\Bundle\StudioBackendBundle\Asset\Service\Data\CustomSettingsServiceInterface:
class: Pimcore\Bundle\StudioBackendBundle\Asset\Service\Data\CustomSettingsService

Pimcore\Bundle\StudioBackendBundle\Asset\Service\Data\CustomMetadataServiceInterface:
class: Pimcore\Bundle\StudioBackendBundle\Asset\Service\Data\CustomMetadataService


Pimcore\Bundle\StudioBackendBundle\Asset\Service\Data\TextServiceInterface:
class: Pimcore\Bundle\StudioBackendBundle\Asset\Service\Data\TextService
Expand Down Expand Up @@ -73,21 +69,12 @@ services:
Pimcore\Bundle\StudioBackendBundle\Asset\Updater\Adapter\ImageAdapter:
tags: [ 'pimcore.studio_backend.update_adapter' ]

Pimcore\Bundle\StudioBackendBundle\Asset\Updater\Adapter\CustomMetadataAdapter:
tags: [ 'pimcore.studio_backend.update_adapter' ]

Pimcore\Bundle\StudioBackendBundle\Asset\Updater\Adapter\CustomSettingsAdapter:
tags: [ 'pimcore.studio_backend.update_adapter' ]

Pimcore\Bundle\StudioBackendBundle\Asset\Updater\Adapter\DataAdapter:
tags: [ 'pimcore.studio_backend.update_adapter' ]

#
# Patcher
#

Pimcore\Bundle\StudioBackendBundle\Asset\Patcher\Adapter\MetadataAdapter:
tags: [ 'pimcore.studio_backend.patch_adapter' ]

#
# Handler
Expand Down
27 changes: 24 additions & 3 deletions config/metadata.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,30 @@ services:
autoconfigure: true
public: false

#
# Repository
#

# controllers are imported separately to make sure they're public
# and have a tag that allows actions to type-hint services
Pimcore\Bundle\StudioBackendBundle\Metadata\Controller\:
resource: '../src/Metadata/Controller'
public: true
tags: [ 'controller.service_arguments' ]

# Service
Pimcore\Bundle\StudioBackendBundle\Metadata\Service\MetadataServiceInterface:
class: Pimcore\Bundle\StudioBackendBundle\Metadata\Service\MetadataService


# Hydrator
Pimcore\Bundle\StudioBackendBundle\Metadata\Hydrator\MetadataHydratorInterface:
class: Pimcore\Bundle\StudioBackendBundle\Metadata\Hydrator\MetadataHydrator

# Repository
Pimcore\Bundle\StudioBackendBundle\Metadata\Repository\MetadataRepositoryInterface:
class: Pimcore\Bundle\StudioBackendBundle\Metadata\Repository\MetadataRepository

Pimcore\Bundle\StudioBackendBundle\Metadata\Updater\Adapter\CustomMetadataAdapter:
tags: [ 'pimcore.studio_backend.update_adapter' ]

# Patcher
Pimcore\Bundle\StudioBackendBundle\Metadata\Patcher\Adapter\CustomMetadataAdapter:
tags: [ 'pimcore.studio_backend.patch_adapter' ]
5 changes: 3 additions & 2 deletions src/Asset/Controller/Data/CustomMetadataController.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use Pimcore\Bundle\StudioBackendBundle\Asset\Service\Data\CustomMetadataServiceInterface;
use Pimcore\Bundle\StudioBackendBundle\Controller\AbstractApiController;
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\AccessDeniedException;
use Pimcore\Bundle\StudioBackendBundle\Metadata\Service\MetadataServiceInterface;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attribute\Content\ItemsJson;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attribute\Parameter\Path\IdParameter;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attribute\Response\DefaultResponses;
Expand All @@ -44,7 +45,7 @@ final class CustomMetadataController extends AbstractApiController

public function __construct(
SerializerInterface $serializer,
private readonly CustomMetadataServiceInterface $customMetadataService
private readonly MetadataServiceInterface $metadataService
) {
parent::__construct($serializer);
}
Expand Down Expand Up @@ -72,6 +73,6 @@ public function __construct(
])]
public function getAssetCustomMetadataById(int $id): JsonResponse
{
return $this->jsonResponse(['items' => $this->customMetadataService->getCustomMetadata($id)]);
return $this->jsonResponse(['items' => $this->metadataService->getCustomMetadata($id)]);
}
}
72 changes: 72 additions & 0 deletions src/Metadata/Controller/CollectionController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php
declare(strict_types=1);

/**
* Pimcore
*
* This source file is available under two different licenses:
* - GNU General Public License version 3 (GPLv3)
* - Pimcore Commercial License (PCL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
* @license http://www.pimcore.org/license GPLv3 and PCL
*/

namespace Pimcore\Bundle\StudioBackendBundle\Metadata\Controller;


use OpenApi\Attributes\Get;
use Pimcore\Bundle\StudioBackendBundle\Controller\AbstractApiController;
use Pimcore\Bundle\StudioBackendBundle\Metadata\MappedParameter\MetadataParameters;
use Pimcore\Bundle\StudioBackendBundle\Metadata\Schema\PredefinedMetadata;
use Pimcore\Bundle\StudioBackendBundle\Metadata\Service\MetadataServiceInterface;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attribute\Content\ItemsJson;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attribute\Parameter\Query\FilterParameter;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attribute\Response\DefaultResponses;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attribute\Response\SuccessResponse;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Config\Tags;
use Pimcore\Bundle\StudioBackendBundle\Util\Constant\HttpResponseCodes;
use Pimcore\Bundle\StudioBackendBundle\Util\Trait\PaginatedResponseTrait;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpKernel\Attribute\MapQueryString;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Serializer\SerializerInterface;

/**
* @internal
*/
final class CollectionController extends AbstractApiController
{
use PaginatedResponseTrait;

public function __construct(
SerializerInterface $serializer,
private readonly MetadataServiceInterface $metadataService,
) {
parent::__construct($serializer);
}

#[Route('/metadata', name: 'pimcore_studio_api_metadata', methods: ['GET'])]
#[Get(
path: self::PREFIX . '/metadata',
operationId: 'metadata_get_collection',
description: 'metadata_get_collection_description',
summary: 'metadata_get_collection_summary',
tags: [Tags::Metadata->name]
)]
#[FilterParameter]
#[SuccessResponse(
description: 'metadata_get_collection_success_response',
content: new ItemsJson(PredefinedMetadata::class)
)]
#[DefaultResponses([
HttpResponseCodes::UNAUTHORIZED,
])]
public function getMetadata(
#[MapQueryString] MetadataParameters $parameters = new MetadataParameters()
): JsonResponse {
return $this->jsonResponse(['items' => $this->metadataService->getPredefinedMetadata($parameters)]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
* @license http://www.pimcore.org/license GPLv3 and PCL
*/

namespace Pimcore\Bundle\StudioBackendBundle\Asset\Event\PreResponse;
namespace Pimcore\Bundle\StudioBackendBundle\Metadata\Event\PreResponse;

use Pimcore\Bundle\StudioBackendBundle\Asset\Schema\CustomMetadata;
use Pimcore\Bundle\StudioBackendBundle\Metadata\Schema\CustomMetadata;
use Pimcore\Bundle\StudioBackendBundle\Event\AbstractPreResponseEvent;

final class CustomMetadataEvent extends AbstractPreResponseEvent
Expand Down
39 changes: 39 additions & 0 deletions src/Metadata/Event/PreResponse/PredefinedMetadataEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php
declare(strict_types=1);

/**
* Pimcore
*
* This source file is available under two different licenses:
* - GNU General Public License version 3 (GPLv3)
* - Pimcore Commercial License (PCL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
* @license http://www.pimcore.org/license GPLv3 and PCL
*/

namespace Pimcore\Bundle\StudioBackendBundle\Metadata\Event\PreResponse;

use Pimcore\Bundle\StudioBackendBundle\Event\AbstractPreResponseEvent;
use Pimcore\Bundle\StudioBackendBundle\Metadata\Schema\PredefinedMetadata;

final class PredefinedMetadataEvent extends AbstractPreResponseEvent
{
public const EVENT_NAME = 'pre_response.asset_predefined_metadata';

public function __construct(
private readonly PredefinedMetadata $predefinedMetadata
) {
parent::__construct($predefinedMetadata);
}

/**
* Use this to get additional infos out of the response object
*/
public function getPredefinedMetadata(): PredefinedMetadata
{
return $this->predefinedMetadata;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* @license http://www.pimcore.org/license GPLv3 and PCL
*/

namespace Pimcore\Bundle\StudioBackendBundle\Asset\Event\PreSet;
namespace Pimcore\Bundle\StudioBackendBundle\Metadata\Event\PreSet;

use Symfony\Contracts\EventDispatcher\Event;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,18 @@
* @license http://www.pimcore.org/license GPLv3 and PCL
*/

namespace Pimcore\Bundle\StudioBackendBundle\Asset\Hydrator;
namespace Pimcore\Bundle\StudioBackendBundle\Metadata\Hydrator;

use Pimcore\Bundle\StudioBackendBundle\Asset\Schema\CustomMetadata;
use Pimcore\Bundle\StudioBackendBundle\Metadata\Schema\CustomMetadata;
use Pimcore\Bundle\StudioBackendBundle\Metadata\Schema\PredefinedMetadata;
use Pimcore\Bundle\StudioBackendBundle\Resolver\Element\ReferenceResolverInterface;
use Pimcore\Model\Element\ElementInterface;
use Pimcore\Model\Metadata\Predefined;

/**
* @internal
*/
final readonly class CustomMetadataHydrator implements CustomMetadataHydratorInterface
final readonly class MetadataHydrator implements MetadataHydratorInterface
{
public function __construct(private ReferenceResolverInterface $referenceResolver)
{
Expand All @@ -42,6 +44,24 @@ public function hydrate(array $customMetadata): CustomMetadata
);
}

public function hydratePredefined(Predefined $predefined): PredefinedMetadata
{
return new PredefinedMetadata(
$predefined->getId(),
$predefined->getName(),
$predefined->getDescription(),
$predefined->getType(),
$predefined->getTargetSubType(),
$this->resolveData(
$predefined->getData(),
$predefined->getType(),
),
$predefined->getConfig(),
$predefined->getLanguage(),
$predefined->getGroup()
);
}

private function resolveData(mixed $data, string $type): mixed
{
return match (true) {
Expand Down
31 changes: 31 additions & 0 deletions src/Metadata/Hydrator/MetadataHydratorInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php
declare(strict_types=1);

/**
* Pimcore
*
* This source file is available under two different licenses:
* - GNU General Public License version 3 (GPLv3)
* - Pimcore Commercial License (PCL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
* @license http://www.pimcore.org/license GPLv3 and PCL
*/

namespace Pimcore\Bundle\StudioBackendBundle\Metadata\Hydrator;

use Pimcore\Bundle\StudioBackendBundle\Metadata\Schema\CustomMetadata;
use Pimcore\Bundle\StudioBackendBundle\Metadata\Schema\PredefinedMetadata;
use Pimcore\Model\Metadata\Predefined;

/**
* @internal
*/
interface MetadataHydratorInterface
{
public function hydrate(array $customMetadata): CustomMetadata;

public function hydratePredefined(Predefined $predefined): PredefinedMetadata;
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,21 @@
* @license http://www.pimcore.org/license GPLv3 and PCL
*/

namespace Pimcore\Bundle\StudioBackendBundle\Asset\Hydrator;

use Pimcore\Bundle\StudioBackendBundle\Asset\Schema\CustomMetadata;
namespace Pimcore\Bundle\StudioBackendBundle\Metadata\MappedParameter;

/**
* @internal
*/
interface CustomMetadataHydratorInterface
final readonly class MetadataParameters
{
public function hydrate(array $customMetadata): CustomMetadata;
public function __construct(
private ?string $filter = null

) {
}

public function getFilter(): ?string
{
return $this->filter;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@
* @license http://www.pimcore.org/license GPLv3 and PCL
*/

namespace Pimcore\Bundle\StudioBackendBundle\Asset\Patcher\Adapter;
namespace Pimcore\Bundle\StudioBackendBundle\Metadata\Patcher\Adapter;

use Pimcore\Bundle\StudioBackendBundle\Asset\Service\Data\CustomMetadataServiceInterface;
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\InvalidArgumentException;
use Pimcore\Bundle\StudioBackendBundle\Metadata\Repository\MetadataRepositoryInterface;
use Pimcore\Bundle\StudioBackendBundle\Metadata\Service\MetadataServiceInterface;
use Pimcore\Bundle\StudioBackendBundle\Patcher\Service\Loader\PatchAdapterInterface;
use Pimcore\Bundle\StudioBackendBundle\Patcher\Service\Loader\TaggedIteratorAdapter;
use Pimcore\Bundle\StudioBackendBundle\Util\Constant\ElementTypes;
Expand All @@ -32,7 +32,7 @@
* @internal
*/
#[AutoconfigureTag(TaggedIteratorAdapter::ADAPTER_TAG)]
final class MetadataAdapter implements PatchAdapterInterface
final class CustomMetadataAdapter implements PatchAdapterInterface
{
private const INDEX_KEY = 'metadata';

Expand Down Expand Up @@ -111,7 +111,7 @@ private function processNewMetadataEntry(array $metadata): array
throw new InvalidArgumentException('Metadata name is required');
}

if (in_array($metadata['name'], CustomMetadataServiceInterface::DEFAULT_METADATA, true)) {
if (in_array($metadata['name'], MetadataServiceInterface::DEFAULT_METADATA, true)) {
return $this->addDefaultMetadata($metadata);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* @license http://www.pimcore.org/license GPLv3 and PCL
*/

namespace Pimcore\Bundle\StudioBackendBundle\Asset\Schema;
namespace Pimcore\Bundle\StudioBackendBundle\Metadata\Schema;

use OpenApi\Attributes\Property;
use OpenApi\Attributes\Schema;
Expand Down
Loading

0 comments on commit fe02194

Please sign in to comment.