-
-
Notifications
You must be signed in to change notification settings - Fork 224
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
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
60 changes: 60 additions & 0 deletions
60
Neos.ContentRepository.Core/Classes/SharedModel/ContentRepository/ContentRepositoryIds.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { | ||
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); | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
...tentRepository.Core/Tests/Unit/SharedModel/ContentRepository/ContentRepositoryIdsTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 ^^
There was a problem hiding this comment.
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