Skip to content

Refactoring & Documentation #7

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

Merged
merged 2 commits into from
Jan 8, 2025
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
1 change: 1 addition & 0 deletions .idea/php.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions .idea/problem-details-symfony-bundle.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
"phpmd/phpmd": "^2.5",
"phpstan/phpstan": "^2.0",
"phpunit/phpunit": "^11.5.0",
"squizlabs/php_codesniffer": "^3.7.2"
"squizlabs/php_codesniffer": "^3.7.2",
"symfony/yaml": "^7.2"
},
"license": "MIT",
"autoload": {
Expand Down
78 changes: 75 additions & 3 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 7 additions & 7 deletions config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,21 @@ services:
arguments:
$validationErrorsBuilder: '@Phauthentic\Symfony\ProblemDetails\Validation\ValidationErrorsBuilder'
$problemDetailsResponseFactory: '@Phauthentic\Symfony\ProblemDetails\FromExceptionEventFactoryInterface'
tags:
- { name: 'phauthentic.problem_details.exception_converter' }
tags: ['phauthentic.problem_details.exception_converter']

Phauthentic\Symfony\ProblemDetails\ExceptionConversion\HttpExceptionConverter:
arguments:
$problemDetailsFactory: '@Phauthentic\Symfony\ProblemDetails\ProblemDetailsFactoryInterface'
tags:
- { name: 'phauthentic.problem_details.exception_converter' }
tags: ['phauthentic.problem_details.exception_converter']

Phauthentic\Symfony\ProblemDetails\ExceptionConversion\GenericExceptionConverter:
tags:
- { name: 'phauthentic.problem_details.exception_converter' }
arguments:
$problemDetailsFactory: '@Phauthentic\Symfony\ProblemDetails\ProblemDetailsFactoryInterface'
tags: ['phauthentic.problem_details.exception_converter']

Phauthentic\Symfony\ProblemDetails\ThrowableToProblemDetailsKernelListener:
public: true
arguments:
$exceptionConverters: !tagged_iterator phauthentic.problem_details.exception_converter
tags:
- { name: 'kernel.event_listener', event: 'kernel.exception', priority: 0 }
- { name: 'kernel.event_listener', event: 'kernel.exception', priority: 0 }
29 changes: 28 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ This bundle provides support for [RFC 9457](https://www.rfc-editor.org/rfc/rfc94
composer require phauthentic/problem-details-symfony-bundle
```

## Docs
## Documentation

Simply inject the `ProblemDetailsFactoryInterface` into your handlers and use it to create `ProblemDetails` responses.

```php
class ExampleController
Expand All @@ -37,6 +39,31 @@ class ExampleController
}
```

### Service Configuration

To add more converters to the kernel listener, you can tag additional services with `phauthentic.problem_details.exception_converter`. They must implement the [ExceptionConverterInterface](src/ExceptionConversion/ExceptionConverterInterface.php).

```yaml
Phauthentic\Symfony\ProblemDetails\ExceptionConversion\ValidationFailedExceptionConverter:
arguments:
$validationErrorsBuilder: '@Phauthentic\Symfony\ProblemDetails\Validation\ValidationErrorsBuilder'
$problemDetailsResponseFactory: '@Phauthentic\Symfony\ProblemDetails\FromExceptionEventFactoryInterface'
tags: ['phauthentic.problem_details.exception_converter']
```

To completely override the list of already configured listeners override

```yaml
Phauthentic\Symfony\ProblemDetails\ThrowableToProblemDetailsKernelListener:
public: true
arguments:
$exceptionConverters: !tagged_iterator phauthentic.problem_details.exception_converter
tags:
- { name: 'kernel.event_listener', event: 'kernel.exception', priority: 0 }
```

in your `services.yaml`.

## Problem Details Example

```text
Expand Down
7 changes: 0 additions & 7 deletions src/ExceptionConversion/GenericExceptionConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,6 @@
* Notice that you might need to adjust the priority of the listener in your services.yaml file to make sure it is
* executed in the right order if you have other listeners.
*
* <code>
* Phauthentic\Symfony\ProblemDetails\ThrowableToProblemDetailsKernelListener:
* arguments: ['%kernel.environment%', { }]
* tags:
* - { name: kernel.event_listener, event: kernel.exception, priority: -20 }
* </code>
*
* @link https://www.rfc-editor.org/rfc/rfc9457.html
*/
class GenericExceptionConverter implements ExceptionConverterInterface
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
/**
*
*/
class GenericThrowableConverterTest extends TestCase
class GenericExceptionConverterTest extends TestCase
{
public function setUp(): void
{
Expand Down
47 changes: 47 additions & 0 deletions tests/Unit/ServiceLoadingTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

declare(strict_types=1);

namespace Phauthentic\Symfony\ProblemDetails\Tests\Unit;

use Phauthentic\Symfony\ProblemDetails\ThrowableToProblemDetailsKernelListener;
use PHPUnit\Framework\TestCase;
use ReflectionClass;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
use Phauthentic\Symfony\ProblemDetails\ExceptionConversion\ValidationFailedExceptionConverter;
use Phauthentic\Symfony\ProblemDetails\ExceptionConversion\HttpExceptionConverter;
use Phauthentic\Symfony\ProblemDetails\ExceptionConversion\GenericExceptionConverter;

/**
*
*/
class ServiceLoadingTest extends TestCase
{
public function testCreateValidResponse(): void
{
// Arrange
$container = new ContainerBuilder();
$loader = new YamlFileLoader($container, new FileLocator(__DIR__ . '/../../config'));
$loader->load('services.yaml');
$container->compile();

// Assert
$listener = $container->get(ThrowableToProblemDetailsKernelListener::class);
$this->assertInstanceOf(ThrowableToProblemDetailsKernelListener::class, $listener);

$reflectionClass = new ReflectionClass($listener);
$converters = $reflectionClass->getProperty('exceptionConverters')->getValue($listener);
$result = [];
foreach ($converters as $converter) {
$result[] = get_class($converter);
}
$this->assertCount(3, $result);
$this->assertEquals([
ValidationFailedExceptionConverter::class,
HttpExceptionConverter::class,
GenericExceptionConverter::class,
], $result);
}
}