diff --git a/core/src/Core.php b/core/src/Core.php index 34e335a339..f28152a7f2 100644 --- a/core/src/Core.php +++ b/core/src/Core.php @@ -3105,13 +3105,15 @@ public function prepareResponse() ]; } - $this['view']->share($data); + $viewData = $this->getDataForView(); + + $this['view']->share(array_merge($data, $viewData)); if ($this->isChunkProcessor('DLTemplate')) { - app('DLTemplate')->blade->share($data); + app('DLTemplate')->blade->share(array_merge($data, $viewData)); } - $tpl = $this['view']->make($template, $this->dataForView); + $tpl = $this['view']->make($template, $viewData); $templateCode = $tpl->render(); } else { // get the template and start parsing! @@ -6639,10 +6641,18 @@ public function addLog($title = 'no title', $msg = '', $type = 1) /** * @param array $data + * @return $this */ public function addDataToView($data = []) { + if (!is_array($data)) { + return $this; + } + $this->dataForView = array_merge($this->dataForView, $data); + $this->shareDataForView($data); + + return $this; } public function getDataForView() @@ -6650,6 +6660,15 @@ public function getDataForView() return $this->dataForView; } + protected function shareDataForView(array $data): void + { + if ($data === [] || !$this->bound('view')) { + return; + } + + $this['view']->share($data); + } + /** * @param string $name * @param string $file diff --git a/core/tests/Unit/CoreAddDataToViewTest.php b/core/tests/Unit/CoreAddDataToViewTest.php new file mode 100644 index 0000000000..52a703a5c8 --- /dev/null +++ b/core/tests/Unit/CoreAddDataToViewTest.php @@ -0,0 +1,70 @@ +newInstanceWithoutConstructor(); +} + +function makeViewFactoryForViewDataTest(): object +{ + return new class { + public array $shared = []; + + public function share($key, $value = null) + { + if (is_array($key)) { + $this->shared = array_merge($this->shared, $key); + + return $key; + } + + $this->shared[$key] = $value; + + return $value; + } + }; +} + +describe('addDataToView', function () { + + test('it stores data and shares it with the view factory', function () { + $core = makeCoreForViewDataTest(); + $view = makeViewFactoryForViewDataTest(); + $core->instance('view', $view); + + $result = $core->addDataToView([ + 'headline' => 'Shared headline', + 'meta' => ['source' => 'unit-test'], + ]); + + expect($result)->toBe($core) + ->and($core->getDataForView())->toMatchArray([ + 'headline' => 'Shared headline', + 'meta' => ['source' => 'unit-test'], + ]) + ->and($view->shared)->toMatchArray([ + 'headline' => 'Shared headline', + 'meta' => ['source' => 'unit-test'], + ]); + }); + + test('it ignores non-array data without clearing existing view data', function () { + $core = makeCoreForViewDataTest(); + $view = makeViewFactoryForViewDataTest(); + $core->instance('view', $view); + + $core->addDataToView(['headline' => 'Shared headline']); + + $result = $core->addDataToView('not-an-array'); + + expect($result)->toBe($core) + ->and($core->getDataForView())->toMatchArray([ + 'headline' => 'Shared headline', + ]) + ->and($view->shared)->toMatchArray([ + 'headline' => 'Shared headline', + ]); + }); +});