-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
wip: register additional properties during compile time
todo: - register additional properties from schema extensions the new way - deprecate event listener - document new registration - add changelog entries (feature, deprecation) - add issue to drop event listener and tx_schema cache
- Loading branch information
1 parent
936202a
commit a6fe752
Showing
9 changed files
with
264 additions
and
1 deletion.
There are no files selected for viewing
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,28 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the "schema" extension for TYPO3 CMS. | ||
* | ||
* For the full copyright and license information, please read the | ||
* LICENSE.txt file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Brotkrueml\Schema\Core; | ||
|
||
/** | ||
* @api | ||
*/ | ||
interface AdditionalPropertiesInterface | ||
{ | ||
/** | ||
* @return non-empty-string | ||
*/ | ||
public function getType(): string; | ||
|
||
/** | ||
* @return list<non-empty-string> | ||
*/ | ||
public function getAdditionalProperties(): array; | ||
} |
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
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,38 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the "schema" extension for TYPO3 CMS. | ||
* | ||
* For the full copyright and license information, please read the | ||
* LICENSE.txt file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Brotkrueml\Schema\DependencyInjection; | ||
|
||
use Brotkrueml\Schema\Type\AdditionalPropertiesProvider; | ||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
final class AdditionalPropertiesPass implements CompilerPassInterface | ||
{ | ||
public function __construct( | ||
private readonly string $tagName, | ||
) {} | ||
|
||
public function process(ContainerBuilder $container): void | ||
{ | ||
if (! $container->hasDefinition(AdditionalPropertiesProvider::class)) { | ||
return; | ||
} | ||
|
||
$providerDefinition = $container->getDefinition(AdditionalPropertiesProvider::class)->setPublic(true); | ||
foreach (\array_keys($container->findTaggedServiceIds($this->tagName)) as $id) { | ||
$providerDefinition->addMethodCall('add', [$id]); | ||
} | ||
} | ||
} |
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,45 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the "schema" extension for TYPO3 CMS. | ||
* | ||
* For the full copyright and license information, please read the | ||
* LICENSE.txt file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Brotkrueml\Schema\Type; | ||
|
||
use Brotkrueml\Schema\Core\AdditionalPropertiesInterface; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
final class AdditionalPropertiesProvider | ||
{ | ||
/** | ||
* @var array<non-empty-string, list<class-string<AdditionalPropertiesInterface>>> | ||
*/ | ||
private array $additionalProperties = []; | ||
|
||
/** | ||
* @param class-string<AdditionalPropertiesInterface> $class | ||
*/ | ||
public function add(string $class): void | ||
{ | ||
$type = (new $class())->getType(); | ||
if (! isset($this->additionalProperties[$type])) { | ||
$this->additionalProperties[$type] = []; | ||
} | ||
$this->additionalProperties[$type][] = $class; | ||
} | ||
|
||
/** | ||
* @return list<class-string<AdditionalPropertiesInterface>> | ||
*/ | ||
public function get(string $type): array | ||
{ | ||
return $this->additionalProperties[$type] ?? []; | ||
} | ||
} |
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
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,29 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the "schema" extension for TYPO3 CMS. | ||
* | ||
* For the full copyright and license information, please read the | ||
* LICENSE.txt file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Brotkrueml\Schema\Tests\Fixtures\Model\AdditionalProperties; | ||
|
||
use Brotkrueml\Schema\Core\AdditionalPropertiesInterface; | ||
|
||
final class Event implements AdditionalPropertiesInterface | ||
{ | ||
public function getType(): string | ||
{ | ||
return 'Event'; | ||
} | ||
|
||
public function getAdditionalProperties(): array | ||
{ | ||
return [ | ||
'additional-event-property', | ||
]; | ||
} | ||
} |
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,29 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the "schema" extension for TYPO3 CMS. | ||
* | ||
* For the full copyright and license information, please read the | ||
* LICENSE.txt file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Brotkrueml\Schema\Tests\Fixtures\Model\AdditionalProperties; | ||
|
||
use Brotkrueml\Schema\Core\AdditionalPropertiesInterface; | ||
|
||
final class Person1 implements AdditionalPropertiesInterface | ||
{ | ||
public function getType(): string | ||
{ | ||
return 'Person'; | ||
} | ||
|
||
public function getAdditionalProperties(): array | ||
{ | ||
return [ | ||
'additional-person-property-1', | ||
]; | ||
} | ||
} |
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,29 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the "schema" extension for TYPO3 CMS. | ||
* | ||
* For the full copyright and license information, please read the | ||
* LICENSE.txt file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Brotkrueml\Schema\Tests\Fixtures\Model\AdditionalProperties; | ||
|
||
use Brotkrueml\Schema\Core\AdditionalPropertiesInterface; | ||
|
||
final class Person2 implements AdditionalPropertiesInterface | ||
{ | ||
public function getType(): string | ||
{ | ||
return 'Person'; | ||
} | ||
|
||
public function getAdditionalProperties(): array | ||
{ | ||
return [ | ||
'additional-person-property-2', | ||
]; | ||
} | ||
} |
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,53 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the "schema" extension for TYPO3 CMS. | ||
* | ||
* For the full copyright and license information, please read the | ||
* LICENSE.txt file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Brotkrueml\Schema\Tests\Unit\Type; | ||
|
||
use Brotkrueml\Schema\Tests\Fixtures\Model\AdditionalProperties\Event; | ||
use Brotkrueml\Schema\Tests\Fixtures\Model\AdditionalProperties\Person1; | ||
use Brotkrueml\Schema\Tests\Fixtures\Model\AdditionalProperties\Person2; | ||
use Brotkrueml\Schema\Type\AdditionalPropertiesProvider; | ||
use PHPUnit\Framework\Attributes\CoversClass; | ||
use PHPUnit\Framework\Attributes\Test; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
#[CoversClass(AdditionalPropertiesProvider::class)] | ||
final class AdditionalPropertiesProviderTest extends TestCase | ||
{ | ||
private AdditionalPropertiesProvider $subject; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->subject = new AdditionalPropertiesProvider(); | ||
} | ||
|
||
#[Test] | ||
public function getReturnsEmptyArrayIfTypeIsNotAvailable(): void | ||
{ | ||
$actual = $this->subject->get('NonExisting'); | ||
|
||
self::assertSame([], $actual); | ||
} | ||
|
||
#[Test] | ||
public function getReturnsPreviouslyAddedClassesCorrectly(): void | ||
{ | ||
$this->subject->add(Person1::class); | ||
$this->subject->add(Person2::class); | ||
$this->subject->add(Event::class); | ||
|
||
$actual = $this->subject->get('Person'); | ||
|
||
self::assertCount(2, $actual); | ||
self::assertContains(Person1::class, $actual); | ||
self::assertContains(Person2::class, $actual); | ||
} | ||
} |