Symfony Webhook Receiver
Registers Symfony Webhook component's webhook receiver features in Drupal.
Before you start
Read Symfony's official Webhook component documentation and
read the additions to that in this pull request if it is still open to understand the fundamental concepts.
Define your own webhook type
To define a custom webhook type, register the required services in your [MY_MODULE].services.yml file:
services:
_defaults:
autowire: true
public: false
my_module.webhook.request_parser.my_type:
parent: symfony_webhook_receiver.request_parser # Or use your own \Symfony\Component\Webhook\Client\RequestParserInterface implementation.
tags:
- { name: webhook.request_parser, type: my_type }
my_module.webhook.remote_event.consumer.my_type:
class: Drupal\my_module\MyTypeConsumer
# Remote Event Consumers have to be public otherwise service locator
# does not find them.
public: true
tags:
- { name: remote_event.consumer, type: my_type }
In this example:
my_typerepresents your custom webhook type.- Replace
my_modulewith your module name. - The
MyTypeConsumerclass must implement the\Symfony\Component\RemoteEvent\Consumer\ConsumerInterface.
Remote Event Consumer auto-discovery
A Remote Event Consumer class residing in the src/RemoteEventConsumer
directory of a module is auto-discoverable. It has to have a
#[AsRemoteEventConsumer] attribute on the class and it must implement the
\Symfony\Component\RemoteEvent\Consumer\ConsumerInterface interface.
See ExampleAutoDiscoverConsumer as example.
The rest of the necessary configuration with defining a parser and secret for
the webhook type remains the same.
Configure secret for webhook type
Secrets are essential for securing webhook communications. To configure the secret for your custom webhook type, follow these steps:
-
Ensure your service configuration is included:
In
web/sites/default/settings.local.php, make sure to includeservices.local.yml(or a dedicated service file) in the container YAML files:$settings['container_yamls'][] = __DIR__ . '/services.local.yml'; -
Define the secret for your webhook type:
In the
services.local.ymlfile, define the secret for your webhook type as a parameter:parameters: symfony_webhook_receiver.request_parser.my_type.secret: 'my_super_secret'Make sure to replace
'my_super_secret'with your actual secret.
Extending functionality
You can enhance and extend the behavior of your webhook receivers in the following ways:
-
Fetch secrets from environment variables:
If you'd prefer to retrieve secrets from environment variables, you will need to create a custom
ServiceProviderand implement aCompilerPass. This will allow you to capture the value from an environment variable and set it as the corresponding container parameter.Since Drupal currently lacks native support for environment variables as container parameters, this approach can be particularly useful.
-
Create a custom WebhookController:
Another option is to build a custom
WebhookControllerthat fetches secrets from an alternative location, such as a secure storage service. Even with a custom controller, you can still leverage the reusable components provided by the Symfony Webhook package.
Depends on
Dependencies of the latest stable release
No dependencies recorded for this project.
Required by
Tracked projects that depend on this one
No tracked projects depend on this one yet.