From f9c0b7ae997bf635db64aac6c7277f2c83785be3 Mon Sep 17 00:00:00 2001 From: Bastian Waidelich Date: Fri, 2 Aug 2024 11:46:37 +0200 Subject: [PATCH 1/3] FEATURE: Expose content repository ids from registry Adds a `getContentRepositoryIds()` getter to the `ContentRepositoryRegistry` that allows to iterate over all configured content repositories. This will be useful for enforcing ACL, cross-CR-linking, etc.. Related: #4726 --- .../ContentRepositoryIds.php | 60 +++++++++++++++++++ .../ContentRepositoryIdsTest.php | 49 +++++++++++++++ .../Classes/ContentRepositoryRegistry.php | 11 +++- 3 files changed, 118 insertions(+), 2 deletions(-) create mode 100644 Neos.ContentRepository.Core/Classes/SharedModel/ContentRepository/ContentRepositoryIds.php create mode 100644 Neos.ContentRepository.Core/Tests/Unit/SharedModel/ContentRepository/ContentRepositoryIdsTest.php diff --git a/Neos.ContentRepository.Core/Classes/SharedModel/ContentRepository/ContentRepositoryIds.php b/Neos.ContentRepository.Core/Classes/SharedModel/ContentRepository/ContentRepositoryIds.php new file mode 100644 index 00000000000..3a5c002afdb --- /dev/null +++ b/Neos.ContentRepository.Core/Classes/SharedModel/ContentRepository/ContentRepositoryIds.php @@ -0,0 +1,60 @@ + + * @api + */ +final readonly class ContentRepositoryIds implements \IteratorAggregate, \Countable +{ + /** + * @var array + */ + private array $ids; + + private function __construct(ContentRepositoryId ...$ids) + { + $this->ids = $ids; + } + + /** + * @param array $ids + */ + public static function fromArray(array $ids): self + { + $processedIds = []; + foreach ($ids as $id) { + if (is_string($id)) { + $id = ContentRepositoryId::fromString($id); + } + if (!$id instanceof ContentRepositoryId) { + throw new \InvalidArgumentException(sprintf('Expected string or instance of %s, got: %s', ContentRepositoryId::class, get_debug_type($id)), 1720424666); + } + $processedIds[] = $id; + } + return new self(...$processedIds); + } + + public function getIterator(): \Traversable + { + return yield from $this->ids; + } + + public function count(): int + { + return count($this->ids); + } +} diff --git a/Neos.ContentRepository.Core/Tests/Unit/SharedModel/ContentRepository/ContentRepositoryIdsTest.php b/Neos.ContentRepository.Core/Tests/Unit/SharedModel/ContentRepository/ContentRepositoryIdsTest.php new file mode 100644 index 00000000000..23fa9105a0f --- /dev/null +++ b/Neos.ContentRepository.Core/Tests/Unit/SharedModel/ContentRepository/ContentRepositoryIdsTest.php @@ -0,0 +1,49 @@ +expectException(\InvalidArgumentException::class); + ContentRepositoryIds::fromArray([1234]); + } +} diff --git a/Neos.ContentRepositoryRegistry/Classes/ContentRepositoryRegistry.php b/Neos.ContentRepositoryRegistry/Classes/ContentRepositoryRegistry.php index 1fd7cc04910..f1c6f43ffc5 100644 --- a/Neos.ContentRepositoryRegistry/Classes/ContentRepositoryRegistry.php +++ b/Neos.ContentRepositoryRegistry/Classes/ContentRepositoryRegistry.php @@ -1,4 +1,5 @@ getFactory($contentRepositoryId)->getOrBuild(); } + public function getContentRepositoryIds(): ContentRepositoryIds + { + return ContentRepositoryIds::fromArray(array_keys($this->settings['contentRepositories'] ?? [])); + } + /** * @internal for test cases only */ @@ -130,7 +137,8 @@ private function getFactory( /** * @throws ContentRepositoryNotFoundException | InvalidConfigurationException */ - private function buildFactory(ContentRepositoryId $contentRepositoryId): ContentRepositoryFactory { + private function buildFactory(ContentRepositoryId $contentRepositoryId): ContentRepositoryFactory + { if (!is_array($this->settings['contentRepositories'] ?? null)) { throw InvalidConfigurationException::fromMessage('No Content Repositories are configured'); } @@ -202,7 +210,6 @@ private function buildContentDimensionSource(ContentRepositoryId $contentReposit $options['contentDimensions'] = Arrays::arrayMergeRecursiveOverrule($contentRepositorySettings['contentDimensions'], $options['contentDimensions'] ?? []); } return $contentDimensionSourceFactory->build($contentRepositoryId, $options); - } /** @param array $contentRepositorySettings */ From 374478e3cf87199d999c6f0c45c50ce59e3e3c13 Mon Sep 17 00:00:00 2001 From: Bastian Waidelich Date: Fri, 2 Aug 2024 12:21:47 +0200 Subject: [PATCH 2/3] Add type hint to satisfy linter --- .../Classes/ContentRepositoryRegistry.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Neos.ContentRepositoryRegistry/Classes/ContentRepositoryRegistry.php b/Neos.ContentRepositoryRegistry/Classes/ContentRepositoryRegistry.php index f1c6f43ffc5..1de26c1378d 100644 --- a/Neos.ContentRepositoryRegistry/Classes/ContentRepositoryRegistry.php +++ b/Neos.ContentRepositoryRegistry/Classes/ContentRepositoryRegistry.php @@ -85,7 +85,9 @@ public function get(ContentRepositoryId $contentRepositoryId): ContentRepository public function getContentRepositoryIds(): ContentRepositoryIds { - return ContentRepositoryIds::fromArray(array_keys($this->settings['contentRepositories'] ?? [])); + /** @var array $contentRepositoryIds */ + $contentRepositoryIds = array_keys($this->settings['contentRepositories'] ?? []); + return ContentRepositoryIds::fromArray($contentRepositoryIds); } /** From 68594fc7938757f41bff5004203b5df6de93912f Mon Sep 17 00:00:00 2001 From: Bastian Waidelich Date: Sat, 3 Aug 2024 14:10:01 +0200 Subject: [PATCH 3/3] Update Neos.ContentRepository.Core/Classes/SharedModel/ContentRepository/ContentRepositoryIds.php Co-authored-by: Marc Henry Schultz <85400359+mhsdesign@users.noreply.github.com> --- .../SharedModel/ContentRepository/ContentRepositoryIds.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Neos.ContentRepository.Core/Classes/SharedModel/ContentRepository/ContentRepositoryIds.php b/Neos.ContentRepository.Core/Classes/SharedModel/ContentRepository/ContentRepositoryIds.php index 3a5c002afdb..0afd45befee 100644 --- a/Neos.ContentRepository.Core/Classes/SharedModel/ContentRepository/ContentRepositoryIds.php +++ b/Neos.ContentRepository.Core/Classes/SharedModel/ContentRepository/ContentRepositoryIds.php @@ -50,7 +50,7 @@ public static function fromArray(array $ids): self public function getIterator(): \Traversable { - return yield from $this->ids; + yield from $this->ids; } public function count(): int