Skip to content

Commit 8b8a5ab

Browse files
committed
feat: register additional properties during compile time
The old way to register additional properties with the PSR-14 event "RegisterAdditionalTypePropertiesEvent" is deprecated.
1 parent 936202a commit 8b8a5ab

File tree

178 files changed

+6309
-681
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

178 files changed

+6309
-681
lines changed

CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

88
## [Unreleased]
99

10+
### Added
11+
- Registration of additional properties by implementing `AdditionalPropertiesInterface`
12+
13+
### Deprecated
14+
- PSR-14 event `RegisterAdditionalTypePropertiesEvent`
15+
1016
### Fixed
1117
- Additional properties cache is not cleared when flushing the cache
1218

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the "schema" extension for TYPO3 CMS.
7+
*
8+
* For the full copyright and license information, please read the
9+
* LICENSE.txt file that was distributed with this source code.
10+
*/
11+
12+
namespace Brotkrueml\Schema\Core;
13+
14+
/**
15+
* @api
16+
*/
17+
interface AdditionalPropertiesInterface
18+
{
19+
/**
20+
* @return non-empty-string
21+
*/
22+
public function getType(): string;
23+
24+
/**
25+
* @return list<non-empty-string>
26+
*/
27+
public function getAdditionalProperties(): array;
28+
}

Classes/Core/Model/AbstractType.php

+13-2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Brotkrueml\Schema\Attributes\Type;
1515
use Brotkrueml\Schema\Event\RegisterAdditionalTypePropertiesEvent;
1616
use Brotkrueml\Schema\Extension;
17+
use Brotkrueml\Schema\Type\AdditionalPropertiesProvider;
1718
use Psr\EventDispatcher\EventDispatcherInterface;
1819
use TYPO3\CMS\Core\Cache\CacheManager;
1920
use TYPO3\CMS\Core\Utility\GeneralUtility;
@@ -58,14 +59,24 @@ protected function addAdditionalProperties(): void
5859
$cache = GeneralUtility::makeInstance(CacheManager::class)->getCache(Extension::CACHE_IDENTIFIER);
5960
$additionalProperties = $cache->get($cacheEntryIdentifier);
6061
if ($additionalProperties === false) {
61-
$event = new RegisterAdditionalTypePropertiesEvent(static::class);
62+
/** @var AdditionalPropertiesProvider $additionalPropertiesProvider */
63+
$additionalPropertiesProvider = GeneralUtility::makeInstance(AdditionalPropertiesProvider::class);
64+
$additionalProperties = $additionalPropertiesProvider->getForType($this->getType());
6265

66+
$event = new RegisterAdditionalTypePropertiesEvent(static::class);
6367
/** @var EventDispatcherInterface $eventDispatcher */
6468
$eventDispatcher = GeneralUtility::makeInstance(EventDispatcherInterface::class);
6569
/** @var RegisterAdditionalTypePropertiesEvent $event */
6670
$event = $eventDispatcher->dispatch($event);
71+
if ($event->haveAdditionalPropertiesRegistered()) {
72+
$message = \sprintf(
73+
'Using the RegisterAdditionalTypePropertiesEvent for "%s" is deprecated since EXT:schema version 3.10.0, it will be removed in version 4.0.0. To register additional properties create a class implementing AdditionalPropertiesInterface. Consult the docs for details.',
74+
$this->getType(),
75+
);
76+
\trigger_error($message, \E_USER_DEPRECATED);
77+
}
6778

68-
$additionalProperties = $event->getAdditionalProperties();
79+
$additionalProperties = [...$additionalProperties, ...$event->getAdditionalProperties()];
6980
$cache->set($cacheEntryIdentifier, $additionalProperties, [], 0);
7081
}
7182

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the "schema" extension for TYPO3 CMS.
7+
*
8+
* For the full copyright and license information, please read the
9+
* LICENSE.txt file that was distributed with this source code.
10+
*/
11+
12+
namespace Brotkrueml\Schema\DependencyInjection;
13+
14+
use Brotkrueml\Schema\Type\AdditionalPropertiesProvider;
15+
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
16+
use Symfony\Component\DependencyInjection\ContainerBuilder;
17+
18+
/**
19+
* @internal
20+
*/
21+
final class AdditionalPropertiesPass implements CompilerPassInterface
22+
{
23+
public function __construct(
24+
private readonly string $tagName,
25+
) {}
26+
27+
public function process(ContainerBuilder $container): void
28+
{
29+
if (! $container->hasDefinition(AdditionalPropertiesProvider::class)) {
30+
return;
31+
}
32+
33+
$providerDefinition = $container->getDefinition(AdditionalPropertiesProvider::class)->setPublic(true);
34+
foreach (\array_keys($container->findTaggedServiceIds($this->tagName)) as $id) {
35+
$providerDefinition->addMethodCall('add', [$id]);
36+
}
37+
}
38+
}

Classes/Event/RegisterAdditionalTypePropertiesEvent.php

+13
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,16 @@
1111

1212
namespace Brotkrueml\Schema\Event;
1313

14+
/**
15+
* @deprecated since 3.10.0, will be removed in 4.0.0. To register additional properties create a class implementing AdditionalPropertiesInterface. Consult the docs for details.
16+
*/
1417
final class RegisterAdditionalTypePropertiesEvent
1518
{
1619
/**
1720
* @var list<string>
1821
*/
1922
private array $additionalProperties = [];
23+
private bool $haveAdditionalPropertiesRegistered = false;
2024

2125
public function __construct(
2226
private readonly string $type,
@@ -39,6 +43,15 @@ public function registerAdditionalProperty(string $propertyName): void
3943
{
4044
if (! \in_array($propertyName, $this->additionalProperties, true)) {
4145
$this->additionalProperties[] = $propertyName;
46+
$this->haveAdditionalPropertiesRegistered = true;
4247
}
4348
}
49+
50+
/**
51+
* @internal
52+
*/
53+
public function haveAdditionalPropertiesRegistered(): bool
54+
{
55+
return $this->haveAdditionalPropertiesRegistered;
56+
}
4457
}

Classes/EventListener/RegisterRemovedTypePropertiesForPhysician.php

-71
This file was deleted.

0 commit comments

Comments
 (0)