From 7962485d83f0c3542c518f1cc860f7ca12479947 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Mon, 4 Oct 2021 15:16:10 -0500 Subject: [PATCH 1/2] initial pass at consume directive --- .../Compilers/Concerns/CompilesComponents.php | 14 ++++++++++ .../View/Concerns/ManagesComponents.php | 26 +++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/src/Illuminate/View/Compilers/Concerns/CompilesComponents.php b/src/Illuminate/View/Compilers/Concerns/CompilesComponents.php index 41f034c05048..82b53e19f72a 100644 --- a/src/Illuminate/View/Compilers/Concerns/CompilesComponents.php +++ b/src/Illuminate/View/Compilers/Concerns/CompilesComponents.php @@ -161,6 +161,20 @@ protected function compileProps($expression) "; } + /** + * Compile the consume statement into valid PHP. + * + * @param string $expression + * @return string + */ + protected function compileConsume($expression) + { + return " \$__value) { + \$__consumeVariable = is_string(\$__key) ? \$__key : \$__value; + \$\$__consumeVariable = is_string(\$__key) ? \$__env->componentDataItem(\$__key, \$__value) : \$__env->componentDataItem(\$__value); +} ?>"; + } + /** * Sanitize the given component attribute value. * diff --git a/src/Illuminate/View/Concerns/ManagesComponents.php b/src/Illuminate/View/Concerns/ManagesComponents.php index 5e8bf68ce6e8..4881c8844e65 100644 --- a/src/Illuminate/View/Concerns/ManagesComponents.php +++ b/src/Illuminate/View/Concerns/ManagesComponents.php @@ -115,6 +115,32 @@ protected function componentData() ); } + /** + * Get an item from the component data that exists above the current component. + * + * @param string $key + * @param mixed $default + * @return mixed|null + */ + public function componentDataItem($key, $default = null) + { + $currentComponent = count($this->componentStack); + + if ($currentComponent === 0) { + return value($default); + } + + for ($i = $currentComponent - 1; $i >= 0; $i--) { + $data = $this->componentData[$i] ?? []; + + if (array_key_exists($key, $data)) { + return $data[$key]; + } + } + + return value($default); + } + /** * Start the slot rendering process. * From d67498571f78eabfccee64f9ce34c8d9348d6d39 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Mon, 4 Oct 2021 16:13:13 -0500 Subject: [PATCH 2/2] rename method --- src/Illuminate/View/Compilers/Concerns/CompilesComponents.php | 2 +- src/Illuminate/View/Concerns/ManagesComponents.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Illuminate/View/Compilers/Concerns/CompilesComponents.php b/src/Illuminate/View/Compilers/Concerns/CompilesComponents.php index 82b53e19f72a..4f1fde48bd3d 100644 --- a/src/Illuminate/View/Compilers/Concerns/CompilesComponents.php +++ b/src/Illuminate/View/Compilers/Concerns/CompilesComponents.php @@ -171,7 +171,7 @@ protected function compileConsume($expression) { return " \$__value) { \$__consumeVariable = is_string(\$__key) ? \$__key : \$__value; - \$\$__consumeVariable = is_string(\$__key) ? \$__env->componentDataItem(\$__key, \$__value) : \$__env->componentDataItem(\$__value); + \$\$__consumeVariable = is_string(\$__key) ? \$__env->getConsumableComponentData(\$__key, \$__value) : \$__env->getConsumableComponentData(\$__value); } ?>"; } diff --git a/src/Illuminate/View/Concerns/ManagesComponents.php b/src/Illuminate/View/Concerns/ManagesComponents.php index 4881c8844e65..15bc0efa408b 100644 --- a/src/Illuminate/View/Concerns/ManagesComponents.php +++ b/src/Illuminate/View/Concerns/ManagesComponents.php @@ -122,7 +122,7 @@ protected function componentData() * @param mixed $default * @return mixed|null */ - public function componentDataItem($key, $default = null) + public function getConsumableComponentData($key, $default = null) { $currentComponent = count($this->componentStack);