From 8e11dc1600f0b1cef3b582bc9953c509550cc95a Mon Sep 17 00:00:00 2001 From: Nuno Maduro Date: Mon, 10 Jul 2023 17:22:21 +0100 Subject: [PATCH] Adds new API for `middleware` and `withTrashed` --- README.md | 8 ++--- functions.php | 33 ++++++++++++------- src/Exceptions/MetadataIntercepted.php | 16 --------- src/InlineMetadataInterceptor.php | 16 +++++---- src/Metadata.php | 23 ++++++++++++- src/Options/PageOptions.php | 25 ++++++++++++++ .../detail.blade.php | 4 +-- ....Tests.Feature.Fixtures.Podcast].blade.php | 7 ++-- .../views/pages/flights/index.blade.php | 8 +++-- 9 files changed, 94 insertions(+), 46 deletions(-) delete mode 100644 src/Exceptions/MetadataIntercepted.php create mode 100644 src/Options/PageOptions.php diff --git a/README.md b/README.md index 2b0bde6..ae981bc 100644 --- a/README.md +++ b/README.md @@ -164,9 +164,9 @@ By default, models that have been soft deleted are not retrieved when resolving ```php @@ -183,9 +183,9 @@ You can apply middleware to a specific page by invoking the `page` function with ```php diff --git a/functions.php b/functions.php index 2f08180..b26dd85 100644 --- a/functions.php +++ b/functions.php @@ -3,18 +3,29 @@ namespace Laravel\Folio; use Closure; -use Illuminate\Container\Container; use Illuminate\Support\Arr; -use Laravel\Folio\Exceptions\MetadataIntercepted; +use Laravel\Folio\Options\PageOptions; -function page(Closure|string|array $middleware = [], bool $withTrashed = false) +/** + * Adds one or more middleware to the current page. + */ +function middleware(Closure|string|array $middleware = []): PageOptions { - if (Container::getInstance()->make(InlineMetadataInterceptor::class)->listening()) { - throw new MetadataIntercepted( - new Metadata( - middleware: collect(Arr::wrap($middleware)), - withTrashed: $withTrashed, - ) - ); - } + app(InlineMetadataInterceptor::class)->whenListening( + fn () => Metadata::instance()->middleware = Metadata::instance()->middleware->merge(Arr::wrap($middleware)), + ); + + return new PageOptions; +} + +/** + * Indicates that the current page should include trashed models. + */ +function withTrashed(bool $withTrashed = true): PageOptions +{ + app(InlineMetadataInterceptor::class)->whenListening( + fn () => Metadata::instance()->withTrashed = $withTrashed, + ); + + return new PageOptions; } diff --git a/src/Exceptions/MetadataIntercepted.php b/src/Exceptions/MetadataIntercepted.php deleted file mode 100644 index 3d4e5e3..0000000 --- a/src/Exceptions/MetadataIntercepted.php +++ /dev/null @@ -1,16 +0,0 @@ -cache[$matchedView->path] = $e->metadata; } finally { ob_get_clean(); + + $metadata = tap(Metadata::instance(), fn() => Metadata::flush()); } - return $this->cache[$matchedView->path] = new Metadata; + return $this->cache[$matchedView->path] = $metadata; } /** @@ -57,6 +56,7 @@ public function listen(callable $callback): void { $this->listening = true; + try { $callback(); } finally { @@ -65,10 +65,12 @@ public function listen(callable $callback): void } /** - * Determine if the interceptor is listening for metadata. + * Execute the callback if the interceptor is listening for metadata. */ - public function listening(): bool + public function whenListening(callable $callback): void { - return $this->listening; + if ($this->listening) { + $callback(); + } } } diff --git a/src/Metadata.php b/src/Metadata.php index fe1ed79..c48a50b 100644 --- a/src/Metadata.php +++ b/src/Metadata.php @@ -6,13 +6,34 @@ class Metadata { + /** + * The current global instance of the metadata, if any. + */ + protected static ?self $instance = null; + /** * Create a new metadata instance. */ - public function __construct( + protected function __construct( public Collection $middleware = new Collection, public bool $withTrashed = false ) { // } + + /** + * Get the current compile context instance or create a new one. + */ + public static function instance(): static + { + return static::$instance ??= new static; + } + + /** + * Flush the current global instance of the compile context. + */ + public static function flush(): void + { + static::$instance = null; + } } diff --git a/src/Options/PageOptions.php b/src/Options/PageOptions.php new file mode 100644 index 0000000..8ec9427 --- /dev/null +++ b/src/Options/PageOptions.php @@ -0,0 +1,25 @@ + diff --git a/tests/Feature/resources/views/pages/deleted-podcasts/[.Tests.Feature.Fixtures.Podcast].blade.php b/tests/Feature/resources/views/pages/deleted-podcasts/[.Tests.Feature.Fixtures.Podcast].blade.php index a5854de..16d789e 100644 --- a/tests/Feature/resources/views/pages/deleted-podcasts/[.Tests.Feature.Fixtures.Podcast].blade.php +++ b/tests/Feature/resources/views/pages/deleted-podcasts/[.Tests.Feature.Fixtures.Podcast].blade.php @@ -1,8 +1,11 @@ +
{{ $podcast->name }}
diff --git a/tests/Feature/resources/views/pages/flights/index.blade.php b/tests/Feature/resources/views/pages/flights/index.blade.php index 4fd2ae2..c2238e8 100644 --- a/tests/Feature/resources/views/pages/flights/index.blade.php +++ b/tests/Feature/resources/views/pages/flights/index.blade.php @@ -1,11 +1,13 @@