Skip to content

Commit b63a3dd

Browse files
committed
Merge branch '7.4' into 8.0
* 7.4: Add `TEST_GENERATE_FIXTURES=1` to generate fixtures in tests [Config] Fix array shape for `canBeEnabled` / `canBeDisabled` [DoctrineBridge] Remove unnecassary check [JsonStreamer] Add synthetic properties support Resolved Conflicts: - src/Symfony/Component/DependencyInjection/Tests/Dumper/PhpDumperTest.php - src/Symfony/Component/DependencyInjection/Tests/Dumper/XmlDumperTest.php - src/Symfony/Component/JsonStreamer/Tests/JsonStreamReaderTest.php
2 parents cfdd903 + 70201ca commit b63a3dd

15 files changed

+152
-12
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ CHANGELOG
88
* Remove `nikic/php-parser` dependency
99
* Add `_current_object` to the context passed to value transformers during write operations
1010
* Add `include_null_properties` option to encode the properties with `null` value
11+
* Add synthetic properties support
1112

1213
7.3
1314
---

Mapping/PropertyMetadata.php

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,31 @@ final class PropertyMetadata
2525
* @param list<string|\Closure> $streamToNativeValueTransformers
2626
*/
2727
public function __construct(
28-
private string $name,
28+
private ?string $name,
2929
private Type $type,
3030
private array $nativeToStreamValueTransformers = [],
3131
private array $streamToNativeValueTransformers = [],
3232
) {
3333
}
3434

35-
public function getName(): string
35+
/**
36+
* @param list<string|\Closure> $nativeToStreamValueTransformers
37+
* @param list<string|\Closure> $streamToNativeValueTransformers
38+
*/
39+
public static function createSynthetic(
40+
Type $type,
41+
array $nativeToStreamValueTransformers = [],
42+
array $streamToNativeValueTransformers = [],
43+
): self {
44+
return new self(null, $type, $nativeToStreamValueTransformers, $streamToNativeValueTransformers);
45+
}
46+
47+
public function getName(): ?string
3648
{
3749
return $this->name;
3850
}
3951

40-
public function withName(string $name): self
52+
public function withName(?string $name): self
4153
{
4254
return new self($name, $this->type, $this->nativeToStreamValueTransformers, $this->streamToNativeValueTransformers);
4355
}

Mapping/Read/AttributePropertyMetadataLoader.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,12 @@ public function load(string $className, array $options = [], array $context = []
4242
$result = [];
4343

4444
foreach ($initialResult as $initialStreamedName => $initialMetadata) {
45+
if (!$initialName = $initialMetadata->getName()) {
46+
continue;
47+
}
48+
4549
try {
46-
$propertyReflection = new \ReflectionProperty($className, $initialMetadata->getName());
50+
$propertyReflection = new \ReflectionProperty($className, $initialName);
4751
} catch (\ReflectionException $e) {
4852
throw new RuntimeException($e->getMessage(), $e->getCode(), $e);
4953
}

Mapping/Write/AttributePropertyMetadataLoader.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,12 @@ public function load(string $className, array $options = [], array $context = []
4242
$result = [];
4343

4444
foreach ($initialResult as $initialStreamedName => $initialMetadata) {
45+
if (!$initialName = $initialMetadata->getName()) {
46+
continue;
47+
}
48+
4549
try {
46-
$propertyReflection = new \ReflectionProperty($className, $initialMetadata->getName());
50+
$propertyReflection = new \ReflectionProperty($className, $initialName);
4751
} catch (\ReflectionException $e) {
4852
throw new RuntimeException($e->getMessage(), $e->getCode(), $e);
4953
}

Read/StreamReaderGenerator.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,10 @@ private function createDataModel(Type $type, array $options = [], array $context
127127
$propertiesMetadata = $this->propertyMetadataLoader->load($className, $options, $context);
128128

129129
foreach ($propertiesMetadata as $streamedName => $propertyMetadata) {
130+
if (!$propertyMetadata->getName()) {
131+
continue;
132+
}
133+
130134
$propertiesNodes[$streamedName] = [
131135
'name' => $propertyMetadata->getName(),
132136
'value' => $this->createDataModel($propertyMetadata->getType(), $options, $context),
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\JsonStreamer\Tests\Fixtures\Mapping;
13+
14+
use Symfony\Component\JsonStreamer\Mapping\PropertyMetadata;
15+
use Symfony\Component\JsonStreamer\Mapping\PropertyMetadataLoaderInterface;
16+
use Symfony\Component\TypeInfo\Type;
17+
18+
final class SyntheticPropertyMetadataLoader implements PropertyMetadataLoaderInterface
19+
{
20+
public function load(string $className, array $options = [], array $context = []): array
21+
{
22+
return [
23+
'synthetic' => PropertyMetadata::createSynthetic(Type::true(), [
24+
self::true(...),
25+
]),
26+
];
27+
}
28+
29+
public static function true(): true
30+
{
31+
return true;
32+
}
33+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace Symfony\Component\JsonStreamer\Tests\Fixtures\Model;
4+
5+
class DummyWithSyntheticProperties
6+
{
7+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
/**
4+
* @return Symfony\Component\JsonStreamer\Tests\Fixtures\Model\DummyWithSyntheticProperties
5+
*/
6+
return static function (string|\Stringable $string, \Psr\Container\ContainerInterface $valueTransformers, \Symfony\Component\JsonStreamer\Read\Instantiator $instantiator, array $options): mixed {
7+
$providers['Symfony\Component\JsonStreamer\Tests\Fixtures\Model\DummyWithSyntheticProperties'] = static function ($data) use ($options, $valueTransformers, $instantiator, &$providers) {
8+
return $instantiator->instantiate(\Symfony\Component\JsonStreamer\Tests\Fixtures\Model\DummyWithSyntheticProperties::class, \array_filter([], static function ($v) {
9+
return '_symfony_missing_value' !== $v;
10+
}));
11+
};
12+
return $providers['Symfony\Component\JsonStreamer\Tests\Fixtures\Model\DummyWithSyntheticProperties'](\Symfony\Component\JsonStreamer\Read\Decoder::decodeString((string) $string));
13+
};
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
/**
4+
* @return Symfony\Component\JsonStreamer\Tests\Fixtures\Model\DummyWithSyntheticProperties
5+
*/
6+
return static function (mixed $stream, \Psr\Container\ContainerInterface $valueTransformers, \Symfony\Component\JsonStreamer\Read\LazyInstantiator $instantiator, array $options): mixed {
7+
$providers['Symfony\Component\JsonStreamer\Tests\Fixtures\Model\DummyWithSyntheticProperties'] = static function ($stream, $offset, $length) use ($options, $valueTransformers, $instantiator, &$providers) {
8+
$data = \Symfony\Component\JsonStreamer\Read\Splitter::splitDict($stream, $offset, $length);
9+
return $instantiator->instantiate(\Symfony\Component\JsonStreamer\Tests\Fixtures\Model\DummyWithSyntheticProperties::class, static function ($object) use ($stream, $data, $options, $valueTransformers, $instantiator, &$providers) {
10+
foreach ($data as $k => $v) {
11+
match ($k) {
12+
default => null,
13+
};
14+
}
15+
});
16+
};
17+
return $providers['Symfony\Component\JsonStreamer\Tests\Fixtures\Model\DummyWithSyntheticProperties']($stream, 0, null);
18+
};
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
/**
4+
* @param Symfony\Component\JsonStreamer\Tests\Fixtures\Model\DummyWithSyntheticProperties $data
5+
*/
6+
return static function (mixed $data, \Psr\Container\ContainerInterface $valueTransformers, array $options): \Traversable {
7+
try {
8+
$prefix1 = '';
9+
yield "{{$prefix1}\"synthetic\":";
10+
yield \json_encode(Symfony\Component\JsonStreamer\Tests\Fixtures\Mapping\SyntheticPropertyMetadataLoader::true(null, ['_current_object' => $data] + $options), \JSON_THROW_ON_ERROR, 511);
11+
yield "}";
12+
} catch (\JsonException $e) {
13+
throw new \Symfony\Component\JsonStreamer\Exception\NotEncodableValueException($e->getMessage(), 0, $e);
14+
}
15+
};

0 commit comments

Comments
 (0)