Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FEATURE: Expose content repository ids from registry #5194

Merged
merged 3 commits into from
Aug 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

/*
* This file is part of the Neos.ContentRepository package.
*
* (c) Contributors of the Neos Project - www.neos.io
*
* This package is Open Source Software. For the full copyright and license
* information, please view the LICENSE file which was distributed with this
* source code.
*/

declare(strict_types=1);

namespace Neos\ContentRepository\Core\SharedModel\ContentRepository;

/**
* @implements \IteratorAggregate<ContentRepositoryId>
* @api
*/
final readonly class ContentRepositoryIds implements \IteratorAggregate, \Countable
{
/**
* @var array<ContentRepositoryId>
*/
private array $ids;

private function __construct(ContentRepositoryId ...$ids)
{
$this->ids = $ids;
}

/**
* @param array<ContentRepositoryId|string> $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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i kinda prefer using language level varadics here to do the type check - as that must be faster - and by the doc comment @param array<ContentRepositoryId|string> this case may actually never occur.

but still ... just nitpicking ^^

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just claim boldly: Performance isn't an issue here.
Even if you had 1000 Content Repositories in one installation, this check would cost nothing basically.
I added this because it just provides a nicer exception message, e.g. when you configure your content repositories incorrectly

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
{
yield from $this->ids;
}

public function count(): int
{
return count($this->ids);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

declare(strict_types=1);

namespace Neos\ContentRepository\Core\Tests\Unit\SharedModel\ContentRepository;

/*
* This file is part of the Neos.ContentRepository package.
*
* (c) Contributors of the Neos Project - www.neos.io
*
* This package is Open Source Software. For the full copyright and license
* information, please view the LICENSE file which was distributed with this
* source code.
*/

use Neos\ContentRepository\Core\SharedModel\ContentRepository\ContentRepositoryId;
use Neos\ContentRepository\Core\SharedModel\ContentRepository\ContentRepositoryIds;
use PHPUnit\Framework\TestCase;

class ContentRepositoryIdsTest extends TestCase
{
/**
* @test
*/
public function fromArraySupportsEmptyArray(): void
{
$contentRepositoryIds = ContentRepositoryIds::fromArray([]);
self::assertCount(0, $contentRepositoryIds);
}

/**
* @test
*/
public function fromArrayConvertsStringsToContentRepositoryIds(): void
{
$contentRepositoryIds = ContentRepositoryIds::fromArray(['some_cr_id', ContentRepositoryId::fromString('other_cr_id')]);
self::assertEquals([ContentRepositoryId::fromString('some_cr_id'), ContentRepositoryId::fromString('other_cr_id')], iterator_to_array($contentRepositoryIds));
}

/**
* @test
*/
public function fromArrayThrowsExceptionForInvalidItem(): void
{
$this->expectException(\InvalidArgumentException::class);
ContentRepositoryIds::fromArray([1234]);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

declare(strict_types=1);

namespace Neos\ContentRepositoryRegistry;
Expand All @@ -17,6 +18,7 @@
use Neos\ContentRepository\Core\Projection\ProjectionCatchUpTriggerInterface;
use Neos\ContentRepository\Core\Projection\ProjectionFactoryInterface;
use Neos\ContentRepository\Core\SharedModel\ContentRepository\ContentRepositoryId;
use Neos\ContentRepository\Core\SharedModel\ContentRepository\ContentRepositoryIds;
use Neos\ContentRepository\Core\SharedModel\User\UserIdProviderInterface;
use Neos\ContentRepositoryRegistry\Exception\ContentRepositoryNotFoundException;
use Neos\ContentRepositoryRegistry\Exception\InvalidConfigurationException;
Expand Down Expand Up @@ -81,6 +83,13 @@ public function get(ContentRepositoryId $contentRepositoryId): ContentRepository
return $this->getFactory($contentRepositoryId)->getOrBuild();
}

public function getContentRepositoryIds(): ContentRepositoryIds
{
/** @var array<string> $contentRepositoryIds */
$contentRepositoryIds = array_keys($this->settings['contentRepositories'] ?? []);
return ContentRepositoryIds::fromArray($contentRepositoryIds);
}

/**
* @internal for test cases only
*/
Expand Down Expand Up @@ -130,7 +139,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');
}
Expand Down Expand Up @@ -202,7 +212,6 @@ private function buildContentDimensionSource(ContentRepositoryId $contentReposit
$options['contentDimensions'] = Arrays::arrayMergeRecursiveOverrule($contentRepositorySettings['contentDimensions'], $options['contentDimensions'] ?? []);
}
return $contentDimensionSourceFactory->build($contentRepositoryId, $options);

}

/** @param array<string, mixed> $contentRepositorySettings */
Expand Down
Loading