Skip to content

[FrameworkBundle][Webhook] Throw when required services are missing when using the Webhook component #50230

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
May 9, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -281,11 +281,11 @@ public function load(array $configs, ContainerBuilder $container)

// If the slugger is used but the String component is not available, we should throw an error
if (!ContainerBuilder::willBeAvailable('symfony/string', SluggerInterface::class, ['symfony/framework-bundle'])) {
$container->register('slugger', 'stdClass')
$container->register('slugger', SluggerInterface::class)
->addError('You cannot use the "slugger" service since the String component is not installed. Try running "composer require symfony/string".');
} else {
if (!ContainerBuilder::willBeAvailable('symfony/translation', LocaleAwareInterface::class, ['symfony/framework-bundle'])) {
$container->register('slugger', 'stdClass')
$container->register('slugger', SluggerInterface::class)
->addError('You cannot use the "slugger" service since the Translation contracts are not installed. Try running "composer require symfony/translation".');
}

Expand Down Expand Up @@ -379,7 +379,7 @@ public function load(array $configs, ContainerBuilder $container)
$container->getDefinition('argument_resolver.request_payload')
->setArguments([])
->addError('You can neither use "#[MapRequestPayload]" nor "#[MapQueryString]" since the Serializer component is not '
.(class_exists(Serializer::class) ? 'enabled. Try setting "framework.serializer" to true.' : 'installed. Try running "composer require symfony/serializer-pack".')
.(class_exists(Serializer::class) ? 'enabled. Try setting "framework.serializer.enabled" to true.' : 'installed. Try running "composer require symfony/serializer-pack".')
)
->addTag('container.error')
->clearTag('kernel.event_subscriber');
Expand Down Expand Up @@ -531,6 +531,24 @@ public function load(array $configs, ContainerBuilder $container)

if ($this->readConfigEnabled('webhook', $container, $config['webhook'])) {
$this->registerWebhookConfiguration($config['webhook'], $container, $loader);

// If Webhook is installed but the HttpClient or Serializer components are not available, we should throw an error
if (!$this->readConfigEnabled('http_client', $container, $config['http_client'])) {
$container->getDefinition('webhook.transport')
->setArguments([])
->addError('You cannot use the "webhook transport" service since the HttpClient component is not '
.(class_exists(ScopingHttpClient::class) ? 'enabled. Try setting "framework.http_client.enabled" to true.' : 'installed. Try running "composer require symfony/http-client".')
)
->addTag('container.error');
}
if (!$this->readConfigEnabled('serializer', $container, $config['serializer'])) {
$container->getDefinition('webhook.body_configurator.json')
->setArguments([])
->addError('You cannot use the "webhook transport" service since the Serializer component is not '
.(class_exists(Serializer::class) ? 'enabled. Try setting "framework.serializer.enabled" to true.' : 'installed. Try running "composer require symfony/serializer-pack".')
)
->addTag('container.error');
}
}

if ($this->readConfigEnabled('remote-event', $container, $config['remote-event'])) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

$container->loadFromExtension('framework', [
'http_method_override' => false,
'webhook' => ['enabled' => true],
'http_client' => ['enabled' => true],
'serializer' => ['enabled' => true],
]);
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

$container->loadFromExtension('framework', [
'http_method_override' => false,
'webhook' => ['enabled' => true],
'http_client' => ['enabled' => true],
'serializer' => ['enabled' => false],
]);
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" ?>

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

<framework:config http-method-override="false">
<framework:webhook enabled="true" />
<framework:http-client enabled="true" />
<framework:serializer enabled="true" />
</framework:config>
</container>
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" ?>

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

<framework:config http-method-override="false">
<framework:webhook enabled="true" />
<framework:http-client enabled="true" />
<framework:serializer enabled="false" />
</framework:config>
</container>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
framework:
http_method_override: false
webhook:
enabled: true
http_client:
enabled: true
serializer:
enabled: true
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
framework:
http_method_override: false
webhook:
enabled: true
http_client:
enabled: true
serializer:
enabled: false
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@
use Symfony\Component\Validator\DependencyInjection\AddConstraintValidatorsPass;
use Symfony\Component\Validator\Validation;
use Symfony\Component\Validator\Validator\ValidatorInterface;
use Symfony\Component\Webhook\Client\RequestParser;
use Symfony\Component\Webhook\Controller\WebhookController;
use Symfony\Component\Workflow;
use Symfony\Component\Workflow\Exception\InvalidDefinitionException;
use Symfony\Component\Workflow\Metadata\InMemoryMetadataStore;
Expand Down Expand Up @@ -2276,6 +2278,38 @@ public function testNotifierWithSpecificMessageBus()
$this->assertEquals(new Reference('app.another_bus'), $container->getDefinition('notifier.channel.sms')->getArgument(1));
}

public function testWebhook()
{
if (!class_exists(WebhookController::class)) {
$this->markTestSkipped('Webhook not available.');
}

$container = $this->createContainerFromFile('webhook');

$this->assertTrue($container->hasAlias(RequestParser::class));
$this->assertSame('webhook.request_parser', (string) $container->getAlias(RequestParser::class));
$this->assertSame(RequestParser::class, $container->getDefinition('webhook.request_parser')->getClass());

$this->assertFalse($container->getDefinition('webhook.transport')->hasErrors());
$this->assertFalse($container->getDefinition('webhook.body_configurator.json')->hasErrors());
}

public function testWebhookWithoutSerializer()
{
if (!class_exists(WebhookController::class)) {
$this->markTestSkipped('Webhook not available.');
}

$container = $this->createContainerFromFile('webhook_without_serializer');

$this->assertFalse($container->getDefinition('webhook.transport')->hasErrors());
$this->assertTrue($container->getDefinition('webhook.body_configurator.json')->hasErrors());
$this->assertSame(
['You cannot use the "webhook transport" service since the Serializer component is not enabled. Try setting "framework.serializer.enabled" to true.'],
$container->getDefinition('webhook.body_configurator.json')->getErrors()
);
}

protected function createContainer(array $data = [])
{
return new ContainerBuilder(new EnvPlaceholderParameterBag(array_merge([
Expand Down