Skip to content

Commit

Permalink
bug #50230 [FrameworkBundle][Webhook] Throw when required services ar…
Browse files Browse the repository at this point in the history
…e missing when using the Webhook component (Jean-Beru)

This PR was squashed before being merged into the 6.3 branch.

Discussion
----------

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

| Q             | A
| ------------- | ---
| Branch?       | 6.3
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | Fix #50211
| License       | MIT
| Doc PR        |

Commits
-------

fd3185e [FrameworkBundle][Webhook] Throw when required services are missing when using the Webhook component
  • Loading branch information
nicolas-grekas committed May 9, 2023
2 parents 6222f8e + fd3185e commit a0b1ddb
Show file tree
Hide file tree
Showing 8 changed files with 115 additions and 3 deletions.
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

0 comments on commit a0b1ddb

Please sign in to comment.