Skip to content

Commit 6db02ad

Browse files
committed
Use outerScope variable instead of Hierarchy object
1 parent 62810c5 commit 6db02ad

File tree

8 files changed

+12
-207
lines changed

8 files changed

+12
-207
lines changed

src/TwigComponent/src/ComponentRenderer.php

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -116,21 +116,16 @@ private function preRender(MountedComponent $mounted, array $context = []): PreR
116116
}
117117

118118
$component = $mounted->getComponent();
119-
120-
// add the "parent" component when rendering a nested embedded component
121-
if (isset($context[PreRenderEvent::EMBEDDED]) && true === $context[PreRenderEvent::EMBEDDED] && isset($context['this'])) {
122-
$hierarchy = $context['this'] instanceof Hierarchy ? $context['this'] : new Hierarchy($context['this']);
123-
if (isset($context['this'])) {
124-
$hierarchy = $hierarchy->add($component);
125-
}
126-
}
127119
$metadata = $this->factory->metadataFor($mounted->getName());
128120
$variables = array_merge(
129121
// first so values can be overridden
130122
$context,
131123

124+
// keep reference to old context
125+
['outerScope' => $context],
126+
132127
// add the component as "this"
133-
['this' => $hierarchy ?? $component],
128+
['this' => $component],
134129

135130
// add computed properties proxy
136131
['computed' => new ComputedPropertiesProxy($component)],

src/TwigComponent/src/Hierarchy.php

Lines changed: 0 additions & 53 deletions
This file was deleted.

src/TwigComponent/tests/Fixtures/templates/components/DivComponent5.html.twig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
I can access my own properties: {{ divComponentName }}.
33
I can access the id of the Generic Element: {{ id }}.
44
This refers to the Generic Element: {{ this.someFunction }}.
5-
To access my own functions I can use this.parent: {{ this.parent.someFunction }}.
5+
To access my own functions I can use outerScope.this: {{ outerScope.this.someFunction }}.
66
I have access to outer context variables like {{ name }}.
77
{{ block(outerBlocks.content) }}
88
</twig:GenericElement>

src/TwigComponent/tests/Fixtures/templates/components/DivComponentWrapper3.html.twig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@
44
And of course the properties from DivComponentWrapper: {{ divComponentWrapperName }}.
55
The less obvious thing is that at this level "this" refers to the component where the content block is used, i.e. the Generic Element.
66
Therefore, functions through this will be {{ this.someFunction }}.
7-
Calls to this.parent will be {{ this.parent.someFunction }}.
7+
Calls to outerScope.this will be {{ outerScope.this.someFunction }}.
88
{{ block(outerBlocks.content) }}
99
</twig:DivComponent5>

src/TwigComponent/tests/Fixtures/templates/embedded_component_blocks_context.html.twig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
Even I can access the id from Generic Element as well: {{ id }}.
44
Even I can access the properties from DivComponent as well: {{ divComponentName }}.
55
Even I can access the properties from DivComponentWrapper as well: {{ divComponentWrapperName }}.
6-
Even I can access the functions of DivComponent via this.parent: {{ this.parent.someFunction }}.
7-
Since we are nesting two levels deep, calls to this.parent.parent will be {{ this.parent.parent.someFunction }}.
6+
Even I can access the functions of DivComponent via outerScope.this: {{ outerScope.this.someFunction }}.
7+
Since we are nesting two levels deep, calls to outerScope.outerScope.this will be {{ outerScope.outerScope.this.someFunction }}.
88
</twig:DivComponentWrapper3>
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
<twig:GenericElement element="div" class="divComponent">
2-
{{ this.parent.foo }}
2+
{{ outerScope.this.foo }}
33
</twig:GenericElement>

src/TwigComponent/tests/Integration/EmbeddedComponentTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,19 +140,19 @@ public function testPassingDownBlocksMultipleLevelsNeedsToBeDoneManually(): void
140140

141141
/**
142142
* Rule 12: Blocks defined within an embedded component can access the context of the block they are replacing.
143-
* Rule 13: Blocks defined within an embedded component can access the context of components up the hierarchy (up to their own level) via "this.parent".
143+
* Rule 13: Blocks defined within an embedded component can access the context of components up the hierarchy (up to their own level) via "outerScope".
144144
*/
145145
public function testBlockDefinitionCanAccessTheContextOfTheDestinationBlocks(): void
146146
{
147147
$this->assertStringContainsStringIgnoringIndentation(
148-
'<div class="divComponent">I can access my own properties: foo.I can access the id of the Generic Element: symfonyIsAwesome.This refers to the Generic Element: calling GenericElement.To access my own functions I can use this.parent: calling DivComponent.I have access to outer context variables like Fabien.I can access the id from Generic Element as well: symfonyIsAwesome.I can access the properties from DivComponent as well: foo.And of course the properties from DivComponentWrapper: bar.The less obvious thing is that at this level "this" refers to the component where the content block is used, i.e. the Generic Element.Therefore, functions through this will be calling GenericElement.Calls to this.parent will be calling DivComponent.Even I can access the id from Generic Element as well: symfonyIsAwesome.Even I can access the properties from DivComponent as well: foo.Even I can access the properties from DivComponentWrapper as well: bar.Even I can access the functions of DivComponent via this.parent: calling DivComponent.Since we are nesting two levels deep, calls to this.parent.parent will be calling DivComponentWrapper.<span class="foo">The Generic Element default foo block</span></div>',
148+
'<div class="divComponent">I can access my own properties: foo.I can access the id of the Generic Element: symfonyIsAwesome.This refers to the Generic Element: calling GenericElement.To access my own functions I can use outerScope.this: calling DivComponent.I have access to outer context variables like Fabien.I can access the id from Generic Element as well: symfonyIsAwesome.I can access the properties from DivComponent as well: foo.And of course the properties from DivComponentWrapper: bar.The less obvious thing is that at this level "this" refers to the component where the content block is used, i.e. the Generic Element.Therefore, functions through this will be calling GenericElement.Calls to outerScope.this will be calling DivComponent.Even I can access the id from Generic Element as well: symfonyIsAwesome.Even I can access the properties from DivComponent as well: foo.Even I can access the properties from DivComponentWrapper as well: bar.Even I can access the functions of DivComponent via outerScope.this: calling DivComponent.Since we are nesting two levels deep, calls to outerScope.outerScope.this will be calling DivComponentWrapper.<span class="foo">The Generic Element default foo block</span></div>',
149149
self::getContainer()->get(Environment::class)->render('embedded_component_blocks_context.html.twig')
150150
);
151151
}
152152

153153
public function testAccessingTheHierarchyTooHighThrowsAnException(): void
154154
{
155-
$this->expectExceptionMessage('Neither the property "parent" nor one of the methods "parent()", "getparent()"/"isparent()"/"hasparent()" or "__call()" exist');
155+
$this->expectExceptionMessage('Key "this" for array with keys "app, __embedded" does not exist.');
156156
self::getContainer()->get(Environment::class)->render('embedded_component_hierarchy_exception.html.twig');
157157
}
158158

src/TwigComponent/tests/Unit/HierarchyTest.php

Lines changed: 0 additions & 137 deletions
This file was deleted.

0 commit comments

Comments
 (0)