Skip to content

Commit

Permalink
misc: merge mapper and normalizer builders
Browse files Browse the repository at this point in the history
  • Loading branch information
romm committed Sep 18, 2023
1 parent 844f089 commit ee212db
Show file tree
Hide file tree
Showing 9 changed files with 139 additions and 182 deletions.
100 changes: 79 additions & 21 deletions src/Library/MapperContainer.php → src/Library/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,19 @@

namespace CuyZ\Valinor\Library;

use CuyZ\Valinor\Cache\ChainCache;
use CuyZ\Valinor\Cache\KeySanitizerCache;
use CuyZ\Valinor\Cache\RuntimeCache;
use CuyZ\Valinor\Cache\Warmup\RecursiveCacheWarmupService;
use CuyZ\Valinor\Definition\FunctionsContainer;
use CuyZ\Valinor\Definition\Repository\AttributesRepository;
use CuyZ\Valinor\Definition\Repository\Cache\CacheClassDefinitionRepository;
use CuyZ\Valinor\Definition\Repository\Cache\CacheFunctionDefinitionRepository;
use CuyZ\Valinor\Definition\Repository\ClassDefinitionRepository;
use CuyZ\Valinor\Definition\Repository\FunctionDefinitionRepository;
use CuyZ\Valinor\Definition\Repository\Reflection\NativeAttributesRepository;
use CuyZ\Valinor\Definition\Repository\Reflection\ReflectionClassDefinitionRepository;
use CuyZ\Valinor\Definition\Repository\Reflection\ReflectionFunctionDefinitionRepository;
use CuyZ\Valinor\Mapper\ArgumentsMapper;
use CuyZ\Valinor\Mapper\Object\Factory\CacheObjectBuilderFactory;
use CuyZ\Valinor\Mapper\Object\Factory\CollisionObjectBuilderFactory;
Expand All @@ -22,14 +30,14 @@
use CuyZ\Valinor\Mapper\Tree\Builder\ArrayNodeBuilder;
use CuyZ\Valinor\Mapper\Tree\Builder\CasterNodeBuilder;
use CuyZ\Valinor\Mapper\Tree\Builder\CasterProxyNodeBuilder;
use CuyZ\Valinor\Mapper\Tree\Builder\ObjectNodeBuilder;
use CuyZ\Valinor\Mapper\Tree\Builder\ErrorCatcherNodeBuilder;
use CuyZ\Valinor\Mapper\Tree\Builder\InterfaceNodeBuilder;
use CuyZ\Valinor\Mapper\Tree\Builder\IterableNodeBuilder;
use CuyZ\Valinor\Mapper\Tree\Builder\ListNodeBuilder;
use CuyZ\Valinor\Mapper\Tree\Builder\NativeClassNodeBuilder;
use CuyZ\Valinor\Mapper\Tree\Builder\NodeBuilder;
use CuyZ\Valinor\Mapper\Tree\Builder\ObjectImplementations;
use CuyZ\Valinor\Mapper\Tree\Builder\ObjectNodeBuilder;
use CuyZ\Valinor\Mapper\Tree\Builder\NativeClassNodeBuilder;
use CuyZ\Valinor\Mapper\Tree\Builder\RootNodeBuilder;
use CuyZ\Valinor\Mapper\Tree\Builder\ScalarNodeBuilder;
use CuyZ\Valinor\Mapper\Tree\Builder\ShapedArrayNodeBuilder;
Expand All @@ -39,7 +47,12 @@
use CuyZ\Valinor\Mapper\TreeMapper;
use CuyZ\Valinor\Mapper\TypeArgumentsMapper;
use CuyZ\Valinor\Mapper\TypeTreeMapper;
use CuyZ\Valinor\Normalizer\FunctionsCheckerNormalizer;
use CuyZ\Valinor\Normalizer\Normalizer;
use CuyZ\Valinor\Normalizer\RecursiveNormalizer;
use CuyZ\Valinor\Type\ClassType;
use CuyZ\Valinor\Type\Parser\Factory\LexingTypeParserFactory;
use CuyZ\Valinor\Type\Parser\Factory\TypeParserFactory;
use CuyZ\Valinor\Type\Parser\TypeParser;
use CuyZ\Valinor\Type\ScalarType;
use CuyZ\Valinor\Type\Types\ArrayType;
Expand All @@ -54,27 +67,24 @@
use function count;

/** @internal */
final class MapperContainer
final class Container
{
private SharedContainer $shared;

/** @var array<class-string, object> */
private array $services = [];

/** @var array<class-string, callable(): object> */
private array $factories;

public function __construct(MapperSettings $settings)
public function __construct(Settings $settings)
{
$this->shared = SharedContainer::new($settings->cache ?? null);
$this->factories = [
TreeMapper::class => fn () => new TypeTreeMapper(
$this->shared->get(TypeParser::class),
$this->get(TypeParser::class),
$this->get(RootNodeBuilder::class)
),

ArgumentsMapper::class => fn () => new TypeArgumentsMapper(
$this->shared->get(FunctionDefinitionRepository::class),
$this->get(FunctionDefinitionRepository::class),
$this->get(RootNodeBuilder::class)
),

Expand All @@ -95,7 +105,7 @@ public function __construct(MapperSettings $settings)
ShapedArrayType::class => new ShapedArrayNodeBuilder($settings->allowSuperfluousKeys),
ScalarType::class => new ScalarNodeBuilder($settings->enableFlexibleCasting),
ClassType::class => new NativeClassNodeBuilder(
$this->shared->get(ClassDefinitionRepository::class),
$this->get(ClassDefinitionRepository::class),
$this->get(ObjectBuilderFactory::class),
$this->get(ObjectNodeBuilder::class),
$settings->enableFlexibleCasting,
Expand All @@ -104,7 +114,7 @@ public function __construct(MapperSettings $settings)

$builder = new UnionNodeBuilder(
$builder,
$this->shared->get(ClassDefinitionRepository::class),
$this->get(ClassDefinitionRepository::class),
$this->get(ObjectBuilderFactory::class),
$this->get(ObjectNodeBuilder::class),
$settings->enableFlexibleCasting
Expand All @@ -113,7 +123,7 @@ public function __construct(MapperSettings $settings)
$builder = new InterfaceNodeBuilder(
$builder,
$this->get(ObjectImplementations::class),
$this->shared->get(ClassDefinitionRepository::class),
$this->get(ClassDefinitionRepository::class),
$this->get(ObjectBuilderFactory::class),
$this->get(ObjectNodeBuilder::class),
$settings->enableFlexibleCasting
Expand All @@ -126,7 +136,7 @@ public function __construct(MapperSettings $settings)
$builder = new ValueAlteringNodeBuilder(
$builder,
new FunctionsContainer(
$this->shared->get(FunctionDefinitionRepository::class),
$this->get(FunctionDefinitionRepository::class),
$settings->valueModifier
)
);
Expand All @@ -141,22 +151,22 @@ public function __construct(MapperSettings $settings)

ObjectImplementations::class => fn () => new ObjectImplementations(
new FunctionsContainer(
$this->shared->get(FunctionDefinitionRepository::class),
$this->get(FunctionDefinitionRepository::class),
$settings->inferredMapping
),
$this->shared->get(TypeParser::class),
$this->get(TypeParser::class),
),

ObjectBuilderFactory::class => function () use ($settings) {
$constructors = new FunctionsContainer(
$this->shared->get(FunctionDefinitionRepository::class),
$this->get(FunctionDefinitionRepository::class),
$settings->customConstructors
);

$factory = new ReflectionObjectBuilderFactory();
$factory = new ConstructorObjectBuilderFactory($factory, $settings->nativeConstructors, $constructors);
$factory = new DateTimeZoneObjectBuilderFactory($factory, $this->shared->get(FunctionDefinitionRepository::class));
$factory = new DateTimeObjectBuilderFactory($factory, $settings->supportedDateFormats, $this->shared->get(FunctionDefinitionRepository::class));
$factory = new DateTimeZoneObjectBuilderFactory($factory, $this->get(FunctionDefinitionRepository::class));
$factory = new DateTimeObjectBuilderFactory($factory, $settings->supportedDateFormats, $this->get(FunctionDefinitionRepository::class));
$factory = new CollisionObjectBuilderFactory($factory);

if (! $settings->allowPermissiveTypes) {
Expand All @@ -169,13 +179,56 @@ public function __construct(MapperSettings $settings)
return new CacheObjectBuilderFactory($factory, $cache);
},

Normalizer::class => function () use ($settings) {
$functions = new FunctionsContainer(
$this->get(FunctionDefinitionRepository::class),
$settings->sortedHandlers()
);

$normalizer = new RecursiveNormalizer($functions);

return new FunctionsCheckerNormalizer($normalizer, $functions);
},

ClassDefinitionRepository::class => fn () => new CacheClassDefinitionRepository(
new ReflectionClassDefinitionRepository(
$this->get(TypeParserFactory::class),
$this->get(AttributesRepository::class),
),
$this->get(CacheInterface::class)
),

FunctionDefinitionRepository::class => fn () => new CacheFunctionDefinitionRepository(
new ReflectionFunctionDefinitionRepository(
$this->get(TypeParserFactory::class),
$this->get(AttributesRepository::class),
),
$this->get(CacheInterface::class)
),

AttributesRepository::class => fn () => new NativeAttributesRepository(),

TypeParserFactory::class => fn () => new LexingTypeParserFactory(),

TypeParser::class => fn () => $this->get(TypeParserFactory::class)->get(),

RecursiveCacheWarmupService::class => fn () => new RecursiveCacheWarmupService(
$this->shared->get(TypeParser::class),
$this->shared->get(CacheInterface::class),
$this->get(TypeParser::class),
$this->get(CacheInterface::class),
$this->get(ObjectImplementations::class),
$this->shared->get(ClassDefinitionRepository::class),
$this->get(ClassDefinitionRepository::class),
$this->get(ObjectBuilderFactory::class)
),

CacheInterface::class => function () use ($settings) {
$cache = new RuntimeCache();

if (isset($settings->cache)) {
$cache = new ChainCache($cache, new KeySanitizerCache($settings->cache));
}

return $cache;
},
];
}

Expand All @@ -189,6 +242,11 @@ public function argumentsMapper(): ArgumentsMapper
return $this->get(ArgumentsMapper::class);
}

public function normalizer(): Normalizer
{
return $this->get(Normalizer::class);
}

public function cacheWarmupService(): RecursiveCacheWarmupService
{
return $this->get(RecursiveCacheWarmupService::class);
Expand Down
55 changes: 0 additions & 55 deletions src/Library/NormalizerContainer.php

This file was deleted.

35 changes: 0 additions & 35 deletions src/Library/NormalizerSettings.php

This file was deleted.

21 changes: 20 additions & 1 deletion src/Library/MapperSettings.php → src/Library/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
use Throwable;

/** @internal */
final class MapperSettings
final class Settings
{
/** @var non-empty-array<non-empty-string> */
public const DEFAULT_SUPPORTED_DATETIME_FORMATS = [
Expand Down Expand Up @@ -47,9 +47,28 @@ final class MapperSettings
/** @var callable(Throwable): ErrorMessage */
public $exceptionFilter;

/** @var array<int, list<callable>> */
public array $handlers = [];

public function __construct()
{
$this->inferredMapping[DateTimeInterface::class] = static fn () => DateTimeImmutable::class;
$this->exceptionFilter = fn (Throwable $exception) => throw $exception;
}

/**
* @return array<callable>
*/
public function sortedHandlers(): array
{
krsort($this->handlers);

$callables = [];

foreach ($this->handlers as $list) {
$callables = [...$callables, ...$list];
}

return $callables;
}
}
4 changes: 2 additions & 2 deletions src/Mapper/Object/Factory/DateTimeObjectBuilderFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use CuyZ\Valinor\Definition\ClassDefinition;
use CuyZ\Valinor\Definition\FunctionObject;
use CuyZ\Valinor\Definition\Repository\FunctionDefinitionRepository;
use CuyZ\Valinor\Library\MapperSettings;
use CuyZ\Valinor\Library\Settings;
use CuyZ\Valinor\Mapper\Object\DateTimeFormatConstructor;
use CuyZ\Valinor\Mapper\Object\FunctionObjectBuilder;
use CuyZ\Valinor\Mapper\Object\NativeConstructorObjectBuilder;
Expand Down Expand Up @@ -44,7 +44,7 @@ public function for(ClassDefinition $class): array

$buildersWithOneArgument = array_filter($builders, fn (ObjectBuilder $builder) => count($builder->describeArguments()) === 1);

if (count($buildersWithOneArgument) === 0 || $this->supportedDateFormats !== MapperSettings::DEFAULT_SUPPORTED_DATETIME_FORMATS) {
if (count($buildersWithOneArgument) === 0 || $this->supportedDateFormats !== Settings::DEFAULT_SUPPORTED_DATETIME_FORMATS) {
$builders[] = $this->internalDateTimeBuilder($class->type());
}

Expand Down
Loading

0 comments on commit ee212db

Please sign in to comment.