Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 05f4b08

Browse filesBrowse files
committed
Refactor and delegate serializing responsibility to Serializer component
1 parent ba07cda commit 05f4b08
Copy full SHA for 05f4b08

40 files changed

+250
-1169
lines changed

‎UPGRADE-4.4.md

Copy file name to clipboardExpand all lines: UPGRADE-4.4.md
+14-25Lines changed: 14 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ Console
1818
Debug
1919
-----
2020

21-
* Deprecated the `Debug` class, use the one from the `ErrorRenderer` component instead
2221
* Deprecated the `FlattenException` class, use the one from the `ErrorRenderer` component instead
2322
* Deprecated the component in favor of the `ErrorHandler` component
2423

@@ -307,45 +306,35 @@ TwigBundle
307306
* Deprecated all built-in error templates, use the error renderer mechanism of the `ErrorRenderer` component
308307
* Deprecated loading custom error templates in non-html formats. Custom HTML error pages based on Twig keep working as before:
309308

310-
Before (`templates/bundles/TwigBundle/Exception/error.jsonld.twig`):
309+
Before (`templates/bundles/TwigBundle/Exception/error.json.twig`):
311310
```twig
312311
{
313-
"@id": "https://example.com",
314-
"@type": "error",
315-
"@context": {
316-
"title": "{{ status_text }}",
317-
"code": {{ status_code }},
318-
"message": "{{ exception.message }}"
319-
}
312+
"type": "https://example.com/error",
313+
"title": "{{ status_text }}",
314+
"status": {{ status_code }}
320315
}
321316
```
322317

323-
After (`App\ErrorRenderer\JsonLdErrorRenderer`):
318+
After (`App\Serializer\ProblemJsonNormalizer`):
324319
```php
325-
class JsonLdErrorRenderer implements ErrorRendererInterface
320+
class ProblemJsonNormalizer implements NormalizerInterface
326321
{
327-
public static function getFormat(): string
322+
public function normalize($exception, $format = null, array $context = [])
328323
{
329-
return 'jsonld';
324+
return [
325+
'type' => 'https://example.com/error',
326+
'title' => $exception->getStatusText(),
327+
'status' => $exception->getStatusCode(),
328+
];
330329
}
331330
332-
public function render(FlattenException $exception): string
331+
public function supportsNormalization($data, $format = null)
333332
{
334-
return json_encode([
335-
'@id' => 'https://example.com',
336-
'@type' => 'error',
337-
'@context' => [
338-
'title' => $exception->getTitle(),
339-
'code' => $exception->getStatusCode(),
340-
'message' => $exception->getMessage(),
341-
],
342-
]);
333+
return 'json' === $format && $data instanceof FlattenException;
343334
}
344335
}
345336
```
346337

347-
Configure your rendering service tagging it with `error_renderer.renderer`.
348-
349338
Validator
350339
---------
351340

‎UPGRADE-5.0.md

Copy file name to clipboardExpand all lines: UPGRADE-5.0.md
-1Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ Console
5757
Debug
5858
-----
5959

60-
* Removed the `Debug` class, use the one from the `ErrorRenderer` component instead
6160
* Removed the `FlattenException` class, use the one from the `ErrorRenderer` component instead
6261
* Removed the component in favor of the `ErrorHandler` component
6362

‎src/Symfony/Bundle/FrameworkBundle/FrameworkBundle.php

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/FrameworkBundle.php
-2Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
use Symfony\Component\DependencyInjection\Compiler\RegisterReverseContainerPass;
3434
use Symfony\Component\DependencyInjection\ContainerBuilder;
3535
use Symfony\Component\ErrorHandler\ErrorHandler;
36-
use Symfony\Component\ErrorRenderer\DependencyInjection\ErrorRendererPass;
3736
use Symfony\Component\EventDispatcher\DependencyInjection\RegisterListenersPass;
3837
use Symfony\Component\Form\DependencyInjection\FormPass;
3938
use Symfony\Component\HttpClient\DependencyInjection\HttpClientPass;
@@ -92,7 +91,6 @@ public function build(ContainerBuilder $container)
9291
KernelEvents::FINISH_REQUEST,
9392
];
9493

95-
$container->addCompilerPass(new ErrorRendererPass());
9694
$container->addCompilerPass(new LoggerPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION, -32);
9795
$container->addCompilerPass(new RegisterControllerArgumentLocatorsPass());
9896
$container->addCompilerPass(new RemoveEmptyControllerArgumentLocatorsPass(), PassConfig::TYPE_BEFORE_REMOVING);

‎src/Symfony/Bundle/FrameworkBundle/Resources/config/console.xml

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/Resources/config/console.xml
-6Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -194,12 +194,6 @@
194194
<tag name="console.command" command="debug:form" />
195195
</service>
196196

197-
<service id="console.command.error_renderer_debug" class="Symfony\Component\ErrorRenderer\Command\DebugCommand">
198-
<argument type="collection" /> <!-- All error renderers are injected here by ErrorRendererPass -->
199-
<argument type="service" id="debug.file_link_formatter" on-invalid="null" />
200-
<tag name="console.command" command="debug:error-renderer" />
201-
</service>
202-
203197
<service id="console.command.secrets_set" class="Symfony\Bundle\FrameworkBundle\Command\SecretsSetCommand">
204198
<argument type="service" id="secrets.vault" />
205199
<argument type="service" id="secrets.local_vault" on-invalid="ignore" />

‎src/Symfony/Bundle/FrameworkBundle/Resources/config/error_renderer.xml

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/Resources/config/error_renderer.xml
+3-20Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,35 +5,18 @@
55
xsi:schemaLocation="http://symfony.com/schema/dic/services https://symfony.com/schema/dic/services/services-1.0.xsd">
66

77
<services>
8-
<service id="error_renderer" class="Symfony\Component\ErrorRenderer\DependencyInjection\LazyLoadingErrorRenderer">
9-
<argument /> <!-- error renderer locator -->
8+
<service id="error_renderer" class="Symfony\Component\ErrorRenderer\ErrorRenderer">
9+
<argument type="service" id="error_renderer.renderer.html" />
10+
<argument type="service" id="serializer" on-invalid="null" />
1011
</service>
1112

1213
<service id="error_renderer.renderer.html" class="Symfony\Component\ErrorRenderer\ErrorRenderer\HtmlErrorRenderer">
13-
<tag name="error_renderer.renderer" />
1414
<argument>%kernel.debug%</argument>
1515
<argument>%kernel.charset%</argument>
1616
<argument type="service" id="debug.file_link_formatter" on-invalid="null" />
1717
<argument>%kernel.project_dir%</argument>
1818
<argument type="service" id="request_stack" />
1919
<argument type="service" id="logger" on-invalid="null" />
2020
</service>
21-
22-
<service id="error_renderer.renderer.json" class="Symfony\Component\ErrorRenderer\ErrorRenderer\JsonErrorRenderer">
23-
<tag name="error_renderer.renderer" />
24-
<argument>%kernel.debug%</argument>
25-
</service>
26-
27-
<service id="error_renderer.renderer.xml" class="Symfony\Component\ErrorRenderer\ErrorRenderer\XmlErrorRenderer">
28-
<tag name="error_renderer.renderer" format="atom" />
29-
<tag name="error_renderer.renderer" />
30-
<argument>%kernel.debug%</argument>
31-
<argument>%kernel.charset%</argument>
32-
</service>
33-
34-
<service id="error_renderer.renderer.txt" class="Symfony\Component\ErrorRenderer\ErrorRenderer\TxtErrorRenderer">
35-
<tag name="error_renderer.renderer" />
36-
<argument>%kernel.debug%</argument>
37-
</service>
3821
</services>
3922
</container>

‎src/Symfony/Bundle/FrameworkBundle/Resources/config/serializer.xml

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/Resources/config/serializer.xml
+6Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,12 @@
5959
<tag name="serializer.normalizer" priority="-900" />
6060
</service>
6161

62+
<service id="serializer.normalizer.problem" class="Symfony\Component\Serializer\Normalizer\ProblemNormalizer">
63+
<argument>%kernel.debug%</argument>
64+
<!-- Run before serializer.normalizer.object -->
65+
<tag name="serializer.normalizer" priority="-890" />
66+
</service>
67+
6268
<service id="serializer.normalizer.object" class="Symfony\Component\Serializer\Normalizer\ObjectNormalizer">
6369
<argument type="service" id="serializer.mapping.class_metadata_factory" />
6470
<argument type="service" id="serializer.name_converter.metadata_aware" />

‎src/Symfony/Bundle/FrameworkBundle/composer.json

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/composer.json
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
"symfony/process": "^3.4|^4.0|^5.0",
5151
"symfony/security-csrf": "^3.4|^4.0|^5.0",
5252
"symfony/security-http": "^3.4|^4.0|^5.0",
53-
"symfony/serializer": "^4.3|^5.0",
53+
"symfony/serializer": "^4.4|^5.0",
5454
"symfony/stopwatch": "^3.4|^4.0|^5.0",
5555
"symfony/translation": "^4.4|^5.0",
5656
"symfony/templating": "^3.4|^4.0|^5.0",

‎src/Symfony/Bundle/SecurityBundle/Tests/Functional/JsonLoginTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/SecurityBundle/Tests/Functional/JsonLoginTest.php
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,6 @@ public function testDefaultJsonLoginBadRequest()
7070

7171
$this->assertSame(400, $response->getStatusCode());
7272
$this->assertSame('application/json', $response->headers->get('Content-Type'));
73-
$this->assertSame(['title' => 'Bad Request', 'status' => 400, 'detail' => 'Whoops, looks like something went wrong.'], json_decode($response->getContent(), true));
73+
$this->assertSame(['type' => 'https://tools.ietf.org/html/rfc2616#section-10', 'title' => 'An error occurred', 'status' => 400, 'detail' => 'Bad Request'], json_decode($response->getContent(), true));
7474
}
7575
}

‎src/Symfony/Bundle/SecurityBundle/Tests/Functional/app/JsonLogin/config.yml

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/SecurityBundle/Tests/Functional/app/JsonLogin/config.yml
+3Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
imports:
22
- { resource: ./../config/framework.yml }
33

4+
framework:
5+
serializer: ~
6+
47
security:
58
encoders:
69
Symfony\Component\Security\Core\User\User: plaintext

‎src/Symfony/Bundle/SecurityBundle/composer.json

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/SecurityBundle/composer.json
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
"symfony/form": "^3.4|^4.0|^5.0",
3636
"symfony/framework-bundle": "^4.4|^5.0",
3737
"symfony/http-foundation": "^3.4|^4.0|^5.0",
38+
"symfony/serializer": "^4.4|^5.0",
3839
"symfony/translation": "^3.4|^4.0|^5.0",
3940
"symfony/twig-bundle": "^4.4|^5.0",
4041
"symfony/twig-bridge": "^3.4|^4.0|^5.0",
+29Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bundle\TwigBundle\DependencyInjection\Compiler;
13+
14+
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
15+
use Symfony\Component\DependencyInjection\ContainerBuilder;
16+
use Symfony\Component\DependencyInjection\Reference;
17+
18+
/**
19+
* Configures the Twig-based HTML error renderer.
20+
*
21+
* @author Yonel Ceruto <yonelceruto@gmail.com>
22+
*/
23+
final class ErrorRendererPass implements CompilerPassInterface
24+
{
25+
public function process(ContainerBuilder $container): void
26+
{
27+
$container->getDefinition('error_renderer')->setArgument(0, new Reference('twig.error_renderer.html'));
28+
}
29+
}

‎src/Symfony/Bundle/TwigBundle/ErrorRenderer/TwigHtmlErrorRenderer.php

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/TwigBundle/ErrorRenderer/TwigHtmlErrorRenderer.php
+3-11Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111

1212
namespace Symfony\Bundle\TwigBundle\ErrorRenderer;
1313

14-
use Symfony\Component\ErrorRenderer\ErrorRenderer\ErrorRendererInterface;
1514
use Symfony\Component\ErrorRenderer\ErrorRenderer\HtmlErrorRenderer;
15+
use Symfony\Component\ErrorRenderer\ErrorRenderer\HtmlErrorRendererInterface;
1616
use Symfony\Component\ErrorRenderer\Exception\FlattenException;
1717
use Twig\Environment;
1818
use Twig\Error\LoaderError;
@@ -24,7 +24,7 @@
2424
*
2525
* @author Yonel Ceruto <yonelceruto@gmail.com>
2626
*/
27-
class TwigHtmlErrorRenderer implements ErrorRendererInterface
27+
class TwigHtmlErrorRenderer implements HtmlErrorRendererInterface
2828
{
2929
private $twig;
3030
private $htmlErrorRenderer;
@@ -37,14 +37,6 @@ public function __construct(Environment $twig, HtmlErrorRenderer $htmlErrorRende
3737
$this->debug = $debug;
3838
}
3939

40-
/**
41-
* {@inheritdoc}
42-
*/
43-
public static function getFormat(): string
44-
{
45-
return 'html';
46-
}
47-
4840
/**
4941
* {@inheritdoc}
5042
*/
@@ -66,7 +58,7 @@ public function render(FlattenException $exception): string
6658
'legacy' => false, // to be removed in 5.0
6759
'exception' => $exception,
6860
'status_code' => $exception->getStatusCode(),
69-
'status_text' => $exception->getTitle(),
61+
'status_text' => $exception->getStatusText(),
7062
]);
7163
}
7264

‎src/Symfony/Bundle/TwigBundle/Resources/config/twig.xml

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/TwigBundle/Resources/config/twig.xml
-1Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,6 @@
163163
</service>
164164

165165
<service id="twig.error_renderer.html" class="Symfony\Bundle\TwigBundle\ErrorRenderer\TwigHtmlErrorRenderer">
166-
<tag name="error_renderer.renderer" priority="1" />
167166
<argument type="service" id="twig" />
168167
<argument type="service" id="error_renderer.renderer.html" />
169168
<argument>%kernel.debug%</argument>

‎src/Symfony/Bundle/TwigBundle/TwigBundle.php

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/TwigBundle/TwigBundle.php
+2Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Bundle\TwigBundle;
1313

14+
use Symfony\Bundle\TwigBundle\DependencyInjection\Compiler\ErrorRendererPass;
1415
use Symfony\Bundle\TwigBundle\DependencyInjection\Compiler\ExceptionListenerPass;
1516
use Symfony\Bundle\TwigBundle\DependencyInjection\Compiler\ExtensionPass;
1617
use Symfony\Bundle\TwigBundle\DependencyInjection\Compiler\RuntimeLoaderPass;
@@ -37,6 +38,7 @@ public function build(ContainerBuilder $container)
3738
$container->addCompilerPass(new TwigLoaderPass());
3839
$container->addCompilerPass(new ExceptionListenerPass());
3940
$container->addCompilerPass(new RuntimeLoaderPass(), PassConfig::TYPE_BEFORE_REMOVING);
41+
$container->addCompilerPass(new ErrorRendererPass());
4042
}
4143

4244
public function registerCommands(Application $application)

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.