Add Doctrine extension.

This commit is contained in:
Pol Dellaiera 2021-06-30 15:47:38 +02:00
parent b41b1346e5
commit d208a79764
2 changed files with 52 additions and 2 deletions

View File

@ -32,9 +32,8 @@ use Chill\MainBundle\Doctrine\DQL\JsonAggregate;
use Chill\MainBundle\Doctrine\DQL\JsonbExistsInArray;
use Chill\MainBundle\Doctrine\DQL\Similarity;
use Chill\MainBundle\Doctrine\DQL\OverlapsI;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Reference;
use Chill\MainBundle\Doctrine\DQL\Replace;
use Chill\MainBundle\Doctrine\ORM\Hydration\FlatHierarchyEntityHydrator;
use Chill\MainBundle\Doctrine\Type\NativeDateIntervalType;
use Chill\MainBundle\Doctrine\Type\PointType;
use Symfony\Component\HttpFoundation\Request;
@ -186,6 +185,9 @@ class ChillMainExtension extends Extension implements PrependExtensionInterface,
'OVERLAPSI' => OverlapsI::class,
],
],
'hydrators' => [
'chill_flat_hierarchy_list' => FlatHierarchyEntityHydrator::class,
],
],
],
);

View File

@ -0,0 +1,48 @@
<?php
declare(strict_types=1);
namespace Chill\MainBundle\Doctrine\ORM\Hydration;
use Doctrine\ORM\Internal\Hydration\ObjectHydrator;
use Generator;
final class FlatHierarchyEntityHydrator extends ObjectHydrator
{
public const LIST = 'chill_flat_hierarchy_list';
protected function hydrateAllData(): array
{
$generator = $this->buildRecursively(
parent::hydrateAllData(),
static fn($value): iterable => $value->getChildren(),
);
$result = [];
foreach ($generator as $entity) {
// We cannot expect anything from the object entity
// so, we cannot expect it to have a ::getId() method.
// So we use spl_object_hash() instead.
$result += [spl_object_hash($entity) => $entity];
}
return array_values($result);
}
/**
* @param iterable<int, SocialIssue> $iterable
* @param callable(SocialIssue): iterable<int, SocialIssue> $childrenAccessor
*
* @return Generator<int, SocialIssue>
*/
private function buildRecursively(iterable $iterable, callable $childrenAccessor): Generator
{
foreach ($iterable as $item)
{
yield $item;
yield from $this->buildRecursively($childrenAccessor($item), $childrenAccessor);
}
}
}