Skip to content

Commit

Permalink
feat: simplify columns - removed column 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 f39dc95 commit ca155c0
Show file tree
Hide file tree
Showing 23 changed files with 435 additions and 291 deletions.
100 changes: 0 additions & 100 deletions src/Column/ColumnFactoryBuilder.php

This file was deleted.

38 changes: 0 additions & 38 deletions src/Column/ColumnFactoryBuilderInterface.php

This file was deleted.

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

namespace Kreyu\Bundle\DataTableBundle\Column;

use Kreyu\Bundle\DataTableBundle\AbstractRegistry;
use Kreyu\Bundle\DataTableBundle\Column\Extension\ColumnExtensionInterface;
use Kreyu\Bundle\DataTableBundle\Column\Extension\ColumnTypeExtensionInterface;
use Kreyu\Bundle\DataTableBundle\Column\Type\ColumnTypeInterface;
use Kreyu\Bundle\DataTableBundle\Column\Type\ResolvedColumnTypeFactoryInterface;
use Kreyu\Bundle\DataTableBundle\Column\Type\ResolvedColumnTypeInterface;
use Kreyu\Bundle\DataTableBundle\Exception\InvalidArgumentException;
use Kreyu\Bundle\DataTableBundle\Exception\LogicException;
use Kreyu\Bundle\DataTableBundle\Exception\UnexpectedTypeException;

/**
* @extends AbstractRegistry<ColumnTypeInterface, ResolvedColumnTypeInterface, ColumnExtensionInterface>
*/
class ColumnRegistry extends AbstractRegistry implements ColumnRegistryInterface
class ColumnRegistry implements ColumnRegistryInterface
{
/**
* @var array<ColumnTypeInterface>
*/
private array $types;

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

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

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

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

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

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

final protected function getTypeClass(): string
private function resolveType(string $name): ResolvedColumnTypeInterface
{
return ColumnTypeInterface::class;
$type = $this->types[$name] ?? throw new InvalidArgumentException(sprintf('The column 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 column 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 ColumnTypeInterface) {
throw new UnexpectedTypeException($type, ColumnTypeInterface::class);
}

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

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

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

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

namespace Kreyu\Bundle\DataTableBundle\Column;

use Kreyu\Bundle\DataTableBundle\Column\Extension\ColumnExtensionInterface;
use Kreyu\Bundle\DataTableBundle\Column\Type\ColumnTypeInterface;
use Kreyu\Bundle\DataTableBundle\Column\Type\ResolvedColumnTypeInterface;

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

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

This file was deleted.

18 changes: 0 additions & 18 deletions src/Column/Extension/ColumnExtensionInterface.php

This file was deleted.

This file was deleted.

30 changes: 0 additions & 30 deletions src/Column/Extension/PreloadedColumnExtension.php

This file was deleted.

Loading

0 comments on commit ca155c0

Please sign in to comment.