From 589e802b522670377753e0fc168a7c5f8df1de97 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Andr=C3=A9?= Date: Sat, 28 Sep 2024 03:37:17 +0200 Subject: [PATCH] [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 pass soon to simplify & clarify `__props`, `__context`, and `props`) --- src/TwigComponent/src/ComponentRenderer.php | 39 ++++++++++----------- 1 file changed, 19 insertions(+), 20 deletions(-) diff --git a/src/TwigComponent/src/ComponentRenderer.php b/src/TwigComponent/src/ComponentRenderer.php index cfe9f208541..3273ba5118f 100644 --- a/src/TwigComponent/src/ComponentRenderer.php +++ b/src/TwigComponent/src/ComponentRenderer.php @@ -112,29 +112,28 @@ private function preRender(MountedComponent $mounted, array $context = []): PreR $classProps = $isAnonymous ? [] : iterator_to_array($this->exposedVariables($component, $metadata->isPublicPropsExposed())); // expose public properties and properties marked with ExposeInTemplate attribute - $props = array_merge($mounted->getInputProps(), $classProps); - $variables = array_merge( - // first so values can be overridden - $context, + $props = [...$mounted->getInputProps(), ...$classProps]; + $event = new PreRenderEvent($mounted, $metadata, [ + ...$context, + ...$props, + $metadata->getAttributesVar() => $mounted->getAttributes(), + ]); + + $this->dispatcher->dispatch($event); + + $event->setVariables([ + ...$event->getVariables(), + // add the component as "this" + 'this' => $component, + 'computed' => new ComputedPropertiesProxy($component), + 'outerScope' => $context, + // keep this line for BC break reasons + '__props' => $classProps, // add the context in a separate variable to keep track // of what is coming from outside the component, excluding props // as they override initial context values - ['__context' => array_diff_key($context, $props)], - // keep reference to old context - ['outerScope' => $context], - // add the component as "this" - ['this' => $component], - // add computed properties proxy - ['computed' => new ComputedPropertiesProxy($component)], - $props, - // keep this line for BC break reasons - ['__props' => $classProps], - // add attributes - [$metadata->getAttributesVar() => $mounted->getAttributes()], - ); - $event = new PreRenderEvent($mounted, $metadata, $variables); - - $this->dispatcher->dispatch($event); + '__context' => array_diff_key($context, $props), + ]); return $event; }