Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restructure metadata and add collection endpoint #596

Merged
merged 8 commits into from
Nov 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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' ]
4 changes: 2 additions & 2 deletions src/Grid/Column/Collector/Asset/MetadataCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@

namespace Pimcore\Bundle\StudioBackendBundle\Grid\Column\Collector\Asset;

use Pimcore\Bundle\StudioBackendBundle\Asset\Service\Data\CustomMetadataServiceInterface;
use Pimcore\Bundle\StudioBackendBundle\Grid\Column\ColumnCollectorInterface;
use Pimcore\Bundle\StudioBackendBundle\Grid\Column\ColumnDefinitionInterface;
use Pimcore\Bundle\StudioBackendBundle\Grid\Column\FrontendType;
use Pimcore\Bundle\StudioBackendBundle\Grid\Schema\ColumnConfiguration;
use Pimcore\Bundle\StudioBackendBundle\Metadata\Repository\MetadataRepositoryInterface;
use Pimcore\Bundle\StudioBackendBundle\Metadata\Service\MetadataServiceInterface;
use Pimcore\Bundle\StudioBackendBundle\Util\Constant\ElementTypes;
use function array_key_exists;

Expand Down Expand Up @@ -59,7 +59,7 @@ public function getColumnConfigurations(array $availableColumnDefinitions): arra
*/
private function getDefaultMetadata(): array
{
$defaultMetadata = CustomMetadataServiceInterface::DEFAULT_METADATA;
$defaultMetadata = MetadataServiceInterface::DEFAULT_METADATA;
$columns = [];
foreach ($defaultMetadata as $metadata) {
$columns[] = new ColumnConfiguration(
Expand Down
47 changes: 47 additions & 0 deletions src/Metadata/Attribute/Request/FilterRequestBody.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?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\Attribute\Request;

use Attribute;
use OpenApi\Attributes\JsonContent;
use OpenApi\Attributes\Property;
use OpenApi\Attributes\RequestBody;

/**
* @internal
*/
#[Attribute(Attribute::TARGET_METHOD)]
final class FilterRequestBody extends RequestBody
{
public function __construct()
{
parent::__construct(
required: false,
content: new JsonContent(
properties: [
new Property(
property: 'filter',
type: 'string',
example: 'author',
nullable: true,
),
],
type: 'object',
),
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@
* @license http://www.pimcore.org/license GPLv3 and PCL
*/

namespace Pimcore\Bundle\StudioBackendBundle\Asset\Controller\Data;
namespace Pimcore\Bundle\StudioBackendBundle\Metadata\Controller\Asset;

use OpenApi\Attributes\Get;
use Pimcore\Bundle\StudioBackendBundle\Asset\Schema\CustomMetadata;
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\Schema\CustomMetadata;
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 @@ -38,13 +38,13 @@
/**
* @internal
*/
final class CustomMetadataController extends AbstractApiController
final class GetController extends AbstractApiController
{
use ElementProviderTrait;

public function __construct(
SerializerInterface $serializer,
private readonly CustomMetadataServiceInterface $customMetadataService
private readonly MetadataServiceInterface $metadataService
) {
parent::__construct($serializer);
}
Expand All @@ -59,7 +59,7 @@ public function __construct(
operationId: 'asset_custom_metadata_get_by_id',
description: 'asset_custom_metadata_get_by_id_description',
summary: 'asset_custom_metadata_get_by_id_summary',
tags: [Tags::Assets->name]
tags: [Tags::Metadata->name]
)]
#[IdParameter(type: ElementTypes::TYPE_ASSET)]
#[SuccessResponse(
Expand All @@ -72,6 +72,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)]);
}
}
71 changes: 71 additions & 0 deletions src/Metadata/Controller/CollectionController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?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\Post;
use Pimcore\Bundle\StudioBackendBundle\Controller\AbstractApiController;
use Pimcore\Bundle\StudioBackendBundle\Metadata\Attribute\Request\FilterRequestBody;
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\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\MapRequestPayload;
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: ['POST'])]
#[Post(
path: self::PREFIX . '/metadata',
operationId: 'metadata_get_collection',
description: 'metadata_get_collection_description',
summary: 'metadata_get_collection_summary',
tags: [Tags::Metadata->name]
)]
#[FilterRequestBody]
#[SuccessResponse(
description: 'metadata_get_collection_success_response',
content: new ItemsJson(PredefinedMetadata::class)
)]
#[DefaultResponses([
HttpResponseCodes::UNAUTHORIZED,
])]
public function getMetadata(
#[MapRequestPayload] 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,10 +14,10 @@
* @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\Event\AbstractPreResponseEvent;
use Pimcore\Bundle\StudioBackendBundle\Metadata\Schema\CustomMetadata;

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,27 @@ 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->resolveDefinitionData(
$predefined->getData(),
$predefined->getType(),
),
$predefined->getConfig(),
$predefined->getLanguage(),
$predefined->getGroup(),
$predefined->getCreationDate(),
$predefined->getModificationDate(),
$predefined->isWriteable()
);
}

private function resolveData(mixed $data, string $type): mixed
{
return match (true) {
Expand All @@ -54,4 +77,17 @@ private function resolveData(mixed $data, string $type): mixed
default => $data,
};
}

private function resolveDefinitionData(mixed $data, string $type): mixed
{
if (!$data) {
return $data;
}

return match ($type) {
'asset', 'document', 'object' => $this->referenceResolver->resolveData($type, (int)$data),
'checkbox' => (bool)$data,
default => $data
};
}
}
Loading
Loading