Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[Notifier] Add mercure bridge #39342

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 1 commit into from
Jan 20, 2021
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
1 change: 1 addition & 0 deletions 1 composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@
"psr/http-client": "^1.0",
"psr/simple-cache": "^1.0",
"egulias/email-validator": "^2.1.10",
"symfony/mercure-bundle": "^0.2",
"symfony/phpunit-bridge": "^5.2",
"symfony/security-acl": "~2.8|~3.0",
"phpdocumentor/reflection-docblock": "^3.0|^4.0|^5.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
use Symfony\Bundle\FrameworkBundle\Routing\AnnotatedRouteControllerLoader;
use Symfony\Bundle\FrameworkBundle\Routing\RouteLoaderInterface;
use Symfony\Bundle\FullStack;
use Symfony\Bundle\MercureBundle\MercureBundle;
use Symfony\Component\Asset\PackageInterface;
use Symfony\Component\BrowserKit\AbstractBrowser;
use Symfony\Component\Cache\Adapter\AdapterInterface;
Expand All @@ -43,6 +44,8 @@
use Symfony\Component\Console\Command\Command;
use Symfony\Component\DependencyInjection\Alias;
use Symfony\Component\DependencyInjection\Argument\ServiceClosureArgument;
use Symfony\Component\DependencyInjection\Argument\ServiceLocatorArgument;
use Symfony\Component\DependencyInjection\Argument\TaggedIteratorArgument;
use Symfony\Component\DependencyInjection\ChildDefinition;
use Symfony\Component\DependencyInjection\Compiler\ServiceLocatorTagPass;
use Symfony\Component\DependencyInjection\ContainerBuilder;
Expand Down Expand Up @@ -112,6 +115,7 @@
use Symfony\Component\Notifier\Bridge\Iqsms\IqsmsTransportFactory;
use Symfony\Component\Notifier\Bridge\LinkedIn\LinkedInTransportFactory;
use Symfony\Component\Notifier\Bridge\Mattermost\MattermostTransportFactory;
use Symfony\Component\Notifier\Bridge\Mercure\MercureTransportFactory;
use Symfony\Component\Notifier\Bridge\Mobyt\MobytTransportFactory;
use Symfony\Component\Notifier\Bridge\Nexmo\NexmoTransportFactory;
use Symfony\Component\Notifier\Bridge\Octopush\OctopushTransportFactory;
Expand Down Expand Up @@ -2242,6 +2246,7 @@ private function registerNotifierConfiguration(array $config, ContainerBuilder $
LinkedInTransportFactory::class => 'notifier.transport_factory.linkedin',
GatewayApiTransportFactory::class => 'notifier.transport_factory.gatewayapi',
OctopushTransportFactory::class => 'notifier.transport_factory.octopush',
MercureTransportFactory::class => 'notifier.transport_factory.mercure',
];

foreach ($classToServices as $class => $service) {
Expand All @@ -2250,6 +2255,15 @@ private function registerNotifierConfiguration(array $config, ContainerBuilder $
}
}

if (class_exists(MercureTransportFactory::class)) {
chalasr marked this conversation as resolved.
Show resolved Hide resolved
if (!class_exists(MercureBundle::class)) {
throw new \LogicException('The MercureBundle is not registered in your application. Try running "composer require symfony/mercure-bundle".');
}

$container->getDefinition($classToServices[MercureTransportFactory::class])
->replaceArgument('$publisherLocator', new ServiceLocatorArgument(new TaggedIteratorArgument('mercure.publisher', null, null, true)));
}

if (isset($config['admin_recipients'])) {
$notifier = $container->getDefinition('notifier');
foreach ($config['admin_recipients'] as $i => $recipient) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use Symfony\Component\Notifier\Bridge\Iqsms\IqsmsTransportFactory;
use Symfony\Component\Notifier\Bridge\LinkedIn\LinkedInTransportFactory;
use Symfony\Component\Notifier\Bridge\Mattermost\MattermostTransportFactory;
use Symfony\Component\Notifier\Bridge\Mercure\MercureTransportFactory;
use Symfony\Component\Notifier\Bridge\Mobyt\MobytTransportFactory;
use Symfony\Component\Notifier\Bridge\Nexmo\NexmoTransportFactory;
use Symfony\Component\Notifier\Bridge\Octopush\OctopushTransportFactory;
Expand Down Expand Up @@ -135,6 +136,10 @@
->parent('notifier.transport_factory.abstract')
->tag('texter.transport_factory')

->set('notifier.transport_factory.mercure', MercureTransportFactory::class)
->parent('notifier.transport_factory.abstract')
->tag('chatter.transport_factory')

->set('notifier.transport_factory.null', NullTransportFactory::class)
->parent('notifier.transport_factory.abstract')
->tag('chatter.transport_factory')
Expand Down
4 changes: 4 additions & 0 deletions 4 src/Symfony/Component/Notifier/Bridge/Mercure/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/Tests export-ignore
/phpunit.xml.dist export-ignore
mtarld marked this conversation as resolved.
Show resolved Hide resolved
/.gitattributes export-ignore
/.gitignore export-ignore
7 changes: 7 additions & 0 deletions 7 src/Symfony/Component/Notifier/Bridge/Mercure/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
CHANGELOG
=========

5.3
---

* Add the bridge
19 changes: 19 additions & 0 deletions 19 src/Symfony/Component/Notifier/Bridge/Mercure/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Copyright (c) 2021 Fabien Potencier

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is furnished
to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
86 changes: 86 additions & 0 deletions 86 src/Symfony/Component/Notifier/Bridge/Mercure/MercureOptions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Notifier\Bridge\Mercure;

use Symfony\Component\Notifier\Message\MessageOptionsInterface;

/**
* @author Mathias Arlaud <mathias.arlaud@gmail.com>
*/
final class MercureOptions implements MessageOptionsInterface
{
private $topics;
private $private;
private $id;
private $type;
private $retry;

/**
* @param string|string[]|null $topics
*/
public function __construct($topics = null, bool $private = false, ?string $id = null, ?string $type = null, ?int $retry = null)
{
if (null !== $topics && !\is_array($topics) && !\is_string($topics)) {
throw new \TypeError(sprintf('"%s()" expects parameter 1 to be an array of strings, a string or null, "%s" given.', __METHOD__, get_debug_type($topics)));
}

$this->topics = null !== $topics ? (array) $topics : null;
$this->private = $private;
$this->id = $id;
$this->type = $type;
$this->retry = $retry;
}

/**
* @return string[]|null
*/
public function getTopics(): ?array
{
return $this->topics;
}

public function isPrivate(): bool
{
return $this->private;
}

public function getId(): ?string
{
return $this->id;
}

public function getType(): ?string
{
return $this->type;
}

public function getRetry(): ?int
{
return $this->retry;
}

public function toArray(): array
{
return [
'topics' => $this->topics,
'private' => $this->private,
'id' => $this->id,
'type' => $this->type,
'retry' => $this->retry,
];
}

public function getRecipientId(): ?string
{
return null;
}
}
100 changes: 100 additions & 0 deletions 100 src/Symfony/Component/Notifier/Bridge/Mercure/MercureTransport.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Notifier\Bridge\Mercure;

use Symfony\Component\Mercure\PublisherInterface;
use Symfony\Component\Mercure\Update;
use Symfony\Component\Notifier\Exception\InvalidArgumentException;
use Symfony\Component\Notifier\Exception\LogicException;
use Symfony\Component\Notifier\Exception\TransportException;
use Symfony\Component\Notifier\Exception\UnsupportedMessageTypeException;
use Symfony\Component\Notifier\Message\ChatMessage;
use Symfony\Component\Notifier\Message\MessageInterface;
use Symfony\Component\Notifier\Message\SentMessage;
use Symfony\Component\Notifier\Transport\AbstractTransport;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
use Symfony\Contracts\HttpClient\Exception\ExceptionInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;

/**
* @author Mathias Arlaud <mathias.arlaud@gmail.com>
*/
final class MercureTransport extends AbstractTransport
{
private $publisher;
private $publisherId;
private $topics;

/**
* @param string|string[]|null $topics
*/
public function __construct(PublisherInterface $publisher, string $publisherId, $topics = null, ?HttpClientInterface $client = null, ?EventDispatcherInterface $dispatcher = null)
{
if (null !== $topics && !\is_array($topics) && !\is_string($topics)) {
throw new \TypeError(sprintf('"%s()" expects parameter 3 to be an array of strings, a string or null, "%s" given.', __METHOD__, get_debug_type($topics)));
}

$this->publisher = $publisher;
$this->publisherId = $publisherId;
$this->topics = $topics ?? 'https://symfony.com/notifier';

parent::__construct($client, $dispatcher);
}

public function __toString(): string
{
return sprintf('mercure://%s?%s', $this->publisherId, http_build_query(['topic' => $this->topics]));
}

public function supports(MessageInterface $message): bool
{
return $message instanceof ChatMessage && (null === $message->getOptions() || $message->getOptions() instanceof MercureOptions);
}

/**
* @see https://symfony.com/doc/current/mercure.html#publishing
*/
protected function doSend(MessageInterface $message): SentMessage
{
if (!$message instanceof ChatMessage) {
throw new UnsupportedMessageTypeException(__CLASS__, ChatMessage::class, $message);
}

if (($options = $message->getOptions()) && !$options instanceof MercureOptions) {
throw new LogicException(sprintf('The "%s" transport only supports instances of "%s" for options.', __CLASS__, MercureOptions::class));
}

if (null === $options) {
$options = new MercureOptions($this->topics);
}

// @see https://www.w3.org/TR/activitystreams-core/#jsonld
$update = new Update($options->getTopics() ?? $this->topics, json_encode([
'@context' => 'https://www.w3.org/ns/activitystreams',
'type' => 'Announce',
'summary' => $message->getSubject(),
]), $options->isPrivate(), $options->getId(), $options->getType(), $options->getRetry());

try {
$messageId = ($this->publisher)($update);

$sentMessage = new SentMessage($message, (string) $this);
$sentMessage->setMessageId($messageId);

return $sentMessage;
} catch (ExceptionInterface $e) {
throw new TransportException(sprintf('Unable to post the Mercure message: "%s".', $e->getResponse()->getContent(false)), $e->getResponse(), $e->getCode(), $e);
} catch (\InvalidArgumentException $e) {
throw new InvalidArgumentException(sprintf('Unable to post the Mercure message: "%s".', $e->getMessage()), $e->getCode(), $e);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Notifier\Bridge\Mercure;

use Symfony\Component\Mercure\PublisherInterface;
use Symfony\Component\Notifier\Exception\LogicException;
use Symfony\Component\Notifier\Exception\UnsupportedSchemeException;
use Symfony\Component\Notifier\Transport\AbstractTransportFactory;
use Symfony\Component\Notifier\Transport\Dsn;
use Symfony\Component\Notifier\Transport\TransportInterface;
use Symfony\Contracts\Service\ServiceProviderInterface;

/**
* @author Mathias Arlaud <mathias.arlaud@gmail.com>
*/
final class MercureTransportFactory extends AbstractTransportFactory
{
private $publisherLocator;

/**
* @param ServiceProviderInterface $publisherLocator A container that holds {@see PublisherInterface} instances
*/
public function __construct(ServiceProviderInterface $publisherLocator)
{
parent::__construct();

$this->publisherLocator = $publisherLocator;
}

/**
* @return MercureTransport
*/
public function create(Dsn $dsn): TransportInterface
mtarld marked this conversation as resolved.
Show resolved Hide resolved
{
if ('mercure' !== $dsn->getScheme()) {
throw new UnsupportedSchemeException($dsn, 'mercure', $this->getSupportedSchemes());
mtarld marked this conversation as resolved.
Show resolved Hide resolved
}

$publisherId = $dsn->getHost();
if (!$this->publisherLocator->has($publisherId)) {
throw new LogicException(sprintf('"%s" not found. Did you mean one of: %s?', $publisherId, implode(', ', array_keys($this->publisherLocator->getProvidedServices()))));
}

$topic = $dsn->getOption('topic');

return new MercureTransport($this->publisherLocator->get($publisherId), $publisherId, $topic);
}

protected function getSupportedSchemes(): array
{
return ['mercure'];
}
}
23 changes: 23 additions & 0 deletions 23 src/Symfony/Component/Notifier/Bridge/Mercure/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
Mercure Notifier
================

Provides [Mercure](https://github.com/symfony/mercure) integration for Symfony Notifier.

DSN example
-----------

```
MERCURE_DSN=mercure://PUBLISHER_SERVICE_ID?topic=TOPIC
```

where:
- `PUBLISHER_SERVICE_ID` is the Mercure publisher service id
- `TOPIC` is the topic IRI (optional, default: `https://symfony.com/notifier`. Could be either a single topic: `topic=https://foo` or multiple topics: `topic[]=/foo/1&topic[]=https://bar`)

Resources
---------

* [Contributing](https://symfony.com/doc/current/contributing/index.html)
* [Report issues](https://github.com/symfony/symfony/issues) and
[send Pull Requests](https://github.com/symfony/symfony/pulls)
in the [main Symfony repository](https://github.com/symfony/symfony)
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.