Skip to content

Commit 3592e6d

Browse files
committed
minor #2220 [TwigComponent] Fix internal variables are dispatched (smnandre)
This PR was merged into the 2.x branch. Discussion ---------- [TwigComponent] Fix internal variables are dispatched The key/values this/computed/outerScope/__props/__context are internal and should not have been exposed in Event * it does not match the documentation * it causes non needed memory usage / log * modifying them would break the entire rendering process (i'll make another PR soon to simplify & clarify `__props`, `__context`, and `props`) Commits ------- 589e802 [TwigComponent] Fix internal variables are dispatched
2 parents ec9b380 + 589e802 commit 3592e6d

File tree

1 file changed

+19
-20
lines changed

1 file changed

+19
-20
lines changed

src/TwigComponent/src/ComponentRenderer.php

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -112,29 +112,28 @@ private function preRender(MountedComponent $mounted, array $context = []): PreR
112112
$classProps = $isAnonymous ? [] : iterator_to_array($this->exposedVariables($component, $metadata->isPublicPropsExposed()));
113113

114114
// expose public properties and properties marked with ExposeInTemplate attribute
115-
$props = array_merge($mounted->getInputProps(), $classProps);
116-
$variables = array_merge(
117-
// first so values can be overridden
118-
$context,
115+
$props = [...$mounted->getInputProps(), ...$classProps];
116+
$event = new PreRenderEvent($mounted, $metadata, [
117+
...$context,
118+
...$props,
119+
$metadata->getAttributesVar() => $mounted->getAttributes(),
120+
]);
121+
122+
$this->dispatcher->dispatch($event);
123+
124+
$event->setVariables([
125+
...$event->getVariables(),
126+
// add the component as "this"
127+
'this' => $component,
128+
'computed' => new ComputedPropertiesProxy($component),
129+
'outerScope' => $context,
130+
// keep this line for BC break reasons
131+
'__props' => $classProps,
119132
// add the context in a separate variable to keep track
120133
// of what is coming from outside the component, excluding props
121134
// as they override initial context values
122-
['__context' => array_diff_key($context, $props)],
123-
// keep reference to old context
124-
['outerScope' => $context],
125-
// add the component as "this"
126-
['this' => $component],
127-
// add computed properties proxy
128-
['computed' => new ComputedPropertiesProxy($component)],
129-
$props,
130-
// keep this line for BC break reasons
131-
['__props' => $classProps],
132-
// add attributes
133-
[$metadata->getAttributesVar() => $mounted->getAttributes()],
134-
);
135-
$event = new PreRenderEvent($mounted, $metadata, $variables);
136-
137-
$this->dispatcher->dispatch($event);
135+
'__context' => array_diff_key($context, $props),
136+
]);
138137

139138
return $event;
140139
}

0 commit comments

Comments
 (0)