Skip to content

Navigation Menu

Sign in
Appearance settings

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

Commit b5cbf1e

Browse filesBrowse files
committed
Add Web push channel to notifier
1 parent fda67f5 commit b5cbf1e
Copy full SHA for b5cbf1e

File tree

11 files changed

+325
-0
lines changed
Filter options

11 files changed

+325
-0
lines changed

‎src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/UnusedTagsPass.php

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/UnusedTagsPass.php
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ class UnusedTagsPass implements CompilerPassInterface
8989
'validator.auto_mapper',
9090
'validator.constraint_validator',
9191
'validator.initializer',
92+
'web_pusher.transport_factory',
9293
];
9394

9495
public function process(ContainerBuilder $container)

‎src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php
+7Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1793,6 +1793,13 @@ private function addNotifierSection(ArrayNodeDefinition $rootNode)
17931793
->prototype('scalar')->end()
17941794
->end()
17951795
->end()
1796+
->fixXmlConfig('web_pusher_transport')
1797+
->children()
1798+
->arrayNode('web_pusher_transports')
1799+
->useAttributeAsKey('name')
1800+
->prototype('scalar')->end()
1801+
->end()
1802+
->end()
17961803
->children()
17971804
->booleanNode('notification_on_failed_messages')->defaultFalse()->end()
17981805
->end()

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

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php
+6Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2196,6 +2196,11 @@ private function registerNotifierConfiguration(array $config, ContainerBuilder $
21962196
} else {
21972197
$container->removeDefinition('texter');
21982198
}
2199+
if ($config['web_pusher_transports']) {
2200+
$container->getDefinition('web_pusher.transports')->setArgument(0, $config['web_pusher_transports']);
2201+
} else {
2202+
$container->removeDefinition('web_pusher');
2203+
}
21992204

22002205
if ($this->mailerConfigEnabled) {
22012206
$sender = $container->getDefinition('mailer.envelope_listener')->getArgument(0);
@@ -2215,6 +2220,7 @@ private function registerNotifierConfiguration(array $config, ContainerBuilder $
22152220
$container->getDefinition('notifier.channel.email')->setArgument(0, null);
22162221
}
22172222
$container->getDefinition('notifier.channel.sms')->setArgument(0, null);
2223+
$container->getDefinition('notifier.channel.web_push')->setArgument(0, null);
22182224
}
22192225

22202226
$container->getDefinition('notifier.channel_policy')->setArgument(0, $config['channel_policy']);

‎src/Symfony/Bundle/FrameworkBundle/Resources/config/notifier.php

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/Resources/config/notifier.php
+28Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,23 @@
1717
use Symfony\Component\Notifier\Channel\ChatChannel;
1818
use Symfony\Component\Notifier\Channel\EmailChannel;
1919
use Symfony\Component\Notifier\Channel\SmsChannel;
20+
use Symfony\Component\Notifier\Channel\WebPushChannel;
2021
use Symfony\Component\Notifier\Chatter;
2122
use Symfony\Component\Notifier\ChatterInterface;
2223
use Symfony\Component\Notifier\EventListener\NotificationLoggerListener;
2324
use Symfony\Component\Notifier\EventListener\SendFailedMessageToNotifierListener;
2425
use Symfony\Component\Notifier\Message\ChatMessage;
2526
use Symfony\Component\Notifier\Message\SmsMessage;
27+
use Symfony\Component\Notifier\Message\WebPushMessage;
2628
use Symfony\Component\Notifier\Messenger\MessageHandler;
2729
use Symfony\Component\Notifier\Notifier;
2830
use Symfony\Component\Notifier\NotifierInterface;
2931
use Symfony\Component\Notifier\Texter;
3032
use Symfony\Component\Notifier\TexterInterface;
3133
use Symfony\Component\Notifier\Transport;
3234
use Symfony\Component\Notifier\Transport\Transports;
35+
use Symfony\Component\Notifier\WebPusher;
36+
use Symfony\Component\Notifier\WebPusherInterface;
3337

3438
return static function (ContainerConfigurator $container) {
3539
$container->services()
@@ -57,6 +61,10 @@
5761
->args([service('mailer.transports'), service('messenger.default_bus')->ignoreOnInvalid()])
5862
->tag('notifier.channel', ['channel' => 'email'])
5963

64+
->set('notifier.channel.web_push', WebPushChannel::class)
65+
->args([service('web_pusher.transports'), service('messenger.default_bus')->ignoreOnInvalid()])
66+
->tag('notifier.channel', ['channel' => 'web_push'])
67+
6068
->set('notifier.monolog_handler', NotifierHandler::class)
6169
->args([service('notifier')])
6270

@@ -103,6 +111,26 @@
103111
->args([service('texter.transports')])
104112
->tag('messenger.message_handler', ['handles' => SmsMessage::class])
105113

114+
->set('web_pusher', WebPusher::class)
115+
->args([
116+
service('web_pusher.transports'),
117+
service('messenger.default_bus')->ignoreOnInvalid(),
118+
service('event_dispatcher')->ignoreOnInvalid(),
119+
])
120+
121+
->alias(WebPusherInterface::class, 'web_pusher')
122+
123+
->set('web_pusher.transports', Transports::class)
124+
->factory([service('web_pusher.transport_factory'), 'fromStrings'])
125+
->args([[]])
126+
127+
->set('web_pusher.transport_factory', Transport::class)
128+
->args([tagged_iterator('web_pusher.transport_factory')])
129+
130+
->set('web_pusher.messenger.web_push_handler', MessageHandler::class)
131+
->args([service('web_pusher.transports')])
132+
->tag('messenger.message_handler', ['handles' => WebPushMessage::class])
133+
106134
->set('notifier.logger_notification_listener', NotificationLoggerListener::class)
107135
->tag('kernel.event_subscriber')
108136
;

‎src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/ConfigurationTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/ConfigurationTest.php
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -515,6 +515,7 @@ class_exists(SemaphoreStore::class) && SemaphoreStore::isSupported() ? 'semaphor
515515
'enabled' => !class_exists(FullStack::class) && class_exists(Notifier::class),
516516
'chatter_transports' => [],
517517
'texter_transports' => [],
518+
'web_pusher_transports' => [],
518519
'channel_policy' => [],
519520
'admin_recipients' => [],
520521
'notification_on_failed_messages' => false,

‎src/Symfony/Component/Notifier/CHANGELOG.md

Copy file name to clipboardExpand all lines: src/Symfony/Component/Notifier/CHANGELOG.md
+5Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
5.3.0
5+
-----
6+
7+
* Add Web push channel to notifier
8+
49
5.2.0
510
-----
611

+52Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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\Channel;
13+
14+
use Symfony\Component\Notifier\Message\WebPushMessage;
15+
use Symfony\Component\Notifier\Notification\Notification;
16+
use Symfony\Component\Notifier\Notification\WebPushNotificationInterface;
17+
use Symfony\Component\Notifier\Recipient\RecipientInterface;
18+
19+
/**
20+
* @author Tomas Norkūnas <norkunas.tom@gmail.com>
21+
*
22+
* @experimental in 5.3
23+
*/
24+
class WebPushChannel extends AbstractChannel
25+
{
26+
public function notify(Notification $notification, RecipientInterface $recipient, string $transportName = null): void
27+
{
28+
$message = null;
29+
if ($notification instanceof WebPushNotificationInterface) {
30+
$message = $notification->asWebPushMessage($recipient, $transportName);
31+
}
32+
33+
if (null === $message) {
34+
$message = WebPushMessage::fromNotification($notification);
35+
}
36+
37+
if (null !== $transportName) {
38+
$message->transport($transportName);
39+
}
40+
41+
if (null === $this->bus) {
42+
$this->transport->send($message);
43+
} else {
44+
$this->bus->dispatch($message);
45+
}
46+
}
47+
48+
public function supports(Notification $notification, RecipientInterface $recipient): bool
49+
{
50+
return true;
51+
}
52+
}
+110Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
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\Message;
13+
14+
use Symfony\Component\Notifier\Notification\Notification;
15+
16+
/**
17+
* @author Tomas Norkūnas <norkunas.tom@gmail.com>
18+
*
19+
* @experimental in 5.3
20+
*/
21+
final class WebPushMessage implements MessageInterface
22+
{
23+
private $transport;
24+
private $subject;
25+
private $content;
26+
private $options;
27+
private $notification;
28+
29+
public function __construct(string $subject, string $content, MessageOptionsInterface $options = null)
30+
{
31+
$this->subject = $subject;
32+
$this->content = $content;
33+
$this->options = $options;
34+
}
35+
36+
public static function fromNotification(Notification $notification): self
37+
{
38+
$message = new self($notification->getSubject(), $notification->getContent());
39+
$message->notification = $notification;
40+
41+
return $message;
42+
}
43+
44+
/**
45+
* @return $this
46+
*/
47+
public function subject(string $subject): self
48+
{
49+
$this->subject = $subject;
50+
51+
return $this;
52+
}
53+
54+
public function getSubject(): string
55+
{
56+
return $this->subject;
57+
}
58+
59+
public function content(string $content): self
60+
{
61+
$this->content = $content;
62+
63+
return $this;
64+
}
65+
66+
public function getContent(): string
67+
{
68+
return $this->content;
69+
}
70+
71+
public function getRecipientId(): ?string
72+
{
73+
return $this->options ? $this->options->getRecipientId() : null;
74+
}
75+
76+
/**
77+
* @return $this
78+
*/
79+
public function options(MessageOptionsInterface $options): self
80+
{
81+
$this->options = $options;
82+
83+
return $this;
84+
}
85+
86+
public function getOptions(): ?MessageOptionsInterface
87+
{
88+
return $this->options;
89+
}
90+
91+
/**
92+
* @return $this
93+
*/
94+
public function transport(?string $transport): self
95+
{
96+
$this->transport = $transport;
97+
98+
return $this;
99+
}
100+
101+
public function getTransport(): ?string
102+
{
103+
return $this->transport;
104+
}
105+
106+
public function getNotification(): ?Notification
107+
{
108+
return $this->notification;
109+
}
110+
}
+23Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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\Notification;
13+
14+
use Symfony\Component\Notifier\Message\WebPushMessage;
15+
use Symfony\Component\Notifier\Recipient\RecipientInterface;
16+
17+
/**
18+
* @experimental in 5.3
19+
*/
20+
interface WebPushNotificationInterface
21+
{
22+
public function asWebPushMessage(RecipientInterface $recipient, string $transport = null): ?WebPushMessage;
23+
}
+67Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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;
13+
14+
use Symfony\Component\EventDispatcher\Event;
15+
use Symfony\Component\EventDispatcher\LegacyEventDispatcherProxy;
16+
use Symfony\Component\Messenger\MessageBusInterface;
17+
use Symfony\Component\Notifier\Event\MessageEvent;
18+
use Symfony\Component\Notifier\Message\MessageInterface;
19+
use Symfony\Component\Notifier\Message\SentMessage;
20+
use Symfony\Component\Notifier\Transport\TransportInterface;
21+
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
22+
23+
/**
24+
* @author Tomas Norkūnas <norkunas.tom@gmail.com>
25+
*
26+
* @experimental in 5.3
27+
*/
28+
final class WebPusher implements WebPusherInterface
29+
{
30+
private $transport;
31+
private $bus;
32+
private $dispatcher;
33+
34+
public function __construct(TransportInterface $transport, MessageBusInterface $bus = null, EventDispatcherInterface $dispatcher = null)
35+
{
36+
$this->transport = $transport;
37+
$this->bus = $bus;
38+
$this->dispatcher = class_exists(Event::class) ? LegacyEventDispatcherProxy::decorate($dispatcher) : $dispatcher;
39+
}
40+
41+
public function __toString(): string
42+
{
43+
return 'web_pusher';
44+
}
45+
46+
public function supports(MessageInterface $message): bool
47+
{
48+
return $this->transport->supports($message);
49+
}
50+
51+
public function send(MessageInterface $message): ?SentMessage
52+
{
53+
if (null === $this->bus) {
54+
$this->transport->send($message);
55+
56+
return null;
57+
}
58+
59+
if (null !== $this->dispatcher) {
60+
$this->dispatcher->dispatch(new MessageEvent($message, true));
61+
}
62+
63+
$this->bus->dispatch($message);
64+
65+
return null;
66+
}
67+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.