Skip to content

Commit f553489

Browse files
committed
[Notify] Add Notify library
1 parent 4baa1a3 commit f553489

21 files changed

+1068
-0
lines changed

.github/workflows/test.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ jobs:
5050
cd src/LazyImage
5151
composer update --prefer-lowest --prefer-dist --no-interaction --no-ansi --no-progress
5252
php vendor/bin/simple-phpunit
53+
- name: Notify
54+
run: |
55+
cd src/Notify
56+
composer update --prefer-lowest --prefer-dist --no-interaction --no-ansi --no-progress
57+
php vendor/bin/simple-phpunit
5358
5459
tests-php8-low-deps:
5560
runs-on: ubuntu-latest
@@ -108,6 +113,11 @@ jobs:
108113
php ../../.github/build-packages.php
109114
composer update --prefer-dist --no-interaction --no-ansi --no-progress
110115
php vendor/bin/simple-phpunit
116+
- name: Notify
117+
run: |
118+
cd src/Notify
119+
composer update --prefer-dist --no-interaction --no-ansi --no-progress
120+
php vendor/bin/simple-phpunit
111121
112122
tests-js:
113123
runs-on: ubuntu-latest

src/Notify/.gitattributes

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/.gitattributes export-ignore
2+
/.gitignore export-ignore
3+
/phpunit.xml.dist export-ignore
4+
/Resources/assets/test export-ignore
5+
/Tests export-ignore

src/Notify/.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/vendor/
2+
.phpunit.result.cache
3+
.php_cs.cache
4+
composer.lock
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
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\UX\Notify\DependencyInjection;
13+
14+
use Symfony\Component\DependencyInjection\ContainerBuilder;
15+
use Symfony\Component\DependencyInjection\Definition;
16+
use Symfony\Component\DependencyInjection\Reference;
17+
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
18+
use Symfony\UX\Notify\Twig\NotifyExtension as TwigNotifyExtension;
19+
use Twig\Environment;
20+
21+
/**
22+
* @author Mathias Arlaud <[email protected]>
23+
*
24+
* @internal
25+
*/
26+
class NotifyExtension extends Extension
27+
{
28+
public function load(array $configs, ContainerBuilder $container)
29+
{
30+
if (class_exists(Environment::class)) {
31+
$container
32+
->setDefinition('notify.twig_extension', new Definition(TwigNotifyExtension::class))
33+
->addArgument(new Reference('mercure.hub.default'))
34+
->addArgument(new Reference('webpack_encore.twig_stimulus_extension'))
35+
->addTag('twig.extension')
36+
->setPublic(false)
37+
;
38+
}
39+
}
40+
}

src/Notify/LICENSE

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Copyright (c) 2020-2021 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.

src/Notify/NotifyBundle.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
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\UX\Notify;
13+
14+
use Symfony\Component\HttpKernel\Bundle\Bundle;
15+
16+
/**
17+
* @author Mathias Arlaud <[email protected]>
18+
*
19+
* @final
20+
* @experimental
21+
*/
22+
class NotifyBundle extends Bundle
23+
{
24+
}

src/Notify/README.md

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
# Symfony UX Notify
2+
3+
Symfony UX Notify is a Symfony bundle integrating realtime native notifications in Symfony applications using [Mercure](https://mercure.rocks/).
4+
It is part of [the Symfony UX initiative](https://symfony.com/ux).
5+
6+
## Installation
7+
8+
Symfony UX Notify requires PHP 7.2+ and Symfony 5.3+.
9+
10+
Install this bundle using Composer and Symfony Flex:
11+
12+
```sh
13+
composer require symfony/ux-notify
14+
15+
# Don't forget to install the JavaScript dependencies as well and compile
16+
yarn install --force
17+
yarn encore dev
18+
```
19+
20+
Also make sure you have at least version 3.0 of [@symfony/stimulus-bridge](https://github.com/symfony/stimulus-bridge) in your `package.json` file.
21+
22+
## Usage
23+
24+
To use Symfony UX Notify you must have a [running Mercure server](https://symfony.com/doc/current/mercure.html#running-a-mercure-hub).
25+
26+
Then, inject the `NotifierInterface` service and send messages on the `chat/mercure` channel.
27+
28+
```php
29+
// ...
30+
use Symfony\Component\Notifier\Notification\Notification;
31+
use Symfony\Component\Notifier\NotifierInterface;
32+
33+
class AnnounceFlashSalesCommand extends Command
34+
{
35+
protected static $defaultName = 'app:flash-sales:announce';
36+
private $notifier;
37+
38+
public function __construct(NotifierInterface $notifier)
39+
{
40+
parent::__construct();
41+
42+
$this->notifier = $notifier;
43+
}
44+
45+
protected function execute(InputInterface $input, OutputInterface $output): int
46+
{
47+
$this->notifier->send(new Notification('Flash sales has been started!', ['chat/mercure']));
48+
49+
return 0;
50+
}
51+
}
52+
```
53+
54+
Finally, native notifications could be displayed using the `notify` Twig function:
55+
56+
```twig
57+
{{ stream_notifications(['/my/topic/1', '/my/topic/2']) }}
58+
{{ stream_notifications() }}
59+
60+
{# Calling notify without parameter will fallback to the following unique topic: 'https://symfony.com/notifier' #}
61+
```
62+
63+
### Using another Mercure hub
64+
By default, the `stream_notifications` is streaming the default Mercure hub. If you wanna change that behavior, you can use a compiler pass:
65+
66+
``` php
67+
// src/DependencyInjection/UseCustomNotifyMercureHubCompilerPass.php
68+
69+
namespace App\DependencyInjection;
70+
71+
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
72+
use Symfony\Component\DependencyInjection\ContainerBuilder;
73+
use Symfony\Component\DependencyInjection\Reference;
74+
75+
final class UseCustomNotifyMercureHubCompilerPass implements CompilerPassInterface
76+
{
77+
public function process(ContainerBuilder $container): void
78+
{
79+
$container->getDefinition('notify.twig_extension')
80+
->replaceArgument(0, new Reference('mercure.hub.custom'));
81+
}
82+
}
83+
```
84+
85+
``` php
86+
// src/Kernel.php
87+
88+
namespace App;
89+
90+
use App\DependencyInjection\UseCustomNotifyMercureHubCompilerPass;
91+
use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
92+
use Symfony\Component\DependencyInjection\ContainerBuilder;
93+
use Symfony\Component\HttpKernel\Kernel as BaseKernel;
94+
95+
final class Kernel extends BaseKernel
96+
{
97+
use MicroKernelTrait;
98+
99+
protected function build(ContainerBuilder $container): void
100+
{
101+
$container->addCompilerPass(new UseCustomNotifyMercureHubCompilerPass());
102+
}
103+
}
104+
```
105+
106+
### Extend the default behavior
107+
108+
Symfony UX Notify allows you to extend its default behavior using a custom Stimulus controller:
109+
110+
```js
111+
// mynotify_controller.js
112+
113+
import { Controller } from '@hotwired/stimulus';
114+
115+
export default class extends Controller {
116+
connect() {
117+
this.element.addEventListener('notify:connect', this._onConnect);
118+
}
119+
120+
disconnect() {
121+
// You should always remove listeners when the controller is disconnected to avoid side effects
122+
this.element.removeEventListener('notify:connect', this._onConnect);
123+
}
124+
125+
_onConnect(event) {
126+
// Event sources have just been created
127+
console.log(event.detail.eventSources);
128+
129+
event.detail.eventSources.forEach((eventSource) => {
130+
eventSource.addEventListener('message', (event) => {
131+
console.log(event); // You can add custom behavior on each event source
132+
});
133+
});
134+
}
135+
}
136+
```
137+
138+
Then in your render call, add your controller as an HTML attribute:
139+
140+
```twig
141+
{{ stream_notifications(options = {'data-controller': 'mynotify'}) }}
142+
```
143+
144+
## Backward Compatibility promise
145+
146+
This bundle aims at following the same Backward Compatibility promise as the Symfony framework:
147+
[https://symfony.com/doc/current/contributing/code/bc.html](https://symfony.com/doc/current/contributing/code/bc.html)
148+
149+
However it is currently considered
150+
[**experimental**](https://symfony.com/doc/current/contributing/code/experimental.html),
151+
meaning it is not bound to Symfony's BC policy for the moment.
152+
153+
## Run tests
154+
155+
### PHP tests
156+
157+
```sh
158+
php vendor/bin/simple-phpunit
159+
```
160+
161+
### JavaScript tests
162+
163+
```sh
164+
cd Resources/assets
165+
yarn test
166+
```

0 commit comments

Comments
 (0)