-
-
Notifications
You must be signed in to change notification settings - Fork 921
Provides an integration with the Symfony Messenger component #2398
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?xml version="1.0" ?> | ||
|
||
<container xmlns="http://symfony.com/schema/dic/services" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> | ||
|
||
<services> | ||
<service id="api_platform.message_bus" alias="message_bus" /> | ||
|
||
<service id="api_platform.messenger.data_persister" class="ApiPlatform\Core\Bridge\Symfony\Messenger\DataPersister" public="false"> | ||
<argument type="service" id="api_platform.metadata.resource.metadata_factory" /> | ||
<argument type="service" id="api_platform.message_bus" /> | ||
|
||
<tag name="api_platform.data_persister" priority="-900" /> | ||
</service> | ||
</services> | ||
|
||
</container> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the API Platform project. | ||
* | ||
* (c) Kévin Dunglas <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace ApiPlatform\Core\Bridge\Symfony\Messenger; | ||
|
||
use ApiPlatform\Core\DataPersister\DataPersisterInterface; | ||
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface; | ||
use ApiPlatform\Core\Util\ClassInfoTrait; | ||
use Symfony\Component\Messenger\Envelope; | ||
use Symfony\Component\Messenger\MessageBusInterface; | ||
|
||
/** | ||
* Dispatches the given resource using the message bus of Symfony Messenger. | ||
* | ||
* @experimental | ||
* | ||
* @author Kévin Dunglas <[email protected]> | ||
*/ | ||
final class DataPersister implements DataPersisterInterface | ||
{ | ||
use ClassInfoTrait; | ||
|
||
private $resourceMetadataFactory; | ||
private $messageBus; | ||
|
||
public function __construct(ResourceMetadataFactoryInterface $resourceMetadataFactory, MessageBusInterface $messageBus) | ||
{ | ||
$this->resourceMetadataFactory = $resourceMetadataFactory; | ||
$this->messageBus = $messageBus; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function supports($data): bool | ||
{ | ||
return true === $this->resourceMetadataFactory->create($this->getObjectClass($data))->getAttribute('messenger'); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function persist($data) | ||
{ | ||
$this->messageBus->dispatch($data); | ||
|
||
return $data; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd return the |
||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function remove($data) | ||
{ | ||
$this->messageBus->dispatch(new Envelope($data, new RemoveStamp())); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the API Platform project. | ||
* | ||
* (c) Kévin Dunglas <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace ApiPlatform\Core\Bridge\Symfony\Messenger; | ||
|
||
use Symfony\Component\Messenger\Stamp\StampInterface; | ||
|
||
/** | ||
* Hints that the resource in the envelope must be removed. | ||
* | ||
* @experimental | ||
* | ||
* @author Kévin Dunglas <[email protected]> | ||
*/ | ||
final class RemoveStamp implements StampInterface | ||
{ | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the API Platform project. | ||
* | ||
* (c) Kévin Dunglas <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace ApiPlatform\Core\Tests\Bridge\Symfony\Messenger; | ||
|
||
use ApiPlatform\Core\Bridge\Symfony\Messenger\DataPersister; | ||
use ApiPlatform\Core\Bridge\Symfony\Messenger\RemoveStamp; | ||
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface; | ||
use ApiPlatform\Core\Metadata\Resource\ResourceMetadata; | ||
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Dummy; | ||
use PHPUnit\Framework\TestCase; | ||
use Prophecy\Argument; | ||
use Symfony\Component\Messenger\Envelope; | ||
use Symfony\Component\Messenger\MessageBusInterface; | ||
|
||
/** | ||
* @author Kévin Dunglas <[email protected]> | ||
*/ | ||
class DataPersisterTest extends TestCase | ||
{ | ||
public function testSupport() | ||
{ | ||
$metadataFactoryProphecy = $this->prophesize(ResourceMetadataFactoryInterface::class); | ||
$metadataFactoryProphecy->create(Dummy::class)->willReturn(new ResourceMetadata(null, null, null, null, null, ['messenger' => true])); | ||
|
||
$dataPersister = new DataPersister($metadataFactoryProphecy->reveal(), $this->prophesize(MessageBusInterface::class)->reveal()); | ||
$this->assertTrue($dataPersister->supports(new Dummy())); | ||
} | ||
|
||
public function testPersist() | ||
{ | ||
$dummy = new Dummy(); | ||
|
||
$messageBus = $this->prophesize(MessageBusInterface::class); | ||
$messageBus->dispatch($dummy)->willReturn(new Envelope(new \stdClass()))->shouldBeCalled(); | ||
|
||
$dataPersister = new DataPersister($this->prophesize(ResourceMetadataFactoryInterface::class)->reveal(), $messageBus->reveal()); | ||
$this->assertSame($dummy, $dataPersister->persist($dummy)); | ||
} | ||
|
||
public function testRemove() | ||
{ | ||
$dummy = new Dummy(); | ||
|
||
$messageBus = $this->prophesize(MessageBusInterface::class); | ||
$messageBus->dispatch(Argument::that(function (Envelope $envelope) { | ||
return null !== $envelope->last(RemoveStamp::class); | ||
}))->willReturn(new Envelope(new \stdClass()))->shouldBeCalled(); | ||
|
||
$dataPersister = new DataPersister($this->prophesize(ResourceMetadataFactoryInterface::class)->reveal(), $messageBus->reveal()); | ||
$dataPersister->remove($dummy); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the API Platform project. | ||
* | ||
* (c) Kévin Dunglas <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace ApiPlatform\Core\Tests\Bridge\Symfony\Messenger; | ||
|
||
use ApiPlatform\Core\Bridge\Symfony\Messenger\RemoveStamp; | ||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\Messenger\Stamp\StampInterface; | ||
|
||
/** | ||
* @author Kévin Dunglas <[email protected]> | ||
*/ | ||
class RemoveStampTest extends TestCase | ||
{ | ||
public function testConstruct() | ||
{ | ||
$this->assertInstanceOf(StampInterface::class, new RemoveStamp()); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we have created a new
ContextAwareDataPersisterInterface
, so that themessenger
attribute could be set at the operation level?