Skip to content

Commit

Permalink
feat: simplify data tables - removed data table extensions, registry …
Browse files Browse the repository at this point in the history
…directly working with container
  • Loading branch information
Kreyu committed Feb 9, 2024
1 parent 12f8c02 commit 7c6d57c
Show file tree
Hide file tree
Showing 29 changed files with 476 additions and 453 deletions.
33 changes: 17 additions & 16 deletions src/DataTableFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,42 +4,43 @@

namespace Kreyu\Bundle\DataTableBundle;

use Kreyu\Bundle\DataTableBundle\Exception\InvalidArgumentException;
use Kreyu\Bundle\DataTableBundle\Query\ProxyQueryFactoryInterface;
use Kreyu\Bundle\DataTableBundle\Query\ProxyQueryInterface;
use Kreyu\Bundle\DataTableBundle\Type\DataTableType;

class DataTableFactory implements DataTableFactoryInterface
{
public function __construct(
private DataTableRegistryInterface $registry,
private ?ProxyQueryFactoryInterface $proxyQueryFactory = null,
private readonly DataTableRegistryInterface $registry,
) {
}

public function create(string $type = DataTableType::class, mixed $query = null, array $options = []): DataTableInterface
public function create(string $type = DataTableType::class, mixed $data = null, array $options = []): DataTableInterface
{
return $this->createBuilder($type, $query, $options)->getDataTable();
return $this->createBuilder($type, $data, $options)->getDataTable();
}

public function createNamed(string $name, string $type = DataTableType::class, mixed $query = null, array $options = []): DataTableInterface
public function createNamed(string $name, string $type = DataTableType::class, mixed $data = null, array $options = []): DataTableInterface
{
return $this->createNamedBuilder($name, $type, $query, $options)->getDataTable();
return $this->createNamedBuilder($name, $type, $data, $options)->getDataTable();
}

public function createBuilder(string $type = DataTableType::class, mixed $query = null, array $options = []): DataTableBuilderInterface
public function createBuilder(string $type = DataTableType::class, mixed $data = null, array $options = []): DataTableBuilderInterface
{
return $this->createNamedBuilder($this->registry->getType($type)->getName(), $type, $query, $options);
return $this->createNamedBuilder($this->registry->getType($type)->getName(), $type, $data, $options);
}

public function createNamedBuilder(string $name, string $type = DataTableType::class, mixed $query = null, array $options = []): DataTableBuilderInterface
public function createNamedBuilder(string $name, string $type = DataTableType::class, mixed $data = null, array $options = []): DataTableBuilderInterface
{
if (null !== $query && !$query instanceof ProxyQueryInterface) {
if (null === $this->proxyQueryFactory) {
throw new InvalidArgumentException(sprintf('Expected query of type %s, %s given', ProxyQueryInterface::class, get_debug_type($query)));
}
$query = $data;

if (null !== $data && !$data instanceof ProxyQueryInterface) {
foreach ($this->registry->getProxyQueryFactories() as $proxyQueryFactory) {
if ($proxyQueryFactory->supports($data)) {
$query = $proxyQueryFactory->create($data);
}

$query = $this->proxyQueryFactory->create($query);
break;
}
}

$type = $this->registry->getType($type);
Expand Down
97 changes: 0 additions & 97 deletions src/DataTableFactoryBuilder.php

This file was deleted.

41 changes: 0 additions & 41 deletions src/DataTableFactoryBuilderInterface.php

This file was deleted.

8 changes: 4 additions & 4 deletions src/DataTableFactoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@

interface DataTableFactoryInterface
{
public function create(string $type, mixed $query = null, array $options = []): DataTableInterface;
public function create(string $type, mixed $data = null, array $options = []): DataTableInterface;

public function createNamed(string $name, string $type, mixed $query = null, array $options = []): DataTableInterface;
public function createNamed(string $name, string $type, mixed $data = null, array $options = []): DataTableInterface;

public function createBuilder(string $type, mixed $query = null, array $options = []): DataTableBuilderInterface;
public function createBuilder(string $type, mixed $data = null, array $options = []): DataTableBuilderInterface;

public function createNamedBuilder(string $name, string $type, mixed $query = null, array $options = []): DataTableBuilderInterface;
public function createNamedBuilder(string $name, string $type, mixed $data = null, array $options = []): DataTableBuilderInterface;
}
128 changes: 116 additions & 12 deletions src/DataTableRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,136 @@

namespace Kreyu\Bundle\DataTableBundle;

use Kreyu\Bundle\DataTableBundle\Extension\DataTableExtensionInterface;
use Kreyu\Bundle\DataTableBundle\Exception\InvalidArgumentException;
use Kreyu\Bundle\DataTableBundle\Exception\LogicException;
use Kreyu\Bundle\DataTableBundle\Exception\UnexpectedTypeException;
use Kreyu\Bundle\DataTableBundle\Extension\DataTableTypeExtensionInterface;
use Kreyu\Bundle\DataTableBundle\Query\ProxyQueryFactoryInterface;
use Kreyu\Bundle\DataTableBundle\Query\ProxyQueryInterface;
use Kreyu\Bundle\DataTableBundle\Type\DataTableTypeInterface;
use Kreyu\Bundle\DataTableBundle\Type\ResolvedDataTableTypeFactoryInterface;
use Kreyu\Bundle\DataTableBundle\Type\ResolvedDataTableTypeInterface;

/**
* @extends AbstractRegistry<DataTableTypeInterface, ResolvedDataTableTypeInterface, DataTableExtensionInterface>
*/
class DataTableRegistry extends AbstractRegistry implements DataTableRegistryInterface
class DataTableRegistry implements DataTableRegistryInterface
{
/**
* @var array<DataTableTypeInterface>
*/
private array $types;

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

/**
* @var array<ProxyQueryFactoryInterface>
*/
private array $proxyQueryFactories;

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

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

/**
* @param iterable<DataTableTypeInterface> $types
* @param iterable<DataTableTypeExtensionInterface> $typeExtensions
* @param iterable<ProxyQueryInterface> $proxyQueryFactories
*/
public function __construct(
iterable $types,
iterable $typeExtensions,
iterable $proxyQueryFactories,
private readonly ResolvedDataTableTypeFactoryInterface $resolvedTypeFactory,
) {
$this->setTypes($types);
$this->setTypeExtensions($typeExtensions);
$this->setProxyQueryFactories($proxyQueryFactories);
}

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

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

public function getProxyQueryFactories(): array
{
return $this->proxyQueryFactories;
}

final protected function getErrorContextName(): string
private function resolveType(string $name): ResolvedDataTableTypeInterface
{
return 'data table';
$type = $this->types[$name] ?? throw new InvalidArgumentException(sprintf('The data table 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 data table 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]);
}
}

final protected function getTypeClass(): string
private function setTypes(iterable $types): void
{
return DataTableTypeInterface::class;
$this->types = [];

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

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

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

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

foreach ($typeExtension::getExtendedTypes() as $extendedType) {
$this->typeExtensions[$extendedType][] = $typeExtension;
}
}
}

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

foreach ($proxyQueryFactories as $proxyQueryFactory) {
if (!$proxyQueryFactory instanceof ProxyQueryFactoryInterface) {
throw new UnexpectedTypeException($proxyQueryFactory, ProxyQueryFactoryInterface::class);
}

$this->proxyQueryFactories[] = $proxyQueryFactory;
}
}
}
11 changes: 8 additions & 3 deletions src/DataTableRegistryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Kreyu\Bundle\DataTableBundle;

use Kreyu\Bundle\DataTableBundle\Extension\DataTableExtensionInterface;
use Kreyu\Bundle\DataTableBundle\Query\ProxyQueryFactoryInterface;
use Kreyu\Bundle\DataTableBundle\Type\DataTableTypeInterface;
use Kreyu\Bundle\DataTableBundle\Type\ResolvedDataTableTypeInterface;

Expand All @@ -16,7 +16,12 @@ interface DataTableRegistryInterface
public function getType(string $name): ResolvedDataTableTypeInterface;

/**
* @return iterable<DataTableExtensionInterface>
* @param class-string<DataTableTypeInterface> $name
*/
public function hasType(string $name): bool;

/**
* @return array<ProxyQueryFactoryInterface>
*/
public function getExtensions(): iterable;
public function getProxyQueryFactories(): array;
}
Loading

0 comments on commit 7c6d57c

Please sign in to comment.