-
-
Notifications
You must be signed in to change notification settings - Fork 546
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[5.x] Add blade
@cascade
directive (#10674)
- Loading branch information
1 parent
4e19a9b
commit 4f38029
Showing
4 changed files
with
133 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |