From 80fcc62479838a847c68087a0478500ab51d0412 Mon Sep 17 00:00:00 2001 From: Bob den Otter Date: Mon, 14 Mar 2022 15:06:37 +0100 Subject: [PATCH 1/2] Bugfix: verify setting correct tags for caching works correctly --- src/Cache/CachingTrait.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/Cache/CachingTrait.php b/src/Cache/CachingTrait.php index 95f515118..c94162641 100644 --- a/src/Cache/CachingTrait.php +++ b/src/Cache/CachingTrait.php @@ -53,6 +53,10 @@ public function getCacheKey(): string public function setCacheTags(array $tags): void { + foreach ($tags as $key => $tag) { + $tags[$key] = preg_replace('~[^\pL\d,]+~u', '-', $tag); + } + $this->cacheTags = $tags; } From deca3ef819d05e8e2537afeefc5d552c9795ac39 Mon Sep 17 00:00:00 2001 From: Bob den Otter Date: Mon, 14 Mar 2022 15:18:44 +0100 Subject: [PATCH 2/2] Better regexing --- src/Cache/CachingTrait.php | 2 +- src/Cache/SelectOptionsCacher.php | 16 +++++++++++++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/Cache/CachingTrait.php b/src/Cache/CachingTrait.php index c94162641..c16b54762 100644 --- a/src/Cache/CachingTrait.php +++ b/src/Cache/CachingTrait.php @@ -54,7 +54,7 @@ public function getCacheKey(): string public function setCacheTags(array $tags): void { foreach ($tags as $key => $tag) { - $tags[$key] = preg_replace('~[^\pL\d,]+~u', '-', $tag); + $tags[$key] = preg_replace('/[^\pL\d,]+/u', '', $tag); } $this->cacheTags = $tags; diff --git a/src/Cache/SelectOptionsCacher.php b/src/Cache/SelectOptionsCacher.php index 191d8b39a..9617e7b54 100644 --- a/src/Cache/SelectOptionsCacher.php +++ b/src/Cache/SelectOptionsCacher.php @@ -14,8 +14,22 @@ class SelectOptionsCacher extends FieldExtension implements CachingInterface public function selectOptionsHelper(string $contentTypeSlug, array $params, Field $field, string $format): array { $this->setCacheKey([$contentTypeSlug, $format] + $params); - $this->setCacheTags([$contentTypeSlug]); + $this->setCacheTags($this->getTags($contentTypeSlug)); return $this->execute([parent::class, __FUNCTION__], [$contentTypeSlug, $params, $field, $format]); } + + /** + * Make sure something like `(pages,entries)` becomes an array like ['pages', 'entries'] + */ + private function getTags(string $contentTypeSlug): array + { + $tags = explode(',', $contentTypeSlug); + + $tags = array_map(function($t) { + return preg_replace('/[^\pL\d,]+/u', '', $t); + }, $tags); + + return $tags; + } }