Skip to content

Commit 199f081

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

21 files changed

+1067
-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: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
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+
65+
By default, the `stream_notifications` is streaming the default Mercure hub. If you wanna change that behavior, you can use a compiler pass:
66+
67+
```php
68+
// src/DependencyInjection/UseCustomNotifyMercureHubCompilerPass.php
69+
70+
namespace App\DependencyInjection;
71+
72+
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
73+
use Symfony\Component\DependencyInjection\ContainerBuilder;
74+
use Symfony\Component\DependencyInjection\Reference;
75+
76+
final class UseCustomNotifyMercureHubCompilerPass implements CompilerPassInterface
77+
{
78+
public function process(ContainerBuilder $container): void
79+
{
80+
$container->getDefinition('notify.twig_extension')
81+
->replaceArgument(0, new Reference('mercure.hub.custom'));
82+
}
83+
}
84+
```
85+
86+
```php
87+
// src/Kernel.php
88+
89+
namespace App;
90+
91+
use App\DependencyInjection\UseCustomNotifyMercureHubCompilerPass;
92+
use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
93+
use Symfony\Component\DependencyInjection\ContainerBuilder;
94+
use Symfony\Component\HttpKernel\Kernel as BaseKernel;
95+
96+
final class Kernel extends BaseKernel
97+
{
98+
use MicroKernelTrait;
99+
100+
protected function build(ContainerBuilder $container): void
101+
{
102+
$container->addCompilerPass(new UseCustomNotifyMercureHubCompilerPass());
103+
}
104+
}
105+
```
106+
107+
### Extend the default behavior
108+
109+
Symfony UX Notify allows you to extend its default behavior using a custom Stimulus controller:
110+
111+
```js
112+
// mynotify_controller.js
113+
114+
import { Controller } from '@hotwired/stimulus';
115+
116+
export default class extends Controller {
117+
connect() {
118+
this.element.addEventListener('notify:connect', this._onConnect);
119+
}
120+
121+
disconnect() {
122+
// You should always remove listeners when the controller is disconnected to avoid side effects
123+
this.element.removeEventListener('notify:connect', this._onConnect);
124+
}
125+
126+
_onConnect(event) {
127+
// Event sources have just been created
128+
console.log(event.detail.eventSources);
129+
130+
event.detail.eventSources.forEach((eventSource) => {
131+
eventSource.addEventListener('message', (event) => {
132+
console.log(event); // You can add custom behavior on each event source
133+
});
134+
});
135+
}
136+
}
137+
```
138+
139+
Then in your render call, add your controller as an HTML attribute:
140+
141+
```twig
142+
{{ stream_notifications(options = {'data-controller': 'mynotify'}) }}
143+
```
144+
145+
## Backward Compatibility promise
146+
147+
This bundle aims at following the same Backward Compatibility promise as the Symfony framework:
148+
[https://symfony.com/doc/current/contributing/code/bc.html](https://symfony.com/doc/current/contributing/code/bc.html)
149+
150+
However it is currently considered
151+
[**experimental**](https://symfony.com/doc/current/contributing/code/experimental.html),
152+
meaning it is not bound to Symfony's BC policy for the moment.
153+
154+
## Run tests
155+
156+
### PHP tests
157+
158+
```sh
159+
php vendor/bin/simple-phpunit
160+
```
161+
162+
### JavaScript tests
163+
164+
```sh
165+
cd Resources/assets
166+
yarn test
167+
```

0 commit comments

Comments
 (0)