Skip to content

Commit 40aec08

Browse files
committed
Remove direction from types repository
1 parent 0ce76cf commit 40aec08

File tree

5 files changed

+22
-43
lines changed

5 files changed

+22
-43
lines changed

src/Context/BuildingContext.php

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,6 @@ class BuildingContext extends MapperContext implements
1717
use InteractWithTypeRepository;
1818

1919
protected function __construct(
20-
/**
21-
* Gets data transformation direction.
22-
*/
23-
public readonly DirectionInterface $direction,
2420
TypeRepositoryInterface $types,
2521
TypeParserInterface $parser,
2622
TypeExtractorInterface $extractor,
@@ -39,11 +35,9 @@ protected function __construct(
3935

4036
public static function createFromMapperContext(
4137
MapperContext $context,
42-
DirectionInterface $direction,
4338
TypeRepositoryInterface $types,
4439
): self {
4540
return new self(
46-
direction: $direction,
4741
types: $types,
4842
parser: $context->parser,
4943
extractor: $context->extractor,

src/Kernel/Repository/Factory/DefaultTypeRepositoryFactory.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
namespace TypeLang\Mapper\Kernel\Repository\Factory;
66

77
use TypeLang\Mapper\Configuration;
8-
use TypeLang\Mapper\Context\DirectionInterface;
98
use TypeLang\Mapper\Context\MapperContext;
109
use TypeLang\Mapper\Kernel\Repository\DecorateByCoercibleTypeRepository;
1110
use TypeLang\Mapper\Kernel\Repository\DecorateByLoggableTypeRepository;
@@ -97,11 +96,10 @@ public function __construct(
9796
),
9897
) {}
9998

100-
public function createTypeRepository(MapperContext $context, DirectionInterface $direction): TypeRepositoryInterface
99+
public function createTypeRepository(MapperContext $context): TypeRepositoryInterface
101100
{
102101
$types = new TypeRepository(
103102
context: $context,
104-
direction: $direction,
105103
builders: $context->platform->getTypes(),
106104
);
107105

src/Kernel/Repository/Factory/TypeRepositoryFactoryInterface.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
namespace TypeLang\Mapper\Kernel\Repository\Factory;
66

7-
use TypeLang\Mapper\Context\DirectionInterface;
87
use TypeLang\Mapper\Context\MapperContext;
98
use TypeLang\Mapper\Kernel\Repository\TypeRepositoryInterface;
109

@@ -18,8 +17,5 @@
1817
*/
1918
interface TypeRepositoryFactoryInterface
2019
{
21-
public function createTypeRepository(
22-
MapperContext $context,
23-
DirectionInterface $direction,
24-
): TypeRepositoryInterface;
20+
public function createTypeRepository(MapperContext $context): TypeRepositoryInterface;
2521
}

src/Kernel/Repository/TypeRepository.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
namespace TypeLang\Mapper\Kernel\Repository;
66

77
use TypeLang\Mapper\Context\BuildingContext;
8-
use TypeLang\Mapper\Context\DirectionInterface;
98
use TypeLang\Mapper\Context\MapperContext;
109
use TypeLang\Mapper\Exception\Definition\TypeNotFoundException;
1110
use TypeLang\Mapper\Type\Builder\TypeBuilderInterface;
@@ -25,12 +24,13 @@ final class TypeRepository implements
2524

2625
private TypeRepositoryInterface $repository;
2726

27+
private ?BuildingContext $building = null;
28+
2829
/**
2930
* @param iterable<mixed, TypeBuilderInterface> $builders
3031
*/
3132
public function __construct(
3233
private readonly MapperContext $context,
33-
private readonly DirectionInterface $direction,
3434
iterable $builders,
3535
) {
3636
$this->repository = $this;
@@ -47,9 +47,8 @@ public function setTypeRepository(TypeRepositoryInterface $parent): void
4747

4848
private function buildType(TypeStatement $statement): TypeInterface
4949
{
50-
$context = BuildingContext::createFromMapperContext(
50+
$context = $this->building ??= BuildingContext::createFromMapperContext(
5151
context: $this->context,
52-
direction: $this->direction,
5352
types: $this->repository,
5453
);
5554

src/Mapper.php

Lines changed: 17 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -23,22 +23,21 @@
2323

2424
final class Mapper implements NormalizerInterface, DenormalizerInterface
2525
{
26-
/**
27-
* @var \WeakMap<DirectionInterface, TypeRepositoryInterface>
28-
*/
29-
private readonly \WeakMap $repository;
26+
private readonly TypeRepositoryInterface $types;
3027

3128
public readonly MapperContext $context;
3229

3330
public function __construct(
3431
private readonly PlatformInterface $platform = new StandardPlatform(),
3532
private readonly Configuration $config = new Configuration(),
3633
) {
37-
$this->repository = new \WeakMap();
38-
3934
$this->context = $this->createMapperContext(
4035
context: $this->createBootContext(),
4136
);
37+
38+
$this->types = $this->createTypeRepository(
39+
context: $this->context,
40+
);
4241
}
4342

4443
private function createBootContext(): BootContext
@@ -72,15 +71,11 @@ private function createTypeParser(BootContext $context): TypeParserInterface
7271
return $factory->createTypeParser($context);
7372
}
7473

75-
private function getTypeRepository(DirectionInterface $direction): TypeRepositoryInterface
74+
private function createTypeRepository(MapperContext $context): TypeRepositoryInterface
7675
{
77-
$factory = $this->config->getTypeRepositoryFactory();
76+
$factory = $context->config->getTypeRepositoryFactory();
7877

79-
return $this->repository[$direction]
80-
??= $factory->createTypeRepository(
81-
context: $this->context,
82-
direction: $direction,
83-
);
78+
return $factory->createTypeRepository($context);
8479
}
8580

8681
private function createRuntimeContext(mixed $value, DirectionInterface $direction): RuntimeContext
@@ -89,7 +84,7 @@ private function createRuntimeContext(mixed $value, DirectionInterface $directio
8984
context: $this->context,
9085
value: $value,
9186
direction: $direction,
92-
types: $this->getTypeRepository($direction),
87+
types: $this->types,
9388
);
9489
}
9590

@@ -166,11 +161,9 @@ public function canMap(DirectionInterface $direction, mixed $value, #[Language('
166161
* @throws TypeNotFoundException in case of type not found
167162
* @throws \Throwable in case of internal error occurs
168163
*/
169-
public function getType(DirectionInterface $direction, #[Language('PHP')] string $type): TypeInterface
164+
public function getType(#[Language('PHP')] string $type): TypeInterface
170165
{
171-
$repository = $this->getTypeRepository($direction);
172-
173-
return $repository->getTypeByStatement(
166+
return $this->types->getTypeByStatement(
174167
statement: $this->context->getStatementByDefinition($type),
175168
);
176169
}
@@ -183,12 +176,11 @@ public function getType(DirectionInterface $direction, #[Language('PHP')] string
183176
* @throws TypeNotFoundException in case of type not found
184177
* @throws \Throwable in case of internal error occurs
185178
*/
186-
public function getTypeByValue(DirectionInterface $direction, mixed $value): TypeInterface
179+
public function getTypeByValue(mixed $value): TypeInterface
187180
{
188-
return $this->getType(
189-
direction: $direction,
190-
type: $this->context->getDefinitionByValue($value),
191-
);
181+
$definition = $this->context->getDefinitionByValue($value);
182+
183+
return $this->getType($definition);
192184
}
193185

194186
/**
@@ -204,12 +196,12 @@ public function getTypeByValue(DirectionInterface $direction, mixed $value): Typ
204196
* @throws TypeNotFoundException
205197
* @throws \Throwable
206198
*/
207-
public function warmup(DirectionInterface $direction, string|object $class): void
199+
public function warmup(string|object $class): void
208200
{
209201
if (\is_object($class)) {
210202
$class = $class::class;
211203
}
212204

213-
$this->getType($direction, $class);
205+
$this->getType($class);
214206
}
215207
}

0 commit comments

Comments
 (0)