-
Notifications
You must be signed in to change notification settings - Fork 19
Add Base implementation of website content resolving #271
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
Merged
alexander-schranz
merged 27 commits into
sulu:0.9
from
Prokyonn:feature/content-resolver
Nov 25, 2024
Merged
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 0f993b4
Add PropertyResolver and MediaResourceLoader
Prokyonn 53343e0
Add review suggestions
Prokyonn 53abf16
Add unit tests
Prokyonn 8f73e54
Add sulu/sulu fork
Prokyonn 14fd110
Keep required sulu version
alexander-schranz 79b1dca
Update Content/Application/ContentResolver/Value/ContentView.php
Prokyonn 182dfcd
Update Content/Application/ResourceLoader/ResourceLoaderInterface.php
Prokyonn 5b85bde
Remove sulu fork
Prokyonn d36e392
Fix tests for php 8.1
Prokyonn a4038bc
Fix tests for php 8.0
Prokyonn 5931803
Enhance content resolver test
Prokyonn fee1c48
Refactor contentResolver and add LinkPropertyProvider
Prokyonn 25f00bb
Add tests
Prokyonn d274fce
Add TeaserSelectionPropertyResolver
Prokyonn db9b1a1
Merge pull request #1 from Prokyonn/enhancement/link-property-provider
Prokyonn 5bf0ca6
Move interfaces and adjust namespaces
Prokyonn c1c6451
Merge pull request #2 from Prokyonn/enhancement/teaser-property-provider
Prokyonn 2096bdf
Define COMPOSER_TOKEN to fix issues with using forks
alexander-schranz a85638a
Use automation bundle 3.0
alexander-schranz 1f4e770
Update Sulu required version
alexander-schranz 05674bf
Define composer requirements for Sulu 3.0
alexander-schranz 4d5c0c4
Fix failings tests
Prokyonn b7f252d
Add debug line
Prokyonn 846848d
Skip resolver tests for now
Prokyonn bb0c1e9
Trigger CI
Prokyonn 85d51e6
Ignore skipped tests
Prokyonn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
185 changes: 185 additions & 0 deletions
185
Content/Application/ContentResolver/ContentResolver.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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']); | ||
| } | ||
|
|
||
| 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
33
Content/Application/ContentResolver/ContentResolverInterface.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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
88
Content/Application/ContentResolver/Resolver/ExcerptResolver.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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
28
Content/Application/ContentResolver/Resolver/ResolverInterface.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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