Issues (32)

Listener/ResponseListener.php (2 issues)

1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of Ekino New Relic bundle.
7
 *
8
 * (c) Ekino - Thomas Rabaix <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Ekino\NewRelicBundle\Listener;
15
16
use Ekino\NewRelicBundle\NewRelic\Config;
17
use Ekino\NewRelicBundle\NewRelic\NewRelicInteractorInterface;
18
use Ekino\NewRelicBundle\Twig\NewRelicExtension;
19
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
20
use Symfony\Component\HttpFoundation\StreamedResponse;
21
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
0 ignored issues
show
The type Symfony\Component\HttpKe...ent\FilterResponseEvent was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
22
use Symfony\Component\HttpKernel\Event\ResponseEvent;
23
use Symfony\Component\HttpKernel\KernelEvents;
24
25
class ResponseListener implements EventSubscriberInterface
26
{
27
    private $newRelic;
28
    private $interactor;
29
    private $instrument;
30
    private $symfonyCache;
31
    private $newRelicTwigExtension;
32
33
    public function __construct(
34
        Config $newRelic,
35
        NewRelicInteractorInterface $interactor,
36
        bool $instrument = false,
37
        bool $symfonyCache = false,
38
        NewRelicExtension $newRelicTwigExtension = null
39
    ) {
40
        $this->newRelic = $newRelic;
41
        $this->interactor = $interactor;
42
        $this->instrument = $instrument;
43
        $this->symfonyCache = $symfonyCache;
44
        $this->newRelicTwigExtension = $newRelicTwigExtension;
45
    }
46
47
    public static function getSubscribedEvents(): array
48
    {
49
        return [
50
            KernelEvents::RESPONSE => [
51
                ['onKernelResponse', -255],
52
            ],
53
        ];
54
    }
55
56
    public function onKernelResponse(KernelResponseEvent $event): void
0 ignored issues
show
The type Ekino\NewRelicBundle\Listener\KernelResponseEvent was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
57
    {
58
        if (!$event->isMasterRequest()) {
59
            return;
60
        }
61
62
        if (null === $this->newRelicTwigExtension || false === $this->newRelicTwigExtension->isUsed()) {
63
            foreach ($this->newRelic->getCustomMetrics() as $name => $value) {
64
                $this->interactor->addCustomMetric((string) $name, (float) $value);
65
            }
66
67
            foreach ($this->newRelic->getCustomParameters() as $name => $value) {
68
                $this->interactor->addCustomParameter((string) $name, $value);
69
            }
70
        }
71
72
        foreach ($this->newRelic->getCustomEvents() as $name => $events) {
73
            foreach ($events as $attributes) {
74
                $this->interactor->addCustomEvent((string) $name, $attributes);
75
            }
76
        }
77
78
        if ($this->instrument) {
79
            if (null === $this->newRelicTwigExtension || false === $this->newRelicTwigExtension->isUsed()) {
80
                $this->interactor->disableAutoRUM();
81
            }
82
83
            // Some requests might not want to get instrumented
84
            if ($event->getRequest()->attributes->get('_instrument', true)) {
85
                $response = $event->getResponse();
86
87
                // We can only instrument HTML responses
88
                if (!$response instanceof StreamedResponse
89
                    && 'text/html' === \substr($response->headers->get('Content-Type', ''), 0, 9)
90
                ) {
91
                    $responseContent = $response->getContent();
92
                    $response->setContent(''); // free the memory
93
94
                    if (null === $this->newRelicTwigExtension || false === $this->newRelicTwigExtension->isHeaderCalled()) {
95
                        $responseContent = \preg_replace('|<head>|i', '$0'.$this->interactor->getBrowserTimingHeader(), $responseContent);
96
                    }
97
98
                    if (null === $this->newRelicTwigExtension || false === $this->newRelicTwigExtension->isFooterCalled()) {
99
                        $responseContent = \preg_replace('|</body>|i', $this->interactor->getBrowserTimingFooter().'$0', $responseContent);
100
                    }
101
102
                    $response->setContent($responseContent);
103
                }
104
            }
105
        }
106
107
        if ($this->symfonyCache) {
108
            $this->interactor->endTransaction();
109
        }
110
    }
111
}
112
113
if (!\class_exists(KernelResponseEvent::class)) {
114
    if (\class_exists(ResponseEvent::class)) {
115
        \class_alias(ResponseEvent::class, KernelResponseEvent::class);
116
    } else {
117
        \class_alias(FilterResponseEvent::class, KernelResponseEvent::class);
118
    }
119
}
120