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

Commit 1521891

Browse files
committed
Add PropertyResolver and MediaResourceLoader
1 parent 74c13b0 commit 1521891

22 files changed

+810
-12
lines changed

Content/Application/ContentAggregator/ContentAggregatorInterface.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@ interface ContentAggregatorInterface
2323
*
2424
* @param ContentRichEntityInterface<T> $contentRichEntity
2525
* @param mixed[] $dimensionAttributes
26-
*
27-
* @return DimensionContentInterface
2826
*/
2927
public function aggregate(ContentRichEntityInterface $contentRichEntity, array $dimensionAttributes): DimensionContentInterface;
3028
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of Sulu.
7+
*
8+
* (c) Sulu GmbH
9+
*
10+
* This source file is subject to the MIT license that is bundled
11+
* with this source code in the file LICENSE.
12+
*/
13+
14+
namespace Sulu\Bundle\ContentBundle\Content\Application\ContentObjects;
15+
16+
class ContentView
17+
{
18+
/**
19+
* @param mixed[] $content
20+
* @param mixed[] $view
21+
*/
22+
private function __construct(
23+
private mixed $content,
24+
private array $view
25+
) {
26+
}
27+
28+
public static function create(mixed $content, array $view): self
29+
{
30+
return new self($content, $view);
31+
}
32+
33+
public static function createResolvable(string $id, string $resourceLoaderKey, array $view): self
34+
{
35+
$resolvableResources = [
36+
new ResolvableResource($id, $resourceLoaderKey),
37+
];
38+
39+
return new self($resolvableResources, $view);
40+
}
41+
42+
public static function createResolvables(array $ids, string $resourceLoaderKey, array $view): self
43+
{
44+
$resolvableResources = [];
45+
46+
foreach ($ids as $id) {
47+
$resolvableResources[] = new ResolvableResource($id, $resourceLoaderKey);
48+
}
49+
50+
return new self($resolvableResources, $view);
51+
}
52+
53+
public function getContent(): mixed
54+
{
55+
return $this->content;
56+
}
57+
58+
public function getView(): array
59+
{
60+
return $this->view;
61+
}
62+
63+
public function setContent(array $content): self
64+
{
65+
$this->content = $content;
66+
67+
return $this;
68+
}
69+
70+
public function setView(array $view): self
71+
{
72+
$this->view = $view;
73+
74+
return $this;
75+
}
76+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of Sulu.
7+
*
8+
* (c) Sulu GmbH
9+
*
10+
* This source file is subject to the MIT license that is bundled
11+
* with this source code in the file LICENSE.
12+
*/
13+
14+
namespace Sulu\Bundle\ContentBundle\Content\Application\ContentObjects;
15+
16+
class ResolvableResource
17+
{
18+
public function __construct(
19+
private string $id,
20+
private string $resourceLoaderKey,
21+
) {
22+
}
23+
24+
public function getId(): string
25+
{
26+
return $this->id;
27+
}
28+
29+
public function getResourceLoaderKey(): string
30+
{
31+
return $this->resourceLoaderKey;
32+
}
33+
}
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of Sulu.
7+
*
8+
* (c) Sulu GmbH
9+
*
10+
* This source file is subject to the MIT license that is bundled
11+
* with this source code in the file LICENSE.
12+
*/
13+
14+
namespace Sulu\Bundle\ContentBundle\Content\Application\ContentResolver;
15+
16+
use Sulu\Bundle\ContentBundle\Content\Application\ContentObjects\ContentView;
17+
use Sulu\Bundle\ContentBundle\Content\Application\ContentObjects\ResolvableResource;
18+
use Sulu\Bundle\ContentBundle\Content\Application\ResourceLoader\ResourceLoaderProvider;
19+
use Sulu\Bundle\ContentBundle\Content\Application\TemplateResolver\TemplateResolverInterface;
20+
use Sulu\Bundle\ContentBundle\Content\Domain\Model\DimensionContentInterface;
21+
22+
class ContentResolver implements ContentResolverInterface
23+
{
24+
public function __construct(
25+
private TemplateResolverInterface $templateResolver,
26+
private ResourceLoaderProvider $resourceLoaderProvider
27+
) {
28+
}
29+
30+
public function resolve(DimensionContentInterface $dimensionContent): array
31+
{
32+
$contentViews = $this->templateResolver->resolve($dimensionContent);
33+
$result = $this->resolveContentViews($contentViews);
34+
$resources = $this->loadResolvableResources($result['resolvableResources'], $dimensionContent->getLocale());
35+
\array_walk_recursive($result['content'], function(&$value) use ($resources) {
36+
if ($value instanceof ResolvableResource) {
37+
$value = $resources[$value->getResourceLoaderKey()][$value->getId()];
38+
}
39+
});
40+
41+
return [
42+
'resource' => $dimensionContent->getResource(),
43+
'content' => $result['content'],
44+
'view' => $result['view'],
45+
];
46+
}
47+
48+
/**
49+
* @param ContentView[]|mixed $contentViews
50+
*
51+
* @return mixed[]
52+
*/
53+
private function resolveContentViews(array $contentViews): array
54+
{
55+
$content = [];
56+
$view = [];
57+
$resolvableResources = [];
58+
59+
foreach ($contentViews as $name => $contentView) {
60+
$result = $this->resolveContentView($contentView, $name);
61+
$content = \array_merge($content, $result['content']);
62+
$view = \array_merge($view, $result['view']);
63+
$resolvableResources = \array_merge_recursive($resolvableResources, $result['resolvableResources']);
64+
}
65+
66+
return [
67+
'content' => $content,
68+
'view' => $view,
69+
'resolvableResources' => $resolvableResources,
70+
];
71+
}
72+
73+
/**
74+
* @param array<string, int|string> $resolvableResourceIds
75+
*/
76+
private function loadResolvableResources(array $resolvableResourceIds, string $locale): array
77+
{
78+
$resources = [];
79+
foreach ($resolvableResourceIds as $resourceLoaderKey => $ids) {
80+
$resourceLoader = $this->resourceLoaderProvider->getResourceLoader($resourceLoaderKey);
81+
if (!$resourceLoader) {
82+
throw new \RuntimeException(\sprintf('ResourceLoader with key "%s" not found', $resourceLoaderKey));
83+
}
84+
85+
$resources[$resourceLoaderKey] = $resourceLoader->load($ids, $locale);
86+
}
87+
88+
return $resources;
89+
}
90+
91+
private function resolveContentView(mixed $contentView, string $name): array
92+
{
93+
$resolvableResources = [];
94+
$content[$name] = $contentView->getContent();
95+
$view[$name] = $contentView->getView();
96+
97+
if (\is_array($content[$name])) {
98+
foreach ($content[$name] as $index => $value) {
99+
$contentViewValues = [];
100+
$otherValues = [];
101+
102+
if (\is_array($value)) {
103+
foreach ($value as $key => $entry) {
104+
if ($entry instanceof ResolvableResource) {
105+
$resolvableResources[$entry->getResourceLoaderKey()][] = $entry->getId();
106+
}
107+
108+
match (true) {
109+
$entry instanceof ContentView => $contentViewValues[$key] = $entry,
110+
default => $otherValues[$key] = $entry,
111+
};
112+
}
113+
114+
$resolvedContentViews = $this->resolveContentViews($contentViewValues);
115+
$result['content'] = \array_merge(
116+
$resolvedContentViews['content'],
117+
$otherValues,
118+
);
119+
$result['view'] = \array_merge(
120+
$resolvedContentViews['view'],
121+
);
122+
123+
$resolvableResources = \array_merge_recursive($resolvableResources, $resolvedContentViews['resolvableResources']);
124+
} else {
125+
if ($value instanceof ResolvableResource) {
126+
$resolvableResources[$value->getResourceLoaderKey()][] = $value->getId();
127+
}
128+
129+
if ($value instanceof ContentView) {
130+
$result = $this->resolveContentView($value, $name);
131+
} else {
132+
$result['content'] = $value;
133+
$result['view'] = [];
134+
}
135+
}
136+
137+
$content[$name][$index] = $result['content'];
138+
$view[$name][$index] = $result['view'];
139+
}
140+
}
141+
142+
return [
143+
'content' => $content,
144+
'view' => $view,
145+
'resolvableResources' => $resolvableResources,
146+
];
147+
}
148+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of Sulu.
7+
*
8+
* (c) Sulu GmbH
9+
*
10+
* This source file is subject to the MIT license that is bundled
11+
* with this source code in the file LICENSE.
12+
*/
13+
14+
namespace Sulu\Bundle\ContentBundle\Content\Application\ContentResolver;
15+
16+
use Sulu\Bundle\ContentBundle\Content\Domain\Model\DimensionContentInterface;
17+
18+
interface ContentResolverInterface
19+
{
20+
public function resolve(DimensionContentInterface $dimensionContent): array;
21+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of Sulu.
7+
*
8+
* (c) Sulu GmbH
9+
*
10+
* This source file is subject to the MIT license that is bundled
11+
* with this source code in the file LICENSE.
12+
*/
13+
14+
namespace Sulu\Bundle\ContentBundle\Content\Application\MetadataResolver;
15+
16+
use Sulu\Bundle\AdminBundle\Metadata\FormMetadata\FormMetadata;
17+
use Sulu\Bundle\AdminBundle\Metadata\FormMetadata\ItemMetadata;
18+
use Sulu\Bundle\AdminBundle\Metadata\FormMetadata\SectionMetadata;
19+
use Sulu\Bundle\ContentBundle\Content\Application\ContentObjects\ContentView;
20+
use Sulu\Bundle\ContentBundle\Content\Application\PropertyResolver\PropertyResolverProvider;
21+
22+
class MetadataResolver
23+
{
24+
public function __construct(
25+
private PropertyResolverProvider $propertyResolverProvider
26+
) {
27+
}
28+
29+
/**
30+
* @param ItemMetadata[] $items
31+
*
32+
* @return ContentView[]
33+
*/
34+
public function resolveItems(array $items, array $data, string $locale): array
35+
{
36+
$contentViews = [];
37+
foreach ($items as $item) {
38+
$name = $item->getName();
39+
$type = $item->getType();
40+
if ($item instanceof SectionMetadata || $item instanceof FormMetadata) {
41+
$contentViews = \array_merge(
42+
$contentViews,
43+
$this->resolveItems($item->getItems(), $data, $locale)
44+
);
45+
} else {
46+
$contentViews[$name] = $this->resolveProperty($type, $data[$name], $locale, ['metadata' => $item]);
47+
}
48+
}
49+
50+
return $contentViews;
51+
}
52+
53+
private function resolveProperty(string $type, mixed $data, string $locale, array $params = []): ContentView
54+
{
55+
$propertyResolver = $this->propertyResolverProvider->getPropertyResolver($type);
56+
57+
return $propertyResolver->resolve($data, $locale, $params);
58+
}
59+
}

0 commit comments

Comments
 (0)