Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
76.47% |
13 / 17 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
| DisableToolbarSubscriber | |
76.47% |
13 / 17 |
|
0.00% |
0 / 2 |
5.33 | |
0.00% |
0 / 1 |
| getSubscribedEvents | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| onKernelResponse | |
92.86% |
13 / 14 |
|
0.00% |
0 / 1 |
4.01 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\EventSubscriber; |
| 4 | |
| 5 | use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
| 6 | use Symfony\Component\HttpKernel\Event\ResponseEvent; |
| 7 | use Symfony\Component\HttpKernel\KernelEvents; |
| 8 | |
| 9 | class DisableToolbarSubscriber implements EventSubscriberInterface |
| 10 | { |
| 11 | private array $routesWithoutToolbar = [ |
| 12 | 'maxfield_export_mobile', |
| 13 | ]; |
| 14 | |
| 15 | public static function getSubscribedEvents(): array |
| 16 | { |
| 17 | return [ |
| 18 | KernelEvents::RESPONSE => ['onKernelResponse', 0], |
| 19 | ]; |
| 20 | } |
| 21 | |
| 22 | public function onKernelResponse(ResponseEvent $event): void |
| 23 | { |
| 24 | $request = $event->getRequest(); |
| 25 | $routeName = $request->attributes->get('_route'); |
| 26 | |
| 27 | if (in_array($routeName, $this->routesWithoutToolbar, true)) { |
| 28 | $response = $event->getResponse(); |
| 29 | $content = $response->getContent(); |
| 30 | |
| 31 | // More aggressive removal - find and remove everything from toolbar comment to end |
| 32 | $pattern = '/<!--\s*START of Symfony Web Debug Toolbar\s*-->.*?<!--\s*END of Symfony Web Debug Toolbar\s*-->/s'; |
| 33 | $content = preg_replace($pattern, '', $content); |
| 34 | |
| 35 | // Also remove any remaining sf-toolbar elements |
| 36 | $content = preg_replace('/<div[^>]*class="sf-toolbar[^"]*"[^>]*>.*?<\/div>\s*<\/div>/s', '', $content); |
| 37 | $content = preg_replace('/<link[^>]*href="[^"]*_wdt[^"]*"[^>]*\/?>/', '', $content); |
| 38 | |
| 39 | // Ensure proper closing tags |
| 40 | if (strpos($content, '</body></html>') === false) { |
| 41 | if (strpos($content, '</body>') === false) { |
| 42 | $content .= '</body></html>'; |
| 43 | } else { |
| 44 | $content = str_replace('</body>', '</body></html>', $content); |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | $response->setContent($content); |
| 49 | } |
| 50 | } |
| 51 | } |