Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 57e28f6

Browse files
MatTheCatnicolas-grekas
authored andcommitted
[FrameworkBundle] Allow configuring framework.exceptions with a config builder
1 parent 3b75e0a commit 57e28f6

File tree

6 files changed

+127
-41
lines changed

6 files changed

+127
-41
lines changed

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

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1194,35 +1194,31 @@ 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')
1201+
->useAttributeAsKey('class')
12001202
->beforeNormalization()
1203+
// Handle legacy XML configuration
12011204
->ifArray()
12021205
->then(function (array $v): array {
12031206
if (!\array_key_exists('exception', $v)) {
12041207
return $v;
12051208
}
12061209

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;
1210+
$v = $v['exception'];
1211+
unset($v['exception']);
1212+
1213+
foreach ($v as &$exception) {
1214+
$exception['class'] = $exception['name'];
1215+
unset($exception['name']);
12191216
}
12201217

1221-
return $exceptions;
1218+
return $v;
12221219
})
12231220
->end()
12241221
->prototype('array')
1225-
->fixXmlConfig('exception')
12261222
->children()
12271223
->scalarNode('log_level')
12281224
->info('The level of log message. Null to let Symfony decide.')

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

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
<xsd:element name="workflow" type="workflow" minOccurs="0" maxOccurs="unbounded" />
3131
<xsd:element name="php-errors" type="php-errors" minOccurs="0" maxOccurs="1" />
3232
<xsd:element name="exceptions" type="exceptions" minOccurs="0" maxOccurs="1" />
33+
<xsd:element name="exception" type="new-exception" minOccurs="0" maxOccurs="unbounded" />
3334
<xsd:element name="lock" type="lock" minOccurs="0" maxOccurs="1" />
3435
<xsd:element name="messenger" type="messenger" minOccurs="0" maxOccurs="1" />
3536
<xsd:element name="http-client" type="http_client" minOccurs="0" maxOccurs="1" />
@@ -361,14 +362,29 @@
361362

362363
<xsd:complexType name="exceptions">
363364
<xsd:sequence>
364-
<xsd:element name="exception" type="exception" minOccurs="0" maxOccurs="unbounded" />
365+
<xsd:element name="exception" type="old-exception" minOccurs="0" maxOccurs="unbounded" />
365366
</xsd:sequence>
366367
</xsd:complexType>
367368

368-
<xsd:complexType name="exception">
369-
<xsd:attribute name="name" type="xsd:string" use="required" />
369+
<xsd:complexType name="exception" abstract="true">
370370
<xsd:attribute name="log-level" type="xsd:string" />
371-
<xsd:attribute name="status-code" type="xsd:int" />
371+
<xsd:attribute name="status-code" type="xsd:integer" />
372+
</xsd:complexType>
373+
374+
<xsd:complexType name="old-exception">
375+
<xsd:complexContent>
376+
<xsd:extension base="exception">
377+
<xsd:attribute name="name" type="xsd:string" use="required" />
378+
</xsd:extension>
379+
</xsd:complexContent>
380+
</xsd:complexType>
381+
382+
<xsd:complexType name="new-exception">
383+
<xsd:complexContent>
384+
<xsd:extension base="exception">
385+
<xsd:attribute name="class" type="xsd:string" use="required" />
386+
</xsd:extension>
387+
</xsd:complexContent>
372388
</xsd:complexType>
373389

374390
<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>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0" ?>
2+
<container xmlns="http://symfony.com/schema/dic/services"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xmlns:framework="http://symfony.com/schema/dic/symfony"
5+
xsi:schemaLocation="http://symfony.com/schema/dic/services https://symfony.com/schema/dic/services/services-1.0.xsd
6+
http://symfony.com/schema/dic/symfony https://symfony.com/schema/dic/symfony/symfony-1.0.xsd">
7+
8+
<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>
15+
</framework:config>
16+
</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()

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,38 @@ public function testMessengerMiddlewareFactoryErroneousFormat()
3232
{
3333
$this->markTestSkipped('XML configuration will not allow erroneous format.');
3434
}
35+
36+
public function testLegacyExceptionsConfig()
37+
{
38+
$container = $this->createContainerFromFile('exceptions_legacy');
39+
40+
$configuration = $container->getDefinition('exception_listener')->getArgument(3);
41+
42+
$this->assertSame([
43+
\Symfony\Component\HttpKernel\Exception\BadRequestHttpException::class,
44+
\Symfony\Component\HttpKernel\Exception\NotFoundHttpException::class,
45+
\Symfony\Component\HttpKernel\Exception\ConflictHttpException::class,
46+
\Symfony\Component\HttpKernel\Exception\ServiceUnavailableHttpException::class,
47+
], array_keys($configuration));
48+
49+
$this->assertEqualsCanonicalizing([
50+
'log_level' => 'info',
51+
'status_code' => 422,
52+
], $configuration[\Symfony\Component\HttpKernel\Exception\BadRequestHttpException::class]);
53+
54+
$this->assertEqualsCanonicalizing([
55+
'log_level' => 'info',
56+
'status_code' => null,
57+
], $configuration[\Symfony\Component\HttpKernel\Exception\NotFoundHttpException::class]);
58+
59+
$this->assertEqualsCanonicalizing([
60+
'log_level' => 'info',
61+
'status_code' => null,
62+
], $configuration[\Symfony\Component\HttpKernel\Exception\ConflictHttpException::class]);
63+
64+
$this->assertEqualsCanonicalizing([
65+
'log_level' => null,
66+
'status_code' => 500,
67+
], $configuration[\Symfony\Component\HttpKernel\Exception\ServiceUnavailableHttpException::class]);
68+
}
3569
}

0 commit comments

Comments
 (0)