Skip to content

Commit

Permalink
feat: simplify actions - removed action extensions, registry directly…
Browse files Browse the repository at this point in the history
… working with container
  • Loading branch information
Kreyu committed Feb 10, 2024
1 parent b5edc26 commit 36a0851
Show file tree
Hide file tree
Showing 22 changed files with 433 additions and 278 deletions.
88 changes: 0 additions & 88 deletions src/Action/ActionFactoryBuilder.php

This file was deleted.

38 changes: 0 additions & 38 deletions src/Action/ActionFactoryBuilderInterface.php

This file was deleted.

101 changes: 88 additions & 13 deletions src/Action/ActionRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,108 @@

namespace Kreyu\Bundle\DataTableBundle\Action;

use Kreyu\Bundle\DataTableBundle\AbstractRegistry;
use Kreyu\Bundle\DataTableBundle\Action\Extension\ActionExtensionInterface;
use Kreyu\Bundle\DataTableBundle\Action\Extension\ActionTypeExtensionInterface;
use Kreyu\Bundle\DataTableBundle\Action\Type\ActionTypeInterface;
use Kreyu\Bundle\DataTableBundle\Action\Type\ResolvedActionTypeFactoryInterface;
use Kreyu\Bundle\DataTableBundle\Action\Type\ResolvedActionTypeInterface;
use Kreyu\Bundle\DataTableBundle\Exception\InvalidArgumentException;
use Kreyu\Bundle\DataTableBundle\Exception\LogicException;
use Kreyu\Bundle\DataTableBundle\Exception\UnexpectedTypeException;

/**
* @extends AbstractRegistry<ActionTypeInterface, ResolvedActionTypeInterface, ActionExtensionInterface>
*/
class ActionRegistry extends AbstractRegistry implements ActionRegistryInterface
class ActionRegistry implements ActionRegistryInterface
{
/**
* @var array<ActionTypeInterface>
*/
private array $types;

/**
* @var array<ActionTypeExtensionInterface>
*/
private array $typeExtensions;

/**
* @var array<ResolvedActionTypeFactoryInterface>
*/
private array $resolvedTypes;

/**
* @var array<class-string<ActionTypeInterface>, bool>
*/
private array $checkedTypes;

/**
* @param iterable<ActionTypeInterface> $types
* @param iterable<ActionTypeExtensionInterface> $typeExtensions
*/
public function __construct(
iterable $types,
iterable $typeExtensions,
private readonly ResolvedActionTypeFactoryInterface $resolvedTypeFactory,
) {
$this->setTypes($types);
$this->setTypeExtensions($typeExtensions);
}

public function getType(string $name): ResolvedActionTypeInterface
{
return $this->doGetType($name);
return $this->resolvedTypes[$name] ??= $this->resolveType($name);
}

final protected function getErrorContextName(): string
public function hasType(string $name): bool
{
return 'action';
return isset($this->types[$name]);
}

final protected function getTypeClass(): string
private function resolveType(string $name): ResolvedActionTypeInterface
{
return ActionTypeInterface::class;
$type = $this->types[$name] ?? throw new InvalidArgumentException(sprintf('The action type %s does not exist', $name));

if (isset($this->checkedTypes[$fqcn = $type::class])) {
$types = implode(' > ', array_merge(array_keys($this->checkedTypes), [$fqcn]));
throw new LogicException(sprintf('Circular reference detected for action type "%s" (%s).', $fqcn, $types));
}

$this->checkedTypes[$fqcn] = true;

$parentType = $type->getParent();

try {
return $this->resolvedTypeFactory->createResolvedType(
type: $type,
typeExtensions: $this->typeExtensions[$type::class] ?? [],
parent: $parentType ? $this->getType($parentType) : null,
);
} finally {
unset($this->checkedTypes[$fqcn]);
}
}

private function setTypes(iterable $types): void
{
$this->types = [];

foreach ($types as $type) {
if (!$type instanceof ActionTypeInterface) {
throw new UnexpectedTypeException($type, ActionTypeInterface::class);
}

$this->types[$type::class] = $type;
}
}

final protected function getExtensionClass(): string
private function setTypeExtensions(iterable $typeExtensions): void
{
return ActionExtensionInterface::class;
$this->typeExtensions = [];

foreach ($typeExtensions as $typeExtension) {
if (!$typeExtension instanceof ActionTypeExtensionInterface) {
throw new UnexpectedTypeException($typeExtension, ActionTypeExtensionInterface::class);
}

foreach ($typeExtension::getExtendedTypes() as $extendedType) {
$this->typeExtensions[$extendedType][] = $typeExtension;
}
}
}
}
5 changes: 2 additions & 3 deletions src/Action/ActionRegistryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Kreyu\Bundle\DataTableBundle\Action;

use Kreyu\Bundle\DataTableBundle\Action\Extension\ActionExtensionInterface;
use Kreyu\Bundle\DataTableBundle\Action\Type\ActionTypeInterface;
use Kreyu\Bundle\DataTableBundle\Action\Type\ResolvedActionTypeInterface;

Expand All @@ -16,7 +15,7 @@ interface ActionRegistryInterface
public function getType(string $name): ResolvedActionTypeInterface;

/**
* @return iterable<ActionExtensionInterface>
* @param class-string<ActionTypeInterface> $name
*/
public function getExtensions(): iterable;
public function hasType(string $name): bool;
}
34 changes: 0 additions & 34 deletions src/Action/Extension/AbstractActionExtension.php

This file was deleted.

18 changes: 0 additions & 18 deletions src/Action/Extension/ActionExtensionInterface.php

This file was deleted.

This file was deleted.

30 changes: 0 additions & 30 deletions src/Action/Extension/PreloadedActionExtension.php

This file was deleted.

Loading

0 comments on commit 36a0851

Please sign in to comment.