Skip to content

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 2 commits into from
Dec 21, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"behat/symfony2-extension": "^2.1.1",
"behatch/contexts": "3.1.0",
"doctrine/annotations": "^1.2",
"doctrine/doctrine-cache-bundle": "^1.3.5",
"doctrine/doctrine-bundle": "^1.8",
"doctrine/orm": "^2.6.3",
"friendsofsymfony/user-bundle": "^2.1",
Expand All @@ -42,7 +43,8 @@
"php-mock/php-mock-phpunit": "^2.0",
"phpdocumentor/reflection-docblock": "^3.0 || ^4.0",
"phpdocumentor/type-resolver": "^0.3 || ^0.4",
"phpunit/phpunit": "^6.1",
"phpspec/prophecy": "^1.8",
"phpunit/phpunit": "^7.5.1",
"psr/log": "^1.0",
"ramsey/uuid": "^3.7",
"ramsey/uuid-doctrine": "^1.4",
Expand All @@ -57,11 +59,10 @@
"symfony/expression-language": "^3.4 || ^4.0",
"symfony/finder": "^3.4 || ^4.0",
"symfony/form": "^3.4 || ^4.0",
"symfony/framework-bundle": "^3.4 || ^4.0",
"symfony/mercure": "*",
"symfony/framework-bundle": "^4.2",
"symfony/mercure-bundle": "*",
"symfony/messenger": "^4.1",
"symfony/phpunit-bridge": "^3.4 || ^4.0",
"symfony/messenger": "^4.2",
"symfony/phpunit-bridge": "^3.4.5 || ^4.0.5",
"symfony/routing": "^3.4 || ^4.0",
"symfony/security": "^3.4 || ^4.0",
"symfony/security-bundle": "^3.4 || ^4.0",
Expand Down
8 changes: 8 additions & 0 deletions src/Annotation/ApiResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
* @Attribute("itemOperations", type="array"),
* @Attribute("maximumItemsPerPage", type="int"),
* @Attribute("mercure", type="mixed"),
* @Attribute("messenger", type="bool"),
* @Attribute("normalizationContext", type="array"),
* @Attribute("openapiContext", type="array"),
* @Attribute("order", type="array"),
Expand Down Expand Up @@ -184,6 +185,13 @@ final class ApiResource
*/
private $mercure;

/**
* @see https://github.com/Haehnchen/idea-php-annotation-plugin/issues/112
*
* @var bool
*/
private $messenger;

/**
* @see https://github.com/Haehnchen/idea-php-annotation-plugin/issues/112
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
use Symfony\Component\Finder\Finder;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\Messenger\MessageBusInterface;
use Symfony\Component\Validator\Validator\ValidatorInterface;
use Symfony\Component\Yaml\Yaml;

Expand Down Expand Up @@ -145,6 +146,10 @@ public function load(array $configs, ContainerBuilder $container)
$this->registerValidatorConfiguration($container, $config);
$this->registerDataCollectorConfiguration($container, $config, $loader);
$this->registerMercureConfiguration($container, $config, $loader, $useDoctrine);

if (interface_exists(MessageBusInterface::class)) {
$loader->load('messenger.xml');
}
}

/**
Expand Down
18 changes: 18 additions & 0 deletions src/Bridge/Symfony/Bundle/Resources/config/messenger.xml
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>
67 changes: 67 additions & 0 deletions src/Bridge/Symfony/Messenger/DataPersister.php
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');
Copy link
Contributor

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 the messenger attribute could be set at the operation level?

}

/**
* {@inheritdoc}
*/
public function persist($data)
{
$this->messageBus->dispatch($data);

return $data;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd return the result of the HandledStamp instead if it exists.

}

/**
* {@inheritdoc}
*/
public function remove($data)
{
$this->messageBus->dispatch(new Envelope($data, new RemoveStamp()));
}
}
27 changes: 27 additions & 0 deletions src/Bridge/Symfony/Messenger/RemoveStamp.php
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
{
}
6 changes: 6 additions & 0 deletions tests/Annotation/ApiResourceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,16 @@ public function testConstruct()
'formats' => ['foo', 'bar' => ['application/bar']],
'filters' => ['foo', 'bar'],
'graphql' => ['query' => ['normalization_context' => ['groups' => ['foo', 'bar']]]],
'inputClass' => 'Foo',
'iri' => 'http://example.com/res',
'itemOperations' => ['foo' => ['bar']],
'maximumItemsPerPage' => 42,
'mercure' => '[\'foo\', object.owner]',
'messenger' => true,
'normalizationContext' => ['groups' => ['bar']],
'order' => ['foo', 'bar' => 'ASC'],
'openapiContext' => ['description' => 'foo'],
'outputClass' => 'Bar',
'paginationClientEnabled' => true,
'paginationClientItemsPerPage' => true,
'paginationClientPartial' => true,
Expand Down Expand Up @@ -76,11 +79,14 @@ public function testConstruct()
'force_eager' => false,
'formats' => ['foo', 'bar' => ['application/bar']],
'filters' => ['foo', 'bar'],
'input_class' => 'Foo',
'maximum_items_per_page' => 42,
'mercure' => '[\'foo\', object.owner]',
'messenger' => true,
'normalization_context' => ['groups' => ['bar']],
'order' => ['foo', 'bar' => 'ASC'],
'openapi_context' => ['description' => 'foo'],
'output_class' => 'Bar',
'pagination_client_enabled' => true,
'pagination_client_items_per_page' => true,
'pagination_client_partial' => true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -768,6 +768,7 @@ private function getBaseContainerBuilderProphecy()
'api_platform.validator',
'api_platform.mercure.listener.response.add_link_header',
'api_platform.doctrine.listener.mercure.publish',
'api_platform.messenger.data_persister',
];

foreach ($definitions as $definition) {
Expand All @@ -776,6 +777,7 @@ private function getBaseContainerBuilderProphecy()

$aliases = [
'api_platform.http_cache.purger' => 'api_platform.http_cache.purger.varnish',
'api_platform.message_bus' => 'message_bus',
EagerLoadingExtension::class => 'api_platform.doctrine.orm.query_extension.eager_loading',
FilterExtension::class => 'api_platform.doctrine.orm.query_extension.filter',
FilterEagerLoadingExtension::class => 'api_platform.doctrine.orm.query_extension.filter_eager_loading',
Expand Down
63 changes: 63 additions & 0 deletions tests/Bridge/Symfony/Messenger/DataPersisterTest.php
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);
}
}
29 changes: 29 additions & 0 deletions tests/Bridge/Symfony/Messenger/RemoveStampTest.php
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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
class AnnotationPropertyMetadataFactoryTest extends TestCase
{
/**
* @dataProvider getDependencies
* @dataProvider dependenciesProvider
*/
public function testCreateProperty(ProphecyInterface $reader, ProphecyInterface $decorated = null, string $description)
{
Expand All @@ -48,7 +48,7 @@ public function testCreateProperty(ProphecyInterface $reader, ProphecyInterface
$this->assertEquals(['foo' => 'bar'], $metadata->getAttributes());
}

public function getDependencies()
public function dependenciesProvider(): array
{
$annotation = new ApiProperty();
$annotation->description = 'description';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
class AnnotationPropertyNameCollectionFactoryTest extends TestCase
{
/**
* @dataProvider getDependencies
* @dataProvider dependenciesProvider
*/
public function testCreate(PropertyNameCollectionFactoryInterface $decorated = null, array $results)
{
Expand All @@ -49,7 +49,7 @@ public function testCreate(PropertyNameCollectionFactoryInterface $decorated = n
$this->assertEquals($results, iterator_to_array($metadata));
}

public function getDependencies()
public function dependenciesProvider(): array
{
$decoratedThrowsNotFound = $this->prophesize(PropertyNameCollectionFactoryInterface::class);
$decoratedThrowsNotFound->create(Dummy::class, [])->willThrow(new ResourceClassNotFoundException())->shouldBeCalled();
Expand All @@ -65,7 +65,7 @@ public function getDependencies()
}

/**
* @dataProvider getUpperCaseDependencies
* @dataProvider upperCaseDependenciesProvider
*/
public function testUpperCaseCreate(ObjectProphecy $decorated = null, array $results)
{
Expand All @@ -83,7 +83,7 @@ public function testUpperCaseCreate(ObjectProphecy $decorated = null, array $res
$this->assertEquals($results, iterator_to_array($metadata));
}

public function getUpperCaseDependencies()
public function upperCaseDependenciesProvider(): array
{
$decoratedThrowsNotFound = $this->prophesize(PropertyNameCollectionFactoryInterface::class);
$decoratedThrowsNotFound->create(UpperCaseIdentifierDummy::class, [])->willThrow(new ResourceClassNotFoundException())->shouldBeCalled();
Expand Down
Loading