Skip to content

Commit

Permalink
[5.x] Add blade @cascade directive (#10674)
Browse files Browse the repository at this point in the history
  • Loading branch information
jacksleight authored Aug 21, 2024
1 parent 4e19a9b commit 4f38029
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/Exceptions/CascadeDataNotFoundException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Statamic\Exceptions;

class CascadeDataNotFoundException extends \Exception
{
protected $key;

public function __construct($key)
{
parent::__construct("Cascade data [{$key}] not found");

$this->key = $key;
}
}
3 changes: 3 additions & 0 deletions src/Providers/ViewServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,9 @@ public function registerBladeDirectives()
Blade::directive('tags', function ($expression) {
return "<?php extract(\Statamic\View\Blade\TagsDirective::handle($expression)) ?>";
});
Blade::directive('cascade', function ($expression) {
return "<?php extract(\Statamic\View\Blade\CascadeDirective::handle($expression)) ?>";
});
}

public function boot()
Expand Down
35 changes: 35 additions & 0 deletions src/View/Blade/CascadeDirective.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Statamic\View\Blade;

use Illuminate\Support\Arr;
use Statamic\Exceptions\CascadeDataNotFoundException;
use Statamic\Facades\Cascade;

class CascadeDirective
{
public static function handle($keys = null): array
{
if (! $data = Cascade::toArray()) {
$data = Cascade::hydrate()->toArray();
}

if (! isset($keys)) {
return $data;
}

return collect($keys)
->mapWithKeys(function ($default, $key) use ($data) {
if (is_numeric($key)) {
$key = $default;
$default = null;
if (! array_key_exists($key, $data)) {
throw new CascadeDataNotFoundException($key);
}
}

return [$key => Arr::get($data, $key, $default)];
})
->all();
}
}
80 changes: 80 additions & 0 deletions tests/View/Blade/CascadeDirectiveTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

namespace Tests\View\Blade;

use PHPUnit\Framework\Attributes\Test;
use Statamic\Exceptions\CascadeDataNotFoundException;
use Statamic\Facades\Cascade;
use Statamic\View\Blade\CascadeDirective;
use Tests\TestCase;

class CascadeDirectiveTest extends TestCase
{
public function setUp(): void
{
parent::setUp();
}

#[Test]
public function it_gets_all_data()
{
$data = CascadeDirective::handle();

$expected = Cascade::toArray();

$this->assertEquals($expected, $data);
}

#[Test]
public function it_gets_no_data()
{
$data = CascadeDirective::handle([]);

$this->assertEmpty($data);
}

#[Test]
public function it_gets_specific_data()
{
$data = CascadeDirective::handle([
'homepage',
'is_homepage',
]);

$expected = Cascade::toArray();

$this->assertCount(2, $data);
$this->assertArrayHasKey('homepage', $data);
$this->assertArrayHasKey('is_homepage', $data);
$this->assertEquals($data['homepage'], $expected['homepage']);
$this->assertEquals($data['is_homepage'], $expected['is_homepage']);
}

#[Test]
public function it_throws_exception_for_missing_data()
{
$this->expectException(CascadeDataNotFoundException::class);
$this->expectExceptionMessage('Cascade data [live_preview] not found');

CascadeDirective::handle([
'live_preview',
]);
}

#[Test]
public function it_uses_fallback_for_missing_data()
{
$data = CascadeDirective::handle([
'homepage',
'live_preview' => false,
]);

$expected = Cascade::toArray();

$this->assertCount(2, $data);
$this->assertArrayHasKey('homepage', $data);
$this->assertArrayHasKey('live_preview', $data);
$this->assertEquals($data['homepage'], $expected['homepage']);
$this->assertEquals($data['live_preview'], false);
}
}

0 comments on commit 4f38029

Please sign in to comment.