Skip to content

Commit 0f57cab

Browse files
committed
move types out of internal
1 parent 7a1d404 commit 0f57cab

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+204
-147
lines changed

src/Symfony/Component/SerDes/Context/ContextBuilder/Deserialize/DeserializeFormatterAttributeContextBuilder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
/**
1919
* @author Mathias Arlaud <[email protected]>
2020
*
21-
* @experimental in 7.0
21+
* @internal
2222
*/
2323
final class DeserializeFormatterAttributeContextBuilder implements DeserializeContextBuilderInterface
2424
{

src/Symfony/Component/SerDes/Internal/Deserialize/Csv/CsvDeserializer.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@
1313

1414
use Symfony\Component\SerDes\Exception\InvalidArgumentException;
1515
use Symfony\Component\SerDes\Internal\Deserialize\Deserializer;
16-
use Symfony\Component\SerDes\Internal\Type;
17-
use Symfony\Component\SerDes\Internal\UnionType;
1816
use Symfony\Component\SerDes\Type\ReflectionTypeExtractor;
17+
use Symfony\Component\SerDes\Type\Type;
18+
use Symfony\Component\SerDes\Type\UnionType;
1919

2020
/**
2121
* @author Mathias Arlaud <[email protected]>

src/Symfony/Component/SerDes/Internal/Deserialize/Deserializer.php

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@
1313

1414
use Symfony\Component\SerDes\Exception\LogicException;
1515
use Symfony\Component\SerDes\Exception\UnexpectedValueException;
16-
use Symfony\Component\SerDes\Internal\Type;
17-
use Symfony\Component\SerDes\Internal\TypeFactory;
18-
use Symfony\Component\SerDes\Internal\UnionType;
1916
use Symfony\Component\SerDes\Type\ReflectionTypeExtractor;
17+
use Symfony\Component\SerDes\Type\Type;
18+
use Symfony\Component\SerDes\Type\TypeFactory;
19+
use Symfony\Component\SerDes\Type\UnionType;
2020

2121
/**
2222
* @author Mathias Arlaud <[email protected]>
@@ -90,12 +90,15 @@ abstract public function deserialize(mixed $resource, Type|UnionType $type, arra
9090
protected function doDeserialize(mixed $dataOrResource, Type|UnionType $type, array $context): mixed
9191
{
9292
if ($type instanceof UnionType) {
93-
if (!isset($context['union_selector'][$typeString = (string) $type])) {
93+
$selectedType = ($context['union_selector'][$typeString = (string) $type] ?? null);
94+
if (null === $selectedType) {
9495
throw new UnexpectedValueException(sprintf('Cannot guess type to use for "%s", you may specify a type in "$context[\'union_selector\'][\'%1$s\']".', (string) $type));
9596
}
9697

9798
/** @var Type $type */
98-
$type = (self::$cache['type'][$typeString] ??= TypeFactory::createFromString($context['union_selector'][$typeString]));
99+
$type = \is_string($selectedType)
100+
? (self::$cache['type'][$selectedType] ??= TypeFactory::createFromString($selectedType))
101+
: $selectedType;
99102
}
100103

101104
if ($type->isScalar()) {
@@ -180,12 +183,14 @@ protected function doDeserialize(mixed $dataOrResource, Type|UnionType $type, ar
180183
}
181184

182185
if (null !== $hook = $context['hooks']['deserialize'][$className] ?? $context['hooks']['deserialize']['object'] ?? null) {
183-
/** @var array{type?: string, context?: array<string, mixed>} $hookResult */
184-
$hookResult = $hook((string) $type, $context);
186+
/** @var array{type?: Type|UnionType|string, context?: array<string, mixed>} $hookResult */
187+
$hookResult = $hook($type, $context);
185188

186189
if (isset($hookResult['type'])) {
187190
/** @var Type $type */
188-
$type = (self::$cache['type'][$hookResult['type']] ??= TypeFactory::createFromString($hookResult['type']));
191+
$type = \is_string($hookResult['type'])
192+
? (self::$cache['type'][$hookResult['type']] ??= TypeFactory::createFromString($hookResult['type']))
193+
: $hookResult['type'];
189194
}
190195

191196
$context = $hookResult['context'] ?? $context;
@@ -202,7 +207,12 @@ protected function doDeserialize(mixed $dataOrResource, Type|UnionType $type, ar
202207
$hookResult = $hook(
203208
$reflection,
204209
$name,
205-
fn (string $type, array $context) => $this->propertyValueCallable(self::$cache['type'][$type] ??= TypeFactory::createFromString($type), $dataOrResource, $value, $context)(),
210+
function (Type|UnionType|string $type, array $context) use ($dataOrResource, $value) {
211+
/** @var Type $type */
212+
$type = \is_string($type) ? (self::$cache['type'][$type] ??= TypeFactory::createFromString($type)) : $type;
213+
214+
return $this->propertyValueCallable($type, $dataOrResource, $value, $context)();
215+
},
206216
$context,
207217
);
208218

@@ -226,7 +236,7 @@ protected function doDeserialize(mixed $dataOrResource, Type|UnionType $type, ar
226236
continue;
227237
}
228238

229-
self::$cache['property_type'][$identifier] ??= TypeFactory::createFromString($this->reflectionTypeExtractor->extractFromProperty($reflection->getProperty($name)));
239+
self::$cache['property_type'][$identifier] ??= $this->reflectionTypeExtractor->extractFromProperty($reflection->getProperty($name));
230240

231241
$valueCallables[$name] = $this->propertyValueCallable(self::$cache['property_type'][$identifier], $dataOrResource, $value, $context);
232242
}

src/Symfony/Component/SerDes/Internal/Deserialize/Json/JsonDictSplitter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
namespace Symfony\Component\SerDes\Internal\Deserialize\Json;
1313

1414
use Symfony\Component\SerDes\Exception\InvalidResourceException;
15-
use Symfony\Component\SerDes\Internal\Type;
15+
use Symfony\Component\SerDes\Type\Type;
1616

1717
/**
1818
* @author Mathias Arlaud <[email protected]>

src/Symfony/Component/SerDes/Internal/Deserialize/Json/JsonEagerDeserializer.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@
1313

1414
use Symfony\Component\SerDes\Exception\UnexpectedValueException;
1515
use Symfony\Component\SerDes\Internal\Deserialize\Deserializer;
16-
use Symfony\Component\SerDes\Internal\Type;
17-
use Symfony\Component\SerDes\Internal\UnionType;
1816
use Symfony\Component\SerDes\Type\ReflectionTypeExtractor;
17+
use Symfony\Component\SerDes\Type\Type;
18+
use Symfony\Component\SerDes\Type\UnionType;
1919

2020
/**
2121
* @author Mathias Arlaud <[email protected]>

src/Symfony/Component/SerDes/Internal/Deserialize/Json/JsonLazyDeserializer.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212
namespace Symfony\Component\SerDes\Internal\Deserialize\Json;
1313

1414
use Symfony\Component\SerDes\Internal\Deserialize\Deserializer;
15-
use Symfony\Component\SerDes\Internal\Type;
16-
use Symfony\Component\SerDes\Internal\UnionType;
1715
use Symfony\Component\SerDes\Type\ReflectionTypeExtractor;
16+
use Symfony\Component\SerDes\Type\Type;
17+
use Symfony\Component\SerDes\Type\UnionType;
1818

1919
/**
2020
* @author Mathias Arlaud <[email protected]>

src/Symfony/Component/SerDes/Internal/Deserialize/Json/JsonListSplitter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
namespace Symfony\Component\SerDes\Internal\Deserialize\Json;
1313

1414
use Symfony\Component\SerDes\Exception\InvalidResourceException;
15-
use Symfony\Component\SerDes\Internal\Type;
15+
use Symfony\Component\SerDes\Type\Type;
1616

1717
/**
1818
* @author Mathias Arlaud <[email protected]>

src/Symfony/Component/SerDes/Internal/Serialize/TemplateGenerator/CsvTemplateGenerator.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@
3131
use Symfony\Component\SerDes\Internal\Serialize\Node\UnaryNode;
3232
use Symfony\Component\SerDes\Internal\Serialize\Node\VariableNode;
3333
use Symfony\Component\SerDes\Internal\Serialize\NodeInterface;
34-
use Symfony\Component\SerDes\Internal\Type;
35-
use Symfony\Component\SerDes\Internal\TypeFactory;
36-
use Symfony\Component\SerDes\Internal\UnionType;
34+
use Symfony\Component\SerDes\Type\Type;
35+
use Symfony\Component\SerDes\Type\TypeFactory;
36+
use Symfony\Component\SerDes\Type\UnionType;
3737

3838
/**
3939
* @author Mathias Arlaud <[email protected]>
@@ -230,7 +230,11 @@ protected function objectNodes(Type $type, array $propertiesInfo, array $context
230230
continue;
231231
}
232232

233-
array_push($nodes, ...$this->generate(TypeFactory::createFromString($propertyInfo['type']), $propertyInfo['accessor'], $propertyInfo['context']));
233+
if (\is_string($propertyInfo['type'])) {
234+
$propertyInfo['type'] = TypeFactory::createFromString($propertyInfo['type']);
235+
}
236+
237+
array_push($nodes, ...$this->generate($propertyInfo['type'], $propertyInfo['accessor'], $propertyInfo['context']));
234238
}
235239

236240
return $nodes;

src/Symfony/Component/SerDes/Internal/Serialize/TemplateGenerator/JsonTemplateGenerator.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222
use Symfony\Component\SerDes\Internal\Serialize\Node\TemplateStringNode;
2323
use Symfony\Component\SerDes\Internal\Serialize\Node\VariableNode;
2424
use Symfony\Component\SerDes\Internal\Serialize\NodeInterface;
25-
use Symfony\Component\SerDes\Internal\Type;
26-
use Symfony\Component\SerDes\Internal\TypeFactory;
25+
use Symfony\Component\SerDes\Type\Type;
26+
use Symfony\Component\SerDes\Type\TypeFactory;
2727

2828
/**
2929
* @author Mathias Arlaud <[email protected]>
@@ -104,13 +104,17 @@ protected function objectNodes(Type $type, array $propertiesInfo, array $context
104104

105105
$encodedName = substr($encodedName, 1, -1);
106106

107+
if (\is_string($propertyInfo['type'])) {
108+
$propertyInfo['type'] = TypeFactory::createFromString($propertyInfo['type']);
109+
}
110+
107111
array_push(
108112
$nodes,
109113
new ExpressionNode(new FunctionNode('\fwrite', [new VariableNode('resource'), new ScalarNode($separator)])),
110114
new ExpressionNode(new FunctionNode('\fwrite', [new VariableNode('resource'), new ScalarNode('"')])),
111115
new ExpressionNode(new FunctionNode('\fwrite', [new VariableNode('resource'), new ScalarNode($encodedName)])),
112116
new ExpressionNode(new FunctionNode('\fwrite', [new VariableNode('resource'), new ScalarNode('":')])),
113-
...$this->generate(TypeFactory::createFromString($propertyInfo['type']), $propertyInfo['accessor'], $propertyInfo['context']),
117+
...$this->generate($propertyInfo['type'], $propertyInfo['accessor'], $propertyInfo['context']),
114118
);
115119

116120
$separator = ',';

src/Symfony/Component/SerDes/Internal/Serialize/TemplateGenerator/TemplateGenerator.php

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@
2525
use Symfony\Component\SerDes\Internal\Serialize\Node\UnaryNode;
2626
use Symfony\Component\SerDes\Internal\Serialize\Node\VariableNode;
2727
use Symfony\Component\SerDes\Internal\Serialize\NodeInterface;
28-
use Symfony\Component\SerDes\Internal\Serialize\TypeSorter;
2928
use Symfony\Component\SerDes\Internal\Serialize\VariableNameScoperTrait;
30-
use Symfony\Component\SerDes\Internal\Type;
31-
use Symfony\Component\SerDes\Internal\TypeFactory;
32-
use Symfony\Component\SerDes\Internal\UnionType;
3329
use Symfony\Component\SerDes\Type\ReflectionTypeExtractor;
30+
use Symfony\Component\SerDes\Type\Type;
31+
use Symfony\Component\SerDes\Type\TypeFactory;
32+
use Symfony\Component\SerDes\Type\TypeSorter;
33+
use Symfony\Component\SerDes\Type\UnionType;
3434

3535
/**
3636
* @author Mathias Arlaud <[email protected]>
@@ -76,8 +76,8 @@ abstract protected function listNodes(Type $type, NodeInterface $accessor, array
7676
abstract protected function dictNodes(Type $type, NodeInterface $accessor, array $context): array;
7777

7878
/**
79-
* @param list<array{name: string, type: string, accessor: NodeInterface, context: array<string, mixed>}> $propertiesInfo
80-
* @param array<string, mixed> $context
79+
* @param list<array{name: string, type: Type|UnionType|string, accessor: NodeInterface, context: array<string, mixed>}> $propertiesInfo
80+
* @param array<string, mixed> $context
8181
*
8282
* @return list<NodeInterface>
8383
*/
@@ -173,10 +173,13 @@ private function nodes(Type|UnionType $type, NodeInterface $accessor, array $con
173173
}
174174

175175
if (null !== $hook = $context['hooks']['serialize'][$className] ?? $context['hooks']['serialize']['object'] ?? null) {
176-
$hookResult = $hook((string) $type, (new Compiler())->compile($accessor)->source(), $context);
176+
$hookResult = $hook($type, (new Compiler())->compile($accessor)->source(), $context);
177+
178+
if (isset($hookResult['type'])) {
179+
/** @var Type $type */
180+
$type = \is_string($hookResult['type']) ? TypeFactory::createFromString($hookResult['type']) : $hookResult['type'];
181+
}
177182

178-
/** @var Type $type */
179-
$type = isset($hookResult['type']) ? TypeFactory::createFromString($hookResult['type']) : $type;
180183
$accessor = isset($hookResult['accessor']) ? new RawNode($hookResult['accessor']) : $accessor;
181184
$context = $hookResult['context'] ?? $context;
182185
}

src/Symfony/Component/SerDes/Internal/Serialize/TemplateGenerator/TemplateGeneratorFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
namespace Symfony\Component\SerDes\Internal\Serialize\TemplateGenerator;
1313

1414
use Symfony\Component\SerDes\Exception\UnsupportedFormatException;
15-
use Symfony\Component\SerDes\Internal\Serialize\TypeSorter;
1615
use Symfony\Component\SerDes\Type\ReflectionTypeExtractor;
16+
use Symfony\Component\SerDes\Type\TypeSorter;
1717

1818
/**
1919
* @author Mathias Arlaud <[email protected]>

src/Symfony/Component/SerDes/Resources/functions.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,10 @@
2121
use Symfony\Component\SerDes\Internal\Serialize\Node\ReturnNode;
2222
use Symfony\Component\SerDes\Internal\Serialize\Node\VariableNode;
2323
use Symfony\Component\SerDes\Internal\Serialize\TemplateGenerator\TemplateGeneratorFactory;
24-
use Symfony\Component\SerDes\Internal\TypeFactory;
2524
use Symfony\Component\SerDes\Template\TemplateHelper;
25+
use Symfony\Component\SerDes\Type\Type;
26+
use Symfony\Component\SerDes\Type\TypeFactory;
27+
use Symfony\Component\SerDes\Type\UnionType;
2628

2729
if (!\function_exists(serialize::class)) {
2830
/**
@@ -34,6 +36,9 @@
3436
function serialize(mixed $data, $resource, string $format, array $context = []): void
3537
{
3638
$type = $context['type'] ?? get_debug_type($data);
39+
if (\is_string($type)) {
40+
$type = TypeFactory::createFromString($type);
41+
}
3742

3843
$cacheDir = $context['cache_dir'] ?? sys_get_temp_dir().\DIRECTORY_SEPARATOR.'symfony_ser_des';
3944
$cacheFilename = (new TemplateHelper())->templateFilename($type, $format, $context);
@@ -58,7 +63,7 @@ function serialize(mixed $data, $resource, string $format, array $context = []):
5863
*
5964
* @param array<string, mixed> $context
6065
*/
61-
function serialize_generate(string $type, string $format, array $context = []): string
66+
function serialize_generate(Type|UnionType $type, string $format, array $context = []): string
6267
{
6368
$compiler = new Compiler();
6469
$accessor = new VariableNode('data');
@@ -75,7 +80,7 @@ function serialize_generate(string $type, string $format, array $context = []):
7580
$argumentsNode = new ArgumentsNode(['data' => 'mixed', 'resource' => 'mixed', 'context' => 'array']);
7681

7782
$compiler->indent();
78-
$bodyNodes = TemplateGeneratorFactory::create($format)->generate(TypeFactory::createFromString($type), $accessor, $context);
83+
$bodyNodes = TemplateGeneratorFactory::create($format)->generate($type, $accessor, $context);
7984
$compiler->outdent();
8085

8186
$compiler->compile(new ExpressionNode(new ReturnNode(new ClosureNode($argumentsNode, 'void', true, $bodyNodes))));
@@ -92,7 +97,7 @@ function serialize_generate(string $type, string $format, array $context = []):
9297
*
9398
* @throws PartialDeserializationException
9499
*/
95-
function deserialize($resource, string $type, string $format, array $context = []): mixed
100+
function deserialize($resource, Type|UnionType $type, string $format, array $context = []): mixed
96101
{
97102
$errors = [];
98103

@@ -102,8 +107,6 @@ function deserialize($resource, string $type, string $format, array $context = [
102107

103108
$context['lazy_reading'] = $context['lazy_reading'] ?? false;
104109

105-
$type = TypeFactory::createFromString($type);
106-
107110
$result = DeserializerFactory::create($format, $context)->deserialize($resource, $type, $context);
108111

109112
if ([] !== $errors) {

src/Symfony/Component/SerDes/Serializer.php

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
use Symfony\Component\SerDes\Context\ContextInterface;
1616
use Symfony\Component\SerDes\Stream\StreamInterface;
1717
use Symfony\Component\SerDes\Template\TemplateHelper;
18+
use Symfony\Component\SerDes\Type\Type;
19+
use Symfony\Component\SerDes\Type\TypeFactory;
20+
use Symfony\Component\SerDes\Type\UnionType;
1821

1922
/**
2023
* @author Mathias Arlaud <[email protected]>
@@ -43,15 +46,19 @@ public function __construct(
4346

4447
public function serialize(mixed $data, string $format, mixed $output, ContextInterface|array $context = []): void
4548
{
49+
if ($output instanceof StreamInterface) {
50+
$output = $output->resource();
51+
}
52+
4653
if ($context instanceof ContextInterface) {
4754
$context = $context->toArray();
4855
}
4956

50-
if ($output instanceof StreamInterface) {
51-
$output = $output->resource();
57+
$context['type'] = $context['type'] ?? get_debug_type($data);
58+
if (\is_string($context['type'])) {
59+
$context['type'] = TypeFactory::createFromString($context['type']);
5260
}
5361

54-
$context['type'] = $context['type'] ?? get_debug_type($data);
5562
$context['cache_dir'] = $context['cache_dir'] ?? $this->templateCacheDir;
5663

5764
$templatePath = $context['cache_dir'].\DIRECTORY_SEPARATOR.$this->templateHelper->templateFilename($context['type'], $format, $context);
@@ -64,16 +71,20 @@ public function serialize(mixed $data, string $format, mixed $output, ContextInt
6471
serialize($data, $output, $format, $context);
6572
}
6673

67-
public function deserialize(mixed $input, string $type, string $format, ContextInterface|array $context = []): mixed
74+
public function deserialize(mixed $input, Type|UnionType|string $type, string $format, ContextInterface|array $context = []): mixed
6875
{
69-
if ($context instanceof ContextInterface) {
70-
$context = $context->toArray();
71-
}
72-
7376
if ($input instanceof StreamInterface) {
7477
$input = $input->resource();
7578
}
7679

80+
if (\is_string($type)) {
81+
$type = TypeFactory::createFromString($type);
82+
}
83+
84+
if ($context instanceof ContextInterface) {
85+
$context = $context->toArray();
86+
}
87+
7788
foreach ($this->deserializeContextBuilders as $contextBuilder) {
7889
$context = $contextBuilder->build($context);
7990
}

src/Symfony/Component/SerDes/SerializerInterface.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
use Symfony\Component\SerDes\Context\ContextInterface;
1515
use Symfony\Component\SerDes\Exception\PartialDeserializationException;
1616
use Symfony\Component\SerDes\Stream\StreamInterface;
17+
use Symfony\Component\SerDes\Type\Type;
18+
use Symfony\Component\SerDes\Type\UnionType;
1719

1820
/**
1921
* @author Mathias Arlaud <[email protected]>
@@ -34,5 +36,5 @@ public function serialize(mixed $data, string $format, mixed $output, ContextInt
3436
*
3537
* @throws PartialDeserializationException
3638
*/
37-
public function deserialize(mixed $input, string $type, string $format, ContextInterface|array $context = []): mixed;
39+
public function deserialize(mixed $input, Type|UnionType|string $type, string $format, ContextInterface|array $context = []): mixed;
3840
}

0 commit comments

Comments
 (0)