8000 [FrameworkBundle] Allow configuring `framework.exceptions` with a con… · symfony/symfony@89fd991 · GitHub
[go: up one dir, main page]

Skip to content

Commit 89fd991

Browse files
committed
[FrameworkBundle] Allow configuring framework.exceptions with a config builder
1 parent 5882b4f commit 89fd991

File tree

4 files changed

+52
-57
lines changed

4 files changed

+52
-57
lines changed

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

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1194,35 +1194,12 @@ private function addExceptionsSection(ArrayNodeDefinition $rootNode)
11941194
$logLevels = (new \ReflectionClass(LogLevel::class))->getConstants();
11951195

11961196
$rootNode
1197+
->fixXmlConfig('exception')
11971198
->children()
11981199
->arrayNode('exceptions')
11991200
->info('Exception handling configuration')
1200-
->beforeNormalization()
1201-
->ifArray()
1202-
->then(function (array $v): array {
1203-
if (!\array_key_exists('exception', $v)) {
1204-
return $v;
1205-
}
1206-
1207-
// Fix XML normalization
1208-
$data = isset($v['exception'][0]) ? $v['exception'] : [$v['exception']];
1209-
$exceptions = [];
1210-
foreach ($data as $exception) {
1211-
$config = [];
1212-
if (\array_key_exists('log-level', $exception)) {
1213-
$config['log_level'] = $exception['log-level'];
1214-
}
1215-
if (\array_key_exists('status-code', $exception)) {
1216-
$config['status_code'] = $exception['status-code'];
1217-
}
1218-
$exceptions[$exception['name']] = $config;
1219-
}
1220-
1221-
return $exceptions;
1222-
})
1223-
->end()
1201+
->useAttributeAsKey('class')
12241202
->prototype('array')
1225-
->fixXmlConfig('exception')
12261203
->children()
12271204
->scalarNode('log_level')
12281205
->info('The level of log message. Null to let Symfony decide.')

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

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +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" />
32+
<xsd:element name="exception" type="exception" minOccurs="0" maxOccurs="unbounded" />
3333
<xsd:element name="lock" type="lock" minOccurs="0" maxOccurs="1" />
3434
<xsd:element name="messenger" type="messenger" minOccurs="0" maxOccurs="1" />
3535
<xsd:element name="http-client" type="http_client" minOccurs="0" maxOccurs="1" />
@@ -359,16 +359,10 @@
359359
<xsd:attribute name="enabled" type="xsd:boolean" />
360360
</xsd:complexType>
361361

362-
<xsd:complexType name="exceptions">
363-
<xsd:sequence>
364-
<xsd:element name="exception" type="exception" minOccurs="0" maxOccurs="unbounded" />
365-
</xsd:sequence>
366-
</xsd:complexType>
367-
368362
<xsd:complexType name="exception">
369-
<xsd:attribute name="name" type="xsd:string" use="required" />
363+
<xsd:attribute name="class" type="xsd:string" use="required" />
370364
<xsd:attribute name="log-level" type="xsd:string" />
371-
<xsd:attribute name="status-code" type="xsd:int" />
365+
<xsd:attribute name="status-code" type="xsd:integer" />
372366
</xsd:complexType>
373367

374368
<xsd:complexType name="marking_store">

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/exceptions.xml

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,25 @@
66
http://symfony.com/schema/dic/symfony https://symfony.com/schema/dic/symfony/symfony-1.0.xsd">
77

88
<framework:config>
9-
<framework:exceptions>
10-
<framework:exception name="Symfony\Component\HttpKernel\Exception\BadRequestHttpException" log-level="info" status-code="422" />
11-
<framework:exception name="Symfony\Component\HttpKernel\Exception\NotFoundHttpException" log-level="info" status-code="0" />
12-
<framework:exception name="Symfony\Component\HttpKernel\Exception\ConflictHttpException" log-level="info" status-code="0" />
13-
<framework:exception name="Symfony\Component\HttpKernel\Exception\ServiceUnavailableHttpException" log-level="null" status-code="500" />
14-
</framework:exceptions>
9+
<framework:exception
10+
class="Symfony\Component\HttpKernel\Exception\BadRequestHttpException"
11+
log-level="info"
12+
status-code="422"
13+
/>
14+
15+
<framework:exception
16+
class="Symfony\Component\HttpKernel\Exception\NotFoundHttpException"
17+
log-level="info"
18+
/>
19+
20+
<framework:exception
21+
class="Symfony\Component\HttpKernel\Exception\ConflictHttpException"
22+
log-level="info"
23+
/>
24+
25+
<framework:exception
26+
class="Symfony\Component\HttpKernel\Exception\ServiceUnavailableHttpException"
27+
status-code="500"
28+
/>
1529
</framework:config>
1630
</container>

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTest.php

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -547,24 +547,34 @@ public function testExceptionsConfig()
547547
{
548548
$container = $this->createContainerFromFile('exceptions');
549549

550+
$configuration = $container->getDefinition('exception_listener')->getArgument(3);
551+
550552
$this->assertSame([
551-
\Symfony\Component\HttpKernel\Exception\BadRequestHttpException::class => [
552-
'log_level' => 'info',
553-
'status_code' => 422,
554-
],
555-
\Symfony\Component\HttpKernel\Exception\NotFoundHttpException::class => [
556-
'log_level' => 'info',
557-
'status_code' => null,
558-
],
559-
\Symfony\Component\HttpKernel\Exception\ConflictHttpException::class => [
560-
'log_level' => 'info',
561-
'status_code' => null,
562-
],
563-
\Symfony\Component\HttpKernel\Exception\ServiceUnavailableHttpException::class => [
564-
'log_level' => null,
565-
'status_code' => 500,
566-
],
567-
], $container->getDefinition('exception_listener')->getArgument(3));
553+
\Symfony\Component\HttpKernel\Exception\BadRequestHttpException::class,
554+
\Symfony\Component\HttpKernel\Exception\NotFoundHttpException::class,
555+
\Symfony\Component\HttpKernel\Exception\ConflictHttpException::class,
556+
\Symfony\Component\HttpKernel\Exception\ServiceUnavailableHttpException::class,
557+
], array_keys($configuration));
558+
559+
$this->assertEqualsCanonicalizing([
560+
'log_level' => 'info',
561+
'status_code' => 422,
562+
], $configuration[\Symfony\Component\HttpKernel\Exception\BadRequestHttpException::class]);
563+
564+
$this->assertEqualsCanonicalizing([
565+
'log_level' => 'info',
566+
'status_code' => null,
567+
], $configuration[\Symfony\Component\HttpKernel\Exception\NotFoundHttpException::class]);
568+
569+
$this->assertEqualsCanonicalizing([
570+
'log_level' => 'info',
571+
'status_code' => null,
572+
], $configuration[\Symfony\Component\HttpKernel\Exception\ConflictHttpException::class]);
573+
574+
$this->assertEqualsCanonicalizing([
575+
'log_level' => null,
576+
'status_code' => 500,
577+
], $configuration[\Symfony\Component\HttpKernel\Exception\ServiceUnavailableHttpException::class]);
568578
}
569579

570580
public function testRouter()

0 commit comments

Comments
 (0)
0