From ebe06dd21e1e867eaa9dbd2ba14c8bb061a82128 Mon Sep 17 00:00:00 2001 From: fsavina Date: Thu, 4 Jul 2019 15:55:33 +0200 Subject: [PATCH 1/2] Add @includeOnce directive --- src/DirectivesRepository.php | 24 ++++++++++++++++++++++++ src/directives.php | 10 ++++++++++ tests/DirectivesTest.php | 7 +++++++ 3 files changed, 41 insertions(+) diff --git a/src/DirectivesRepository.php b/src/DirectivesRepository.php index 8083762..206d02d 100644 --- a/src/DirectivesRepository.php +++ b/src/DirectivesRepository.php @@ -6,6 +6,11 @@ class DirectivesRepository { + /** + * @var array + */ + protected static $views_registry = []; + /** * Register the directives. * @@ -42,4 +47,23 @@ public static function stripQuotes($expression) { return str_replace(["'", '"'], '', $expression); } + + /** + * Check if a view has not been included yet. + * @param string $view + * + * @return bool + */ + public static function viewWasNotIncluded ( $view ) + { + return ! in_array ( $view, static::$views_registry ); + } + + /** + * @param string $view + */ + public static function addIncludedView ( $view ) + { + static::$views_registry[] = $view; + } } diff --git a/src/directives.php b/src/directives.php index e3fe454..97afedf 100644 --- a/src/directives.php +++ b/src/directives.php @@ -64,6 +64,16 @@ return ''; }, + /* + |--------------------------------------------------------------------- + | @includeOnce + |--------------------------------------------------------------------- + */ + + 'includeOnce' => function ($expression) { + return "make({$expression}, \Illuminate\Support\Arr::except(get_defined_vars(), array('__data', '__path')))->render();\Appstract\BladeDirectives\DirectivesRepository::addIncludedView({$expression});} ?>"; + }, + /* |--------------------------------------------------------------------- | @mix diff --git a/tests/DirectivesTest.php b/tests/DirectivesTest.php index c0e6b02..e44be6b 100644 --- a/tests/DirectivesTest.php +++ b/tests/DirectivesTest.php @@ -42,6 +42,13 @@ public function test_is_not_null() $this->assertBladeRenders('It is not null', $blade, ['variable' => 'not null']); } + public function test_include_once () + { + $blade = "@includeOnce('not-checkout')"; + $this->assertBladeRenders('Do something only if this is not the checkout', $blade); + $this->assertBladeRenders('', $blade); // The second time the view shouldn't be included + } + public function test_mix() { // See /tests/laravel/public/mix-manifest.json for CSS file name From 23f6dced522e6179327009123f36745407ddf641 Mon Sep 17 00:00:00 2001 From: fsavina Date: Thu, 4 Jul 2019 16:05:47 +0200 Subject: [PATCH 2/2] Fix StyleCI conflicts --- src/DirectivesRepository.php | 6 +++--- tests/DirectivesTest.php | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/DirectivesRepository.php b/src/DirectivesRepository.php index 206d02d..b2d7842 100644 --- a/src/DirectivesRepository.php +++ b/src/DirectivesRepository.php @@ -54,15 +54,15 @@ public static function stripQuotes($expression) * * @return bool */ - public static function viewWasNotIncluded ( $view ) + public static function viewWasNotIncluded($view) { - return ! in_array ( $view, static::$views_registry ); + return ! in_array($view, static::$views_registry); } /** * @param string $view */ - public static function addIncludedView ( $view ) + public static function addIncludedView($view) { static::$views_registry[] = $view; } diff --git a/tests/DirectivesTest.php b/tests/DirectivesTest.php index e44be6b..fe62f4c 100644 --- a/tests/DirectivesTest.php +++ b/tests/DirectivesTest.php @@ -42,7 +42,7 @@ public function test_is_not_null() $this->assertBladeRenders('It is not null', $blade, ['variable' => 'not null']); } - public function test_include_once () + public function test_include_once() { $blade = "@includeOnce('not-checkout')"; $this->assertBladeRenders('Do something only if this is not the checkout', $blade);