|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace Shoot\Shoot\Twig\NodeVisitor; |
|
5
|
|
|
|
|
6
|
|
|
use Shoot\Shoot\ModelAlreadyAssignedException; |
|
7
|
|
|
use Shoot\Shoot\PresentationModel; |
|
8
|
|
|
use Shoot\Shoot\Twig\Node\DisplayEndNode; |
|
9
|
|
|
use Shoot\Shoot\Twig\Node\DisplayStartNode; |
|
10
|
|
|
use Shoot\Shoot\Twig\Node\FindPresentationModelInterface; |
|
11
|
|
|
use Shoot\Shoot\Twig\Node\ModelNode; |
|
12
|
|
|
use SplObjectStorage; |
|
13
|
|
|
use Twig\Environment; |
|
14
|
|
|
use Twig\Node\ModuleNode; |
|
15
|
|
|
use Twig\Node\Node; |
|
16
|
|
|
use Twig\NodeVisitor\NodeVisitorInterface; |
|
17
|
|
|
use Twig\Source; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Walks through model tags to assign presentation models to templates. |
|
21
|
|
|
* |
|
22
|
|
|
* @internal |
|
23
|
|
|
*/ |
|
24
|
|
|
final class ModelNodeVisitor implements FindPresentationModelInterface, NodeVisitorInterface |
|
25
|
|
|
{ |
|
26
|
|
|
/** @var SplObjectStorage */ |
|
27
|
|
|
private $presentationModels; |
|
28
|
|
|
|
|
29
|
7 |
|
public function __construct() |
|
30
|
|
|
{ |
|
31
|
7 |
|
$this->presentationModels = new SplObjectStorage(); |
|
32
|
7 |
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @param Node $node |
|
36
|
|
|
* @param Environment $environment |
|
37
|
|
|
* |
|
38
|
|
|
* @return Node |
|
39
|
|
|
*/ |
|
40
|
7 |
|
public function enterNode(Node $node, Environment $environment): Node |
|
41
|
|
|
{ |
|
42
|
7 |
|
if ($node instanceof ModelNode) { |
|
43
|
7 |
|
$this->assign($node->getSourceContext(), $node->getAttribute('presentation_model')); |
|
|
|
|
|
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
7 |
|
return $node; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @param Node $node |
|
51
|
|
|
* @param Environment $environment |
|
52
|
|
|
* |
|
53
|
|
|
* @return Node |
|
54
|
|
|
*/ |
|
55
|
7 |
|
public function leaveNode(Node $node, Environment $environment): Node |
|
56
|
|
|
{ |
|
57
|
7 |
|
if (!($node instanceof ModuleNode)) { |
|
58
|
7 |
|
return $node; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
6 |
|
if ($node->hasAttribute('embedded_templates')) { |
|
62
|
|
|
/** @var ModuleNode $embeddedTemplate */ |
|
63
|
6 |
|
foreach ($node->getAttribute('embedded_templates') as $embeddedTemplate) { |
|
64
|
1 |
|
$embeddedTemplate->setAttribute('is_embedded', true); |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
6 |
|
$node->setNode('display_start', new DisplayStartNode($node, $this)); |
|
69
|
6 |
|
$node->setNode('display_end', new DisplayEndNode($node)); |
|
70
|
|
|
|
|
71
|
6 |
|
return $node; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Returns the presentation model for the given view. |
|
76
|
|
|
* |
|
77
|
|
|
* @param Source $source |
|
78
|
|
|
* |
|
79
|
|
|
* @return string |
|
80
|
|
|
*/ |
|
81
|
6 |
|
public function for(Source $source): string |
|
82
|
|
|
{ |
|
83
|
6 |
|
if (isset($this->presentationModels[$source])) { |
|
84
|
6 |
|
return (string)$this->presentationModels[$source]; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
1 |
|
return PresentationModel::class; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @return int |
|
92
|
|
|
*/ |
|
93
|
7 |
|
public function getPriority(): int |
|
94
|
|
|
{ |
|
95
|
|
|
// Should be between -10 and 10, with 0 as the default |
|
96
|
7 |
|
return -10; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @param Source $source |
|
101
|
|
|
* @param string $presentationModel |
|
102
|
|
|
* |
|
103
|
|
|
* @return void |
|
104
|
|
|
* |
|
105
|
|
|
* @throws ModelAlreadyAssignedException |
|
106
|
|
|
*/ |
|
107
|
7 |
|
private function assign(Source $source, string $presentationModel): void |
|
108
|
|
|
{ |
|
109
|
7 |
|
if (isset($this->presentationModels[$source])) { |
|
110
|
1 |
|
throw new ModelAlreadyAssignedException("A presentation model has already been assigned to {$source->getName()}"); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
7 |
|
$this->presentationModels[$source] = $presentationModel; |
|
114
|
7 |
|
} |
|
115
|
|
|
} |
|
116
|
|
|
|