1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace RewriteUrl\EventListeners; |
4
|
|
|
|
5
|
|
|
use RewriteUrl\Model\RewriteurlRule; |
6
|
|
|
use RewriteUrl\Model\RewriteurlRuleQuery; |
7
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
8
|
|
|
use Symfony\Component\HttpFoundation\RequestStack; |
9
|
|
|
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent; |
10
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
11
|
|
|
use Symfony\Component\HttpKernel\KernelEvents; |
12
|
|
|
use Thelia\Core\HttpKernel\Exception\RedirectException; |
13
|
|
|
use Thelia\Core\HttpFoundation\Request as TheliaRequest; |
14
|
|
|
use Thelia\Tools\URL; |
15
|
|
|
|
16
|
|
|
class KernelExceptionListener implements EventSubscriberInterface |
17
|
|
|
{ |
18
|
|
|
/** @var RequestStack */ |
19
|
|
|
private $requestStack; |
20
|
|
|
|
21
|
|
|
public function __construct(RequestStack $requestStack) |
22
|
|
|
{ |
23
|
|
|
$this->requestStack = $requestStack; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
|
27
|
|
|
public static function getSubscribedEvents() |
28
|
|
|
{ |
29
|
|
|
return [ |
30
|
|
|
KernelEvents::EXCEPTION => ['onKernelHttpNotFoundException', 300], |
31
|
|
|
]; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function onKernelHttpNotFoundException(GetResponseForExceptionEvent $event) |
35
|
|
|
{ |
36
|
|
|
if ($event->getException() instanceof NotFoundHttpException) { |
|
|
|
|
37
|
|
|
$urlTool = URL::getInstance(); |
38
|
|
|
|
39
|
|
|
$ruleCollection = RewriteurlRuleQuery::create() |
40
|
|
|
->filterByOnly404(1) |
41
|
|
|
->orderByPosition() |
42
|
|
|
->find(); |
43
|
|
|
|
44
|
|
|
$request = $this->requestStack->getCurrentRequest(); |
45
|
|
|
$pathInfo = $request instanceof TheliaRequest ? $request->getRealPathInfo() : $request->getPathInfo(); |
|
|
|
|
46
|
|
|
|
47
|
|
|
/** @var RewriteurlRule $rule */ |
48
|
|
|
foreach ($ruleCollection as $rule) { |
49
|
|
|
if ($rule->isMatching($pathInfo, $request->query->all())) { |
50
|
|
|
$event->setException(new RedirectException($urlTool->absoluteUrl($rule->getRedirectUrl()), 301)); |
51
|
|
|
return; |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
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.