Skip to content

Commit 0c2cb54

Browse files
committed
fix: handle defaultResponseHandlers config flag
1 parent 030db18 commit 0c2cb54

File tree

4 files changed

+60
-47
lines changed

4 files changed

+60
-47
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,11 @@ Now you can just create a `App\Responder\MyGoodRuntimeExceptionHandler` as descr
150150
### Default response handlers
151151

152152
The bundle automatically adds some response handlers for basic types with negative priority so that they will be called if none of your response handlers stops propagation earlier.
153-
If you don't want the default handlers to be added, you can set `pitch_adr.defaultResponseHandlers: false` on your container parameters.
153+
If you don't want the default handlers to be added, you can modify this behavior per bundle configuration.
154+
```yaml
155+
pitch_adr:
156+
defaultResponseHandlers: false # defaults to true
157+
```
154158
155159
### Prioritised response handlers
156160

src/DependencyInjection/Configuration.php

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
namespace Pitch\AdrBundle\DependencyInjection;
33

44
use RuntimeException;
5+
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
56
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
67
use Symfony\Component\Config\Definition\ConfigurationInterface;
78

@@ -13,31 +14,28 @@ public function getConfigTreeBuilder()
1314

1415
/** @var ArrayNodeDefinition */
1516
$root = $treeBuilder->getRootNode();
16-
17-
$root->children()
18-
->arrayNode('graceful')
19-
->defaultValue([
20-
['value' => RuntimeException::class]
21-
])
22-
->arrayPrototype()
23-
->beforeNormalization()
24-
->ifString()->then(function ($v) {
25-
return [ 'value' => $v ];
26-
})
27-
->end()
28-
->children()
29-
->scalarNode('value')->end()
30-
->arrayNode('not')
31-
->beforeNormalization()
32-
->ifString()->then(function ($v) {
33-
return [ $v ];
34-
})
35-
->end()
36-
->scalarPrototype()->end()
37-
->end()
38-
->end()
39-
->end()
40-
->end();
17+
$rootChildren = $root->children();
18+
19+
$graceful = $rootChildren->arrayNode('graceful');
20+
$graceful->defaultValue([
21+
['value' => RuntimeException::class]
22+
]);
23+
$gracefulPrototype = $graceful->arrayPrototype();
24+
$gracefulPrototype->beforeNormalization()
25+
->ifString()->then(function ($v) {
26+
return [ 'value' => $v ];
27+
});
28+
$gracefulChildren = $gracefulPrototype->children();
29+
$gracefulChildren->scalarNode('value');
30+
$gracefulNot = $gracefulChildren->arrayNode('not');
31+
$gracefulNot->beforeNormalization()
32+
->ifString()->then(function ($v) {
33+
return [ $v ];
34+
});
35+
$gracefulNot->scalarPrototype();
36+
37+
$rootChildren->booleanNode('defaultResponseHandlers')
38+
->defaultValue(true);
4139

4240
return $treeBuilder;
4341
}

src/DependencyInjection/PitchAdrExtension.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public function load(array $configs, ContainerBuilder $container)
2828
$loader->load('adr.php');
2929
$loader->load('debug.php');
3030

31-
if ($config[static::ALIAS . '.defaultResponseHandlers'] ?? true) {
31+
if ($config['defaultResponseHandlers']) {
3232
$loader->load('handler.php');
3333
}
3434

test/DependencyInjection/ConfigurationTest.php

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,35 +11,25 @@ public function provideNormalize(): array
1111
{
1212
return [
1313
[
14-
[],
14+
null,
1515
[
16-
'graceful' => [
17-
['value' => RuntimeException::class],
18-
],
16+
['value' => RuntimeException::class],
1917
],
2018
],
2119
[
2220
[
23-
'graceful' => [
24-
RuntimeException::class,
25-
]
21+
RuntimeException::class,
2622
],
2723
[
28-
'graceful' => [
29-
['value' => RuntimeException::class, 'not' => []],
30-
],
24+
['value' => RuntimeException::class, 'not' => []],
3125
],
3226
],
3327
[
3428
[
35-
'graceful' => [
36-
['value' => RuntimeException::class, 'not' => OutOfBoundsException::class],
37-
],
29+
['value' => RuntimeException::class, 'not' => OutOfBoundsException::class],
3830
],
3931
[
40-
'graceful' => [
41-
['value' => RuntimeException::class, 'not' => [OutOfBoundsException::class]],
42-
],
32+
['value' => RuntimeException::class, 'not' => [OutOfBoundsException::class]],
4333
],
4434
],
4535
];
@@ -48,12 +38,33 @@ public function provideNormalize(): array
4838
/**
4939
* @dataProvider provideNormalize
5040
*/
51-
public function testNormalize(
41+
public function testNormalizeGraceful(
5242
$config,
53-
$processedConfig
43+
$expectedProcessedGraceful
5444
) {
55-
$processor = new Processor();
45+
$processedConfig = $this->processConfig(isset($config) ? [
46+
'graceful' => $config
47+
] : []);
5648

57-
$this->assertEquals($processedConfig, $processor->processConfiguration(new Configuration(), [$config]));
49+
$this->assertArrayHasKey('graceful', $processedConfig);
50+
$this->assertEquals($expectedProcessedGraceful, $processedConfig['graceful']);
51+
}
52+
53+
public function testDefaultResponseHandlers()
54+
{
55+
$this->assertTrue($this->processConfig()['defaultResponseHandlers']);
56+
57+
$this->assertFalse($this->processConfig([
58+
'defaultResponseHandlers' => false,
59+
])['defaultResponseHandlers']);
60+
}
61+
62+
private function processConfig(
63+
array ...$configs
64+
) {
65+
return (new Processor())->processConfiguration(
66+
new Configuration(),
67+
$configs,
68+
);
5869
}
5970
}

0 commit comments

Comments
 (0)