diff --git a/src/Illuminate/View/Compilers/Concerns/CompilesComponents.php b/src/Illuminate/View/Compilers/Concerns/CompilesComponents.php index 41f034c05048..4f1fde48bd3d 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->getConsumableComponentData(\$__key, \$__value) : \$__env->getConsumableComponentData(\$__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..15bc0efa408b 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 getConsumableComponentData($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. *