Skip to content

Commit 5701093

Browse files
authored
Merge pull request #2569 from soyuka/messenger-per-operation
Allow per-operation messenger configuration
2 parents 92adbec + 80632f7 commit 5701093

File tree

5 files changed

+65
-15
lines changed

5 files changed

+65
-15
lines changed

src/Bridge/Symfony/Messenger/DataPersister.php

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313

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

16-
use ApiPlatform\Core\DataPersister\DataPersisterInterface;
16+
use ApiPlatform\Core\Api\OperationType;
17+
use ApiPlatform\Core\DataPersister\ContextAwareDataPersisterInterface;
1718
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface;
1819
use ApiPlatform\Core\Util\ClassInfoTrait;
1920
use Symfony\Component\Messenger\Envelope;
@@ -27,7 +28,7 @@
2728
*
2829
* @author Kévin Dunglas <[email protected]>
2930
*/
30-
final class DataPersister implements DataPersisterInterface
31+
final class DataPersister implements ContextAwareDataPersisterInterface
3132
{
3233
use ClassInfoTrait;
3334

@@ -43,9 +44,20 @@ public function __construct(ResourceMetadataFactoryInterface $resourceMetadataFa
4344
/**
4445
* {@inheritdoc}
4546
*/
46-
public function supports($data): bool
47+
public function supports($data, array $context = []): bool
4748
{
48-
return true === $this->resourceMetadataFactory->create($this->getObjectClass($data))->getAttribute('messenger');
49+
$resourceMetadata = $this->resourceMetadataFactory->create($this->getObjectClass($data));
50+
if (null !== $operationName = $context['collection_operation_name'] ?? $context['item_operation_name'] ?? null) {
51+
return true === $resourceMetadata->getTypedOperationAttribute(
52+
$context['collection_operation_name'] ?? false ? OperationType::COLLECTION : OperationType::ITEM,
53+
$operationName,
54+
'messenger',
55+
false,
56+
true
57+
);
58+
}
59+
60+
return true === $resourceMetadata->getAttribute('messenger');
4961
}
5062

5163
/**
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <[email protected]>
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+
declare(strict_types=1);
13+
14+
namespace ApiPlatform\Core\DataPersister;
15+
16+
/**
17+
* Manages data persistence.
18+
*
19+
* @author Antoine Bluchet <[email protected]>
20+
*/
21+
interface ContextAwareDataPersisterInterface extends DataPersisterInterface
22+
{
23+
/**
24+
* {@inheritdoc}
25+
*/
26+
public function supports($data, array $context = []): bool;
27+
}

src/EventListener/WriteListener.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public function onKernelView(GetResponseForControllerResultEvent $event)
4949
}
5050

5151
$controllerResult = $event->getControllerResult();
52-
if (!$this->dataPersister->supports($controllerResult)) {
52+
if (!$this->dataPersister->supports($controllerResult, $attributes)) {
5353
return;
5454
}
5555

tests/Bridge/Symfony/Messenger/DataPersisterTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,14 @@ public function testHandle()
7272
$dataPersister = new DataPersister($this->prophesize(ResourceMetadataFactoryInterface::class)->reveal(), $messageBus->reveal());
7373
$this->assertSame('result', $dataPersister->persist($dummy));
7474
}
75+
76+
public function testSupportsPerOperation()
77+
{
78+
$resourceMetadata = (new ResourceMetadata())->withCollectionOperations(['operation' => ['messenger' => true]]);
79+
$metadataFactoryProphecy = $this->prophesize(ResourceMetadataFactoryInterface::class);
80+
$metadataFactoryProphecy->create(Dummy::class)->willReturn($resourceMetadata);
81+
82+
$dataPersister = new DataPersister($metadataFactoryProphecy->reveal(), $this->prophesize(MessageBusInterface::class)->reveal());
83+
$this->assertTrue($dataPersister->supports(new Dummy(), ['collection_operation_name' => 'operation']));
84+
}
7585
}

tests/EventListener/WriteListenerTest.php

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\ConcreteDummy;
2222
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Dummy;
2323
use PHPUnit\Framework\TestCase;
24+
use Prophecy\Argument;
2425
use Symfony\Component\HttpFoundation\Request;
2526
use Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent;
2627
use Symfony\Component\HttpKernel\HttpKernelInterface;
@@ -36,7 +37,7 @@ public function testOnKernelViewWithControllerResultAndPersist()
3637
$dummy->setName('Dummyrino');
3738

3839
$dataPersisterProphecy = $this->prophesize(DataPersisterInterface::class);
39-
$dataPersisterProphecy->supports($dummy)->willReturn(true)->shouldBeCalled();
40+
$dataPersisterProphecy->supports($dummy, Argument::type('array'))->willReturn(true)->shouldBeCalled();
4041
$dataPersisterProphecy->persist($dummy)->willReturn($dummy)->shouldBeCalled();
4142

4243
$iriConverterProphecy = $this->prophesize(IriConverterInterface::class);
@@ -71,7 +72,7 @@ public function testOnKernelViewWithControllerResultAndPersistReturningVoid()
7172
$dummy->setName('Dummyrino');
7273

7374
$dataPersisterProphecy = $this->prophesize(DataPersisterInterface::class);
74-
$dataPersisterProphecy->supports($dummy)->willReturn(true)->shouldBeCalled();
75+
$dataPersisterProphecy->supports($dummy, Argument::type('array'))->willReturn(true)->shouldBeCalled();
7576
$dataPersisterProphecy->persist($dummy)->shouldBeCalled();
7677

7778
$request = new Request([], [], ['_api_resource_class' => Dummy::class]);
@@ -104,7 +105,7 @@ public function testOnKernelViewWithControllerResultAndPersistWithImmutableResou
104105
$dummy2->setName('Dummyferoce');
105106

106107
$dataPersisterProphecy = $this->prophesize(DataPersisterInterface::class);
107-
$dataPersisterProphecy->supports($dummy)->willReturn(true)->shouldBeCalled();
108+
$dataPersisterProphecy->supports($dummy, Argument::type('array'))->willReturn(true)->shouldBeCalled();
108109

109110
$iriConverterProphecy = $this->prophesize(IriConverterInterface::class);
110111
$iriConverterProphecy->getIriFromItem($dummy)->willReturn('/dummy/1')->shouldBeCalled();
@@ -141,7 +142,7 @@ public function testOnKernelViewDoNotCallIriConverterWhenOutputClassDisabled()
141142
$dummy->setName('Dummyrino');
142143

143144
$dataPersisterProphecy = $this->prophesize(DataPersisterInterface::class);
144-
$dataPersisterProphecy->supports($dummy)->willReturn(true)->shouldBeCalled();
145+
$dataPersisterProphecy->supports($dummy, Argument::type('array'))->willReturn(true)->shouldBeCalled();
145146

146147
$iriConverterProphecy = $this->prophesize(IriConverterInterface::class);
147148
$iriConverterProphecy->getIriFromItem($dummy)->shouldNotBeCalled();
@@ -170,7 +171,7 @@ public function testOnKernelViewWithControllerResultAndRemove()
170171
$dummy->setName('Dummyrino');
171172

172173
$dataPersisterProphecy = $this->prophesize(DataPersisterInterface::class);
173-
$dataPersisterProphecy->supports($dummy)->willReturn(true)->shouldBeCalled();
174+
$dataPersisterProphecy->supports($dummy, Argument::type('array'))->willReturn(true)->shouldBeCalled();
174175
$dataPersisterProphecy->remove($dummy)->shouldBeCalled();
175176

176177
$iriConverterProphecy = $this->prophesize(IriConverterInterface::class);
@@ -195,7 +196,7 @@ public function testOnKernelViewWithSafeMethod()
195196
$dummy->setName('Dummyrino');
196197

197198
$dataPersisterProphecy = $this->prophesize(DataPersisterInterface::class);
198-
$dataPersisterProphecy->supports($dummy)->shouldNotBeCalled();
199+
$dataPersisterProphecy->supports($dummy, Argument::type('array'))->shouldNotBeCalled();
199200
$dataPersisterProphecy->persist($dummy)->shouldNotBeCalled();
200201
$dataPersisterProphecy->remove($dummy)->shouldNotBeCalled();
201202

@@ -221,7 +222,7 @@ public function testOnKernelViewWithPersistFlagOff()
221222
$dummy->setName('Dummyrino');
222223

223224
$dataPersisterProphecy = $this->prophesize(DataPersisterInterface::class);
224-
$dataPersisterProphecy->supports($dummy)->shouldNotBeCalled();
225+
$dataPersisterProphecy->supports($dummy, Argument::type('array'))->shouldNotBeCalled();
225226
$dataPersisterProphecy->persist($dummy)->shouldNotBeCalled();
226227
$dataPersisterProphecy->remove($dummy)->shouldNotBeCalled();
227228

@@ -247,7 +248,7 @@ public function testOnKernelViewWithNoResourceClass()
247248
$dummy->setName('Dummyrino');
248249

249250
$dataPersisterProphecy = $this->prophesize(DataPersisterInterface::class);
250-
$dataPersisterProphecy->supports($dummy)->shouldNotBeCalled();
251+
$dataPersisterProphecy->supports($dummy, Argument::type('array'))->shouldNotBeCalled();
251252
$dataPersisterProphecy->persist($dummy)->shouldNotBeCalled();
252253
$dataPersisterProphecy->remove($dummy)->shouldNotBeCalled();
253254

@@ -272,7 +273,7 @@ public function testOnKernelViewWithParentResourceClass()
272273
$dummy = new ConcreteDummy();
273274

274275
$dataPersisterProphecy = $this->prophesize(DataPersisterInterface::class);
275-
$dataPersisterProphecy->supports($dummy)->willReturn(true)->shouldBeCalled();
276+
$dataPersisterProphecy->supports($dummy, Argument::type('array'))->willReturn(true)->shouldBeCalled();
276277
$dataPersisterProphecy->persist($dummy)->willReturn($dummy)->shouldBeCalled();
277278

278279
$iriConverterProphecy = $this->prophesize(IriConverterInterface::class);
@@ -297,7 +298,7 @@ public function testOnKernelViewWithNoDataPersisterSupport()
297298
$dummy->setName('Dummyrino');
298299

299300
$dataPersisterProphecy = $this->prophesize(DataPersisterInterface::class);
300-
$dataPersisterProphecy->supports($dummy)->willReturn(false)->shouldBeCalled();
301+
$dataPersisterProphecy->supports($dummy, Argument::type('array'))->willReturn(false)->shouldBeCalled();
301302
$dataPersisterProphecy->persist($dummy)->shouldNotBeCalled();
302303
$dataPersisterProphecy->remove($dummy)->shouldNotBeCalled();
303304

0 commit comments

Comments
 (0)