Skip to content

Commit 39b4e43

Browse files
authored
fix: deprecate data transformers (#4722)
1 parent f966dde commit 39b4e43

21 files changed

+131
-132
lines changed

src/Core/Bridge/Symfony/Messenger/DataTransformer.php

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

1414
namespace ApiPlatform\Core\Bridge\Symfony\Messenger;
1515

16-
class_exists(\ApiPlatform\Symfony\Messenger\DataTransformer::class);
16+
use ApiPlatform\Core\DataTransformer\DataTransformerInterface;
17+
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface;
18+
use ApiPlatform\Exception\OperationNotFoundException;
19+
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
20+
use ApiPlatform\Util\ClassInfoTrait;
1721

18-
if (false) {
19-
final class DataTransformer extends \ApiPlatform\Symfony\Messenger\DataTransformer
22+
/**
23+
* Transforms an Input to itself. This gives the ability to send the Input to a
24+
* message handler and process it asynchronously.
25+
*
26+
* @author Antoine Bluchet <[email protected]>
27+
*/
28+
final class DataTransformer implements DataTransformerInterface
29+
{
30+
use ClassInfoTrait;
31+
32+
/**
33+
* @var ResourceMetadataCollectionFactoryInterface|ResourceMetadataFactoryInterface
34+
*/
35+
private $resourceMetadataFactory;
36+
37+
public function __construct($resourceMetadataFactory)
2038
{
39+
$this->resourceMetadataFactory = $resourceMetadataFactory;
40+
41+
if (!$resourceMetadataFactory instanceof ResourceMetadataCollectionFactoryInterface) {
42+
trigger_deprecation('api-platform/core', '2.7', sprintf('Use "%s" instead of "%s".', ResourceMetadataCollectionFactoryInterface::class, ResourceMetadataFactoryInterface::class));
43+
}
44+
}
45+
46+
/**
47+
* {@inheritdoc}
48+
*
49+
* @return object
50+
*/
51+
public function transform($object, string $to, array $context = [])
52+
{
53+
return $object;
54+
}
55+
56+
/**
57+
* {@inheritdoc}
58+
*/
59+
public function supportsTransformation($data, string $to, array $context = []): bool
60+
{
61+
if (
62+
\is_object($data) // data is not normalized yet, it should be an array
63+
||
64+
null === ($context['input']['class'] ?? null)
65+
) {
66+
return false;
67+
}
68+
69+
if ($this->resourceMetadataFactory instanceof ResourceMetadataCollectionFactoryInterface) {
70+
try {
71+
$resourceMetadataCollection = $this->resourceMetadataFactory->create($context['resource_class'] ?? $to);
72+
$operation = $resourceMetadataCollection->getOperation($context['operation_name'] ?? null);
73+
74+
return 'input' === $operation->getMessenger();
75+
} catch (OperationNotFoundException $e) {
76+
return false;
77+
}
78+
}
79+
80+
$metadata = $this->resourceMetadataFactory->create($context['resource_class'] ?? $to);
81+
82+
if (isset($context['graphql_operation_name'])) {
83+
return 'input' === $metadata->getGraphqlAttribute($context['graphql_operation_name'], 'messenger', null, true);
84+
}
85+
86+
if (!isset($context['operation_type'])) {
87+
return 'input' === $metadata->getAttribute('messenger');
88+
}
89+
90+
return 'input' === $metadata->getTypedOperationAttribute(
91+
$context['operation_type'],
92+
$context[$context['operation_type'].'_operation_name'] ?? '',
93+
'messenger',
94+
null,
95+
true
96+
);
2197
}
2298
}

src/DataTransformer/DataTransformerInitializerInterface.php renamed to src/Core/DataTransformer/DataTransformerInitializerInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
declare(strict_types=1);
1313

14-
namespace ApiPlatform\DataTransformer;
14+
namespace ApiPlatform\Core\DataTransformer;
1515

1616
interface DataTransformerInitializerInterface extends DataTransformerInterface
1717
{

src/DataTransformer/DataTransformerInterface.php renamed to src/Core/DataTransformer/DataTransformerInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
declare(strict_types=1);
1313

14-
namespace ApiPlatform\DataTransformer;
14+
namespace ApiPlatform\Core\DataTransformer;
1515

1616
/**
1717
* Transforms a DTO or an Anonymous class to a Resource object.

src/Serializer/AbstractItemNormalizer.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,13 @@
1616
use ApiPlatform\Api\IriConverterInterface;
1717
use ApiPlatform\Api\UrlGeneratorInterface;
1818
use ApiPlatform\Core\Api\IriConverterInterface as LegacyIriConverterInterface;
19+
use ApiPlatform\Core\Bridge\Symfony\Messenger\DataTransformer as MessengerDataTransformer;
1920
use ApiPlatform\Core\DataProvider\ItemDataProviderInterface;
21+
use ApiPlatform\Core\DataTransformer\DataTransformerInitializerInterface;
22+
use ApiPlatform\Core\DataTransformer\DataTransformerInterface;
2023
use ApiPlatform\Core\Metadata\Property\Factory\PropertyMetadataFactoryInterface as LegacyPropertyMetadataFactoryInterface;
2124
use ApiPlatform\Core\Metadata\Property\PropertyMetadata;
2225
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface;
23-
use ApiPlatform\DataTransformer\DataTransformerInitializerInterface;
24-
use ApiPlatform\DataTransformer\DataTransformerInterface;
2526
use ApiPlatform\Exception\InvalidArgumentException;
2627
use ApiPlatform\Exception\InvalidValueException;
2728
use ApiPlatform\Exception\ItemNotFoundException;
@@ -110,6 +111,16 @@ public function __construct(PropertyNameCollectionFactoryInterface $propertyName
110111
$this->allowPlainIdentifiers = $allowPlainIdentifiers;
111112

112113
$this->dataTransformers = $dataTransformers;
114+
115+
// Just skip our data transformer to trigger a proper deprecation
116+
$customDataTransformers = array_filter(\is_array($dataTransformers) ? $dataTransformers : iterator_to_array($dataTransformers), function ($dataTransformer) {
117+
return !$dataTransformer instanceof MessengerDataTransformer;
118+
});
119+
120+
if (\count($customDataTransformers)) {
121+
trigger_deprecation('api-platform/core', '2.7', 'The DataTransformer pattern is deprecated, use a Provider or a Processor and either use your input or return a new output there.');
122+
}
123+
113124
if ($resourceMetadataFactory && !$resourceMetadataFactory instanceof ResourceMetadataCollectionFactoryInterface) {
114125
trigger_deprecation('api-platform/core', '2.7', sprintf('Use "%s" instead of "%s".', ResourceMetadataCollectionFactoryInterface::class, ResourceMetadataFactoryInterface::class));
115126
}

src/Symfony/Bundle/Resources/config/messenger.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
<tag name="api_platform.state_processor" priority="-900" />
2222
</service>
2323

24-
<service id="api_platform.messenger.data_transformer" class="ApiPlatform\Symfony\Messenger\DataTransformer" public="false">
24+
<service id="api_platform.messenger.data_transformer" class="ApiPlatform\Core\Bridge\Symfony\Messenger\DataTransformer" public="false">
2525
<argument type="service" id="api_platform.metadata.resource.metadata_collection_factory.retro_compatible" />
2626

2727
<tag name="api_platform.data_transformer" priority="-10" />

src/Symfony/Messenger/DataTransformer.php

Lines changed: 0 additions & 100 deletions
This file was deleted.

src/deprecated_interfaces.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,6 @@
6565
ApiPlatform\Core\DataProvider\PaginatorInterface::class => ApiPlatform\State\Pagination\PaginatorInterface::class,
6666
ApiPlatform\Core\DataProvider\PartialPaginatorInterface::class => ApiPlatform\State\Pagination\PartialPaginatorInterface::class,
6767

68-
// DataTransformer
69-
ApiPlatform\Core\DataTransformer\DataTransformerInitializerInterface::class => ApiPlatform\DataTransformer\DataTransformerInitializerInterface::class,
70-
ApiPlatform\Core\DataTransformer\DataTransformerInterface::class => ApiPlatform\DataTransformer\DataTransformerInterface::class,
71-
7268
// Documentation
7369
ApiPlatform\Core\Documentation\DocumentationInterface::class => ApiPlatform\Documentation\DocumentationInterface::class,
7470

src/deprecation.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,6 @@ class_alias($interfaceName, $oldInterfaceName);
188188
// Bridge\Symfony\Messenger
189189
ApiPlatform\Core\Bridge\Symfony\Messenger\ContextStamp::class => ApiPlatform\Symfony\Messenger\ContextStamp::class,
190190
ApiPlatform\Core\Bridge\Symfony\Messenger\DispatchTrait::class => ApiPlatform\Symfony\Messenger\DispatchTrait::class,
191-
ApiPlatform\Core\Bridge\Symfony\Messenger\DataTransformer::class => ApiPlatform\Symfony\Messenger\DataTransformer::class,
192191
ApiPlatform\Core\Bridge\Symfony\Messenger\RemoveStamp::class => ApiPlatform\Symfony\Messenger\RemoveStamp::class,
193192

194193
// Bridge\Symfony\PropertyInfo\Metadata\Property => Metadata\Property

tests/Symfony/Messenger/DataTransformerTest.php renamed to tests/Core/Bridge/Symfony/Messenger/DataTransformerTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@
1414
namespace ApiPlatform\Tests\Symfony\Messenger;
1515

1616
use ApiPlatform\Core\Api\OperationType;
17+
use ApiPlatform\Core\Bridge\Symfony\Messenger\DataTransformer;
1718
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface;
1819
use ApiPlatform\Core\Metadata\Resource\ResourceMetadata;
1920
use ApiPlatform\Core\Tests\ProphecyTrait;
20-
use ApiPlatform\Symfony\Messenger\DataTransformer;
2121
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\Dummy;
2222
use PHPUnit\Framework\TestCase;
2323

tests/Fixtures/TestBundle/DataTransformer/CustomInputDtoDataTransformer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
namespace ApiPlatform\Tests\Fixtures\TestBundle\DataTransformer;
1515

16-
use ApiPlatform\DataTransformer\DataTransformerInterface;
16+
use ApiPlatform\Core\DataTransformer\DataTransformerInterface;
1717
use ApiPlatform\Serializer\AbstractItemNormalizer;
1818
use ApiPlatform\Tests\Fixtures\TestBundle\Document\DummyDtoCustom as DummyDtoCustomDocument;
1919
use ApiPlatform\Tests\Fixtures\TestBundle\Dto\CustomInputDto;

0 commit comments

Comments
 (0)