Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Decouple $includeThrowableDetail from generic debug config #52

Merged
merged 4 commits into from
Feb 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
froschdesign marked this conversation as resolved.
Show resolved Hide resolved
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;
Ocramius marked this conversation as resolved.
Show resolved Hide resolved
$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));
}
}