Skip to content

Commit c3256bf

Browse files
author
Kirill Nesmeyanov
committed
Split types parser and types repository
1 parent 04c4038 commit c3256bf

35 files changed

+379
-176
lines changed

src/Mapper.php

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,39 @@
1212
use TypeLang\Mapper\Runtime\Configuration;
1313
use TypeLang\Mapper\Runtime\Context\RootContext;
1414
use TypeLang\Mapper\Runtime\EvolvableConfigurationInterface;
15+
use TypeLang\Mapper\Runtime\Parser\InMemoryTypeParser;
16+
use TypeLang\Mapper\Runtime\Parser\TypeParser;
17+
use TypeLang\Mapper\Runtime\Parser\TypeParserInterface;
18+
use TypeLang\Mapper\Runtime\Repository\InMemoryTypeRepository;
1519
use TypeLang\Mapper\Runtime\Repository\TypeRepository;
20+
use TypeLang\Mapper\Runtime\Repository\TypeRepositoryInterface;
1621
use TypeLang\Mapper\Type\TypeInterface;
1722

1823
final class Mapper implements
1924
NormalizerInterface,
2025
DenormalizerInterface,
2126
EvolvableConfigurationInterface
2227
{
23-
private readonly TypeRepository $types;
28+
private readonly TypeRepositoryInterface $types;
29+
30+
private readonly TypeParserInterface $parser;
2431

2532
public function __construct(
2633
private readonly PlatformInterface $platform = new StandardPlatform(),
2734
private Configuration $config = new Configuration(),
2835
) {
29-
$this->types = new TypeRepository($this->platform);
36+
$this->parser = new InMemoryTypeParser(
37+
delegate: TypeParser::createFromPlatform(
38+
platform: $platform,
39+
),
40+
);
41+
42+
$this->types = new InMemoryTypeRepository(
43+
delegate: TypeRepository::createFromPlatform(
44+
platform: $platform,
45+
parser: $this->parser,
46+
),
47+
);
3048
}
3149

3250
public function withObjectsAsArrays(?bool $enabled = null): self
@@ -60,11 +78,21 @@ public function getPlatform(): PlatformInterface
6078
*
6179
* @api
6280
*/
63-
public function getTypes(): TypeRepository
81+
public function getTypes(): TypeRepositoryInterface
6482
{
6583
return $this->types;
6684
}
6785

86+
/**
87+
* Returns current types parser.
88+
*
89+
* @api
90+
*/
91+
public function getParser(): TypeParserInterface
92+
{
93+
return $this->parser;
94+
}
95+
6896
/**
6997
* @throws RuntimeExceptionInterface
7098
* @throws TypeNotFoundException
@@ -77,6 +105,7 @@ public function normalize(mixed $value, #[Language('PHP')] ?string $type = null)
77105
return $instance->cast($value, RootContext::forNormalization(
78106
value: $value,
79107
config: $this->config,
108+
parser: $this->parser,
80109
types: $this->types,
81110
));
82111
}
@@ -92,6 +121,7 @@ public function isNormalizable(mixed $value, #[Language('PHP')] ?string $type =
92121
return $instance->match($value, RootContext::forNormalization(
93122
value: $value,
94123
config: $this->config,
124+
parser: $this->parser,
95125
types: $this->types,
96126
));
97127
}
@@ -108,6 +138,7 @@ public function denormalize(mixed $value, #[Language('PHP')] string $type): mixe
108138
return $instance->cast($value, RootContext::forDenormalization(
109139
value: $value,
110140
config: $this->config,
141+
parser: $this->parser,
111142
types: $this->types,
112143
));
113144
}
@@ -123,6 +154,7 @@ public function isDenormalizable(mixed $value, #[Language('PHP')] string $type):
123154
return $instance->match($value, RootContext::forDenormalization(
124155
value: $value,
125156
config: $this->config,
157+
parser: $this->parser,
126158
types: $this->types,
127159
));
128160
}

src/Mapping/Driver/AttributeDriver.php

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,18 @@
1111
use TypeLang\Mapper\Mapping\Metadata\ClassMetadata;
1212
use TypeLang\Mapper\Mapping\Metadata\TypeMetadata;
1313
use TypeLang\Mapper\Mapping\SkipWhen;
14-
use TypeLang\Mapper\Runtime\Repository\TypeRepository;
14+
use TypeLang\Mapper\Runtime\Parser\TypeParserInterface;
15+
use TypeLang\Mapper\Runtime\Repository\TypeRepositoryInterface;
1516

1617
final class AttributeDriver extends LoadableDriver
1718
{
1819
#[\Override]
19-
protected function load(\ReflectionClass $reflection, ClassMetadata $class, TypeRepository $types): void
20-
{
20+
protected function load(
21+
\ReflectionClass $reflection,
22+
ClassMetadata $class,
23+
TypeRepositoryInterface $types,
24+
TypeParserInterface $parser,
25+
): void {
2126
foreach ($reflection->getProperties() as $property) {
2227
$metadata = $class->getPropertyOrCreate($property->getName());
2328

@@ -28,7 +33,7 @@ protected function load(\ReflectionClass $reflection, ClassMetadata $class, Type
2833
$attribute = $this->findPropertyAttribute($property, MapType::class);
2934

3035
if ($attribute !== null) {
31-
$type = $this->createType($attribute->type, $property, $types);
36+
$type = $this->createType($attribute->type, $property, $types, $parser);
3237

3338
$metadata->setTypeInfo($type);
3439
}
@@ -50,7 +55,7 @@ protected function load(\ReflectionClass $reflection, ClassMetadata $class, Type
5055
$attribute = $this->findPropertyAttribute($property, SkipWhen::class);
5156

5257
if ($attribute !== null) {
53-
$type = $this->createType($attribute->type, $property, $types);
58+
$type = $this->createType($attribute->type, $property, $types, $parser);
5459

5560
$metadata->setSkipCondition($type);
5661
}
@@ -63,9 +68,13 @@ protected function load(\ReflectionClass $reflection, ClassMetadata $class, Type
6368
* @throws PropertyTypeNotFoundException
6469
* @throws \Throwable
6570
*/
66-
private function createType(string $type, \ReflectionProperty $property, TypeRepository $types): TypeMetadata
67-
{
68-
$statement = $types->getStatementByType($type);
71+
private function createType(
72+
string $type,
73+
\ReflectionProperty $property,
74+
TypeRepositoryInterface $types,
75+
TypeParserInterface $parser,
76+
): TypeMetadata {
77+
$statement = $parser->getStatementByType($type);
6978

7079
$class = $property->getDeclaringClass();
7180

src/Mapping/Driver/DocBlockDriver.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
use TypeLang\Mapper\Mapping\Metadata\ClassMetadata;
1313
use TypeLang\Mapper\Mapping\Metadata\PropertyMetadata;
1414
use TypeLang\Mapper\Mapping\Metadata\TypeMetadata;
15-
use TypeLang\Mapper\Runtime\Repository\TypeRepository;
15+
use TypeLang\Mapper\Runtime\Parser\TypeParserInterface;
16+
use TypeLang\Mapper\Runtime\Repository\TypeRepositoryInterface;
1617
use TypeLang\Parser\Node\Stmt\TypeStatement;
1718
use TypeLang\PHPDoc\Parser;
1819
use TypeLang\PHPDoc\Standard\ParamTagFactory;
@@ -114,8 +115,13 @@ private function findType(\ReflectionClass $class, PropertyMetadata $meta): ?Typ
114115
return $this->classProperties->findType($property);
115116
}
116117

117-
protected function load(\ReflectionClass $reflection, ClassMetadata $class, TypeRepository $types): void
118-
{
118+
#[\Override]
119+
protected function load(
120+
\ReflectionClass $reflection,
121+
ClassMetadata $class,
122+
TypeRepositoryInterface $types,
123+
TypeParserInterface $parser,
124+
): void {
119125
foreach ($reflection->getProperties() as $property) {
120126
$metadata = $class->getPropertyOrCreate($property->getName());
121127

src/Mapping/Driver/Driver.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,20 @@
55
namespace TypeLang\Mapper\Mapping\Driver;
66

77
use TypeLang\Mapper\Mapping\Metadata\ClassMetadata;
8-
use TypeLang\Mapper\Runtime\Repository\TypeRepository;
8+
use TypeLang\Mapper\Runtime\Parser\TypeParserInterface;
9+
use TypeLang\Mapper\Runtime\Repository\TypeRepositoryInterface;
910

1011
abstract class Driver implements DriverInterface
1112
{
1213
public function __construct(
1314
private readonly DriverInterface $delegate = new NullDriver(),
1415
) {}
1516

16-
public function getClassMetadata(\ReflectionClass $class, TypeRepository $types): ClassMetadata
17-
{
18-
return $this->delegate->getClassMetadata($class, $types);
17+
public function getClassMetadata(
18+
\ReflectionClass $class,
19+
TypeRepositoryInterface $types,
20+
TypeParserInterface $parser,
21+
): ClassMetadata {
22+
return $this->delegate->getClassMetadata($class, $types, $parser);
1923
}
2024
}

src/Mapping/Driver/DriverInterface.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
namespace TypeLang\Mapper\Mapping\Driver;
66

77
use TypeLang\Mapper\Mapping\Metadata\ClassMetadata;
8-
use TypeLang\Mapper\Runtime\Repository\TypeRepository;
8+
use TypeLang\Mapper\Runtime\Parser\TypeParserInterface;
9+
use TypeLang\Mapper\Runtime\Repository\TypeRepositoryInterface;
910

1011
interface DriverInterface
1112
{
@@ -16,5 +17,9 @@ interface DriverInterface
1617
*
1718
* @return ClassMetadata<T>
1819
*/
19-
public function getClassMetadata(\ReflectionClass $class, TypeRepository $types): ClassMetadata;
20+
public function getClassMetadata(
21+
\ReflectionClass $class,
22+
TypeRepositoryInterface $types,
23+
TypeParserInterface $parser,
24+
): ClassMetadata;
2025
}

src/Mapping/Driver/InMemoryCachedDriver.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
namespace TypeLang\Mapper\Mapping\Driver;
66

77
use TypeLang\Mapper\Mapping\Metadata\ClassMetadata;
8-
use TypeLang\Mapper\Runtime\Repository\TypeRepository;
8+
use TypeLang\Mapper\Runtime\Parser\TypeParserInterface;
9+
use TypeLang\Mapper\Runtime\Repository\TypeRepositoryInterface;
910

1011
final class InMemoryCachedDriver extends Driver
1112
{
@@ -14,10 +15,13 @@ final class InMemoryCachedDriver extends Driver
1415
*/
1516
private array $memory = [];
1617

17-
public function getClassMetadata(\ReflectionClass $class, TypeRepository $types): ClassMetadata
18-
{
18+
public function getClassMetadata(
19+
\ReflectionClass $class,
20+
TypeRepositoryInterface $types,
21+
TypeParserInterface $parser,
22+
): ClassMetadata {
1923
// @phpstan-ignore-next-line : class-string<T> key contains ClassMetadata<T> instance
2024
return $this->memory[$class->name]
21-
??= parent::getClassMetadata($class, $types);
25+
??= parent::getClassMetadata($class, $types, $parser);
2226
}
2327
}

src/Mapping/Driver/LoadableDriver.php

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66

77
use TypeLang\Mapper\Exception\Definition\DefinitionException;
88
use TypeLang\Mapper\Mapping\Metadata\ClassMetadata;
9-
use TypeLang\Mapper\Runtime\Repository\TypeRepository;
9+
use TypeLang\Mapper\Runtime\Parser\TypeParserInterface;
10+
use TypeLang\Mapper\Runtime\Repository\TypeRepositoryInterface;
1011

1112
/**
1213
* Implements each driver that can supplement or modify existing
@@ -33,16 +34,20 @@ public function __construct(
3334
* @return ClassMetadata<TArg>
3435
* @throws \Throwable in case of internal error occurred
3536
*/
36-
public function getClassMetadata(\ReflectionClass $class, TypeRepository $types): ClassMetadata
37-
{
37+
public function getClassMetadata(
38+
\ReflectionClass $class,
39+
TypeRepositoryInterface $types,
40+
TypeParserInterface $parser,
41+
): ClassMetadata {
3842
if (isset(self::$metadata[$class->getName()])) {
3943
/** @var ClassMetadata<TArg> */
4044
return self::$metadata[$class->getName()];
4145
}
4246

43-
self::$metadata[$class->getName()] = $metadata = parent::getClassMetadata($class, $types);
47+
self::$metadata[$class->getName()] = $metadata
48+
= parent::getClassMetadata($class, $types, $parser);
4449

45-
$this->load($class, $metadata, $types);
50+
$this->load($class, $metadata, $types, $parser);
4651

4752
try {
4853
return $metadata;
@@ -63,6 +68,7 @@ public function getClassMetadata(\ReflectionClass $class, TypeRepository $types)
6368
abstract protected function load(
6469
\ReflectionClass $reflection,
6570
ClassMetadata $class,
66-
TypeRepository $types,
71+
TypeRepositoryInterface $types,
72+
TypeParserInterface $parser,
6773
): void;
6874
}

src/Mapping/Driver/NullDriver.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,16 @@
55
namespace TypeLang\Mapper\Mapping\Driver;
66

77
use TypeLang\Mapper\Mapping\Metadata\ClassMetadata;
8-
use TypeLang\Mapper\Runtime\Repository\TypeRepository;
8+
use TypeLang\Mapper\Runtime\Parser\TypeParserInterface;
9+
use TypeLang\Mapper\Runtime\Repository\TypeRepositoryInterface;
910

1011
final class NullDriver implements DriverInterface
1112
{
12-
public function getClassMetadata(\ReflectionClass $class, TypeRepository $types): ClassMetadata
13-
{
13+
public function getClassMetadata(
14+
\ReflectionClass $class,
15+
TypeRepositoryInterface $types,
16+
TypeParserInterface $parser,
17+
): ClassMetadata {
1418
return new ClassMetadata($class->getName());
1519
}
1620
}

src/Mapping/Driver/Psr16CachedDriver.php

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66

77
use Psr\SimpleCache\CacheInterface;
88
use TypeLang\Mapper\Mapping\Metadata\ClassMetadata;
9-
use TypeLang\Mapper\Runtime\Repository\TypeRepository;
9+
use TypeLang\Mapper\Runtime\Parser\TypeParserInterface;
10+
use TypeLang\Mapper\Runtime\Repository\TypeRepositoryInterface;
1011

1112
final class Psr16CachedDriver extends CachedDriver
1213
{
@@ -19,11 +20,18 @@ public function __construct(
1920
parent::__construct($prefix, $ttl, $delegate);
2021
}
2122

22-
public function getClassMetadata(\ReflectionClass $class, TypeRepository $types): ClassMetadata
23-
{
23+
public function getClassMetadata(
24+
\ReflectionClass $class,
25+
TypeRepositoryInterface $types,
26+
TypeParserInterface $parser,
27+
): ClassMetadata {
2428
$index = $this->getKey($class);
2529

26-
$result = $this->cache->get($index, fn() => parent::getClassMetadata($class, $types));
30+
$result = $this->cache->get(
31+
$index,
32+
fn(): ClassMetadata
33+
=> parent::getClassMetadata($class, $types, $parser)
34+
);
2735

2836
if ($result instanceof \Closure) {
2937
$result = $result();

src/Mapping/Driver/ReflectionDriver.php

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
use TypeLang\Mapper\Mapping\Metadata\ClassMetadata;
1010
use TypeLang\Mapper\Mapping\Metadata\PropertyMetadata;
1111
use TypeLang\Mapper\Mapping\Metadata\TypeMetadata;
12-
use TypeLang\Mapper\Runtime\Repository\TypeRepository;
12+
use TypeLang\Mapper\Runtime\Parser\TypeParserInterface;
13+
use TypeLang\Mapper\Runtime\Repository\TypeRepositoryInterface;
1314
use TypeLang\Parser\Node\FullQualifiedName;
1415
use TypeLang\Parser\Node\Identifier;
1516
use TypeLang\Parser\Node\Name;
@@ -22,8 +23,12 @@
2223
final class ReflectionDriver extends LoadableDriver
2324
{
2425
#[\Override]
25-
protected function load(\ReflectionClass $reflection, ClassMetadata $class, TypeRepository $types): void
26-
{
26+
protected function load(
27+
\ReflectionClass $reflection,
28+
ClassMetadata $class,
29+
TypeRepositoryInterface $types,
30+
TypeParserInterface $parser,
31+
): void {
2732
foreach ($reflection->getProperties() as $property) {
2833
if (!self::isValidProperty($property)) {
2934
continue;
@@ -68,9 +73,13 @@ private function fillDefaultValue(\ReflectionProperty $property, PropertyMetadat
6873
/**
6974
* @throws PropertyTypeNotFoundException
7075
* @throws \InvalidArgumentException
76+
* @throws \Throwable
7177
*/
72-
private function fillType(\ReflectionProperty $property, PropertyMetadata $meta, TypeRepository $types): void
73-
{
78+
private function fillType(
79+
\ReflectionProperty $property,
80+
PropertyMetadata $meta,
81+
TypeRepositoryInterface $types,
82+
): void {
7483
$statement = $this->getTypeStatement($property);
7584

7685
try {

0 commit comments

Comments
 (0)