Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1191540

Browse files
committed
[Webhook][RemoteEvent] Add the components
1 parent f3a9a0e commit 1191540

File tree

94 files changed

+2756
-11
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

94 files changed

+2756
-11
lines changed

composer.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@
9494
"symfony/property-info": "self.version",
9595
"symfony/proxy-manager-bridge": "self.version",
9696
"symfony/rate-limiter": "self.version",
97+
"symfony/remote-event": "self.version",
9798
"symfony/routing": "self.version",
9899
"symfony/security-bundle": "self.version",
99100
"symfony/security-core": "self.version",
@@ -113,6 +114,7 @@
113114
"symfony/var-exporter": "self.version",
114115
"symfony/web-link": "self.version",
115116
"symfony/web-profiler-bundle": "self.version",
117+
"symfony/webhook": "self.version",
116118
"symfony/workflow": "self.version",
117119
"symfony/yaml": "self.version"
118120
},

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

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,13 @@
3636
use Symfony\Component\PropertyAccess\PropertyAccessor;
3737
use Symfony\Component\PropertyInfo\PropertyInfoExtractorInterface;
3838
use Symfony\Component\RateLimiter\Policy\TokenBucketLimiter;
39+
use Symfony\Component\RemoteEvent\Event;
3940
use Symfony\Component\Semaphore\Semaphore;
4041
use Symfony\Component\Serializer\Serializer;
4142
use Symfony\Component\Translation\Translator;
4243
use Symfony\Component\Uid\Factory\UuidFactory;
4344
use Symfony\Component\Validator\Validation;
45+
use Symfony\Component\Webhook\Controller\WebhookController;
4446
use Symfony\Component\WebLink\HttpHeaderSerializer;
4547
use Symfony\Component\Workflow\WorkflowEvents;
4648

@@ -181,6 +183,8 @@ public function getConfigTreeBuilder(): TreeBuilder
181183
$this->addRateLimiterSection($rootNode, $enableIfStandalone);
182184
$this->addUidSection($rootNode, $enableIfStandalone);
183185
$this->addHtmlSanitizerSection($rootNode, $enableIfStandalone);
186+
$this->addWebhookSection($rootNode, $enableIfStandalone);
187+
$this->addRemoteEventSection($rootNode, $enableIfStandalone);
184188

185189
return $treeBuilder;
186190
}
@@ -2056,6 +2060,46 @@ private function addNotifierSection(ArrayNodeDefinition $rootNode, callable $ena
20562060
;
20572061
}
20582062

2063+
private function addWebhookSection(ArrayNodeDefinition $rootNode, callable $enableIfStandalone)
2064+
{
2065+
$rootNode
2066+
->children()
2067+
->arrayNode('webhook')
2068+
->info('Webhook configuration')
2069+
->{$enableIfStandalone('symfony/webhook', WebhookController::class)}()
2070+
->children()
2071+
->arrayNode('routing')
2072+
->normalizeKeys(false)
2073+
->useAttributeAsKey('type')
2074+
->prototype('array')
2075+
->children()
2076+
->scalarNode('service')
2077+
->isRequired()
2078+
->cannotBeEmpty()
2079+
->end()
2080+
->scalarNode('secret')
2081+
->defaultValue('')
2082+
->end()
2083+
->end()
2084+
->end()
2085+
->end()
2086+
->end()
2087+
->end()
2088+
;
2089+
}
2090+
2091+
private function addRemoteEventSection(ArrayNodeDefinition $rootNode, callable $enableIfStandalone)
2092+
{
2093+
$rootNode
2094+
->children()
2095+
->arrayNode('remote-event')
2096+
->info('RemoteEvent configuration')
2097+
->{$enableIfStandalone('symfony/remote-event', Event::class)}()
2098+
->end()
2099+
->end()
2100+
;
2101+
}
2102+
20592103
private function addRateLimiterSection(ArrayNodeDefinition $rootNode, callable $enableIfStandalone)
20602104
{
20612105
$rootNode

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

Lines changed: 72 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,11 @@
100100
use Symfony\Component\Mailer\Bridge\Infobip\Transport\InfobipTransportFactory as InfobipMailerTransportFactory;
101101
use Symfony\Component\Mailer\Bridge\Mailchimp\Transport\MandrillTransportFactory;
102102
use Symfony\Component\Mailer\Bridge\Mailgun\Transport\MailgunTransportFactory;
103+
use Symfony\Component\Mailer\Bridge\Mailgun\Webhook\MailgunRequestParser;
103104
use Symfony\Component\Mailer\Bridge\Mailjet\Transport\MailjetTransportFactory;
104105
use Symfony\Component\Mailer\Bridge\OhMySmtp\Transport\OhMySmtpTransportFactory;
105106
use Symfony\Component\Mailer\Bridge\Postmark\Transport\PostmarkTransportFactory;
107+
use Symfony\Component\Mailer\Bridge\Postmark\Webhook\PostmarkRequestParser;
106108
use Symfony\Component\Mailer\Bridge\Sendgrid\Transport\SendgridTransportFactory;
107109
use Symfony\Component\Mailer\Bridge\Sendinblue\Transport\SendinblueTransportFactory;
108110
use Symfony\Component\Mailer\Command\MailerTestCommand;
@@ -205,6 +207,9 @@
205207
use Symfony\Component\RateLimiter\LimiterInterface;
206208
use Symfony\Component\RateLimiter\RateLimiterFactory;
207209
use Symfony\Component\RateLimiter\Storage\CacheStorage;
210+
use Symfony\Component\RemoteEvent\Attribute\AsRemoteEventConsumer;
211+
use Symfony\Component\RemoteEvent\Consumer\ConsumerInterface;
212+
use Symfony\Component\RemoteEvent\Event;
208213
use Symfony\Component\Routing\Loader\Psr4DirectoryLoader;
209214
use Symfony\Component\Security\Core\AuthenticationEvents;
210215
use Symfony\Component\Security\Core\Exception\AuthenticationException;
@@ -239,6 +244,7 @@
239244
use Symfony\Component\Validator\Mapping\Loader\PropertyInfoLoader;
240245
use Symfony\Component\Validator\ObjectInitializerInterface;
241246
use Symfony\Component\Validator\Validation;
247+
use Symfony\Component\Webhook\Controller\WebhookController;
242248
use Symfony\Component\WebLink\HttpHeaderSerializer;
243249
use Symfony\Component\Workflow;
244250
use Symfony\Component\Workflow\WorkflowInterface;
@@ -396,7 +402,7 @@ public function load(array $configs, ContainerBuilder $container)
396402
}
397403

398404
if ($this->readConfigEnabled('mailer', $container, $config['mailer'])) {
399-
$this->registerMailerConfiguration($config['mailer'], $container, $loader);
405+
$this->registerMailerConfiguration($config['mailer'], $container, $loader, $this->readConfigEnabled('webhook', $container, $config['webhook']));
400406

401407
if (!$this->hasConsole() || !class_exists(MailerTestCommand::class)) {
402408
$container->removeDefinition('console.command.mailer_test');
@@ -551,12 +557,20 @@ public function load(array $configs, ContainerBuilder $container)
551557

552558
// notifier depends on messenger, mailer being registered
553559
if ($this->readConfigEnabled('notifier', $container, $config['notifier'])) {
554-
$this->registerNotifierConfiguration($config['notifier'], $container, $loader);
560+
$this->registerNotifierConfiguration($config['notifier'], $container, $loader, $this->readConfigEnabled('webhook', $container, $config['webhook']));
555561
}
556562

557563
// profiler depends on form, validation, translation, messenger, mailer, http-client, notifier, serializer being registered
558564
$this->registerProfilerConfiguration($config['profiler'], $container, $loader);
559565

566+
if ($this->readConfigEnabled('webhook', $container, $config['webhook'])) {
567+
$this->registerWebhookConfiguration($config['webhook'], $container, $loader);
568+
}
569+
570+
if ($this->readConfigEnabled('remote-event', $container, $config['remote-event'])) {
571+
$this->registerRemoteEventConfiguration($config['remote-event'], $container, $loader);
572+
}
573+
560574
if ($this->readConfigEnabled('html_sanitizer', $container, $config['html_sanitizer'])) {
561575
if (!class_exists(HtmlSanitizerConfig::class)) {
562576
throw new LogicException('HtmlSanitizer support cannot be enabled as the HtmlSanitizer component is not installed. Try running "composer require symfony/html-sanitizer".');
@@ -655,6 +669,8 @@ public function load(array $configs, ContainerBuilder $container)
655669
->addTag('messenger.transport_factory');
656670
$container->registerForAutoconfiguration(MimeTypeGuesserInterface::class)
657671
->addTag('mime.mime_type_guesser');
672+
$container->registerForAutoconfiguration(ConsumerInterface::class)
673+
->addTag('remote_event.consumer');
658674
$container->registerForAutoconfiguration(LoggerAwareInterface::class)
659675
->addMethodCall('setLogger', [new Reference('logger')]);
660676

@@ -671,7 +687,9 @@ public function load(array $configs, ContainerBuilder $container)
671687
$container->registerAttributeForAutoconfiguration(AsController::class, static function (ChildDefinition $definition, AsController $attribute): void {
672688
$definition->addTag('controller.service_arguments');
673689
});
674-
690+
$container->registerAttributeForAutoconfiguration(AsRemoteEventConsumer::class, static function (ChildDefinition $definition, AsRemoteEventConsumer $attribute): void {
691+
$definition->addTag('remote_event.consumer', ['consumer' => $attribute->name]);
692+
});
675693
$container->registerAttributeForAutoconfiguration(AsMessageHandler::class, static function (ChildDefinition $definition, AsMessageHandler $attribute, \ReflectionClass|\ReflectionMethod $reflector): void {
676694
$tagAttributes = get_object_vars($attribute);
677695
$tagAttributes['from_transport'] = $tagAttributes['fromTransport'];
@@ -2436,7 +2454,7 @@ private function registerRetryableHttpClient(array $options, string $name, Conta
24362454
->addTag('monolog.logger', ['channel' => 'http_client']);
24372455
}
24382456

2439-
private function registerMailerConfiguration(array $config, ContainerBuilder $container, PhpFileLoader $loader)
2457+
private function registerMailerConfiguration(array $config, ContainerBuilder $container, PhpFileLoader $loader, bool $webhookEnabled)
24402458
{
24412459
if (!class_exists(Mailer::class)) {
24422460
throw new LogicException('Mailer support cannot be enabled as the component is not installed. Try running "composer require symfony/mailer".');
@@ -2479,6 +2497,19 @@ private function registerMailerConfiguration(array $config, ContainerBuilder $co
24792497
}
24802498
}
24812499

2500+
$webhookRequestParsers = [
2501+
MailgunRequestParser::class => 'mailer.webhook_request_parser.mailgun',
2502+
PostmarkRequestParser::class => 'mailer.webhook_request_parser.postmark',
2503+
];
2504+
2505+
foreach ($webhookRequestParsers as $class => $service) {
2506+
$package = substr($service, \strlen('mailer.transport_factory.'));
2507+
2508+
if (!ContainerBuilder::willBeAvailable(sprintf('symfony/%s-mailer', 'gmail' === $package ? 'google' : $package), $class, ['symfony/framework-bundle', 'symfony/mailer'])) {
2509+
$container->removeDefinition($service);
2510+
}
2511+
}
2512+
24822513
$envelopeListener = $container->getDefinition('mailer.envelope_listener');
24832514
$envelopeListener->setArgument(0, $config['envelope']['sender'] ?? null);
24842515
$envelopeListener->setArgument(1, $config['envelope']['recipients'] ?? null);
@@ -2501,9 +2532,13 @@ private function registerMailerConfiguration(array $config, ContainerBuilder $co
25012532
if (!class_exists(MessengerTransportListener::class)) {
25022533
$container->removeDefinition('mailer.messenger_transport_listener');
25032534
}
2535+
2536+
if ($webhookEnabled && class_exists(Event::class)) {
2537+
$loader->load('mailer_webhook.php');
2538+
}
25042539
}
25052540

2506-
private function registerNotifierConfiguration(array $config, ContainerBuilder $container, PhpFileLoader $loader)
2541+
private function registerNotifierConfiguration(array $config, ContainerBuilder $container, PhpFileLoader $loader, bool $webhookEnabled)
25072542
{
25082543
if (!class_exists(Notifier::class)) {
25092544
throw new LogicException('Notifier support cannot be enabled as the component is not installed. Try running "composer require symfony/notifier".');
@@ -2654,6 +2689,38 @@ private function registerNotifierConfiguration(array $config, ContainerBuilder $
26542689
$notifier->addMethodCall('addAdminRecipient', [new Reference($id)]);
26552690
}
26562691
}
2692+
2693+
if ($webhookEnabled && class_exists(Event::class)) {
2694+
$loader->load('notifier_webhook.php');
2695+
}
2696+
}
2697+
2698+
private function registerWebhookConfiguration(array $config, ContainerBuilder $container, PhpFileLoader $loader)
2699+
{
2700+
if (!class_exists(WebhookController::class)) {
2701+
throw new LogicException('Webhook support cannot be enabled as the component is not installed. Try running "composer require symfony/webhook".');
2702+
}
2703+
2704+
$loader->load('webhook.php');
2705+
2706+
$parsers = [];
2707+
foreach ($config['routing'] as $type => $config) {
2708+
// FIXME: xml config in the schema to be fixed
2709+
$parsers[$type] = [
2710+
'parser' => $container->findDefinition($config['service']),
2711+
'secret' => $config['secret']
2712+
];
2713+
}
2714+
$container->getDefinition(WebhookController::class)->replaceArgument(0, $parsers);
2715+
}
2716+
2717+
private function registerRemoteEventConfiguration(array $config, ContainerBuilder $container, PhpFileLoader $loader)
2718+
{
2719+
if (!class_exists(Event::class)) {
2720+
throw new LogicException('RemoteEvent support cannot be enabled as the component is not installed. Try running "composer require symfony/remote-event".');
2721+
}
2722+
2723+
$loader->load('remote_event.php');
26572724
}
26582725

26592726
private function registerRateLimiterConfiguration(array $config, ContainerBuilder $container, PhpFileLoader $loader)
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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\Component\DependencyInjection\Loader\Configurator;
13+
14+
use Symfony\Component\Mailer\Bridge\Mailgun\RemoteEvent\MailgunPayloadConverter;
15+
use Symfony\Component\Mailer\Bridge\Mailgun\RemoteEvent\PostmarkPayloadConverter;
16+
use Symfony\Component\Mailer\Bridge\Mailgun\Webhook\MailgunRequestParser;
17+
use Symfony\Component\Mailer\Bridge\Postmark\Webhook\PostmarkRequestParser;
18+
19+
return static function (ContainerConfigurator $container) {
20+
$container->services()
21+
->set('mailer.payload_converter.mailgun', MailgunPayloadConverter::class)
22+
->set('mailer.webhook_request_parser.mailgun', MailgunRequestParser::class)
23+
->args([service('mailer.payload_converter.mailgun')])
24+
->alias(MailgunRequestParser::class, 'mailer.webhook_request_parser.mailgun')
25+
26+
->set('mailer.payload_converter.postmark', PostmarkPayloadConverter::class)
27+
->set('mailer.webhook_request_parser.postmark', PostmarkRequestParser::class)
28+
->args([service('mailer.payload_converter.postmark')])
29+
->alias(PostmarkRequestParser::class, 'mailer.webhook_request_parser.postmark')
30+
;
31+
};
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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\Component\DependencyInjection\Loader\Configurator;
13+
14+
use Symfony\Component\Notifier\Bridge\Twilio\Webhook\TwilioRequestParser;
15+
16+
return static function (ContainerConfigurator $container) {
17+
$container->services()
18+
->set('notifier.webhook_request_parser.twilio', TwilioRequestParser::class)
19+
->alias(TwilioRequestParser::class, 'notifier.webhook_request_parser.twilio')
20+
;
21+
};
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\Component\DependencyInjection\Loader\Configurator;
13+
14+
use Symfony\Component\RemoteEvent\Messenger\ConsumeRemoteEventHandler;
15+
16+
return static function (ContainerConfigurator $container) {
17+
$container->services()
18+
->set('remote_event.messenger.handler', ConsumeRemoteEventHandler::class)
19+
->args([
20+
tagged_locator('remote_event.consumer'),
21+
])
22+
->tag('messenger.message_handler')
23+
;
24+
};

src/Symfony/Bundle/FrameworkBundle/Resources/config/schema/symfony-1.0.xsd

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@
4141
<xsd:element name="notifier" type="notifier" minOccurs="0" maxOccurs="1" />
4242
<xsd:element name="html-sanitizer" type="html-sanitizer" minOccurs="0" maxOccurs="1" />
4343
<xsd:element name="enabled-locale" type="xsd:string" minOccurs="0" maxOccurs="unbounded" />
44+
<xsd:element name="webhook" type="mailer" minOccurs="0" maxOccurs="1" />
45+
<xsd:element name="remote-event" type="mailer" minOccurs="0" maxOccurs="1" />
4446
</xsd:choice>
4547

4648
<xsd:attribute name="http-method-override" type="xsd:boolean" />
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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\Component\DependencyInjection\Loader\Configurator;
13+
14+
use Symfony\Component\Webhook\Controller\WebhookController;
15+
use Symfony\Component\Webhook\Messenger\SendWebhookHandler;
16+
use Symfony\Component\Webhook\Client\RequestParser;
17+
use Symfony\Component\Webhook\Server\Transport;
18+
use Symfony\Component\Webhook\Server\HeadersGenerator;
19+
use Symfony\Component\Webhook\Server\HeaderSignatureGenerator;
20+
use Symfony\Component\Webhook\Server\JsonBodyGenerator;
21+
22+
return static function (ContainerConfigurator $container) {
23+
$container->services()
24+
->set('webhook.transport', Transport::class)
25+
->args([
26+
service('http_client'),
27+
service('webhook.headers_generator'),
28+
service('webhook.body_generator.json'),
29+
service('webhook.signer'),
30+
])
31+
32+
->set('webhook.headers_generator', HeadersGenerator::class)
33+
34+
->set('webhook.body_generator.json', JsonBodyGenerator::class)
35+
->args([
36+
service('serializer'),
37+
])
38+
39+
->set('webhook.signer', HeaderSignatureGenerator::class)
40+
41+
->set('webhook.messenger.send_handler', SendWebhookHandler::class)
42+
->args([
43+
service('webhook.transport'),
44+
])
45+
->tag('messenger.message_handler')
46+
47+
->set(RequestParser::class, RequestParser::class)
48+
49+
->set(WebhookController::class, WebhookController::class)
50+
->public()
51+
->args([
52+
abstract_arg('user defined parsers'),
53+
])
54+
->tag('controller.service_arguments')
55+
;
56+
};

0 commit comments

Comments
 (0)