Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 22 additions & 3 deletions core/src/Core.php
Original file line number Diff line number Diff line change
Expand Up @@ -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!
Expand Down Expand Up @@ -6639,17 +6641,34 @@ 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()
{
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
Expand Down
70 changes: 70 additions & 0 deletions core/tests/Unit/CoreAddDataToViewTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

use EvolutionCMS\Core;

function makeCoreForViewDataTest(): Core
{
return (new ReflectionClass(Core::class))->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',
]);
});
});