From 2e824c8803024b1424cbb4f86cdec92309933ce8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Chris=20M=C3=BCller?= Date: Sat, 15 Feb 2025 18:52:59 +0100 Subject: [PATCH] wip: introduce TypeFactorySubstitute todo: - add unit tests - add documentation --- Classes/EventListener/AddBreadcrumbList.php | 4 +- Classes/EventListener/AddWebPageType.php | 4 +- Classes/Testing/TypeFactorySubstitute.php | 73 +++++++++++++++++++ Classes/Type/TypeFactory.php | 4 +- Classes/Type/TypeFactoryInterface.php | 19 +++++ Configuration/Services.php | 1 + .../Developer/_Api/_MyController1.php | 4 +- .../Developer/_Api/_MyController2.php | 4 +- .../Developer/_Api/_MyController3.php | 4 +- .../Developer/_Api/_MyController4.php | 4 +- .../Developer/_Api/_MyController5.php | 4 +- .../_Api/_MyControllerBlankNodeIdentifier.php | 4 +- .../Developer/_Api/_MyControllerMultiple.php | 4 +- .../_Api/_MyControllerNodeIdentifier.php | 4 +- .../Developer/_Breadcrumb/_MyController.php | 4 +- .../_ExtendingVocabulary/_MyController.php | 4 +- .../Developer/_Index/_MyController1.php | 4 +- .../Developer/_Index/_MyController2.php | 4 +- .../Developer/_MainEntity/_MyController.php | 4 +- .../Developer/_WebPage/_MyController1.php | 4 +- .../Developer/_WebPage/_MyController2.php | 4 +- 21 files changed, 129 insertions(+), 36 deletions(-) create mode 100644 Classes/Testing/TypeFactorySubstitute.php create mode 100644 Classes/Type/TypeFactoryInterface.php diff --git a/Classes/EventListener/AddBreadcrumbList.php b/Classes/EventListener/AddBreadcrumbList.php index b6f5ee87..44c895d8 100644 --- a/Classes/EventListener/AddBreadcrumbList.php +++ b/Classes/EventListener/AddBreadcrumbList.php @@ -14,7 +14,7 @@ use Brotkrueml\Schema\Configuration\Configuration; use Brotkrueml\Schema\Core\Model\TypeInterface; use Brotkrueml\Schema\Event\RenderAdditionalTypesEvent; -use Brotkrueml\Schema\Type\TypeFactory; +use Brotkrueml\Schema\Type\TypeFactoryInterface; use TYPO3\CMS\Core\Domain\Repository\PageRepository; use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer; use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController; @@ -33,7 +33,7 @@ final class AddBreadcrumbList public function __construct( private readonly Configuration $configuration, private readonly ContentObjectRenderer $contentObjectRenderer, - private readonly TypeFactory $typeFactory, + private readonly TypeFactoryInterface $typeFactory, ) {} public function __invoke(RenderAdditionalTypesEvent $event): void diff --git a/Classes/EventListener/AddWebPageType.php b/Classes/EventListener/AddWebPageType.php index f7ac11b2..e03107ce 100644 --- a/Classes/EventListener/AddWebPageType.php +++ b/Classes/EventListener/AddWebPageType.php @@ -13,7 +13,7 @@ use Brotkrueml\Schema\Configuration\Configuration; use Brotkrueml\Schema\Event\RenderAdditionalTypesEvent; -use Brotkrueml\Schema\Type\TypeFactory; +use Brotkrueml\Schema\Type\TypeFactoryInterface; use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController; /** @@ -25,7 +25,7 @@ final class AddWebPageType public function __construct( private readonly Configuration $configuration, - private readonly TypeFactory $typeFactory, + private readonly TypeFactoryInterface $typeFactory, ) {} public function __invoke(RenderAdditionalTypesEvent $event): void diff --git a/Classes/Testing/TypeFactorySubstitute.php b/Classes/Testing/TypeFactorySubstitute.php new file mode 100644 index 00000000..a2eb6cf4 --- /dev/null +++ b/Classes/Testing/TypeFactorySubstitute.php @@ -0,0 +1,73 @@ + + */ + private array $types = []; + + public function addType(string $name, string $class): self + { + if (! $class instanceof TypeInterface) { + throw new \InvalidArgumentException($class . ' is not of type TypeInterface!'); + } + + $this->types[$name] = $class; + + return $this; + } + + public function create(string ...$type): TypeInterface + { + if ($type === []) { + throw new \DomainException( + 'At least one type has to be given as argument', + 1621787452, + ); + } + + $type = \array_unique($type); + if (\count($type) === 1) { + return $this->createSingle($type[0]); + } + + return $this->createMultiple($type); + } + + private function createSingle($type): TypeInterface + { + if (isset($this->types[$type])) { + return new $this->types[$type](); + } + + throw ModelClassNotFoundException::fromType($type); + } + + /** + * @param list $types + */ + private function createMultiple(array $types): MultipleType + { + return new MultipleType(...\array_map( + fn(string $type): TypeInterface => $this->createSingle($type), + $types, + )); + } +} diff --git a/Classes/Type/TypeFactory.php b/Classes/Type/TypeFactory.php index abb92284..0895cf1a 100644 --- a/Classes/Type/TypeFactory.php +++ b/Classes/Type/TypeFactory.php @@ -15,7 +15,7 @@ use Brotkrueml\Schema\Core\Model\TypeInterface; use TYPO3\CMS\Core\Utility\GeneralUtility; -final class TypeFactory +final class TypeFactory implements TypeFactoryInterface { public function create(string ...$type): TypeInterface { @@ -60,7 +60,7 @@ private function createSingle(string $type): TypeInterface } /** - * @param string[] $types + * @param list $types */ private function createMultiple(array $types): MultipleType { diff --git a/Classes/Type/TypeFactoryInterface.php b/Classes/Type/TypeFactoryInterface.php new file mode 100644 index 00000000..466ff82f --- /dev/null +++ b/Classes/Type/TypeFactoryInterface.php @@ -0,0 +1,19 @@ +hasDefinition(AdminPanelConfigurationService::class)) { $excludes[] = __DIR__ . '/../Classes/AdminPanel'; diff --git a/Documentation/Developer/_Api/_MyController1.php b/Documentation/Developer/_Api/_MyController1.php index f0ec8214..751b2eb0 100644 --- a/Documentation/Developer/_Api/_MyController1.php +++ b/Documentation/Developer/_Api/_MyController1.php @@ -4,12 +4,12 @@ namespace MyVendor\MyExtension\Controller; -use Brotkrueml\Schema\Type\TypeFactory; +use Brotkrueml\Schema\Type\TypeFactoryInterface; final class MyController { public function __construct( - private readonly TypeFactory $typeFactory, + private readonly TypeFactoryInterface $typeFactory, ) {} public function doSomething(): void diff --git a/Documentation/Developer/_Api/_MyController2.php b/Documentation/Developer/_Api/_MyController2.php index fd7f2855..1de7a447 100644 --- a/Documentation/Developer/_Api/_MyController2.php +++ b/Documentation/Developer/_Api/_MyController2.php @@ -5,12 +5,12 @@ namespace MyVendor\MyExtension\Controller; use Brotkrueml\Schema\Model\Enumeration\GenderType; -use Brotkrueml\Schema\Type\TypeFactory; +use Brotkrueml\Schema\Type\TypeFactoryInterface; final class MyController { public function __construct( - private readonly TypeFactory $typeFactory, + private readonly TypeFactoryInterface $typeFactory, ) {} public function doSomething(): void diff --git a/Documentation/Developer/_Api/_MyController3.php b/Documentation/Developer/_Api/_MyController3.php index fef1ea31..a69fe234 100644 --- a/Documentation/Developer/_Api/_MyController3.php +++ b/Documentation/Developer/_Api/_MyController3.php @@ -4,12 +4,12 @@ namespace MyVendor\MyExtension\Controller; -use Brotkrueml\Schema\Type\TypeFactory; +use Brotkrueml\Schema\Type\TypeFactoryInterface; final class MyController { public function __construct( - private readonly TypeFactory $typeFactory, + private readonly TypeFactoryInterface $typeFactory, ) {} public function doSomething(): void diff --git a/Documentation/Developer/_Api/_MyController4.php b/Documentation/Developer/_Api/_MyController4.php index f1240133..61da6a82 100644 --- a/Documentation/Developer/_Api/_MyController4.php +++ b/Documentation/Developer/_Api/_MyController4.php @@ -4,12 +4,12 @@ namespace MyVendor\MyExtension\Controller; -use Brotkrueml\Schema\Type\TypeFactory; +use Brotkrueml\Schema\Type\TypeFactoryInterface; final class MyController { public function __construct( - private readonly TypeFactory $typeFactory, + private readonly TypeFactoryInterface $typeFactory, ) {} public function doSomething(): void diff --git a/Documentation/Developer/_Api/_MyController5.php b/Documentation/Developer/_Api/_MyController5.php index d0d643db..209921b7 100644 --- a/Documentation/Developer/_Api/_MyController5.php +++ b/Documentation/Developer/_Api/_MyController5.php @@ -5,13 +5,13 @@ namespace MyVendor\MyExtension\Controller; use Brotkrueml\Schema\Manager\SchemaManager; -use Brotkrueml\Schema\Type\TypeFactory; +use Brotkrueml\Schema\Type\TypeFactoryInterface; final class MyController { public function __construct( private readonly SchemaManager $schemaManager, - private readonly TypeFactory $typeFactory, + private readonly TypeFactoryInterface $typeFactory, ) {} public function doSomething(): void diff --git a/Documentation/Developer/_Api/_MyControllerBlankNodeIdentifier.php b/Documentation/Developer/_Api/_MyControllerBlankNodeIdentifier.php index bc31d139..6913873c 100644 --- a/Documentation/Developer/_Api/_MyControllerBlankNodeIdentifier.php +++ b/Documentation/Developer/_Api/_MyControllerBlankNodeIdentifier.php @@ -6,13 +6,13 @@ use Brotkrueml\Schema\Core\Model\BlankNodeIdentifier; use Brotkrueml\Schema\Manager\SchemaManager; -use Brotkrueml\Schema\Type\TypeFactory; +use Brotkrueml\Schema\Type\TypeFactoryInterface; final class MyController { public function __construct( private readonly SchemaManager $schemaManager, - private readonly TypeFactory $typeFactory, + private readonly TypeFactoryInterface $typeFactory, ) {} public function doSomething(): void diff --git a/Documentation/Developer/_Api/_MyControllerMultiple.php b/Documentation/Developer/_Api/_MyControllerMultiple.php index 84ced327..461f0317 100644 --- a/Documentation/Developer/_Api/_MyControllerMultiple.php +++ b/Documentation/Developer/_Api/_MyControllerMultiple.php @@ -5,13 +5,13 @@ namespace MyVendor\MyExtension\Controller; use Brotkrueml\Schema\Manager\SchemaManager; -use Brotkrueml\Schema\Type\TypeFactory; +use Brotkrueml\Schema\Type\TypeFactoryInterface; final class MyController { public function __construct( private readonly SchemaManager $schemaManager, - private readonly TypeFactory $typeFactory, + private readonly TypeFactoryInterface $typeFactory, ) {} public function doSomething(): void diff --git a/Documentation/Developer/_Api/_MyControllerNodeIdentifier.php b/Documentation/Developer/_Api/_MyControllerNodeIdentifier.php index 601a3d5c..623ffa07 100644 --- a/Documentation/Developer/_Api/_MyControllerNodeIdentifier.php +++ b/Documentation/Developer/_Api/_MyControllerNodeIdentifier.php @@ -6,13 +6,13 @@ use Brotkrueml\Schema\Core\Model\NodeIdentifier; use Brotkrueml\Schema\Manager\SchemaManager; -use Brotkrueml\Schema\Type\TypeFactory; +use Brotkrueml\Schema\Type\TypeFactoryInterface; final class MyController { public function __construct( private readonly SchemaManager $schemaManager, - private readonly TypeFactory $typeFactory, + private readonly TypeFactoryInterface $typeFactory, ) {} public function doSomething(): void diff --git a/Documentation/Developer/_Breadcrumb/_MyController.php b/Documentation/Developer/_Breadcrumb/_MyController.php index 2f4c03fe..a07de704 100644 --- a/Documentation/Developer/_Breadcrumb/_MyController.php +++ b/Documentation/Developer/_Breadcrumb/_MyController.php @@ -5,13 +5,13 @@ namespace MyVendor\MyExtension\Controller; use Brotkrueml\Schema\Manager\SchemaManager; -use Brotkrueml\Schema\Type\TypeFactory; +use Brotkrueml\Schema\Type\TypeFactoryInterface; final class MyController { public function __construct( private readonly SchemaManager $schemaManager, - private readonly TypeFactory $typeFactory, + private readonly TypeFactoryInterface $typeFactory, ) {} public function createBreadcrumb(array $breadcrumb): void diff --git a/Documentation/Developer/_ExtendingVocabulary/_MyController.php b/Documentation/Developer/_ExtendingVocabulary/_MyController.php index 0aabea39..300584f4 100644 --- a/Documentation/Developer/_ExtendingVocabulary/_MyController.php +++ b/Documentation/Developer/_ExtendingVocabulary/_MyController.php @@ -5,13 +5,13 @@ namespace MyVendor\MyExtension\Controller; use Brotkrueml\Schema\Manager\SchemaManager; -use Brotkrueml\Schema\Type\TypeFactory; +use Brotkrueml\Schema\Type\TypeFactoryInterface; final class MyController { public function __construct( private readonly SchemaManager $schemaManager, - private readonly TypeFactory $typeFactory, + private readonly TypeFactoryInterface $typeFactory, ) {} public function createVirtualLocation(): void diff --git a/Documentation/Developer/_Index/_MyController1.php b/Documentation/Developer/_Index/_MyController1.php index 7e8397b1..69178b09 100644 --- a/Documentation/Developer/_Index/_MyController1.php +++ b/Documentation/Developer/_Index/_MyController1.php @@ -4,12 +4,12 @@ namespace MyVendor\MyExtension\Controller; -use Brotkrueml\Schema\Type\TypeFactory; +use Brotkrueml\Schema\Type\TypeFactoryInterface; final class MyController { public function __construct( - private readonly TypeFactory $typeFactory, + private readonly TypeFactoryInterface $typeFactory, ) {} public function doSomething(): void diff --git a/Documentation/Developer/_Index/_MyController2.php b/Documentation/Developer/_Index/_MyController2.php index ef1a0c80..a9abeffa 100644 --- a/Documentation/Developer/_Index/_MyController2.php +++ b/Documentation/Developer/_Index/_MyController2.php @@ -5,13 +5,13 @@ namespace MyVendor\MyExtension\Controller; use Brotkrueml\Schema\Manager\SchemaManager; -use Brotkrueml\Schema\Type\TypeFactory; +use Brotkrueml\Schema\Type\TypeFactoryInterface; final class MyController { public function __construct( private readonly SchemaManager $schemaManager, - private readonly TypeFactory $typeFactory, + private readonly TypeFactoryInterface $typeFactory, ) {} public function doSomething(): void diff --git a/Documentation/Developer/_MainEntity/_MyController.php b/Documentation/Developer/_MainEntity/_MyController.php index 6b8986e1..8ae6c69e 100644 --- a/Documentation/Developer/_MainEntity/_MyController.php +++ b/Documentation/Developer/_MainEntity/_MyController.php @@ -5,13 +5,13 @@ namespace MyVendor\MyExtension\Controller; use Brotkrueml\Schema\Manager\SchemaManager; -use Brotkrueml\Schema\Type\TypeFactory; +use Brotkrueml\Schema\Type\TypeFactoryInterface; final class MyController { public function __construct( private readonly SchemaManager $schemaManager, - private readonly TypeFactory $typeFactory, + private readonly TypeFactoryInterface $typeFactory, ) {} public function addMainEntity(): void diff --git a/Documentation/Developer/_WebPage/_MyController1.php b/Documentation/Developer/_WebPage/_MyController1.php index f342738d..2e41cb72 100644 --- a/Documentation/Developer/_WebPage/_MyController1.php +++ b/Documentation/Developer/_WebPage/_MyController1.php @@ -5,13 +5,13 @@ namespace MyVendor\MyExtension\Controller; use Brotkrueml\Schema\Manager\SchemaManager; -use Brotkrueml\Schema\Type\TypeFactory; +use Brotkrueml\Schema\Type\TypeFactoryInterface; final class MyController { public function __construct( private readonly SchemaManager $schemaManager, - private readonly TypeFactory $typeFactory, + private readonly TypeFactoryInterface $typeFactory, ) {} public function doSomething(): void diff --git a/Documentation/Developer/_WebPage/_MyController2.php b/Documentation/Developer/_WebPage/_MyController2.php index ff96fd1e..8a1b2bc4 100644 --- a/Documentation/Developer/_WebPage/_MyController2.php +++ b/Documentation/Developer/_WebPage/_MyController2.php @@ -5,13 +5,13 @@ namespace MyVendor\MyExtension\Controller; use Brotkrueml\Schema\Manager\SchemaManager; -use Brotkrueml\Schema\Type\TypeFactory; +use Brotkrueml\Schema\Type\TypeFactoryInterface; final class MyController { public function __construct( private readonly SchemaManager $schemaManager, - private readonly TypeFactory $typeFactory, + private readonly TypeFactoryInterface $typeFactory, ) {} public function doSomething(): void