Skip to content

Commit

Permalink
Merge pull request #52 from pascalheidmann/separate-details-config
Browse files Browse the repository at this point in the history
Decouple `$includeThrowableDetail` from generic `debug` config
  • Loading branch information
Ocramius committed Feb 26, 2024
2 parents 97b490e + 5a773ab commit 7d04492
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 4 deletions.
17 changes: 17 additions & 0 deletions docs/book/response.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,23 @@ This package also provides a factory for generating the
value is provided as the `$defaultTypesMap` parameter; see the
[default types documentation](default-types.md) for details on defining
this map. (Since 1.1.0.)
- If the service contains a `problem-details` key with an array value
containing a `include-throwable-details` key,
and that value is a boolean,
that value is used instead of global `debug` value for the `$includeThrowableDetail` parameter.
(Since 1.14.0.)

### Example configuration

```php
[
'debug' => true,
'problem-details' => [
'json_flags' => JSON_PRETTY_PRINT,
'include-throwable-details' => true,
]
]
```

If any of the above config values are not present, a `null` value will be
passed, allowing the default value to be used.
Expand Down
13 changes: 10 additions & 3 deletions src/ProblemDetailsResponseFactoryFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,22 @@ public function __invoke(ContainerInterface $container): ProblemDetailsResponseF
{
$config = $container->has('config') ? $container->get('config') : [];
Assert::isArrayAccessible($config);
$debug = isset($config['debug']) && is_bool($config['debug']) ? $config['debug'] : null;
$includeThrowableDetail = $debug ?? ProblemDetailsResponseFactory::EXCLUDE_THROWABLE_DETAILS;
$debug = isset($config['debug']) && is_bool($config['debug']) ? $config['debug'] : null;
$debug ??= ProblemDetailsResponseFactory::EXCLUDE_THROWABLE_DETAILS;

$problemDetailsConfig = $config['problem-details'] ?? [];
Assert::isArrayAccessible($problemDetailsConfig);

$includeThrowableDetail = isset($problemDetailsConfig['include-throwable-details'])
&& is_bool($problemDetailsConfig['include-throwable-details'])
? $problemDetailsConfig['include-throwable-details'] : null;
$includeThrowableDetail ??= $debug;

$jsonFlags = $problemDetailsConfig['json_flags'] ?? null;
assert($jsonFlags === null || is_int($jsonFlags));
$defaultTypesMap = $problemDetailsConfig['default_types_map'] ?? [];
Assert::isArray($defaultTypesMap);

foreach ($defaultTypesMap as $key => $value) {
assert(is_int($key));
assert(is_string($value));
Expand All @@ -38,7 +45,7 @@ public function __invoke(ContainerInterface $container): ProblemDetailsResponseF

return new ProblemDetailsResponseFactory(
$this->detectResponseFactory($container),
$includeThrowableDetail,
$debug,
$jsonFlags,
$includeThrowableDetail,
ProblemDetailsResponseFactory::DEFAULT_DETAIL_MESSAGE,
Expand Down
23 changes: 22 additions & 1 deletion test/ProblemDetailsResponseFactoryFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public function testLackOfConfigServiceResultsInFactoryUsingDefaults(): void
{
$response = $this->createMock(ResponseInterface::class);
$response->method('withStatus')->willReturnSelf();
$this->container->set(ResponseInterface::class, static fn () => $response);
$this->container->set(ResponseInterface::class, static fn() => $response);

$factoryFactory = new ProblemDetailsResponseFactoryFactory();
$factory = $factoryFactory($this->container);
Expand Down Expand Up @@ -132,4 +132,25 @@ public function testUsesDefaultTypesSettingFromConfigWhenPresent(): void

self::assertSame($expectedDefaultTypes, $defaultTypesMap->getValue($factory));
}

public function testUsesIncludeThrowableDetailsSettingFromConfigWhenPresent(): void
{
$this->container->set(
'config',
[
'problem-details' => [
'include-throwable-details' => ProblemDetailsResponseFactory::INCLUDE_THROWABLE_DETAILS,
],
]
);
$this->container->set(ResponseInterface::class, static fn() => null);

$factoryFactory = new ProblemDetailsResponseFactoryFactory();
$factory = $factoryFactory($this->container);
$isDebug = (new ReflectionObject($factory))->getProperty('isDebug');
$exceptionDetailsInResponse = (new ReflectionObject($factory))->getProperty('exceptionDetailsInResponse');

self::assertSame(ProblemDetailsResponseFactory::EXCLUDE_THROWABLE_DETAILS, $isDebug->getValue($factory));
self::assertSame(true, $exceptionDetailsInResponse->getValue($factory));
}
}

0 comments on commit 7d04492

Please sign in to comment.