Movatterモバイル変換


[0]ホーム

URL:


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

Sign up
Appearance settings

Commitda77722

Browse files
committed
Add ErrorController and PreviewErrorController to FrameworkBundle
1 parent22319a9 commitda77722

File tree

21 files changed

+165
-107
lines changed

21 files changed

+165
-107
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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+
namespaceSymfony\Bundle\FrameworkBundle\Controller;
13+
14+
useSymfony\Component\ErrorRenderer\ErrorRenderer;
15+
useSymfony\Component\ErrorRenderer\Exception\ErrorRendererNotFoundException;
16+
useSymfony\Component\ErrorRenderer\Exception\FlattenException;
17+
useSymfony\Component\HttpFoundation\Request;
18+
useSymfony\Component\HttpFoundation\Response;
19+
20+
/**
21+
* Renders error or exception pages for a given FlattenException.
22+
*
23+
* @author Yonel Ceruto <yonelceruto@gmail.com>
24+
*/
25+
class ErrorController
26+
{
27+
private$errorRenderer;
28+
29+
publicfunction__construct(ErrorRenderer$errorRenderer)
30+
{
31+
$this->errorRenderer =$errorRenderer;
32+
}
33+
34+
publicfunction__invoke(Request$request,FlattenException$exception)
35+
{
36+
try {
37+
returnnewResponse($this->errorRenderer->render($exception,$request->getPreferredFormat()),$exception->getStatusCode(),$exception->getHeaders());
38+
}catch (ErrorRendererNotFoundException$e) {
39+
returnnewResponse($this->errorRenderer->render($exception),$exception->getStatusCode(),$exception->getHeaders());
40+
}
41+
}
42+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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+
namespaceSymfony\Bundle\FrameworkBundle\Controller;
13+
14+
useSymfony\Component\ErrorRenderer\ErrorRenderer;
15+
useSymfony\Component\ErrorRenderer\Exception\FlattenException;
16+
useSymfony\Component\HttpFoundation\Request;
17+
useSymfony\Component\HttpFoundation\Response;
18+
useSymfony\Component\HttpKernel\HttpKernelInterface;
19+
20+
/**
21+
* It will create a test exception and forward it to the exception controller
22+
* if configured, otherwise it use the native error rendering mechanism.
23+
*
24+
* @author Matthias Pigulla <mp@webfactory.de>
25+
* @author Yonel Ceruto <yonelceruto@gmail.com>
26+
*
27+
* @internal
28+
*/
29+
class PreviewErrorController
30+
{
31+
private$kernel;
32+
private$errorRenderer;
33+
private$controller;
34+
35+
publicfunction__construct(HttpKernelInterface$kernel,ErrorRenderer$errorRenderer,$controller)
36+
{
37+
$this->kernel =$kernel;
38+
$this->errorRenderer =$errorRenderer;
39+
$this->controller =$controller;
40+
}
41+
42+
publicfunction__invoke(Request$request,$code)
43+
{
44+
$exception = FlattenException::createFromThrowable(new \Exception('Something has intentionally gone wrong.'),$code, ['X-Debug' =>false]);
45+
46+
if (null !==$this->controller) {
47+
/*
48+
* This Request mimics the parameters set by
49+
* \Symfony\Component\HttpKernel\EventListener\ExceptionListener::duplicateRequest, with
50+
* the additional "showException" flag.
51+
*/
52+
$subRequest =$request->duplicate(null,null, [
53+
'_controller' =>$this->controller,
54+
'exception' =>$exception,
55+
'logger' =>null,
56+
'showException' =>false,
57+
]);
58+
59+
return$this->kernel->handle($subRequest, HttpKernelInterface::SUB_REQUEST);
60+
}
61+
62+
returnnewResponse($this->errorRenderer->render($exception,$request->getPreferredFormat()),$code);
63+
}
64+
}

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
useSymfony\Component\Mailer\Mailer;
2929
useSymfony\Component\Messenger\MessageBusInterface;
3030
useSymfony\Component\PropertyInfo\PropertyInfoExtractorInterface;
31-
useSymfony\Component\Security\Csrf\CsrfTokenManagerInterface;
3231
useSymfony\Component\Serializer\Serializer;
3332
useSymfony\Component\Translation\Translator;
3433
useSymfony\Component\Validator\Validation;
@@ -84,6 +83,10 @@ public function getConfigTreeBuilder()
8483
->beforeNormalization()->ifString()->then(function ($v) {return [$v]; })->end()
8584
->prototype('scalar')->end()
8685
->end()
86+
->scalarNode('error_controller')
87+
->defaultValue('error_controller')
88+
->treatNullLike('error_controller')
89+
->end()
8790
->end()
8891
;
8992

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@ public function load(array $configs, ContainerBuilder $container)
211211
$container->setParameter('kernel.http_method_override',$config['http_method_override']);
212212
$container->setParameter('kernel.trusted_hosts',$config['trusted_hosts']);
213213
$container->setParameter('kernel.default_locale',$config['default_locale']);
214+
$container->setParameter('kernel.error_controller',$config['error_controller']);
214215

215216
if (!$container->hasParameter('debug.file_link_format')) {
216217
if (!$container->hasParameter('templating.helper.code.file_link_format')) {

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@
2121
<argument>%kernel.debug%</argument>
2222
<argumenttype="service"id="debug.file_link_formatter" />
2323
<argument>%kernel.debug%</argument>
24-
<argument>%kernel.charset%</argument>
25-
<argumenttype="service"id="error_renderer"on-invalid="null" />
2624
</service>
2725

2826
<serviceid="debug.file_link_formatter"class="Symfony\Component\HttpKernel\Debug\FileLinkFormatter">
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
3+
<routesxmlns="http://symfony.com/schema/routing"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xsi:schemaLocation="http://symfony.com/schema/routing https://symfony.com/schema/routing/routing-1.0.xsd">
6+
7+
<routeid="_preview_error"path="/{code}.{_format}">
8+
<defaultkey="_controller">preview_error_controller</default>
9+
<defaultkey="_format">html</default>
10+
<requirementkey="code">\d+</requirement>
11+
</route>
12+
</routes>

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,5 +88,23 @@
8888
<serviceid="disallow_search_engine_index_response_listener"class="Symfony\Component\HttpKernel\EventListener\DisallowRobotsIndexingListener">
8989
<tagname="kernel.event_subscriber" />
9090
</service>
91+
92+
<serviceid="error_controller"class="Symfony\Bundle\FrameworkBundle\Controller\ErrorController"public="true">
93+
<argumenttype="service"id="error_renderer" />
94+
</service>
95+
96+
<serviceid="preview_error_controller"class="Symfony\Bundle\FrameworkBundle\Controller\PreviewErrorController"public="true">
97+
<argumenttype="service"id="http_kernel" />
98+
<argumenttype="service"id="error_renderer" />
99+
<argument>%kernel.error_controller%</argument>
100+
</service>
101+
102+
<serviceid="exception_listener"class="Symfony\Component\HttpKernel\EventListener\ExceptionListener">
103+
<tagname="kernel.event_subscriber" />
104+
<tagname="monolog.logger"channel="request" />
105+
<argument>%kernel.error_controller%</argument>
106+
<argumenttype="service"id="logger"on-invalid="null" />
107+
<argument>%kernel.debug%</argument>
108+
</service>
91109
</services>
92110
</container>

‎src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/ConfigurationTest.php‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,7 @@ class_exists(SemaphoreStore::class) && SemaphoreStore::isSupported() ? 'semaphor
371371
'dsn' =>'smtp://null',
372372
'enabled' => !class_exists(FullStack::class) &&class_exists(Mailer::class),
373373
],
374+
'error_controller' =>'error_controller',
374375
];
375376
}
376377
}

‎src/Symfony/Bundle/SecurityBundle/Tests/Functional/app/ExceptionController.php‎

Lines changed: 0 additions & 37 deletions
This file was deleted.

‎src/Symfony/Bundle/SecurityBundle/Tests/Functional/app/JsonLogin/bundles.php‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,5 @@
1212
return [
1313
newSymfony\Bundle\SecurityBundle\SecurityBundle(),
1414
newSymfony\Bundle\FrameworkBundle\FrameworkBundle(),
15-
newSymfony\Bundle\TwigBundle\TwigBundle(),
1615
newSymfony\Bundle\SecurityBundle\Tests\Functional\Bundle\JsonLoginBundle\JsonLoginBundle(),
1716
];

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp