1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the FOSRestBundle package. |
5
|
|
|
* |
6
|
|
|
* (c) FriendsOfSymfony <http://friendsofsymfony.github.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 FOS\RestBundle\EventListener; |
13
|
|
|
|
14
|
|
|
use FOS\RestBundle\FOSRestBundle; |
15
|
|
|
use Psr\Log\LoggerInterface; |
16
|
|
|
use Symfony\Component\Debug\Exception\FlattenException as LegacyFlattenException; |
17
|
|
|
use Symfony\Component\ErrorRenderer\Exception\FlattenException; |
18
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
19
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
20
|
|
|
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent; |
21
|
|
|
use Symfony\Component\HttpKernel\Event\KernelEvent; |
22
|
|
|
use Symfony\Component\HttpKernel\EventListener\ExceptionListener as HttpKernelExceptionListener; |
23
|
|
|
use Symfony\Component\HttpKernel\KernelEvents; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* ExceptionListener. |
27
|
|
|
* |
28
|
|
|
* @author Ener-Getick <[email protected]> |
29
|
|
|
* |
30
|
|
|
* @internal |
31
|
|
|
*/ |
32
|
|
|
class ExceptionListener implements EventSubscriberInterface |
33
|
10 |
|
{ |
34
|
|
|
private $exceptionListener; |
35
|
10 |
|
private $dispatcher; |
36
|
|
|
|
37
|
10 |
|
public function __construct($controller, ?LoggerInterface $logger, EventDispatcherInterface $dispatcher) |
38
|
1 |
|
{ |
39
|
|
|
$this->exceptionListener = new HttpKernelExceptionListener($controller, $logger); |
40
|
|
|
$this->dispatcher = $dispatcher; |
41
|
9 |
|
} |
42
|
9 |
|
|
43
|
|
|
public function onKernelException(GetResponseForExceptionEvent $event) |
44
|
|
|
{ |
45
|
|
|
$request = $event->getRequest(); |
46
|
|
|
|
47
|
16 |
|
if (!$request->attributes->get(FOSRestBundle::ZONE_ATTRIBUTE, true)) { |
48
|
|
|
return; |
49
|
|
|
} |
50
|
16 |
|
|
51
|
16 |
|
$exception = $event->getException(); |
52
|
|
|
$requestListener = function (KernelEvent $event) use (&$requestListener, $exception) { |
53
|
|
|
$request = $event->getRequest(); |
54
|
|
|
|
55
|
|
|
if (!$event->isMasterRequest() && ($request->attributes->get('exception') instanceof FlattenException || $request->attributes->get('exception') instanceof LegacyFlattenException)) { |
|
|
|
|
56
|
|
|
$request->attributes->set('exception', $exception); |
57
|
9 |
|
} |
58
|
|
|
|
59
|
|
|
$this->dispatcher->removeListener(KernelEvents::REQUEST, $requestListener); |
60
|
9 |
|
}; |
61
|
9 |
|
$this->dispatcher->addListener(KernelEvents::REQUEST, $requestListener); |
62
|
9 |
|
|
63
|
9 |
|
$this->exceptionListener->onKernelException($event); |
64
|
9 |
|
} |
65
|
9 |
|
|
66
|
|
|
/** |
67
|
9 |
|
* {@inheritdoc} |
68
|
|
|
*/ |
69
|
|
|
public static function getSubscribedEvents() |
70
|
|
|
{ |
71
|
|
|
return array( |
72
|
|
|
KernelEvents::EXCEPTION => array('onKernelException', -100), |
73
|
|
|
); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
This error could be the result of:
1. Missing dependencies
PHP Analyzer uses your
composer.json
file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects thecomposer.json
to be in the root folder of your repository.Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the
require
orrequire-dev
section?2. Missing use statement
PHP does not complain about undefined classes in
ìnstanceof
checks. For example, the following PHP code will work perfectly fine:If you have not tested against this specific condition, such errors might go unnoticed.