Skip to content

Commit 16ab0ea

Browse files
committed
[Notify] Add Notify library
1 parent a02daec commit 16ab0ea

22 files changed

+1081
-0
lines changed

.github/workflows/test.yaml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,15 @@ jobs:
101101
run: php vendor/bin/simple-phpunit
102102
working-directory: src/LazyImage
103103

104+
- name: Notify Dependencies
105+
uses: ramsey/composer-install@v2
106+
with:
107+
working-directory: src/Notify
108+
dependency-versions: lowest
109+
- name: Notify Tests
110+
run: php vendor/bin/simple-phpunit
111+
working-directory: src/Notify
112+
104113
tests-php8-low-deps:
105114
runs-on: ubuntu-latest
106115
steps:
@@ -184,6 +193,14 @@ jobs:
184193
working-directory: src/LiveComponent
185194
run: php vendor/bin/simple-phpunit
186195

196+
- name: Notify Dependencies
197+
uses: ramsey/composer-install@v2
198+
with:
199+
working-directory: src/Notify
200+
- name: Notify Tests
201+
working-directory: src/Notify
202+
run: php vendor/bin/simple-phpunit
203+
187204
tests-js:
188205
runs-on: ubuntu-latest
189206
steps:

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: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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\Config\Definition\Builder\TreeBuilder;
15+
use Symfony\Component\Config\Definition\ConfigurationInterface;
16+
17+
/**
18+
* @author Mathias Arlaud <[email protected]>
19+
*
20+
* @internal
21+
*/
22+
final class Configuration implements ConfigurationInterface
23+
{
24+
public function getConfigTreeBuilder(): TreeBuilder
25+
{
26+
$treeBuilder = new TreeBuilder('notify');
27+
$rootNode = $treeBuilder->getRootNode();
28+
$rootNode
29+
->children()
30+
->scalarNode('mercure_hub')
31+
->info('Mercube hub service id')
32+
->defaultValue('mercure.hub.default')
33+
->end()
34+
->end()
35+
;
36+
37+
return $treeBuilder;
38+
}
39+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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\Reference;
16+
use Symfony\Component\HttpKernel\DependencyInjection\ConfigurableExtension;
17+
use Symfony\UX\Notify\Twig\NotifyExtension as TwigNotifyExtension;
18+
use Symfony\WebpackEncoreBundle\Twig\StimulusTwigExtension;
19+
use Twig\Environment;
20+
21+
/**
22+
* @author Mathias Arlaud <[email protected]>
23+
*
24+
* @internal
25+
*/
26+
final class NotifyExtension extends ConfigurableExtension
27+
{
28+
/**
29+
* {@inheritdoc}
30+
*/
31+
public function loadInternal(array $config, ContainerBuilder $container)
32+
{
33+
if (class_exists(Environment::class) && class_exists(StimulusTwigExtension::class)) {
34+
$container->register('notify.twig_extension', TwigNotifyExtension::class)
35+
->setArguments([
36+
new Reference($config['mercure_hub']),
37+
new Reference('webpack_encore.twig_stimulus_extension'),
38+
])
39+
->addTag('twig.extension')
40+
;
41+
}
42+
}
43+
}

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: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
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+
## Configuration
23+
24+
```yaml
25+
# config/packages/notify.yaml
26+
27+
notify:
28+
# Specifies the Mercure hub to use. Defaults to "mercure.hub.default"
29+
mercure_hub: mercure.hub.custom
30+
```
31+
32+
## Usage
33+
34+
To use Symfony UX Notify you must have a [running Mercure server](https://symfony.com/doc/current/mercure.html#running-a-mercure-hub).
35+
36+
Then, inject the `NotifierInterface` service and send messages on the `chat/mercure` channel.
37+
38+
```php
39+
// ...
40+
use Symfony\Component\Notifier\Notification\Notification;
41+
use Symfony\Component\Notifier\NotifierInterface;
42+
43+
class AnnounceFlashSalesCommand extends Command
44+
{
45+
protected static $defaultName = 'app:flash-sales:announce';
46+
private $notifier;
47+
48+
public function __construct(NotifierInterface $notifier)
49+
{
50+
parent::__construct();
51+
52+
$this->notifier = $notifier;
53+
}
54+
55+
protected function execute(InputInterface $input, OutputInterface $output): int
56+
{
57+
$this->notifier->send(new Notification('Flash sales has been started!', ['chat/mercure']));
58+
59+
return 0;
60+
}
61+
}
62+
```
63+
64+
Finally, native notifications could be displayed using the `notify` Twig function:
65+
66+
```twig
67+
{{ stream_notifications(['/my/topic/1', '/my/topic/2']) }}
68+
{{ stream_notifications() }}
69+
70+
{# Calling notify without parameter will fallback to the following unique topic: 'https://symfony.com/notifier' #}
71+
```
72+
73+
### Extend the default behavior
74+
75+
Symfony UX Notify allows you to extend its default behavior using a custom Stimulus controller:
76+
77+
```js
78+
// mynotify_controller.js
79+
80+
import { Controller } from '@hotwired/stimulus';
81+
82+
export default class extends Controller {
83+
connect() {
84+
this.element.addEventListener('notify:connect', this._onConnect);
85+
}
86+
87+
disconnect() {
88+
// You should always remove listeners when the controller is disconnected to avoid side effects
89+
this.element.removeEventListener('notify:connect', this._onConnect);
90+
}
91+
92+
_onConnect(event) {
93+
// Event sources have just been created
94+
console.log(event.detail.eventSources);
95+
96+
event.detail.eventSources.forEach((eventSource) => {
97+
eventSource.addEventListener('message', (event) => {
98+
console.log(event); // You can add custom behavior on each event source
99+
});
100+
});
101+
}
102+
}
103+
```
104+
105+
Then in your render call, add your controller as an HTML attribute:
106+
107+
```twig
108+
{{ stream_notifications(options = {'data-controller': 'mynotify'}) }}
109+
```
110+
111+
## Backward Compatibility promise
112+
113+
This bundle aims at following the same Backward Compatibility promise as the Symfony framework:
114+
[https://symfony.com/doc/current/contributing/code/bc.html](https://symfony.com/doc/current/contributing/code/bc.html)
115+
116+
However it is currently considered
117+
[**experimental**](https://symfony.com/doc/current/contributing/code/experimental.html),
118+
meaning it is not bound to Symfony's BC policy for the moment.
119+
120+
## Run tests
121+
122+
### PHP tests
123+
124+
```sh
125+
php vendor/bin/simple-phpunit
126+
```
127+
128+
### JavaScript tests
129+
130+
```sh
131+
cd Resources/assets
132+
yarn test
133+
```

0 commit comments

Comments
 (0)