Repository navigation
Expand file tree
/
Copy pathExceptionSubscriber.php
More file actions
138 lines (114 loc) · 4.05 KB
/
Copy pathExceptionSubscriber.php
File metadata and controls
138 lines (114 loc) · 4.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<?php
namespace SyncEngine\EventSubscriber;
use Doctrine\DBAL\Exception\DatabaseObjectNotFoundException;
use Doctrine\DBAL\Exception\InvalidFieldNameException;
use Doctrine\ORM\Persisters\PersisterException;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Http\RememberMe\RememberMeHandlerInterface;
use SyncEngine\Framework\ModuleRegistryManager;
use SyncEngine\Service\System;
class ExceptionSubscriber implements EventSubscriberInterface
{
private const MODULE_RECOVERY_PARAM = '_module_recovery';
private const MODULE_RECOVERY_MAX = 10;
public function __construct(
private readonly KernelInterface $kernel,
private readonly RequestStack $requestStack,
private readonly UrlGeneratorInterface $urlGenerator,
private readonly TokenStorageInterface $tokenStorage,
private readonly ModuleRegistryManager $moduleRegistryManager,
private readonly System $system,
private readonly ?RememberMeHandlerInterface $rememberMeService = null
) {}
public static function getSubscribedEvents(): array
{
return [
ExceptionEvent::class => [
['processException', 10],
//['logException', 0],
//['notifyException', -10],
],
];
}
public function processException( ExceptionEvent $event ): void
{
if ( $this->kernel->isDebug() ) {
return;
}
$request = $this->requestStack->getCurrentRequest();
if ( ! $request ) {
return;
}
$exception = $event->getThrowable();
if ( $exception instanceof \ErrorException ) {
$recoveryAttempt = (int) $request->query->get( self::MODULE_RECOVERY_PARAM, 0 );
if ( $recoveryAttempt < self::MODULE_RECOVERY_MAX ) {
$packages = $this->moduleRegistryManager->extractModulePackagesFromThrowable( $exception );
if ( $packages ) {
if ( 0 === $recoveryAttempt ) {
$this->system->runCommand( 'cache:clear', silent: false );
$event->allowCustomResponseCode();
$event->setResponse(
new RedirectResponse(
$this->urlGenerator->generate(
'syncengine_modules',
[ self::MODULE_RECOVERY_PARAM => 1 ]
)
)
);
return;
}
$this->moduleRegistryManager->appendDisabledModules(
$packages,
'Auto-disabled after runtime module exception: ' . $exception->getMessage()
);
$this->system->runCommand( 'cache:clear', silent: false );
if ( $request->hasSession() ) {
$request->getSession()->getFlashBag()->add(
'warning',
'Modules were auto-disabled after runtime errors: ' . implode( ', ', $packages )
);
}
$event->allowCustomResponseCode();
$event->setResponse(
new RedirectResponse(
$this->urlGenerator->generate(
'syncengine_modules',
[ self::MODULE_RECOVERY_PARAM => $recoveryAttempt + 1 ]
)
)
);
return;
}
}
}
if (
$exception instanceof DatabaseObjectNotFoundException ||
$exception instanceof PersisterException ||
$exception instanceof InvalidFieldNameException
) {
$currentRoute = $request->attributes->get( '_route' );
if ( 'syncengine_install_repair' === $currentRoute && str_contains( $exception->getMessage(), '.user' ) ) {
$this->tokenStorage->setToken( null );
$this->rememberMeService?->clearRememberMeCookie();
}
$error = $request->query->get( 'error' ) ?: $exception->getMessage();
$event->allowCustomResponseCode();
$event->setResponse(
new RedirectResponse( $this->urlGenerator->generate( 'syncengine_install_repair', [ 'error' => $error, 'redirect_to' => $request->getUri() ] ) )
);
}
}
public function logException( ExceptionEvent $event ): void {
// Nothing yet.
}
public function notifyException( ExceptionEvent $event ): void {
// Nothing yet.
}
}