Skip to content

Commit 070c50c

Browse files
authored
Merge pull request #1 from veewee/basic-implementation
Basic implementation
2 parents f857822 + 9b54346 commit 070c50c

25 files changed

Lines changed: 1118 additions & 2 deletions

.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/vendor/
2+
composer.lock
3+
.php_cs.cache
4+
.php_cs
5+
grumphp.yml
6+
phpunit.xml
7+
var

.php_cs.dist

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<?php
2+
3+
return PhpCsFixer\Config::create()
4+
->setFinder(
5+
\Symfony\Component\Finder\Finder::create()
6+
->in([
7+
__DIR__ . '/src',
8+
__DIR__ . '/test',
9+
])
10+
)
11+
->setRiskyAllowed(true)
12+
->setRules([
13+
'@Symfony' => true,
14+
'align_multiline_comment' => true,
15+
'array_indentation' => true,
16+
'array_syntax' => ['syntax' => 'short'],
17+
'backtick_to_shell_exec' => true,
18+
'blank_line_before_statement' => true,
19+
'class_keyword_remove' => false,
20+
'combine_consecutive_issets' => true,
21+
'combine_consecutive_unsets' => true,
22+
'comment_to_phpdoc' => true,
23+
'compact_nullable_typehint' => true,
24+
'date_time_immutable' => true,
25+
'declare_strict_types' => true,
26+
'doctrine_annotation_array_assignment' => true,
27+
'doctrine_annotation_braces' => true,
28+
'doctrine_annotation_indentation' => true,
29+
'doctrine_annotation_spaces' => true,
30+
'escape_implicit_backslashes' => true,
31+
'explicit_indirect_variable' => true,
32+
'explicit_string_variable' => true,
33+
'final_internal_class' => false,
34+
'fully_qualified_strict_types' => true,
35+
'general_phpdoc_annotation_remove' => false,
36+
'header_comment' => false,
37+
'heredoc_to_nowdoc' => false,
38+
'linebreak_after_opening_tag' => true,
39+
'list_syntax' => true,
40+
'mb_str_functions' => true,
41+
'method_chaining_indentation' => true,
42+
'multiline_comment_opening_closing' => true,
43+
'multiline_whitespace_before_semicolons' => true,
44+
'native_function_invocation' => false,
45+
'no_alternative_syntax' => true,
46+
'no_blank_lines_before_namespace' => false,
47+
'no_null_property_initialization' => true,
48+
'no_php4_constructor' => true,
49+
'no_short_echo_tag' => false,
50+
'no_superfluous_elseif' => true,
51+
'no_unreachable_default_argument_value' => true,
52+
'no_useless_else' => true,
53+
'no_useless_return' => true,
54+
'not_operator_with_space' => false,
55+
'not_operator_with_successor_space' => false,
56+
'ordered_class_elements' => false,
57+
'ordered_imports' => true,
58+
'php_unit_dedicate_assert' => false,
59+
'php_unit_expectation' => false,
60+
'php_unit_mock' => false,
61+
'php_unit_namespaced' => false,
62+
'php_unit_no_expectation_annotation' => false,
63+
'php_unit_ordered_covers' => true,
64+
'php_unit_set_up_tear_down_visibility' => true,
65+
'php_unit_strict' => false,
66+
'php_unit_test_annotation' => false,
67+
'php_unit_test_class_requires_covers' => false,
68+
'phpdoc_add_missing_param_annotation' => true,
69+
'phpdoc_order' => true,
70+
'phpdoc_types_order' => ['null_adjustment' => 'always_last'],
71+
'pow_to_exponentiation' => true,
72+
'psr0' => true,
73+
'random_api_migration' => false,
74+
'simplified_null_return' => true,
75+
'static_lambda' => false,
76+
'strict_comparison' => true,
77+
'strict_param' => true,
78+
'string_line_ending' => true,
79+
'ternary_to_null_coalescing' => true,
80+
'void_return' => true,
81+
'yoda_style' => true,
82+
])
83+
;

.travis.yml

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
language: php
2+
3+
env:
4+
global:
5+
- DEFAULT_COMPOSER_FLAGS="--optimize-autoloader --no-interaction --no-progress"
6+
- COMPOSER_FLAGS=""
7+
8+
before_install:
9+
- alias composer=composer\ --no-interaction && composer selfupdate
10+
11+
cache:
12+
directories:
13+
- .composer/cache
14+
15+
matrix:
16+
fast_finish: true
17+
allow_failures:
18+
- php: nightly
19+
20+
jobs:
21+
include:
22+
- &STANDARD_TEST_JOB
23+
stage: Test
24+
php: 7.2
25+
install:
26+
- travis_retry composer update $DEFAULT_COMPOSER_FLAGS $COMPOSER_FLAGS
27+
- composer info -D | sort
28+
script:
29+
- vendor/bin/grumphp run
30+
-
31+
<<: *STANDARD_TEST_JOB
32+
stage: Test
33+
php: 7.2
34+
env: COMPOSER_FLAGS="--prefer-stable --prefer-lowest"
35+
-
36+
<<: *STANDARD_TEST_JOB
37+
stage: Test
38+
php: nightly
39+
env: COMPOSER_FLAGS="--ignore-platform-reqs" PHP_CS_FIXER_IGNORE_ENV=1 PHP_CS_FIXER_FUTURE_MODE=1
40+
script:
41+
- vendor/bin/grumphp run
42+
43+
allow_failures:
44+
- php: nightly

CONTRIBUTING

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Contributing
2+
3+
Api Problem Bundle is an open source, community-driven project. If you'd like to contribute,
4+
feel free to do this, but remember to follow this few simple rules:
5+
6+
## Branching strategy
7+
8+
- __Always__ base your changes on the `master` branch (all new development happens here),
9+
- When you create Pull Request, always select `master` branch as target, otherwise it
10+
will be closed (this is selected by default).
11+
12+
## Coverage
13+
14+
- All classes that interact solely with the core logic should be covered by Tests
15+
16+
## Code style / Formatting
17+
18+
- All code in the `src` and `test` folder must follow the PSR-2 standard and should comply with the php-cs-fixer config.

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2018 Phpro
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

README.md

Lines changed: 111 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,111 @@
1-
# api-problem-bundle
2-
RFC7807 Problem details integration for Symfony
1+
[![Travis](https://img.shields.io/travis/phpro/api-problem-bundle/master.svg)](http://travis-ci.org/phpro/api-problem-bundle)
2+
[![Installs](https://img.shields.io/packagist/dt/phpro/api-problem-bundle.svg)](https://packagist.org/packages/phpro/api-problem-bundle/stats)
3+
[![Packagist](https://img.shields.io/packagist/v/phpro/api-problem-bundle.svg)](https://packagist.org/packages/phpro/api-problem-bundle)
4+
5+
6+
# Api Problem Bundle
7+
8+
This package provides a [RFC7807](https://tools.ietf.org/html/rfc7807) Problem details exception listener for Symfony.
9+
Internal, this package uses the models provided by `phpro/api-problem`](https://www.github.com/phpro/api-problem).
10+
When an `ApiProblemException` is triggered, this bundle will return the correct response.
11+
12+
13+
## Installation
14+
15+
```sh
16+
composer require phpro/api-problem-bundle
17+
```
18+
19+
If you are not using the contrib recipes of `symfony/flex`, you'll have to manually add the bundle to your bundles file:
20+
21+
```php
22+
// config/bundles.php
23+
24+
return [
25+
// ...
26+
Phpro\ApiProblemBundle\ApiProblemBundle::class => ['all' => true],
27+
];
28+
```
29+
30+
## Supported response formats
31+
32+
- application/problem+json
33+
34+
35+
## How it works
36+
37+
```
38+
Use Phpro\ApiProblem\Exception\ApiProblemException
39+
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
40+
41+
SomeController {
42+
43+
/**
44+
* @Route('/some-route', defaults={"_format" = "json"})
45+
*/
46+
someAction() {
47+
throw new ApiProblemException(
48+
new HttpApiProblem('400', 'It aint all bad ...')
49+
);
50+
}
51+
}
52+
```
53+
54+
When the controller is marked as a "json" format or the request `Content-Type` is `*/json`, this bundle kicks in.
55+
It will transform the exception to following response:
56+
57+
Headers:
58+
```
59+
Content-Type: application/problem+json
60+
```
61+
62+
Body:
63+
```json
64+
{
65+
"status": 400,
66+
"type": "http:\/\/www.w3.org\/Protocols\/rfc2616\/rfc2616-sec10.html",
67+
"title": "Bad Request",
68+
"detail": "It ain't all bad ..."
69+
}
70+
```
71+
72+
## Adding custom exception transformations
73+
74+
Currently, we automatically transform exceptions from following packages to an ApiProblem instance:
75+
76+
- phpro/api-problem
77+
- symfony/http-kernel
78+
- symfony/security
79+
80+
Besides that, all other errors are transformed to a basic `ExceptionApiProblem` instance.
81+
82+
If you want to add custom transformations, you can implement the `ExceptionTransformerInterface`
83+
and register it in the symfony container with the `phpro.api_problem.exception_transformer` tag.
84+
85+
```php
86+
use Phpro\ApiProblemBundle\Transformer\ExceptionTransformerInterface;
87+
88+
class MyTransformer implements ExceptionTransformerInterface
89+
{
90+
public function transform(\Throwable $exception): ApiProblemInterface
91+
{
92+
return new MyApiProblem($exception);
93+
}
94+
95+
public function accepts(\Throwable $exception): bool
96+
{
97+
return $exception instanceof MyException;
98+
}
99+
}
100+
```
101+
102+
## About
103+
104+
### Submitting bugs and feature requests
105+
106+
Bugs and feature request are tracked on [GitHub](https://github.com/phpro/api-problem-bundle/issues).
107+
Please take a look at our rules before [contributing your code](CONTRIBUTING).
108+
109+
### License
110+
111+
api-problem-bundle is licensed under the [MIT License](LICENSE).

composer.json

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
{
2+
"name": "phpro/api-problem-bundle",
3+
"description": "RFC7807 Problem details integration for Symfony",
4+
"type": "library",
5+
"license": "MIT",
6+
"authors": [
7+
{
8+
"name": "Toon Verwerft",
9+
"email": "toon.verwerft@phpro.be"
10+
}
11+
],
12+
"require": {
13+
"php": "^7.2",
14+
"phpro/api-problem": "^1.0",
15+
"symfony/dependency-injection": "^4.1",
16+
"symfony/event-dispatcher": "^4.1",
17+
"symfony/http-kernel": "^4.1"
18+
},
19+
"require-dev": {
20+
"friendsofphp/php-cs-fixer": "^2.12",
21+
"matthiasnoback/symfony-dependency-injection-test": "^3.0",
22+
"phpro/grumphp": "^0.14.1",
23+
"phpunit/phpunit": "^7.2",
24+
"symfony/security": "^4.1"
25+
},
26+
"config": {
27+
"sort-packages": true
28+
},
29+
"autoload": {
30+
"psr-4": {
31+
"Phpro\\ApiProblemBundle\\": "src/"
32+
}
33+
},
34+
"autoload-dev": {
35+
"psr-4": {
36+
"PhproTest\\ApiProblemBundle\\": "test/"
37+
}
38+
}
39+
}

config/services.xml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<container xmlns="http://symfony.com/schema/dic/services"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
5+
<services>
6+
<service
7+
id="Phpro\ApiProblemBundle\EventListener\JsonApiProblemExceptionListener"
8+
class="Phpro\ApiProblemBundle\EventListener\JsonApiProblemExceptionListener"
9+
>
10+
<argument key="$exceptionTransformer" type="service" id="Phpro\ApiProblemBundle\Transformer\Chain" />
11+
<argument key="$debug" >%kernel.debug%</argument>
12+
<tag name="kernel.event_listener" event="kernel.exception" method="onKernelException" />
13+
</service>
14+
<service id="Phpro\ApiProblemBundle\Transformer\Chain" class="Phpro\ApiProblemBundle\Transformer\Chain">
15+
<argument type="tagged" tag="phpro.api_problem.exception_transformer" />
16+
</service>
17+
<service id="Phpro\ApiProblemBundle\Transformer\ApiProblemExceptionTransformer" class="Phpro\ApiProblemBundle\Transformer\ApiProblemExceptionTransformer">
18+
<tag name="phpro.api_problem.exception_transformer" />
19+
</service>
20+
<service id="Phpro\ApiProblemBundle\Transformer\HttpExceptionTransformer" class="Phpro\ApiProblemBundle\Transformer\HttpExceptionTransformer">
21+
<tag name="phpro.api_problem.exception_transformer" />
22+
</service>
23+
<service id="Phpro\ApiProblemBundle\Transformer\SecurityExceptionTransformer" class="Phpro\ApiProblemBundle\Transformer\SecurityExceptionTransformer">
24+
<tag name="phpro.api_problem.exception_transformer" />
25+
</service>
26+
</services>
27+
</container>

grumphp.yml.dist

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
parameters:
2+
git_dir: .
3+
bin_dir: vendor/bin
4+
tasks:
5+
phpcsfixer2:
6+
config: ".php_cs.dist"
7+
config_contains_finder: true
8+
metadata:
9+
priority: 10000
10+
phpunit:
11+
metadata:
12+
priority: 9000
13+
clover_coverage:
14+
clover_file: var/coverage.xml
15+
level: 100
16+
metadata:
17+
priority: 8000
18+
composer:
19+
no_check_lock: true
20+
xmllint:
21+
load_from_net: true
22+
scheme_validation: true
23+
triggered_by: ['xml']

0 commit comments

Comments
 (0)