WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
0f0a6e6
Add own implementation of Controller
alexander-schranz Aug 8, 2024
0f993b4
Add PropertyResolver and MediaResourceLoader
Prokyonn Sep 2, 2024
53343e0
Add review suggestions
Prokyonn Sep 26, 2024
53abf16
Add unit tests
Prokyonn Sep 30, 2024
8f73e54
Add sulu/sulu fork
Prokyonn Oct 14, 2024
14fd110
Keep required sulu version
alexander-schranz Oct 16, 2024
79b1dca
Update Content/Application/ContentResolver/Value/ContentView.php
Prokyonn Oct 16, 2024
182dfcd
Update Content/Application/ResourceLoader/ResourceLoaderInterface.php
Prokyonn Oct 16, 2024
5b85bde
Remove sulu fork
Prokyonn Oct 16, 2024
d36e392
Fix tests for php 8.1
Prokyonn Oct 17, 2024
a4038bc
Fix tests for php 8.0
Prokyonn Oct 17, 2024
5931803
Enhance content resolver test
Prokyonn Oct 28, 2024
fee1c48
Refactor contentResolver and add LinkPropertyProvider
Prokyonn Nov 5, 2024
25f00bb
Add tests
Prokyonn Nov 11, 2024
d274fce
Add TeaserSelectionPropertyResolver
Prokyonn Nov 11, 2024
db9b1a1
Merge pull request #1 from Prokyonn/enhancement/link-property-provider
Prokyonn Nov 12, 2024
5bf0ca6
Move interfaces and adjust namespaces
Prokyonn Nov 12, 2024
c1c6451
Merge pull request #2 from Prokyonn/enhancement/teaser-property-provider
Prokyonn Nov 12, 2024
2096bdf
Define COMPOSER_TOKEN to fix issues with using forks
alexander-schranz Nov 18, 2024
a85638a
Use automation bundle 3.0
alexander-schranz Nov 18, 2024
1f4e770
Update Sulu required version
alexander-schranz Nov 18, 2024
05674bf
Define composer requirements for Sulu 3.0
alexander-schranz Nov 18, 2024
4d5c0c4
Fix failings tests
Prokyonn Nov 18, 2024
b7f252d
Add debug line
Prokyonn Nov 22, 2024
846848d
Skip resolver tests for now
Prokyonn Nov 22, 2024
bb0c1e9
Trigger CI
Prokyonn Nov 22, 2024
85d51e6
Ignore skipped tests
Prokyonn Nov 22, 2024
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
185 changes: 185 additions & 0 deletions Content/Application/ContentResolver/ContentResolver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
<?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\ContentBundle\Content\Application\ContentResolver;

use Sulu\Bundle\ContentBundle\Content\Application\ContentResolver\Resolver\ResolverInterface;
use Sulu\Bundle\ContentBundle\Content\Application\ContentResolver\Resolver\TemplateResolver;
use Sulu\Bundle\ContentBundle\Content\Application\ContentResolver\Value\ContentView;
use Sulu\Bundle\ContentBundle\Content\Application\ContentResolver\Value\ResolvableResource;
use Sulu\Bundle\ContentBundle\Content\Application\ResourceLoader\ResourceLoaderProvider;
use Sulu\Bundle\ContentBundle\Content\Domain\Model\DimensionContentInterface;

class ContentResolver implements ContentResolverInterface
{
/**
* @param iterable<ResolverInterface> $contentResolvers
*/
public function __construct(
private iterable $contentResolvers,
private ResourceLoaderProvider $resourceLoaderProvider
) {
}

/**
* @return array{
* resource: object,
* content: mixed,
* view: mixed[]
* }
*/
public function resolve(DimensionContentInterface $dimensionContent): array
{
$contentViews = [];
foreach ($this->contentResolvers as $key => $contentResolver) {
$contentView = $contentResolver->resolve($dimensionContent);

if ($contentResolver instanceof TemplateResolver) {
/** @var mixed[] $content */
$content = $contentView->getContent();
$contentViews = \array_merge($contentViews, $content);
continue;
}

$contentViews[$key] = $contentView;
}

$result = $this->resolveContentViews($contentViews);
$resources = $this->loadResolvableResources($result['resolvableResources'], $dimensionContent->getLocale());
\array_walk_recursive($result['content'], function(&$value) use ($resources) {
if ($value instanceof ResolvableResource) {
$value = $resources[$value->getResourceLoaderKey()][$value->getId()];
}
});

return [
'resource' => $dimensionContent->getResource(),
'content' => $result['content'],
'view' => $result['view'],
];
}

/**
* @param ContentView[] $contentViews
*
* @return array{
* content: mixed[],
* view: mixed[],
* resolvableResources: array<string, array<int|string>>
* }
*/
private function resolveContentViews(array $contentViews): array
{
$content = [];
$view = [];
$resolvableResources = [];

foreach ($contentViews as $name => $contentView) {
$result = $this->resolveContentView($contentView, $name);
$content = \array_merge($content, $result['content']);
$view = \array_merge($view, $result['view']);
$resolvableResources = \array_merge_recursive($resolvableResources, $result['resolvableResources']);
Comment on lines +84 to +86
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merge behave sometimes strange for nested arrays maybe we need to use array_replace / array_recursive_replace

}

return [
'content' => $content,
'view' => $view,
'resolvableResources' => $resolvableResources,
];
}

/**
* @param array<string, array<int|string>> $resolvableResourceIds
*
* @return array<string, mixed[]>
*/
private function loadResolvableResources(array $resolvableResourceIds, ?string $locale): array
{
$resources = [];
foreach ($resolvableResourceIds as $resourceLoaderKey => $ids) {
$resourceLoader = $this->resourceLoaderProvider->getResourceLoader($resourceLoaderKey);
if (!$resourceLoader) {
throw new \RuntimeException(\sprintf('ResourceLoader with key "%s" not found', $resourceLoaderKey));
}

$resources[$resourceLoaderKey] = $resourceLoader->load($ids, $locale);
}

return $resources;
}

/**
* @return array{
* content: mixed[],
* view: mixed[],
* resolvableResources: array<string, array<int|string>>
* }
*/
private function resolveContentView(ContentView $contentView, string $name): array
{
$resolvableResources = [];
$content[$name] = $contentView->getContent();
$view[$name] = $contentView->getView();

if (\is_array($content[$name])) {
foreach ($content[$name] as $index => $value) {
$contentViewValues = [];
$otherValues = [];

if (\is_array($value)) {
foreach ($value as $key => $entry) {
if ($entry instanceof ResolvableResource) {
$resolvableResources[$entry->getResourceLoaderKey()][] = $entry->getId();
}

match (true) {
$entry instanceof ContentView => $contentViewValues[$key] = $entry,
default => $otherValues[$key] = $entry,
};
}

$resolvedContentViews = $this->resolveContentViews($contentViewValues);
$result['content'] = \array_merge(
$resolvedContentViews['content'],
$otherValues,
);
$result['view'] = \array_merge(
$resolvedContentViews['view'],
);

$resolvableResources = \array_merge_recursive($resolvableResources, $resolvedContentViews['resolvableResources']);
} else {
if ($value instanceof ResolvableResource) {
$resolvableResources[$value->getResourceLoaderKey()][] = $value->getId();
}

if ($value instanceof ContentView) {
$result = $this->resolveContentView($value, $index);
} else {
$result['content'] = $value;
$result['view'] = [];
}
}

$content[$name][$index] = $result['content'];
$view[$name][$index] = $result['view'];
}
}

return [
'content' => $content,
'view' => $view,
'resolvableResources' => $resolvableResources,
];
}
}
33 changes: 33 additions & 0 deletions Content/Application/ContentResolver/ContentResolverInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?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\ContentBundle\Content\Application\ContentResolver;

use Sulu\Bundle\ContentBundle\Content\Domain\Model\ContentRichEntityInterface;
use Sulu\Bundle\ContentBundle\Content\Domain\Model\DimensionContentInterface;

interface ContentResolverInterface
{
/**
* @template T of ContentRichEntityInterface
*
* @param DimensionContentInterface<T> $dimensionContent
*
* @return array{
* resource: object,
* content: mixed,
* view: mixed[]
* }
*/
public function resolve(DimensionContentInterface $dimensionContent): array;
}
88 changes: 88 additions & 0 deletions Content/Application/ContentResolver/Resolver/ExcerptResolver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?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\ContentBundle\Content\Application\ContentResolver\Resolver;

use Sulu\Bundle\AdminBundle\Metadata\FormMetadata\FormMetadata;
use Sulu\Bundle\AdminBundle\Metadata\MetadataProviderInterface;
use Sulu\Bundle\CategoryBundle\Entity\CategoryInterface;
use Sulu\Bundle\ContentBundle\Content\Application\ContentResolver\Value\ContentView;
use Sulu\Bundle\ContentBundle\Content\Application\MetadataResolver\MetadataResolver;
use Sulu\Bundle\ContentBundle\Content\Domain\Model\DimensionContentInterface;
use Sulu\Bundle\ContentBundle\Content\Domain\Model\ExcerptInterface;
use Sulu\Bundle\TagBundle\Tag\TagInterface;

class ExcerptResolver implements ResolverInterface
{
public function __construct(
private MetadataProviderInterface $formMetadataProvider,
private MetadataResolver $metadataResolver
) {
}

public function resolve(DimensionContentInterface $dimensionContent): ContentView
{
if (!$dimensionContent instanceof ExcerptInterface) {
throw new \RuntimeException('DimensionContent needs to extend the ' . ExcerptInterface::class);
}

/** @var string $locale */
$locale = $dimensionContent->getLocale();

/** @var FormMetadata $formMetadata */
$formMetadata = $this->formMetadataProvider->getMetadata($this->getFormKey(), $locale, []);

return ContentView::create(
$this->metadataResolver->resolveItems(
$formMetadata->getItems(),
$this->getExcerptData($dimensionContent),
$locale
),
[]
);
}

protected function getFormKey(): string
{
return 'content_excerpt';
}

/**
* @return array{
* excerptTitle: string|null,
* excerptMore: string|null,
* excerptDescription: string|null,
* excerptCategories: int[],
* excerptTags: int[],
* excerptIcon: array{id: int}|null,
* excerptImage: array{id: int}|null
* }
*/
protected function getExcerptData(ExcerptInterface $dimensionContent): array
{
return [
'excerptTitle' => $dimensionContent->getExcerptTitle(),
'excerptMore' => $dimensionContent->getExcerptMore(),
'excerptDescription' => $dimensionContent->getExcerptDescription(),
'excerptCategories' => \array_map(
fn (CategoryInterface $category) => $category->getId(),
$dimensionContent->getExcerptCategories()
),
'excerptTags' => \array_map(
fn (TagInterface $tag) => $tag->getId(), $dimensionContent->getExcerptTags()
),
'excerptIcon' => $dimensionContent->getExcerptIcon(),
'excerptImage' => $dimensionContent->getExcerptImage(),
];
}
}
28 changes: 28 additions & 0 deletions Content/Application/ContentResolver/Resolver/ResolverInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?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\ContentBundle\Content\Application\ContentResolver\Resolver;

use Sulu\Bundle\ContentBundle\Content\Application\ContentResolver\Value\ContentView;
use Sulu\Bundle\ContentBundle\Content\Domain\Model\ContentRichEntityInterface;
use Sulu\Bundle\ContentBundle\Content\Domain\Model\DimensionContentInterface;

interface ResolverInterface
{
/**
* @template T of ContentRichEntityInterface
*
* @param DimensionContentInterface<T> $dimensionContent
*/
public function resolve(DimensionContentInterface $dimensionContent): ContentView;
}
Loading
Loading