1 | <?php |
||
2 | |||
3 | namespace Khusseini\PimcoreRadBrickBundle; |
||
4 | |||
5 | use Pimcore\Model\Document\Tag; |
||
6 | use Pimcore\Model\Document\Tag\Area\Info; |
||
7 | use Pimcore\Templating\Renderer\TagRenderer; |
||
8 | |||
9 | class AreabrickRenderer |
||
10 | { |
||
11 | /** |
||
12 | * @var AreabrickConfigurator |
||
13 | */ |
||
14 | private $configurator; |
||
15 | |||
16 | /** |
||
17 | * @var TagRenderer |
||
18 | */ |
||
19 | private $tagRenderer; |
||
20 | |||
21 | 5 | public function __construct( |
|
22 | AreabrickConfigurator $areabrickConfigurator, |
||
23 | TagRenderer $tagRenderer |
||
24 | ) { |
||
25 | 5 | $this->configurator = $areabrickConfigurator; |
|
26 | 5 | $this->tagRenderer = $tagRenderer; |
|
27 | 5 | } |
|
28 | |||
29 | 5 | public function getConfigurator(): AreabrickConfigurator |
|
30 | { |
||
31 | 5 | return $this->configurator; |
|
32 | } |
||
33 | |||
34 | 3 | public function render(string $name, Info $info) |
|
35 | { |
||
36 | 3 | $configurator = $this->getConfigurator(); |
|
37 | 3 | if (!$configurator->hasAreabrickConfig($name)) { |
|
38 | 1 | throw new \InvalidArgumentException(sprintf('Areabrick \'%s\' has no configuration.', $name)); |
|
39 | } |
||
40 | |||
41 | 2 | $context = new Context($info->getView(), $info->getRequest()); |
|
42 | |||
43 | $renderArguments = $this |
||
44 | 2 | ->getConfigurator() |
|
45 | 2 | ->compileAreaBrick($name, $context); |
|
46 | |||
47 | 2 | $this->processRenderArguments( |
|
48 | 2 | $info, |
|
49 | $renderArguments |
||
50 | ); |
||
51 | 2 | } |
|
52 | |||
53 | /** |
||
54 | * @param Iterator<RenderArgument> $renderArguments |
||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
55 | */ |
||
56 | 2 | public function processRenderArguments( |
|
57 | Info $info, |
||
58 | \Iterator $renderArguments, |
||
59 | \ArrayAccess $container = null, |
||
60 | \ArrayAccess $referencesContainer = null, |
||
61 | string $parentName = '' |
||
62 | ): void { |
||
63 | 2 | if (!$container) { |
|
64 | 2 | $container = $info->getView(); |
|
65 | } |
||
66 | |||
67 | 2 | if (!$referencesContainer) { |
|
68 | 2 | $referencesContainer = new \ArrayObject(); |
|
69 | } |
||
70 | |||
71 | 2 | foreach ($renderArguments as $name => $renderArgument) { |
|
72 | 2 | $referenceId = $parentName ? $parentName.'_'.$name : $name; |
|
73 | |||
74 | 2 | if ('null' === $renderArgument->getType()) { |
|
75 | 1 | continue; |
|
76 | } |
||
77 | |||
78 | 2 | if ('collection' === $renderArgument->getType()) { |
|
79 | 1 | $tag = new \ArrayObject(); |
|
80 | 1 | $this->processRenderArguments( |
|
81 | 1 | $info, |
|
82 | 1 | new \ArrayIterator($renderArgument->getValue()), |
|
83 | $tag, |
||
84 | $referencesContainer, |
||
85 | $referenceId |
||
86 | ); |
||
87 | 1 | $tag = (array) $tag; |
|
88 | 2 | } elseif ('editable' === $renderArgument->getType()) { |
|
89 | 2 | $tag = $this->renderArgument($info, $renderArgument, $referenceId); |
|
90 | 1 | } elseif ('reference' === $renderArgument->getType()) { |
|
91 | 1 | $reference = $renderArgument->getValue(); |
|
92 | 1 | $tag = $referencesContainer[$reference]; |
|
93 | } else { |
||
94 | 1 | $tag = $renderArgument->getValue(); |
|
95 | } |
||
96 | |||
97 | 2 | $referencesContainer[$referenceId] = $tag; |
|
98 | 2 | $container[$name] = $tag; |
|
99 | } |
||
100 | 2 | } |
|
101 | |||
102 | 2 | protected function renderArgument(Info $info, RenderArgument $renderArgument, string $nameOverride = ''): ?Tag |
|
103 | { |
||
104 | 2 | $config = $renderArgument->getValue(); |
|
105 | |||
106 | 2 | if (!\is_array($config) |
|
107 | 2 | || !isset($config['type']) |
|
108 | ) { |
||
109 | 1 | return null; |
|
110 | } |
||
111 | |||
112 | 2 | if (!isset($config['options'])) { |
|
113 | 1 | $config['options'] = []; |
|
114 | } |
||
115 | |||
116 | return $this |
||
0 ignored issues
–
show
|
|||
117 | 2 | ->tagRenderer |
|
118 | 2 | ->render( |
|
119 | 2 | $info->getDocument(), |
|
120 | 2 | $config['type'], |
|
121 | 2 | $nameOverride ?: $renderArgument->getName(), |
|
122 | 2 | $config['options'] |
|
123 | ); |
||
124 | } |
||
125 | |||
126 | 3 | public function getAreabrickConfig(string $name): array |
|
127 | { |
||
128 | 3 | $configurator = $this->getConfigurator(); |
|
129 | 3 | $configurator->resolveAreaBrickConfig($name); |
|
130 | |||
131 | 3 | return $configurator->getAreabrickConfig($name); |
|
132 | } |
||
133 | } |
||
134 |