Skip to content

Commit a97e8c7

Browse files
committed
Merge branch '5.4' into 6.0
* 5.4: (30 commits) [HttpKernel] Add support for configuring log level, and status code by exception class Add return types to HttpCache createSurrogate and createStore methods [Serializer] Fix denormalizing of array with empty body Fix framework configuration when messenger uses without console [Security] Remove annoying deprecation in `UsageTrackingTokenStorage` [Mailer] Improve error message when STARTTLS fails [Security] Add alias for FirewallMapInterface to @security.firewall.map Bump Symfony version to 5.3.10 Update VERSION for 5.3.9 Fix CHANGELOG Update CHANGELOG for 5.3.9 Bump Symfony version to 4.4.33 Update VERSION for 4.4.32 Fix CHANGELOG Update CHANGELOG for 4.4.32 [Workflow] Remove dead code in XML schemas [Runtime] Fix test for env var names Bump Symfony version to 5.3.9 Update VERSION for 5.3.8 Update CHANGELOG for 5.3.8 ...
2 parents 8badd64 + 521875e commit a97e8c7

File tree

55 files changed

+346
-41
lines changed

Some content is hidden

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

55 files changed

+346
-41
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ CHANGELOG
3131
* Deprecate the `cache.adapter.doctrine` service
3232
* Add support for resetting container services after each messenger message
3333
* Add `configureContainer()`, `configureRoutes()`, `getConfigDir()` and `getBundlesPath()` to `MicroKernelTrait`
34+
* Add support for configuring log level, and status code by exception class
3435

3536
5.3
3637
---

DependencyInjection/Configuration.php

Lines changed: 64 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Doctrine\Common\Annotations\Annotation;
1515
use Doctrine\DBAL\Connection;
16+
use Psr\Log\LogLevel;
1617
use Symfony\Bundle\FullStack;
1718
use Symfony\Component\Asset\Package;
1819
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
@@ -135,6 +136,7 @@ public function getConfigTreeBuilder(): TreeBuilder
135136
$this->addPropertyInfoSection($rootNode, $enableIfStandalone);
136137
$this->addCacheSection($rootNode, $willBeAvailable);
137138
$this->addPhpErrorsSection($rootNode);
139+
$this->addExceptionsSection($rootNode);
138140
$this->addWebLinkSection($rootNode, $enableIfStandalone);
139141
$this->addLockSection($rootNode, $enableIfStandalone);
140142
$this->addMessengerSection($rootNode, $enableIfStandalone);
@@ -1141,6 +1143,64 @@ private function addPhpErrorsSection(ArrayNodeDefinition $rootNode)
11411143
;
11421144
}
11431145

1146+
private function addExceptionsSection(ArrayNodeDefinition $rootNode)
1147+
{
1148+
$logLevels = (new \ReflectionClass(LogLevel::class))->getConstants();
1149+
1150+
$rootNode
1151+
->children()
1152+
->arrayNode('exceptions')
1153+
->info('Exception handling configuration')
1154+
->beforeNormalization()
1155+
->ifArray()
1156+
->then(function (array $v): array {
1157+
if (!\array_key_exists('exception', $v)) {
1158+
return $v;
1159+
}
1160+
1161+
// Fix XML normalization
1162+
$data = isset($v['exception'][0]) ? $v['exception'] : [$v['exception']];
1163+
$exceptions = [];
1164+
foreach ($data as $exception) {
1165+
$config = [];
1166+
if (\array_key_exists('log-level', $exception)) {
1167+
$config['log_level'] = $exception['log-level'];
1168+
}
1169+
if (\array_key_exists('status-code', $exception)) {
1170+
$config['status_code'] = $exception['status-code'];
1171+
}
1172+
$exceptions[$exception['name']] = $config;
1173+
}
1174+
1175+
return $exceptions;
1176+
})
1177+
->end()
1178+
->prototype('array')
1179+
->fixXmlConfig('exception')
1180+
->children()
1181+
->scalarNode('log_level')
1182+
->info('The level of log message. Null to let Symfony decide.')
1183+
->validate()
1184+
->ifTrue(function ($v) use ($logLevels) { return !\in_array($v, $logLevels); })
1185+
->thenInvalid(sprintf('The log level is not valid. Pick one among "%s".', implode('", "', $logLevels)))
1186+
->end()
1187+
->defaultNull()
1188+
->end()
1189+
->scalarNode('status_code')
1190+
->info('The status code of the response. Null to let Symfony decide.')
1191+
->validate()
1192+
->ifTrue(function ($v) { return !\in_array($v, range(100, 499)); })
1193+
->thenInvalid('The log level is not valid. Pick one among between 100 et 599.')
1194+
->end()
1195+
->defaultNull()
1196+
->end()
1197+
->end()
1198+
->end()
1199+
->end()
1200+
->end()
1201+
;
1202+
}
1203+
11441204
private function addLockSection(ArrayNodeDefinition $rootNode, callable $enableIfStandalone)
11451205
{
11461206
$rootNode
@@ -1311,10 +1371,6 @@ function ($a) {
13111371
->fixXmlConfig('option')
13121372
->children()
13131373
->scalarNode('dsn')->end()
1314-
->booleanNode('reset_on_message')
1315-
->defaultFalse()
1316-
->info('Reset container services after each message. Turn it on when the transport is async and run in a worker.')
1317-
->end()
13181374
->scalarNode('serializer')->defaultNull()->info('Service id of a custom serializer to use.')->end()
13191375
->arrayNode('options')
13201376
->normalizeKeys(false)
@@ -1352,6 +1408,10 @@ function ($a) {
13521408
->defaultNull()
13531409
->info('Transport name to send failed messages to (after all retries have failed).')
13541410
->end()
1411+
->booleanNode('reset_on_message')
1412+
->defaultNull()
1413+
->info('Reset container services after each message.')
1414+
->end()
13551415
->scalarNode('default_bus')->defaultNull()->end()
13561416
->arrayNode('buses')
13571417
->defaultValue(['messenger.bus.default' => ['default_middleware' => true, 'middleware' => []]])

DependencyInjection/FrameworkExtension.php

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ public function load(array $configs, ContainerBuilder $container)
241241

242242
$container->registerAliasForArgument('parameter_bag', PsrContainerInterface::class);
243243

244-
if (class_exists(Application::class)) {
244+
if ($this->hasConsole()) {
245245
$loader->load('console.php');
246246

247247
if (!class_exists(BaseXliffLintCommand::class)) {
@@ -426,6 +426,8 @@ public function load(array $configs, ContainerBuilder $container)
426426
$this->registerPropertyAccessConfiguration($config['property_access'], $container, $loader);
427427
$this->registerSecretsConfiguration($config['secrets'], $container, $loader);
428428

429+
$container->getDefinition('exception_listener')->replaceArgument(3, $config['exceptions']);
430+
429431
if ($this->isConfigEnabled($container, $config['serializer'])) {
430432
if (!class_exists(\Symfony\Component\Serializer\Serializer::class)) {
431433
throw new LogicException('Serializer support cannot be enabled as the Serializer component is not installed. Try running "composer require symfony/serializer-pack".');
@@ -598,6 +600,11 @@ public function getConfiguration(array $config, ContainerBuilder $container): ?C
598600
return new Configuration($container->getParameter('kernel.debug'));
599601
}
600602

603+
protected function hasConsole(): bool
604+
{
605+
return class_exists(Application::class);
606+
}
607+
601608
private function registerFormConfiguration(array $config, ContainerBuilder $container, PhpFileLoader $loader)
602609
{
603610
$loader->load('form.php');
@@ -1917,7 +1924,6 @@ private function registerMessengerConfiguration(array $config, ContainerBuilder
19171924

19181925
$senderAliases = [];
19191926
$transportRetryReferences = [];
1920-
$transportNamesForResetServices = [];
19211927
foreach ($config['transports'] as $name => $transport) {
19221928
$serializerId = $transport['serializer'] ?? 'messenger.default_serializer';
19231929
$transportDefinition = (new Definition(TransportInterface::class))
@@ -1946,18 +1952,6 @@ private function registerMessengerConfiguration(array $config, ContainerBuilder
19461952

19471953
$transportRetryReferences[$name] = new Reference($retryServiceId);
19481954
}
1949-
if ($transport['reset_on_message']) {
1950-
$transportNamesForResetServices[] = $name;
1951-
}
1952-
}
1953-
1954-
if ($transportNamesForResetServices) {
1955-
$container
1956-
->getDefinition('messenger.listener.reset_services')
1957-
->replaceArgument(1, $transportNamesForResetServices)
1958-
;
1959-
} else {
1960-
$container->removeDefinition('messenger.listener.reset_services');
19611955
}
19621956

19631957
$senderReferences = [];
@@ -2029,6 +2023,19 @@ private function registerMessengerConfiguration(array $config, ContainerBuilder
20292023
$container->removeDefinition('console.command.messenger_failed_messages_show');
20302024
$container->removeDefinition('console.command.messenger_failed_messages_remove');
20312025
}
2026+
2027+
if (false === $config['reset_on_message']) {
2028+
throw new LogicException('The "framework.messenger.reset_on_message" configuration option can be set to "true" only. To prevent services resetting after each message you can set the "--no-reset" option in "messenger:consume" command.');
2029+
}
2030+
2031+
if (!$container->hasDefinition('console.command.messenger_consume_messages')) {
2032+
$container->removeDefinition('messenger.listener.reset_services');
2033+
} elseif (null === $config['reset_on_message']) {
2034+
trigger_deprecation('symfony/framework-bundle', '5.4', 'Not setting the "framework.messenger.reset_on_message" configuration option is deprecated, it will default to "true" in version 6.0.');
2035+
2036+
$container->getDefinition('console.command.messenger_consume_messages')->replaceArgument(5, null);
2037+
$container->removeDefinition('messenger.listener.reset_services');
2038+
}
20322039
}
20332040

20342041
private function registerCacheConfiguration(array $config, ContainerBuilder $container)

HttpCache/HttpCache.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,17 @@ protected function getOptions(): array
7979
return [];
8080
}
8181

82+
/**
83+
* @return SurrogateInterface
84+
*/
8285
protected function createSurrogate()
8386
{
8487
return $this->surrogate ?? new Esi();
8588
}
8689

90+
/**
91+
* @return StoreInterface
92+
*/
8793
protected function createStore()
8894
{
8995
return $this->store ?? new Store($this->cacheDir ?: $this->kernel->getCacheDir().'/http_cache');

Resources/config/console.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@
142142
service('event_dispatcher'),
143143
service('logger')->nullOnInvalid(),
144144
[], // Receiver names
145+
service('messenger.listener.reset_services')->nullOnInvalid(),
145146
])
146147
->tag('console.command')
147148
->tag('monolog.logger', ['channel' => 'messenger'])

Resources/config/messenger.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,9 +201,7 @@
201201
->set('messenger.listener.reset_services', ResetServicesListener::class)
202202
->args([
203203
service('services_resetter'),
204-
abstract_arg('receivers names'),
205204
])
206-
->tag('kernel.event_subscriber')
207205

208206
->set('messenger.routable_message_bus', RoutableMessageBus::class)
209207
->args([

Resources/config/schema/symfony-1.0.xsd

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
<xsd:element name="cache" type="cache" minOccurs="0" maxOccurs="1" />
3030
<xsd:element name="workflow" type="workflow" minOccurs="0" maxOccurs="unbounded" />
3131
<xsd:element name="php-errors" type="php-errors" minOccurs="0" maxOccurs="1" />
32+
<xsd:element name="exceptions" type="exceptions" minOccurs="0" maxOccurs="1" />
3233
<xsd:element name="lock" type="lock" minOccurs="0" maxOccurs="1" />
3334
<xsd:element name="messenger" type="messenger" minOccurs="0" maxOccurs="1" />
3435
<xsd:element name="http-client" type="http_client" minOccurs="0" maxOccurs="1" />
@@ -312,6 +313,7 @@
312313

313314
<xsd:complexType name="workflow">
314315
<xsd:sequence>
316+
<xsd:element name="audit-trail" type="audit_trail" minOccurs="0" maxOccurs="1" />
315317
<xsd:element name="initial-marking" type="xsd:string" minOccurs="0" maxOccurs="unbounded" />
316318
<xsd:element name="marking-store" type="marking_store" minOccurs="0" maxOccurs="1" />
317319
<xsd:element name="support" type="xsd:string" minOccurs="0" maxOccurs="unbounded" />
@@ -341,6 +343,22 @@
341343
<xsd:attribute name="logLevel" type="xsd:string" />
342344
</xsd:complexType>
343345

346+
<xsd:complexType name="audit_trail">
347+
<xsd:attribute name="enabled" type="xsd:boolean" />
348+
</xsd:complexType>
349+
350+
<xsd:complexType name="exceptions">
351+
<xsd:sequence>
352+
<xsd:element name="exception" type="exception" minOccurs="0" maxOccurs="unbounded" />
353+
</xsd:sequence>
354+
</xsd:complexType>
355+
356+
<xsd:complexType name="exception">
357+
<xsd:attribute name="name" type="xsd:string" use="required" />
358+
<xsd:attribute name="log-level" type="xsd:string" />
359+
<xsd:attribute name="status-code" type="xsd:int" />
360+
</xsd:complexType>
361+
344362
<xsd:complexType name="marking_store">
345363
<xsd:sequence>
346364
<xsd:element name="argument" type="xsd:string" minOccurs="0" maxOccurs="unbounded" />
@@ -469,6 +487,7 @@
469487
<xsd:attribute name="default-bus" type="xsd:string" />
470488
<xsd:attribute name="enabled" type="xsd:boolean" />
471489
<xsd:attribute name="failure-transport" type="xsd:string" />
490+
<xsd:attribute name="reset-on-message" type="xsd:boolean" />
472491
</xsd:complexType>
473492

474493
<xsd:complexType name="messenger_serializer">
@@ -505,7 +524,6 @@
505524
<xsd:attribute name="serializer" type="xsd:string" />
506525
<xsd:attribute name="dsn" type="xsd:string" />
507526
<xsd:attribute name="failure-transport" type="xsd:string" />
508-
<xsd:attribute name="reset-on-message" type="xsd:boolean" />
509527
</xsd:complexType>
510528

511529
<xsd:complexType name="messenger_retry_strategy">

Resources/config/web.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@
102102
param('kernel.error_controller'),
103103
service('logger')->nullOnInvalid(),
104104
param('kernel.debug'),
105+
abstract_arg('an exceptions to log & status code mapping'),
105106
])
106107
->tag('kernel.event_subscriber')
107108
->tag('monolog.logger', ['channel' => 'request'])

Tests/DependencyInjection/ConfigurationTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -532,6 +532,7 @@ class_exists(SemaphoreStore::class) && SemaphoreStore::isSupported() ? 'semaphor
532532
],
533533
'default_bus' => null,
534534
'buses' => ['messenger.bus.default' => ['default_middleware' => true, 'middleware' => []]],
535+
'reset_on_message' => null,
535536
],
536537
'disallow_search_engine_index' => true,
537538
'http_client' => [
@@ -575,6 +576,7 @@ class_exists(SemaphoreStore::class) && SemaphoreStore::isSupported() ? 'semaphor
575576
'name_based_uuid_version' => 5,
576577
'time_based_uuid_version' => 6,
577578
],
579+
'exceptions' => [],
578580
];
579581
}
580582
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
4+
5+
$container->loadFromExtension('framework', [
6+
'exceptions' => [
7+
BadRequestHttpException::class => [
8+
'log_level' => 'info',
9+
'status_code' => 422,
10+
],
11+
],
12+
]);

0 commit comments

Comments
 (0)