Skip to content

Notifier Component #33687

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
"symfony/messenger": "self.version",
"symfony/mime": "self.version",
"symfony/monolog-bridge": "self.version",
"symfony/notifier": "self.version",
"symfony/options-resolver": "self.version",
"symfony/postmark-mailer": "self.version",
"symfony/process": "self.version",
Expand Down
79 changes: 79 additions & 0 deletions src/Symfony/Bridge/Monolog/Handler/NotifierHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bridge\Monolog\Handler;

use Monolog\Handler\AbstractHandler;
use Monolog\Logger;
use Symfony\Component\Notifier\Notification\Notification;
use Symfony\Component\Notifier\Notifier;
use Symfony\Component\Notifier\NotifierInterface;

/**
* Uses Notifier as a log handler.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class NotifierHandler extends AbstractHandler
{
private $notifier;

public function __construct(NotifierInterface $notifier, int $level = Logger::ERROR, bool $bubble = true)
{
$this->notifier = $notifier;

parent::__construct($level < Logger::ERROR ? Logger::ERROR : $level, $bubble);
}

public function handle(array $record): bool
{
if (!$this->isHandling($record)) {
return false;
}

$this->notify([$record]);

return !$this->bubble;
}

public function handleBatch(array $records): void
{
if ($records = array_filter($records, [$this, 'isHandling'])) {
$this->notify($records);
}
}

private function notify(array $records): void
{
$record = $this->getHighestRecord($records);
if (($record['context']['exception'] ?? null) instanceof \Throwable) {
$notification = Notification::fromThrowable($record['context']['exception']);
} else {
$notification = new Notification($record['message']);
}

$notification->importanceFromLogLevelName(Logger::getLevelName($record['level']));

$this->notifier->send($notification, ...$this->notifier->getAdminRecipients());
}

private function getHighestRecord(array $records)
{
$highestRecord = null;
foreach ($records as $record) {
if (null === $highestRecord || $highestRecord['level'] < $record['level']) {
$highestRecord = $record;
}
}

return $highestRecord;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
use Symfony\Component\Lock\Store\SemaphoreStore;
use Symfony\Component\Mailer\Mailer;
use Symfony\Component\Messenger\MessageBusInterface;
use Symfony\Component\Notifier\Notifier;
use Symfony\Component\PropertyInfo\PropertyInfoExtractorInterface;
use Symfony\Component\Serializer\Serializer;
use Symfony\Component\Translation\Translator;
Expand Down Expand Up @@ -114,6 +115,7 @@ public function getConfigTreeBuilder()
$this->addRobotsIndexSection($rootNode);
$this->addHttpClientSection($rootNode);
$this->addMailerSection($rootNode);
$this->addNotifierSection($rootNode);

return $treeBuilder;
}
Expand Down Expand Up @@ -1475,4 +1477,50 @@ private function addMailerSection(ArrayNodeDefinition $rootNode)
->end()
;
}

private function addNotifierSection(ArrayNodeDefinition $rootNode)
{
$rootNode
->children()
->arrayNode('notifier')
->info('Notifier configuration')
->{!class_exists(FullStack::class) && class_exists(Notifier::class) ? 'canBeDisabled' : 'canBeEnabled'}()
->fixXmlConfig('chatter_transport')
->children()
->arrayNode('chatter_transports')
->useAttributeAsKey('name')
->prototype('scalar')->end()
->end()
->end()
->fixXmlConfig('texter_transport')
->children()
->arrayNode('texter_transports')
->useAttributeAsKey('name')
->prototype('scalar')->end()
->end()
->end()
->children()
->arrayNode('channel_policy')
->useAttributeAsKey('name')
->prototype('array')
->beforeNormalization()->ifString()->then(function (string $v) { return [$v]; })->end()
->prototype('scalar')->end()
->end()
->end()
->end()
->fixXmlConfig('admin_recipient')
->children()
->arrayNode('admin_recipients')
->prototype('array')
->children()
->scalarNode('email')->cannotBeEmpty()->end()
->scalarNode('phone')->defaultNull()->end()
->end()
->end()
->end()
->end()
->end()
->end()
;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,12 @@
use Symfony\Component\Messenger\Transport\TransportInterface;
use Symfony\Component\Mime\MimeTypeGuesserInterface;
use Symfony\Component\Mime\MimeTypes;
use Symfony\Component\Notifier\Bridge\Nexmo\NexmoTransportFactory;
use Symfony\Component\Notifier\Bridge\Slack\SlackTransportFactory;
use Symfony\Component\Notifier\Bridge\Telegram\TelegramTransportFactory;
use Symfony\Component\Notifier\Bridge\Twilio\TwilioTransportFactory;
use Symfony\Component\Notifier\Notifier;
use Symfony\Component\Notifier\Recipient\AdminRecipient;
use Symfony\Component\PropertyAccess\PropertyAccessor;
use Symfony\Component\PropertyInfo\PropertyAccessExtractorInterface;
use Symfony\Component\PropertyInfo\PropertyDescriptionExtractorInterface;
Expand Down Expand Up @@ -297,6 +303,10 @@ public function load(array $configs, ContainerBuilder $container)
$this->registerMailerConfiguration($config['mailer'], $container, $loader);
}

if ($this->isConfigEnabled($container, $config['notifier'])) {
$this->registerNotifierConfiguration($config['notifier'], $container, $loader);
}

$propertyInfoEnabled = $this->isConfigEnabled($container, $config['property_info']);
$this->registerValidationConfiguration($config['validation'], $container, $loader, $propertyInfoEnabled);
$this->registerEsiConfiguration($config['esi'], $container, $loader);
Expand Down Expand Up @@ -1848,6 +1858,67 @@ private function registerMailerConfiguration(array $config, ContainerBuilder $co
$envelopeListener->setArgument(1, $recipients);
}

private function registerNotifierConfiguration(array $config, ContainerBuilder $container, XmlFileLoader $loader)
{
if (!class_exists(Notifier::class)) {
throw new LogicException('Notifier support cannot be enabled as the component is not installed. Try running "composer require symfony/notifier".');
}

$loader->load('notifier.xml');
$loader->load('notifier_transports.xml');

if ($config['chatter_transports']) {
$container->getDefinition('chatter.transports')->setArgument(0, $config['chatter_transports']);
} else {
$container->removeDefinition('chatter');
}
if ($config['texter_transports']) {
$container->getDefinition('texter.transports')->setArgument(0, $config['texter_transports']);
} else {
$container->removeDefinition('texter');
}

if ($this->mailerConfigEnabled) {
$sender = $container->getDefinition('mailer.envelope_listener')->getArgument(0);
$container->getDefinition('notifier.channel.email')->setArgument(2, $sender);
} else {
$container->removeDefinition('notifier.channel.email');
}

if (!$this->messengerConfigEnabled) {
$container->removeDefinition('notifier.failed_message_listener');
} else {
// as we have a bus, the channels don't need the transports
$container->getDefinition('notifier.channel.chat')->setArgument(0, null);
$container->getDefinition('notifier.channel.email')->setArgument(0, null);
$container->getDefinition('notifier.channel.sms')->setArgument(0, null);
}

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

$classToServices = [
SlackTransportFactory::class => 'notifier.transport_factory.slack',
TelegramTransportFactory::class => 'notifier.transport_factory.telegram',
NexmoTransportFactory::class => 'notifier.transport_factory.nexmo',
TwilioTransportFactory::class => 'notifier.transport_factory.twilio',
];

foreach ($classToServices as $class => $service) {
if (!class_exists($class)) {
$container->removeDefinition($service);
}
}

if (isset($config['admin_recipients'])) {
$notifier = $container->getDefinition('notifier');
foreach ($config['admin_recipients'] as $i => $recipient) {
$id = 'notifier.admin_recipient.'.$i;
$container->setDefinition($id, new Definition(AdminRecipient::class, [$recipient['email'], $recipient['phone']]));
$notifier->addMethodCall('addAdminRecipient', [new Reference($id)]);
}
}
}

/**
* {@inheritdoc}
*/
Expand Down
94 changes: 94 additions & 0 deletions src/Symfony/Bundle/FrameworkBundle/Resources/config/notifier.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?xml version="1.0" encoding="UTF-8" ?>

<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">

<services>
<service id="notifier" class="Symfony\Component\Notifier\Notifier">
<argument type="tagged_locator" tag="notifier.channel" index-by="channel" />
<argument type="service" id="notifier.channel_policy" on-invalid="ignore" />
</service>
<service id="Symfony\Component\Notifier\NotifierInterface" alias="notifier" />

<service id="notifier.channel_policy" class="Symfony\Component\Notifier\Channel\ChannelPolicy">
<argument type="collection" /> <!-- policy -->
</service>

<service id="notifier.channel.browser" class="Symfony\Component\Notifier\Channel\BrowserChannel">
<argument type="service" id="request_stack" />
<tag name="notifier.channel" channel="browser" />
</service>

<service id="notifier.channel.chat" class="Symfony\Component\Notifier\Channel\ChatChannel">
<argument type="service" id="chatter.transports" />
<argument type="service" id="messenger.default_bus" on-invalid="ignore" />
<tag name="notifier.channel" channel="chat" />
</service>

<service id="notifier.channel.sms" class="Symfony\Component\Notifier\Channel\SmsChannel">
<argument type="service" id="texter.transports" />
<argument type="service" id="messenger.default_bus" on-invalid="ignore" />
<tag name="notifier.channel" channel="sms" />
</service>

<service id="notifier.channel.email" class="Symfony\Component\Notifier\Channel\EmailChannel">
<argument type="service" id="mailer.transports" />
<argument type="service" id="messenger.default_bus" on-invalid="ignore" />
<tag name="notifier.channel" channel="email" />
</service>

<service id="notifier.monolog_handler" class="Symfony\Bridge\Monolog\Handler\NotifierHandler">
<argument type="service" id="notifier" />
</service>

<service id="notifier.failed_message_listener" class="Symfony\Component\Notifier\EventListener\SendFailedMessageToNotifierListener">
<argument type="service" id="notifier" />
<tag name="kernel.event_subscriber" />
</service>

<!-- chatter -->
<service id="chatter" class="Symfony\Component\Notifier\Chatter">
<argument type="service" id="chatter.transports" />
<argument type="service" id="messenger.default_bus" on-invalid="ignore" />
<argument type="service" id="event_dispatcher" on-invalid="ignore" />
</service>
<service id="Symfony\Component\Notifier\ChatterInterface" alias="chatter" />

<service id="chatter.transports" class="Symfony\Component\Notifier\Transport\Transports">
<factory service="chatter.transport_factory" method="fromStrings" />
<argument type="collection" /> <!-- transports -->
</service>

<service id="chatter.transport_factory" class="Symfony\Component\Notifier\Transport">
<argument type="tagged_iterator" tag="chatter.transport_factory" />
</service>

<service id="chatter.messenger.chat_handler" class="Symfony\Component\Notifier\Messenger\MessageHandler">
<argument type="service" id="chatter.transports" />
<tag name="messenger.message_handler" handles="Symfony\Component\Notifier\Message\ChatMessage" />
</service>

<!-- texter -->
<service id="texter" class="Symfony\Component\Notifier\Texter">
<argument type="service" id="texter.transports" />
<argument type="service" id="messenger.default_bus" on-invalid="ignore" />
<argument type="service" id="event_dispatcher" on-invalid="ignore" />
</service>
<service id="Symfony\Component\Notifier\TexterInterface" alias="texter" />

<service id="texter.transports" class="Symfony\Component\Notifier\Transport\Transports">
<factory service="texter.transport_factory" method="fromStrings" />
<argument type="collection" /> <!-- transports -->
</service>

<service id="texter.transport_factory" class="Symfony\Component\Notifier\Transport">
<argument type="tagged_iterator" tag="texter.transport_factory" />
</service>

<service id="texter.messenger.sms_handler" class="Symfony\Component\Notifier\Messenger\MessageHandler">
<argument type="service" id="texter.transports" />
<tag name="messenger.message_handler" handles="Symfony\Component\Notifier\Message\SmsMessage" />
</service>
</services>
</container>
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?xml version="1.0" encoding="UTF-8" ?>

<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">

<services>
<service id="notifier.transport_factory.abstract" class="Symfony\Component\Notifier\Transport\AbstractTransportFactory" abstract="true">
<argument type="service" id="event_dispatcher" />
<argument type="service" id="http_client" on-invalid="ignore" />
</service>

<service id="notifier.transport_factory.slack" class="Symfony\Component\Notifier\Bridge\Slack\SlackTransportFactory" parent="notifier.transport_factory.abstract">
<tag name="chatter.transport_factory" />
</service>

<service id="notifier.transport_factory.telegram" class="Symfony\Component\Notifier\Bridge\Telegram\TelegramTransportFactory" parent="notifier.transport_factory.abstract">
<tag name="chatter.transport_factory" />
</service>

<service id="notifier.transport_factory.nexmo" class="Symfony\Component\Notifier\Bridge\Nexmo\NexmoTransportFactory" parent="notifier.transport_factory.abstract">
<tag name="texter.transport_factory" />
</service>

<service id="notifier.transport_factory.twilio" class="Symfony\Component\Notifier\Bridge\Twilio\TwilioTransportFactory" parent="notifier.transport_factory.abstract">
<tag name="texter.transport_factory" />
</service>

<service id="notifier.transport_factory.null" class="Symfony\Component\Notifier\Transport\NullTransportFactory" parent="notifier.transport_factory.abstract">
<tag name="notifier.transport_factory" />
</service>
</services>
</container>
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use Symfony\Component\Lock\Store\SemaphoreStore;
use Symfony\Component\Mailer\Mailer;
use Symfony\Component\Messenger\MessageBusInterface;
use Symfony\Component\Notifier\Notifier;

class ConfigurationTest extends TestCase
{
Expand Down Expand Up @@ -411,6 +412,13 @@ class_exists(SemaphoreStore::class) && SemaphoreStore::isSupported() ? 'semaphor
'transports' => [],
'enabled' => !class_exists(FullStack::class) && class_exists(Mailer::class),
],
'notifier' => [
'enabled' => !class_exists(FullStack::class) && class_exists(Notifier::class),
'chatter_transports' => [],
'texter_transports' => [],
'channel_policy' => [],
'admin_recipients' => [],
],
'error_controller' => 'error_controller',
];
}
Expand Down
Loading