8000 [Notifier] Add mercure bridge · symfony/symfony@faa0539 · GitHub
[go: up one dir, main page]

Skip to content

Commit faa0539

Browse files
committed
[Notifier] Add mercure bridge
1 parent 31ee43a commit faa0539

File tree

16 files changed

+513
-0
lines changed

16 files changed

+513
-0
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@
130130
"psr/http-client": "^1.0",
131131
"psr/simple-cache": "^1.0",
132132
"egulias/email-validator": "~1.2,>=1.2.8|~2.0",
133+
"symfony/mercure-bundle": "^0.2",
133134
"symfony/phpunit-bridge": "^5.2",
134135
"symfony/security-acl": "~2.8|~3.0",
135136
"phpdocumentor/reflection-docblock": "^3.0|^4.0|^5.0",

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
use Symfony\Bundle\FrameworkBundle\Routing\AnnotatedRouteControllerLoader;
2626
use Symfony\Bundle\FrameworkBundle\Routing\RouteLoaderInterface;
2727
use Symfony\Bundle\FullStack;
28+
use Symfony\Bundle\MercureBundle\MercureBundle;
2829
use Symfony\Component\Asset\PackageInterface;
2930
use Symfony\Component\BrowserKit\AbstractBrowser;
3031
use Symfony\Component\Cache\Adapter\AdapterInterface;
@@ -43,6 +44,8 @@
4344
use Symfony\Component\Console\Command\Command;
4445
use Symfony\Component\DependencyInjection\Alias;
4546
use Symfony\Component\DependencyInjection\Argument\ServiceClosureArgument;
47+
use Symfony\Component\DependencyInjection\Argument\ServiceLocatorArgument;
48+
use Symfony\Component\DependencyInjection\Argument\TaggedIteratorArgument;
4649
use Symfony\Component\DependencyInjection\ChildDefinition;
4750
use Symfony\Component\DependencyInjection\Compiler\ServiceLocatorTagPass;
4851
use Symfony\Component\DependencyInjection\ContainerBuilder;
@@ -109,6 +112,7 @@
109112
use Symfony\Component\Notifier\Bridge\Infobip\InfobipTransportFactory;
110113
use Symfony\Component\Notifier\Bridge\LinkedIn\LinkedInTransportFactory;
111114
use Symfony\Component\Notifier\Bridge\Mattermost\MattermostTransportFactory;
115+
use Symfony\Component\Notifier\Bridge\Mercure\MercureTransportFactory;
112116
use Symfony\Component\Notifier\Bridge\Mobyt\MobytTransportFactory;
113117
use Symfony\Component\Notifier\Bridge\Nexmo\NexmoTransportFactory;
114118
use Symfony\Component\Notifier\Bridge\OvhCloud\OvhCloudTransportFactory;
@@ -2236,6 +2240,7 @@ private function registerNotifierConfiguration(array $config, ContainerBuilder $
22362240
SendinblueNotifierTransportFactory::class => 'notifier.transport_factory.sendinblue',
22372241
DiscordTransportFactory::class => 'notifier.transport_factory.discord',
22382242
LinkedInTransportFactory::class => 'notifier.transport_factory.linkedin',
2243+
MercureTransportFactory::class => 'notifier.transport_factory.mercure',
22392244
];
22402245

22412246
foreach ($classToServices as $class => $service) {
@@ -2244,6 +2249,12 @@ private function registerNotifierConfiguration(array $config, ContainerBuilder $
22442249
}
22452250
}
22462251

2252+
// If symfony/mercure-notifier and symfony/mercure-bundle are installed
2253+
if (class_exists(MercureTransportFactory::class) && class_exists(MercureBundle::class)) {
2254+
$container->getDefinition($classToServices[MercureTransportFactory::class])
2255+
->replaceArgument('$publisherLocator', new ServiceLocatorArgument(new TaggedIteratorArgument('mercure.publisher', null, null, true)));
2256+
}
2257+
22472258
if (isset($config['admin_recipients'])) {
22482259
$notifier = $container->getDefinition('notifier');
22492260
foreach ($config['admin_recipients'] as $i => $recipient) {

src/Symfony/Bundle/FrameworkBundle/Resources/config/notifier_transports.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Symfony\Component\Notifier\Bridge\Infobip\InfobipTransportFactory;
2020
use Symfony\Component\Notifier\Bridge\LinkedIn\LinkedInTransportFactory;
2121
use Symfony\Component\Notifier\Bridge\Mattermost\MattermostTransportFactory;
22+
use Symfony\Component\Notifier\Bridge\Mercure\MercureTransportFactory;
2223
use Symfony\Component\Notifier\Bridge\Mobyt\MobytTransportFactory;
2324
use Symfony\Component\Notifier\Bridge\Nexmo\NexmoTransportFactory;
2425
use Symfony\Component\Notifier\Bridge\OvhCloud\OvhCloudTransportFactory;
@@ -115,6 +116,10 @@
115116
->parent('notifier.transport_factory.abstract')
116117
->tag('chatter.transport_factory')
117118

119+
->set('notifier.transport_factory.mercure', MercureTransportFactory::class)
120+
->parent('notifier.transport_factory.abstract')
121+
->tag('chatter.transport_factory')
122+
118123
->set('notifier.transport_factory.null', NullTransportFactory::class)
119124
->parent('notifier.transport_factory.abstract')
120125
->tag('chatter.transport_factory')
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/.gitattributes export-ignore
2+
/.gitignore export-ignore
3+
/phpunit.xml.dist export-ignore
4+
/Tests export-ignore
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
CHANGELOG
2+
=========
3+
4+
5.3.0
5+
-----
6+
7+
* Added the bridge
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Copyright (c) 2020 Fabien Potencier
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy
4+
of this software and associated documentation files (the "Software"), to deal
5+
in the Software without restriction, including without limitation the rights
6+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
copies of the Software, and to permit persons to whom the Software is furnished
8+
to do so, subject to the following conditions:
9+
10+
The above copyright notice and this permission notice shall be included in all
11+
copies or substantial portions of the Software.
12+
13+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
THE SOFTWARE.
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
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+
namespace Symfony\Component\Notifier\Bridge\Mercure;
13+
14+
use Symfony\Component\Notifier\Message\MessageOptionsInterface;
15+
16+
/**
17+
* @author Mathias Arlaud <mathias.arlaud@gmail.com>
18+
*
19+
* @experimental in 5.3
20+
*/
21+
final class MercureOptions implements MessageOptionsInterface
22+
{
23+
private $topic;
24+
25+
public function __construct(string $topic)
26+
{
27+
$this->topic = $topic;
28+
}
29+
30+
public function toArray(): array
31+
{
32+
return [
33+
'topic' => $this->topic,
34+
];
35+
}
36+
37+
public function getRecipientId(): ?string
38+
{
39+
return $this->topic;
40+
}
41+
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
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+
namespace Symfony\Component\Notifier\Bridge\Mercure;
13+
14+
use Psr\Container\ContainerInterface;
15+
use Symfony\Component\Mercure\Update;
16+
use Symfony\Component\Notifier\Exception\InvalidArgumentException;
17+
use Symfony\Component\Notifier\Exception\LogicException;
18+
use Symfony\Component\Notifier\Exception\TransportException;
19+
use Symfony\Component\Notifier\Message\ChatMessage;
20+
use Symfony\Component\Notifier\Message\MessageInterface;
21+
use Symfony\Component\Notifier\Message\SentMessage;
22+
use Symfony\Component\Notifier\Transport\AbstractTransport;
23+
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
24+
use Symfony\Contracts\HttpClient\Exception\HttpExceptionInterface;
25+
use Symfony\Contracts\HttpClient\HttpClientInterface;
26+
27+
/**
28+
* @author Mathias Arlaud <mathias.arlaud@gmail.com>
29+
*
30+
* @internal
31+
*
32+
* @experimental in 5.3
33+
*/
34+
final class MercureTransport extends AbstractTransport
35+
{
36+
private $publishers;
37+
private $publisherId;
38+
private $topic;
39+
40+
public function __construct(ContainerInterface $publisherLocator, string $publisherId, ?string $topic = null, HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null)
41+
{
42+
$this->publishers = $publisherLocator;
43+
$this->publisherId = $publisherId;
44+
$this->topic = $topic ?? 'https://symfony.com/notifier';
45+
46+
parent::__construct($client, $dispatcher);
47+
}
48+
49+
public function __toString(): string
50+
{
51+
return sprintf('mercure://%s?topic=%s', $this->publisherId, urlencode($this->topic));
52+
}
53+
54+
public function supports(MessageInterface $message): bool
55+
{
56+
return $message instanceof ChatMessage && (null === $message->getOptions() || $message->getOptions() instanceof MercureOptions);
57+
}
58+
59+
/**
60+
* @see https://symfony.com/doc/current/mercure.html#publishing
61+
*/
62+
protected function doSend(MessageInterface $message): SentMessage
63+
{
64+
if (!$message instanceof ChatMessage) {
65+
throw new LogicException(sprintf('The "%s" transport only supports instances of "%s" (instance of "%s" given).', __CLASS__, ChatMessage::class, get_debug_type($message)));
66+
}
67+
68+
if (!$this->publishers->has($this->publisherId)) {
69+
throw new LogicException(sprintf('"%s" was not found in publishers.', $this->publisherId));
70+
}
71+
72+
// @see https://www.w3.org/TR/activitystreams-core/#jsonld
73+
$update = new Update(urlencode($message->getRecipientId() ?? $this->topic), json_encode([
74+
'@context' => 'https://www.w3.org/ns/activitystreams',
75+
'type' => 'Announce',
76+
'summary' => $message->getSubject(),
77+
]));
78+
79+
try {
80+
$messageId = ($this->publishers->get($this->publisherId))($update);
81+
} catch (HttpExceptionInterface $e) {
82+
throw new TransportException(sprintf('Unable to post the Mercure message: "%s".', $e->getResponse()->getContent(false)), $e->getResponse(), $e->getCode(), $e->getPrevious());
83+
} catch (\InvalidArgumentException $e) {
84+
throw new InvalidArgumentException(sprintf('Unable to post the Mercure message: "%s".', $e->getMessage()), $e->getCode(), $e->getPrevious());
85+
}
86+
87+
$sentMessage = new SentMessage($message, (string) $this);
88+
$sentMessage->setMessageId($messageId);
89+
90+
return $sentMessage;
91+
}
92+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
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+
namespace Symfony\Component\Notifier\Bridge\Mercure;
13+
14+
use Psr\Container\ContainerInterface;
15+
use Symfony\Component\Notifier\Exception\UnsupportedSchemeException;
16+
use Symfony\Component\Notifier\Transport\AbstractTransportFactory;
17+
use Symfony\Component\Notifier\Transport\Dsn;
18+
use Symfony\Component\Notifier\Transport\TransportInterface;
19+
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
20+
use Symfony\Contracts\HttpClient\HttpClientInterface;
21+
22+
/**
23+
* @author Mathias Arlaud <mathias.arlaud@gmail.com>
24+
*
25+
* @experimental in 5.3
26+
*/
27+
final class MercureTransportFactory extends AbstractTransportFactory
28+
{
29+
private $publisherLocator;
30+
31+
public function __construct(EventDispatcherInterface $dispatcher = null, HttpClientInterface $client = null, ContainerInterface $publisherLocator = null)
32+
{
33+
$this->publisherLocator = $publisherLocator;
34+
35+
parent::__construct($dispatcher, $client);
36+
}
37+
38+
/**
39+
* @return MercureTransport
40+
*/
41+
public function create(Dsn $dsn): TransportInterface
42+
{
43+
if ('mercure' !== $dsn->getScheme()) {
44+
throw new UnsupportedSchemeException($dsn, 'mercure', $this->getSupportedSchemes());
45+
}
46+
47+
$publisherId = $dsn->getHost();
48+
$topic = $dsn->getOption('topic');
49+
50+
return (new MercureTransport($this->publisherLocator, $publisherId, $topic));
51+
}
52+
53+
protected function getSupportedSchemes(): array
54+
{
55+
return ['mercure'];
56+
}
57+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
Mercure Notifier
2+
===================
3+
4+
Provides Mercure integration for Symfony Notifier.
5+
6+
DSN example
7+
-----------
8+
9+
```
10+
// .env file
11+
MERCURE_DSN=mercure://PUBLISHER_ID?topic=TOPIC
12+
```
13+
14+
where:
15+
- `PUBLISHER_ID` is the Mercure publisher service id
16+
- `TOPIC` is the default topic IRI (optional, default: `https://symfony.com/notifier`)
17+
18+
Resources
19+
---------
20+
21+
* [Contributing](https://symfony.com/doc/current/contributing/index.html)
22+
* [Report issues](https://github.com/symfony/symfony/issues) and
23+
[send Pull Requests](https://github.com/symfony/symfony/pulls)
24+
in the [main Symfony repository](https://github.com/symfony/symfony)

0 commit comments

Comments
 (0)
0