Skip to content

Commit

Permalink
wip: introduce TypeFactorySubstitute
Browse files Browse the repository at this point in the history
todo:
- add unit tests
- add documentation
  • Loading branch information
brotkrueml committed Feb 15, 2025
1 parent 42cf072 commit c141e25
Show file tree
Hide file tree
Showing 21 changed files with 123 additions and 36 deletions.
4 changes: 2 additions & 2 deletions Classes/EventListener/AddBreadcrumbList.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions Classes/EventListener/AddWebPageType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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
Expand Down
67 changes: 67 additions & 0 deletions Classes/Testing/TypeFactorySubstitute.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?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\Testing;

use Brotkrueml\Schema\Core\Model\MultipleType;
use Brotkrueml\Schema\Core\Model\TypeInterface;
use Brotkrueml\Schema\Type\ModelClassNotFoundException;
use Brotkrueml\Schema\Type\TypeFactoryInterface;

final class TypeFactorySubstitute implements TypeFactoryInterface
{
/**
* @var array<string, TypeInterface>
*/
private array $types = [];

public function addType(string $name, TypeInterface $class): void
{
$this->types[$name] = $class;
}

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);

Check failure on line 45 in Classes/Testing/TypeFactorySubstitute.php

View workflow job for this annotation

GitHub Actions / Code Quality

Parameter #1 $types of method Brotkrueml\Schema\Testing\TypeFactorySubstitute::createMultiple() expects list<string>, non-empty-array<int|string, string> given.
}

private function createSingle($type): TypeInterface

Check failure on line 48 in Classes/Testing/TypeFactorySubstitute.php

View workflow job for this annotation

GitHub Actions / Code Quality

Method Brotkrueml\Schema\Testing\TypeFactorySubstitute::createSingle() has parameter $type with no type specified.
{
if (isset($this->types[$type])) {
return $this->types[$type];
}

throw ModelClassNotFoundException::fromType($type);
}

/**
* @param list<string> $types
*/
private function createMultiple(array $types): MultipleType
{
return new MultipleType(...\array_map(
fn(string $type): TypeInterface => $this->createSingle($type),
$types,
));
}
}
4 changes: 2 additions & 2 deletions Classes/Type/TypeFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -60,7 +60,7 @@ private function createSingle(string $type): TypeInterface
}

/**
* @param string[] $types
* @param list<string> $types
*/
private function createMultiple(array $types): MultipleType
{
Expand Down
19 changes: 19 additions & 0 deletions Classes/Type/TypeFactoryInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?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\Model\TypeInterface;

interface TypeFactoryInterface
{
public function create(string ...$type): TypeInterface;
}
1 change: 1 addition & 0 deletions Configuration/Services.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@

$excludes = [
__DIR__ . '/../Classes/Extension.php',
__DIR__ . '/../Classes/Testing',
];
if (! $builder->hasDefinition(AdminPanelConfigurationService::class)) {
$excludes[] = __DIR__ . '/../Classes/AdminPanel';
Expand Down
4 changes: 2 additions & 2 deletions Documentation/Developer/_Api/_MyController1.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions Documentation/Developer/_Api/_MyController2.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions Documentation/Developer/_Api/_MyController3.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions Documentation/Developer/_Api/_MyController4.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions Documentation/Developer/_Api/_MyController5.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions Documentation/Developer/_Api/_MyControllerMultiple.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions Documentation/Developer/_Api/_MyControllerNodeIdentifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions Documentation/Developer/_Breadcrumb/_MyController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions Documentation/Developer/_Index/_MyController1.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions Documentation/Developer/_Index/_MyController2.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions Documentation/Developer/_MainEntity/_MyController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions Documentation/Developer/_WebPage/_MyController1.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions Documentation/Developer/_WebPage/_MyController2.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit c141e25

Please sign in to comment.