Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FRAM-57] Entity denormalisation (#4)
* feat: Entity denormalisation (#FRAM-57) * ci: fix psalm types
- Loading branch information
1 parent
0bf5199
commit 86f5eb2
Showing
60 changed files
with
1,742 additions
and
561 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
src/Bundle/DependencyInjection/Compiler/RegisterIndexConfigurationCompilerPass.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
|
||
namespace Bdf\Prime\Indexer\Bundle\DependencyInjection\Compiler; | ||
|
||
use Bdf\Prime\Indexer\Resolver\MappingResolver; | ||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
|
||
/** | ||
* Locate all services with tag "prime.indexer.configuration" and register into the mapping resolver | ||
* | ||
* @see MappingResolver::register() | ||
*/ | ||
final class RegisterIndexConfigurationCompilerPass implements CompilerPassInterface | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function process(ContainerBuilder $container) | ||
{ | ||
$resolverDefinition = $container->findDefinition(MappingResolver::class); | ||
|
||
foreach ($container->findTaggedServiceIds('prime.indexer.configuration') as $id => $_) { | ||
try { | ||
$definition = $container->findDefinition($id); | ||
$definition->setPublic(true); | ||
|
||
$className = $definition->getClass(); | ||
|
||
if (!$className || !class_exists($className)) { | ||
continue; | ||
} | ||
|
||
$r = new \ReflectionClass($className); | ||
$config = $r->newInstanceWithoutConstructor(); | ||
|
||
$resolverDefinition->addMethodCall('register', [$id, $config->entity()]); | ||
} catch (\Exception $e) { | ||
// Ignore | ||
} | ||
} | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
src/Bundle/DependencyInjection/Compiler/RegisterIndexFactoryCompilerPass.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
|
||
namespace Bdf\Prime\Indexer\Bundle\DependencyInjection\Compiler; | ||
|
||
use Bdf\Prime\Indexer\Bundle\Factory\IndexFactoryInterface; | ||
use Bdf\Prime\Indexer\IndexFactory; | ||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Reference; | ||
|
||
/** | ||
* Register custom index factories tagged with "prime.indexer.factory" | ||
* | ||
* @see IndexFactory | ||
*/ | ||
final class RegisterIndexFactoryCompilerPass implements CompilerPassInterface | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function process(ContainerBuilder $container) | ||
{ | ||
$factory = $container->findDefinition(IndexFactory::class); | ||
$factories = []; | ||
|
||
foreach ($container->findTaggedServiceIds('prime.indexer.factory') as $id => $tags) { | ||
/** @var class-string<IndexFactoryInterface> $factoryClass */ | ||
$factoryClass = $container->findDefinition($id)->getClass(); | ||
|
||
$factories[$factoryClass::type()] = new Reference($id); | ||
} | ||
|
||
$factory->replaceArgument(0, $factories); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
namespace Bdf\Prime\Indexer\Bundle\Factory; | ||
|
||
use Bdf\Prime\Indexer\Denormalize\DenormalizedIndex; | ||
use Bdf\Prime\Indexer\Denormalize\DenormalizerInterface; | ||
use Bdf\Prime\Indexer\IndexFactory; | ||
use Bdf\Prime\Indexer\IndexInterface; | ||
|
||
/** | ||
* Index factory for denormalized index | ||
* | ||
* @implements IndexFactoryInterface<DenormalizerInterface> | ||
*/ | ||
final class DenormalizerIndexFactory implements IndexFactoryInterface | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
* | ||
* @param DenormalizerInterface $config | ||
*/ | ||
public function __invoke($config, IndexFactory $factory = null): IndexInterface | ||
{ | ||
if (!$factory) { | ||
throw new \InvalidArgumentException('$factory is mandatory'); | ||
} | ||
|
||
return new DenormalizedIndex($config, $factory->for($config->denormalizedClass())); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public static function type(): string | ||
{ | ||
return DenormalizerInterface::class; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
<?php | ||
|
||
namespace Bdf\Prime\Indexer\Denormalize; | ||
|
||
use Bdf\Collection\Stream\Streams; | ||
use Bdf\Prime\Indexer\IndexInterface; | ||
use Bdf\Prime\Indexer\QueryInterface; | ||
|
||
/** | ||
* Index adapter for handle denormalizer | ||
*/ | ||
final class DenormalizedIndex implements IndexInterface | ||
{ | ||
private DenormalizerInterface $denormalizer; | ||
private IndexInterface $index; | ||
|
||
/** | ||
* DenormalizedIndex constructor. | ||
* | ||
* @param DenormalizerInterface $denormalizer | ||
* @param IndexInterface $index | ||
*/ | ||
public function __construct(DenormalizerInterface $denormalizer, IndexInterface $index) | ||
{ | ||
$this->denormalizer = $denormalizer; | ||
$this->index = $index; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function config() | ||
{ | ||
return $this->denormalizer; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function add($entity): void | ||
{ | ||
$this->index->add($this->denormalizer->denormalize($entity)); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function contains($entity): bool | ||
{ | ||
return $this->index->contains($this->denormalizer->denormalize($entity)); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function remove($entity): void | ||
{ | ||
$this->index->remove($this->denormalizer->denormalize($entity)); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function update($entity, ?array $attributes = null): void | ||
{ | ||
$this->index->update($this->denormalizer->denormalize($entity), $attributes); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function query(bool $withDefaultScope = true): QueryInterface | ||
{ | ||
return $this->index->query($withDefaultScope); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function create(iterable $entities = [], array $options = []): void | ||
{ | ||
$this->index->create( | ||
Streams::wrap($entities)->map([$this->denormalizer, 'denormalize']), | ||
$options | ||
); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function drop(): void | ||
{ | ||
$this->index->drop(); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function __call(string $name, array $arguments): QueryInterface | ||
{ | ||
return $this->index->$name(...$arguments); | ||
} | ||
} |
Oops, something went wrong.