21
21
use ApiPlatform \Exception \RuntimeException ;
22
22
use ApiPlatform \GraphQl \Subscription \MercureSubscriptionIriGeneratorInterface as GraphQlMercureSubscriptionIriGeneratorInterface ;
23
23
use ApiPlatform \GraphQl \Subscription \SubscriptionManagerInterface as GraphQlSubscriptionManagerInterface ;
24
+ use ApiPlatform \Metadata \HttpOperation ;
24
25
use ApiPlatform \Metadata \Resource \Factory \ResourceMetadataCollectionFactoryInterface ;
25
26
use ApiPlatform \Symfony \Messenger \DispatchTrait ;
26
27
use ApiPlatform \Util \ResourceClassInfoTrait ;
@@ -63,7 +64,7 @@ final class PublishMercureUpdatesListener
63
64
/**
64
65
* @param array<string, string[]|string> $formats
65
66
*/
66
- public function __construct (ResourceClassResolverInterface $ resourceClassResolver , private readonly IriConverterInterface $ iriConverter , ResourceMetadataCollectionFactoryInterface $ resourceMetadataFactory , private readonly SerializerInterface $ serializer , private readonly array $ formats , MessageBusInterface $ messageBus = null , private readonly ?HubRegistry $ hubRegistry = null , private readonly ?GraphQlSubscriptionManagerInterface $ graphQlSubscriptionManager = null , private readonly ?GraphQlMercureSubscriptionIriGeneratorInterface $ graphQlMercureSubscriptionIriGenerator = null , ExpressionLanguage $ expressionLanguage = null )
67
+ public function __construct (ResourceClassResolverInterface $ resourceClassResolver , private readonly IriConverterInterface $ iriConverter , ResourceMetadataCollectionFactoryInterface $ resourceMetadataFactory , private readonly SerializerInterface $ serializer , private readonly array $ formats , MessageBusInterface $ messageBus = null , private readonly ?HubRegistry $ hubRegistry = null , private readonly ?GraphQlSubscriptionManagerInterface $ graphQlSubscriptionManager = null , private readonly ?GraphQlMercureSubscriptionIriGeneratorInterface $ graphQlMercureSubscriptionIriGenerator = null , ExpressionLanguage $ expressionLanguage = null , private bool $ includeType = false )
67
68
{
68
69
if (null === $ messageBus && null === $ hubRegistry ) {
69
70
throw new InvalidArgumentException ('A message bus or a hub registry must be provided. ' );
@@ -84,6 +85,10 @@ public function __construct(ResourceClassResolverInterface $resourceClassResolve
84
85
new ExpressionFunction ('iri ' , static fn (string $ apiResource , int $ referenceType = UrlGeneratorInterface::ABS_URL ): string => sprintf ('iri(%s, %d) ' , $ apiResource , $ referenceType ), static fn (array $ arguments , $ apiResource , int $ referenceType = UrlGeneratorInterface::ABS_URL ): string => $ iriConverter ->getIriFromResource ($ apiResource , $ referenceType ))
85
86
);
86
87
}
88
+
89
+ if (false === $ this ->includeType ) {
90
+ trigger_deprecation ('api-platform/core ' , '3.1 ' , 'Having mercure.include_type (always include @type in Mercure updates, even delete ones) set to false in the configuration is deprecated. It will be true by default in API Platform 4.0. ' );
91
+ }
87
92
}
88
93
89
94
/**
@@ -150,8 +155,9 @@ private function storeObjectToPublish(object $object, string $property): void
150
155
return ;
151
156
}
152
157
158
+ $ operation = $ this ->resourceMetadataFactory ->create ($ resourceClass )->getOperation ();
153
159
try {
154
- $ options = $ this -> resourceMetadataFactory -> create ( $ resourceClass )-> getOperation () ->getMercure () ?? false ;
160
+ $ options = $ operation ->getMercure () ?? false ;
155
161
} catch (OperationNotFoundException ) {
156
162
return ;
157
163
}
@@ -208,9 +214,15 @@ private function storeObjectToPublish(object $object, string $property): void
208
214
}
209
215
210
216
if ('deletedObjects ' === $ property ) {
217
+ $ types = $ operation instanceof HttpOperation ? $ operation ->getTypes () : null ;
218
+ if (null === $ types ) {
219
+ $ types = [$ operation ->getShortName ()];
220
+ }
221
+
211
222
$ this ->deletedObjects [(object ) [
212
223
'id ' => $ this ->iriConverter ->getIriFromResource ($ object ),
213
224
'iri ' => $ this ->iriConverter ->getIriFromResource ($ object , UrlGeneratorInterface::ABS_URL ),
225
+ 'type ' => 1 === \count ($ types ) ? $ types [0 ] : $ types ,
214
226
]] = $ options ;
215
227
216
228
return ;
@@ -222,12 +234,12 @@ private function storeObjectToPublish(object $object, string $property): void
222
234
private function publishUpdate (object $ object , array $ options , string $ type ): void
223
235
{
224
236
if ($ object instanceof \stdClass) {
225
- // By convention, if the object has been deleted, we send only its IRI.
237
+ // By convention, if the object has been deleted, we send only its IRI and its type .
226
238
// This may change in the feature, because it's not JSON Merge Patch compliant,
227
239
// and I'm not a fond of this approach.
228
240
$ iri = $ options ['topics ' ] ?? $ object ->iri ;
229
241
/** @var string $data */
230
- $ data = json_encode (['@id ' => $ object ->id ], \JSON_THROW_ON_ERROR );
242
+ $ data = json_encode (['@id ' => $ object ->id ] + ( $ this -> includeType ? [ ' @type ' => $ object -> type ] : []) , \JSON_THROW_ON_ERROR );
231
243
} else {
232
244
$ resourceClass = $ this ->getObjectClass ($ object );
233
245
$ context = $ options ['normalization_context ' ] ?? $ this ->resourceMetadataFactory ->create ($ resourceClass )->getOperation ()->getNormalizationContext () ?? [];
0 commit comments