From 1a59c60ae56cc2a78e9ddac34ab133c33157e181 Mon Sep 17 00:00:00 2001 From: Sylvester Damgaard Date: Wed, 21 Feb 2024 10:46:05 +0100 Subject: [PATCH 1/6] Make tags with old __call methods macroable by using wildcard method instead. Add events to mutate the collections tag before and after fetching entries. --- src/Events/CollectionTagFetchedEntries.php | 18 ++++++++++++++++++ src/Events/CollectionTagFetchingEntries.php | 15 +++++++++++++++ src/Tags/Asset.php | 2 +- src/Tags/Assets.php | 2 +- src/Tags/Collection/Collection.php | 13 ++++++++----- src/Tags/Glide.php | 2 +- src/Tags/ParentTags.php | 2 +- src/Tags/Scope.php | 2 +- src/Tags/Section.php | 2 +- src/Tags/Tags.php | 2 +- src/Tags/Theme.php | 2 +- src/Tags/Yields.php | 2 +- 12 files changed, 50 insertions(+), 14 deletions(-) create mode 100644 src/Events/CollectionTagFetchedEntries.php create mode 100644 src/Events/CollectionTagFetchingEntries.php diff --git a/src/Events/CollectionTagFetchedEntries.php b/src/Events/CollectionTagFetchedEntries.php new file mode 100644 index 0000000000..821ae4521c --- /dev/null +++ b/src/Events/CollectionTagFetchedEntries.php @@ -0,0 +1,18 @@ +entries = $entries; + $this->tag = $tag; + } +} diff --git a/src/Events/CollectionTagFetchingEntries.php b/src/Events/CollectionTagFetchingEntries.php new file mode 100644 index 0000000000..2682a99f88 --- /dev/null +++ b/src/Events/CollectionTagFetchingEntries.php @@ -0,0 +1,15 @@ +tag = $tag; + } +} diff --git a/src/Tags/Asset.php b/src/Tags/Asset.php index 0d346f976f..bb6f98e64c 100644 --- a/src/Tags/Asset.php +++ b/src/Tags/Asset.php @@ -21,7 +21,7 @@ class Asset extends Assets * * @return string */ - public function __call($method, $arguments) + public function wildcard($method) { $value = Arr::get($this->context, $this->method); $value = (array) $value; diff --git a/src/Tags/Assets.php b/src/Tags/Assets.php index b1d3c396b6..9ef77dcc8b 100644 --- a/src/Tags/Assets.php +++ b/src/Tags/Assets.php @@ -27,7 +27,7 @@ class Assets extends Tags * * @return string */ - public function __call($method, $arguments) + public function wildcard($method) { $value = Arr::get($this->context, $this->method); diff --git a/src/Tags/Collection/Collection.php b/src/Tags/Collection/Collection.php index dc3b316c60..8a2142269b 100644 --- a/src/Tags/Collection/Collection.php +++ b/src/Tags/Collection/Collection.php @@ -2,6 +2,8 @@ namespace Statamic\Tags\Collection; +use Statamic\Events\CollectionTagFetchedEntries; +use Statamic\Events\CollectionTagFetchingEntries; use Statamic\Facades\Entry; use Statamic\Tags\Concerns; use Statamic\Tags\Tags; @@ -15,13 +17,11 @@ class Collection extends Tags /** * {{ collection:* }} ... {{ /collection:* }}. */ - public function __call($method, $args) + public function wildcard($method) { $this->params['from'] = $this->method; - return $this->output( - $this->entries()->get() - ); + return $this->index(); } /** @@ -33,8 +33,10 @@ public function index() return $this->context->value('collection'); } + $entries = $this->entries()->get(); + CollectionTagFetchedEntries::dispatch($entries, $this); return $this->output( - $this->entries()->get() + $entries ); } @@ -96,6 +98,7 @@ public function newer() protected function entries() { + CollectionTagFetchingEntries::dispatch($this); return new Entries($this->params); } diff --git a/src/Tags/Glide.php b/src/Tags/Glide.php index bfd584c92a..1f89c2ea49 100644 --- a/src/Tags/Glide.php +++ b/src/Tags/Glide.php @@ -28,7 +28,7 @@ class Glide extends Tags * @param array $args * @return string */ - public function __call($method, $args) + public function wildcard($method) { $tag = explode(':', $this->tag, 2)[1]; diff --git a/src/Tags/ParentTags.php b/src/Tags/ParentTags.php index 640622803a..9a53a1ebd5 100644 --- a/src/Tags/ParentTags.php +++ b/src/Tags/ParentTags.php @@ -22,7 +22,7 @@ class ParentTags extends Tags * @param array $args * @return mixed */ - public function __call($method, $args) + public function wildcard($method) { $var_name = Stringy::removeLeft($this->tag, 'parent:'); diff --git a/src/Tags/Scope.php b/src/Tags/Scope.php index 1bf769b23d..54edc29879 100644 --- a/src/Tags/Scope.php +++ b/src/Tags/Scope.php @@ -6,7 +6,7 @@ class Scope extends Tags { - public function __call($method, $args) + public function wildcard($method) { throw_unless($this->isPair, new \Exception('Scope tag must be a pair')); diff --git a/src/Tags/Section.php b/src/Tags/Section.php index fc596799a7..1051ae8aa6 100644 --- a/src/Tags/Section.php +++ b/src/Tags/Section.php @@ -6,7 +6,7 @@ class Section extends Tags { - public function __call($method, $args) + public function wildcard($method) { $name = explode(':', $this->tag)[1]; diff --git a/src/Tags/Tags.php b/src/Tags/Tags.php index ac43708882..4c873ab2fe 100644 --- a/src/Tags/Tags.php +++ b/src/Tags/Tags.php @@ -137,7 +137,7 @@ public function __call($method, $args) if (static::hasMacro($method)) { $macro = static::$macros[$method]; - if ($macro instanceof Closure) { + if ($macro instanceof \Closure) { $macro = $macro->bindTo($this, static::class); } diff --git a/src/Tags/Theme.php b/src/Tags/Theme.php index b7c84999a9..808bc101ed 100644 --- a/src/Tags/Theme.php +++ b/src/Tags/Theme.php @@ -20,7 +20,7 @@ class Theme extends Tags * @param array $arguments Unused * @return string */ - public function __call($method, $arguments) + public function wildcard($method) { return $this->path($method); } diff --git a/src/Tags/Yields.php b/src/Tags/Yields.php index dd7445dee9..063fe12d67 100644 --- a/src/Tags/Yields.php +++ b/src/Tags/Yields.php @@ -8,7 +8,7 @@ class Yields extends Tags { protected static $aliases = ['yield']; - public function __call($method, $args) + public function wildcard($method) { $name = explode(':', $this->tag)[1]; From 28b7c3ca9d3d6619c17378c7d2e3c1d73b339863 Mon Sep 17 00:00:00 2001 From: Sylvester Damgaard Date: Wed, 21 Feb 2024 10:54:25 +0100 Subject: [PATCH 2/6] Fix issue with type hinting --- src/Events/CollectionTagFetchedEntries.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Events/CollectionTagFetchedEntries.php b/src/Events/CollectionTagFetchedEntries.php index 821ae4521c..012a15b98e 100644 --- a/src/Events/CollectionTagFetchedEntries.php +++ b/src/Events/CollectionTagFetchedEntries.php @@ -3,6 +3,7 @@ namespace Statamic\Events; use Statamic\Entries\EntryCollection; +use Statamic\Extensions\Pagination\LengthAwarePaginator; use Statamic\Tags\Collection\Collection; class CollectionTagFetchedEntries extends Event @@ -10,7 +11,7 @@ class CollectionTagFetchedEntries extends Event public EntryCollection $entries; public Collection $tag; - public function __construct(EntryCollection $entries, Collection $tag) + public function __construct(EntryCollection|LengthAwarePaginator $entries, Collection $tag) { $this->entries = $entries; $this->tag = $tag; From d54f6436b88e4dd26ff0c3ec3d3f611c0c513b19 Mon Sep 17 00:00:00 2001 From: Sylvester Damgaard Date: Wed, 21 Feb 2024 11:02:40 +0100 Subject: [PATCH 3/6] Fix issue with type hinting --- src/Events/CollectionTagFetchedEntries.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Events/CollectionTagFetchedEntries.php b/src/Events/CollectionTagFetchedEntries.php index 012a15b98e..e74c98e6e1 100644 --- a/src/Events/CollectionTagFetchedEntries.php +++ b/src/Events/CollectionTagFetchedEntries.php @@ -8,7 +8,7 @@ class CollectionTagFetchedEntries extends Event { - public EntryCollection $entries; + public EntryCollection|LengthAwarePaginator $entries; public Collection $tag; public function __construct(EntryCollection|LengthAwarePaginator $entries, Collection $tag) From bc7ded597e12a13292730b4486eb536acf08b92d Mon Sep 17 00:00:00 2001 From: Sylvester Damgaard Date: Wed, 21 Feb 2024 11:51:43 +0100 Subject: [PATCH 4/6] Linting --- src/Tags/Collection/Collection.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Tags/Collection/Collection.php b/src/Tags/Collection/Collection.php index 8a2142269b..92cc8cd502 100644 --- a/src/Tags/Collection/Collection.php +++ b/src/Tags/Collection/Collection.php @@ -35,6 +35,7 @@ public function index() $entries = $this->entries()->get(); CollectionTagFetchedEntries::dispatch($entries, $this); + return $this->output( $entries ); @@ -99,6 +100,7 @@ public function newer() protected function entries() { CollectionTagFetchingEntries::dispatch($this); + return new Entries($this->params); } From 2b3402e9d4c558f1e69324dac40290f8c305ec92 Mon Sep 17 00:00:00 2001 From: Sylvester Damgaard Date: Wed, 21 Feb 2024 20:45:23 +0100 Subject: [PATCH 5/6] Remove event stuff --- src/Events/CollectionTagFetchedEntries.php | 19 ------------------- src/Events/CollectionTagFetchingEntries.php | 15 --------------- src/Tags/Collection/Collection.php | 7 +------ 3 files changed, 1 insertion(+), 40 deletions(-) delete mode 100644 src/Events/CollectionTagFetchedEntries.php delete mode 100644 src/Events/CollectionTagFetchingEntries.php diff --git a/src/Events/CollectionTagFetchedEntries.php b/src/Events/CollectionTagFetchedEntries.php deleted file mode 100644 index e74c98e6e1..0000000000 --- a/src/Events/CollectionTagFetchedEntries.php +++ /dev/null @@ -1,19 +0,0 @@ -entries = $entries; - $this->tag = $tag; - } -} diff --git a/src/Events/CollectionTagFetchingEntries.php b/src/Events/CollectionTagFetchingEntries.php deleted file mode 100644 index 2682a99f88..0000000000 --- a/src/Events/CollectionTagFetchingEntries.php +++ /dev/null @@ -1,15 +0,0 @@ -tag = $tag; - } -} diff --git a/src/Tags/Collection/Collection.php b/src/Tags/Collection/Collection.php index 92cc8cd502..60be4f9172 100644 --- a/src/Tags/Collection/Collection.php +++ b/src/Tags/Collection/Collection.php @@ -33,11 +33,8 @@ public function index() return $this->context->value('collection'); } - $entries = $this->entries()->get(); - CollectionTagFetchedEntries::dispatch($entries, $this); - return $this->output( - $entries + $this->entries()->get() ); } @@ -99,8 +96,6 @@ public function newer() protected function entries() { - CollectionTagFetchingEntries::dispatch($this); - return new Entries($this->params); } From ce80a338828de9ec7794eeaafd0e6708878edea3 Mon Sep 17 00:00:00 2001 From: Sylvester Damgaard Date: Wed, 21 Feb 2024 21:00:48 +0100 Subject: [PATCH 6/6] Remove unused namespaces --- src/Tags/Collection/Collection.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/Tags/Collection/Collection.php b/src/Tags/Collection/Collection.php index 60be4f9172..73a22a0d32 100644 --- a/src/Tags/Collection/Collection.php +++ b/src/Tags/Collection/Collection.php @@ -2,8 +2,6 @@ namespace Statamic\Tags\Collection; -use Statamic\Events\CollectionTagFetchedEntries; -use Statamic\Events\CollectionTagFetchingEntries; use Statamic\Facades\Entry; use Statamic\Tags\Concerns; use Statamic\Tags\Tags;