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

Enhance property resolver service configurations #5

Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ public function load(array $configs, ContainerBuilder $container)
}

if (
InstalledVersions::isInstalled('sulu/sulu-content-bundle')
&& \version_compare(InstalledVersions::getVersion('sulu/sulu-content-bundle') ?? '0.0.0', '0.9', '>=')
&& \version_compare(InstalledVersions::getVersion('sulu/sulu-content-bundle') ?? '0.0.0', '0.10', '<')
InstalledVersions::isInstalled('sulu/content-bundle')
&& \version_compare(InstalledVersions::getVersion('sulu/content-bundle') ?? '0.0.0', '0.9', '>=')
&& \version_compare(InstalledVersions::getVersion('sulu/content-bundle') ?? '0.0.0', '0.10', '<')
) {
$loader->load('services_content.xml');
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

declare(strict_types=1);

/*
* This file is part of Sulu.
*
* (c) Sulu GmbH
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Sulu\Bundle\CategoryBundle\Infrastructure\Sulu\Content\PropertyResolver;

use Sulu\Bundle\CategoryBundle\Infrastructure\Sulu\Content\ResourceLoader\CategoryResourceLoader;
use Sulu\Bundle\ContentBundle\Content\Application\ContentResolver\Value\ContentView;
use Sulu\Bundle\ContentBundle\Content\Application\PropertyResolver\PropertyResolverInterface;

/**
* @internal if you need to override this service, create a new service with based on PropertyResolverInterface instead of extending this class
*
* @final
*/
class SingleCategorySelectionPropertyResolver implements PropertyResolverInterface
{
public function resolve(mixed $data, string $locale, array $params = []): ContentView
{
if (!\is_string($data) || '' === $data) {
return ContentView::create([], $params);
}

/** @var string $resourceLoaderKey */
$resourceLoaderKey = $params['resourceLoader'] ?? CategoryResourceLoader::getKey();

return ContentView::createResolvables(
$data,
$resourceLoaderKey,
$params,
);
}

public static function getType(): string
{
return 'single_category_selection';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
class="Sulu\Bundle\CategoryBundle\Infrastructure\Sulu\Content\ResourceLoader\CategoryResourceLoader">
<argument type="service" id="sulu_category.category_manager"/>

<tag name="sulu_content.resource_loader" type="media"/>
<tag name="sulu_content.resource_loader" type="category"/>
</service>
</services>
</container>
Original file line number Diff line number Diff line change
Expand Up @@ -279,9 +279,9 @@ public function load(array $configs, ContainerBuilder $container)
$loader->load('command.xml');

if (
InstalledVersions::isInstalled('sulu/sulu-content-bundle')
&& \version_compare(InstalledVersions::getVersion('sulu/sulu-content-bundle') ?? '0.0.0', '0.9', '>=')
&& \version_compare(InstalledVersions::getVersion('sulu/sulu-content-bundle') ?? '0.0.0', '0.10', '<')
InstalledVersions::isInstalled('sulu/content-bundle')
&& \version_compare(InstalledVersions::getVersion('sulu/content-bundle') ?? '0.0.0', '0.9', '>=')
&& \version_compare(InstalledVersions::getVersion('sulu/content-bundle') ?? '0.0.0', '0.10', '<')
) {
$loader->load('services_content.xml');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@
<service id="sulu_contact.contact_resource_loader" class="Sulu\Bundle\ContactBundle\Infrastructure\Sulu\Content\ResourceLoader\ContactResourceLoader">
<argument type="service" id="sulu_contact.contact_manager"/>

<tag name="sulu_content.resource_loader"/>
<tag name="sulu_content.resource_loader" type="contact"/>
</service>

<service id="sulu_contact.account_resource_loader" class="Sulu\Bundle\ContactBundle\Infrastructure\Sulu\Content\ResourceLoader\AccountResourceLoader">
<argument type="service" id="sulu_contact.account_manager"/>

<tag name="sulu_content.resource_loader"/>
<tag name="sulu_content.resource_loader" type="account"/>
</service>
</services>
</container>
Original file line number Diff line number Diff line change
Expand Up @@ -366,9 +366,9 @@ public function load(array $configs, ContainerBuilder $container)
}

if (
InstalledVersions::isInstalled('sulu/sulu-content-bundle')
&& \version_compare(InstalledVersions::getVersion('sulu/sulu-content-bundle') ?? '0.0.0', '0.9', '>=')
&& \version_compare(InstalledVersions::getVersion('sulu/sulu-content-bundle') ?? '0.0.0', '0.10', '<')
InstalledVersions::isInstalled('sulu/content-bundle')
&& \version_compare(InstalledVersions::getVersion('sulu/content-bundle') ?? '0.0.0', '0.9', '>=')
&& \version_compare(InstalledVersions::getVersion('sulu/content-bundle') ?? '0.0.0', '0.10', '<')
) {
$loader->load('services_content.xml');
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

declare(strict_types=1);

/*
* This file is part of Sulu.
*
* (c) Sulu GmbH
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Sulu\Bundle\MediaBundle\Infrastructure\Sulu\Content\PropertyResolver;

use Sulu\Bundle\ContentBundle\Content\Application\ContentResolver\Value\ContentView;
use Sulu\Bundle\ContentBundle\Content\Application\PropertyResolver\PropertyResolverInterface;
use Sulu\Bundle\MediaBundle\Infrastructure\Sulu\Content\ResourceLoader\CollectionResourceLoader;

/**
* @internal if you need to override this service, create a new service with based on PropertyResolverInterface instead of extending this class
*
* @final
*/
class CollectionSelectionPropertyResolver implements PropertyResolverInterface
{
public function resolve(mixed $data, string $locale, array $params = []): ContentView
{
if (!\is_array($data)
|| 0 === \count($data)
|| !\array_is_list($data)
) {
return ContentView::create([], ['ids' => [], ...$params]);
}

/** @var string $resourceLoaderKey */
$resourceLoaderKey = $params['resourceLoader'] ?? CollectionResourceLoader::getKey();

return ContentView::createResolvables(
$data,
$resourceLoaderKey,
['ids' => $data, ...$params],
);
}

public static function getType(): string
{
return 'collection_selection';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
<?php

declare(strict_types=1);

/*
* This file is part of Sulu.
*
* (c) Sulu GmbH
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Sulu\Bundle\MediaBundle\Infrastructure\Sulu\Content\PropertyResolver;

use Psr\Log\LoggerInterface;
use Sulu\Bundle\AdminBundle\Metadata\FormMetadata\FieldMetadata;
use Sulu\Bundle\AdminBundle\Metadata\FormMetadata\FormMetadata;
use Sulu\Bundle\ContentBundle\Content\Application\ContentResolver\Value\ContentView;
use Sulu\Bundle\ContentBundle\Content\Application\ContentResolver\Value\ResolvableResource;
use Sulu\Bundle\ContentBundle\Content\Application\MetadataResolver\MetadataResolver;
use Sulu\Bundle\ContentBundle\Content\Application\PropertyResolver\PropertyResolverInterface;
use Sulu\Bundle\MediaBundle\Infrastructure\Sulu\Content\ResourceLoader\MediaResourceLoader;

/**
* @internal if you need to override this service, create a new service with based on ResourceLoaderInterface instead of extending this class
*
* @final
*/
class ImageMapPropertyResolver implements PropertyResolverInterface // TODO we may should implement a PropertyResolverAwareMetadataInterface
{
private MetadataResolver $metadataResolver;

public function __construct(
private readonly LoggerInterface $logger,
private readonly bool $debug = false,
) {
}

/**
* @internal
*
* Prevent circular dependency by injecting the MetadataResolver after instantiation
*/
public function setMetadataResolver(MetadataResolver $metadataResolver): void
{
$this->metadataResolver = $metadataResolver;
}

public function resolve(mixed $data, string $locale, array $params = []): ContentView
{
$hotspots = (\is_array($data) && isset($data['hotspots']) && \is_array($data['hotspots'])) && \array_is_list($data['hotspots'])
? $data['hotspots']
: [];

$hotspots = [] !== $hotspots ? $this->resolveHotspots($hotspots, $locale, $params) : ContentView::create([], []);

$returnedParams = $params;
unset($returnedParams['metadata']); // TODO we may should implement a PropertyResolverAwareMetadataInterface

if (!\is_array($data)
|| !isset($data['imageId'])
|| !\is_numeric($data['imageId'])
) {
return ContentView::create([
'image' => null,
'hotspots' => $hotspots->getContent(),
], [
'imageId' => null,
'hotspots' => $hotspots->getView(),
...$returnedParams,
]);
}

/** @var string $resourceLoaderKey */
$resourceLoaderKey = $params['resourceLoader'] ?? MediaResourceLoader::getKey();
$imageId = (int) $data['imageId'];

return ContentView::create(
[
'image' => new ResolvableResource($imageId, $resourceLoaderKey),
'hotspots' => $hotspots->getContent(),
],
[
'imageId' => $imageId,
'hotspots' => $hotspots->getView(),
...$returnedParams,
],
);
}

/**
* @param non-empty-array<array<mixed>> $hotspots
* @param array<string, mixed> $params
*/
private function resolveHotspots(array $hotspots, string $locale, array $params): ContentView
{
$metadata = $params['metadata'] ?? null;
\assert($metadata instanceof FieldMetadata, 'Metadata must be set to resolve hotspots.');
$metadataTypes = $metadata->getTypes();
$content = [];
$view = [];
foreach ($hotspots as $key => $block) {
if (!\is_array($block) || !isset($block['type']) || !\is_string($block['type'])) {
continue;
}
if (!isset($block['hotspot']) || !\is_array($block['hotspot'])) {
continue;
}

$type = $block['type'];
$formMetadata = $metadataTypes[$type] ?? null;

if (!$formMetadata instanceof FormMetadata) {
$errorMessage = \sprintf(
'Metadata type "%s" in "%s" not found, founded types are: "%s"',
$type,
$metadata->getName(),
\implode('", "', \array_keys($metadataTypes)),
);

$this->logger->error($errorMessage);

if ($this->debug) {
throw new \UnexpectedValueException($errorMessage);
}

$type = $metadata->getDefaultType();
$formMetadata = $metadataTypes[$type] ?? null;
if (!$formMetadata instanceof FormMetadata) {
continue;
}
}

$content[$key] = [
'type' => $type,
'hotspot' => $block['hotspot'],
];

$view[$key] = [];

foreach ($this->metadataResolver->resolveItems($formMetadata->getItems(), $block, $locale) as $field => $resolvedItem) {
$content[$key][$field] = $resolvedItem->getContent();
$view[$key][$field] = $resolvedItem->getView();
}
}

return ContentView::create(\array_values($content), \array_values($view));
}

public static function getType(): string
{
return 'image_map';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

declare(strict_types=1);

/*
* This file is part of Sulu.
*
* (c) Sulu GmbH
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Sulu\Bundle\MediaBundle\Infrastructure\Sulu\Content\PropertyResolver;

use Sulu\Bundle\ContentBundle\Content\Application\ContentResolver\Value\ContentView;
use Sulu\Bundle\ContentBundle\Content\Application\PropertyResolver\PropertyResolverInterface;
use Sulu\Bundle\MediaBundle\Infrastructure\Sulu\Content\ResourceLoader\CollectionResourceLoader;

/**
* @internal if you need to override this service, create a new service with based on PropertyResolverInterface instead of extending this class
*
* @final
*/
class SingleCollectionSelectionPropertyResolver implements PropertyResolverInterface
{
public function resolve(mixed $data, string $locale, array $params = []): ContentView
{
if (!\is_numeric($data)) {
return ContentView::create(null, ['id' => null, ...$params]);
}

/** @var string $resourceLoaderKey */
$resourceLoaderKey = $params['resourceLoader'] ?? CollectionResourceLoader::getKey();

return ContentView::createResolvable(
(int) $data,
$resourceLoaderKey,
[
'id' => $data,
...$params,
],
);
}

public static function getType(): string
{
return 'single_collection_selection';
}
}
Loading
Loading