Skip to content

Commit ee74fcc

Browse files
committed
Merge branch 'ACP2E-3183' of https://github.com/adobe-commerce-tier-4/magento2ce into PR-08-29-2024
2 parents 3c6c9a5 + 18b8133 commit ee74fcc

File tree

11 files changed

+371
-0
lines changed

11 files changed

+371
-0
lines changed

app/code/Magento/Backend/view/adminhtml/layout/default.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
<body>
1616
<attribute name="id" value="html-body"/>
1717
<block name="require.js" class="Magento\Backend\Block\Page\RequireJs" template="Magento_Backend::page/js/require_js.phtml"/>
18+
<block class="Magento\Framework\View\Element\Template" name="head.critical" as="head.critical" template="Magento_Backend::page/container.phtml"/>
1819
<block class="Magento\Framework\View\Element\Template" name="head.additional" template="Magento_Backend::page/container.phtml"/>
1920
<referenceContainer name="global.notices">
2021
<block class="Magento\Backend\Block\Page\Notices" name="global_notices" as="global_notices" template="Magento_Backend::page/notices.phtml"/>

app/code/Magento/NewRelicReporting/Model/NewRelicWrapper.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
class NewRelicWrapper
1616
{
1717
private const NEWRELIC_APPNAME = 'newrelic.appname';
18+
private const NEWRELIC_AUTO_INSTRUMENT = 'newrelic.browser_monitoring.auto_instrument';
1819

1920
/**
2021
* Wrapper for 'newrelic_add_custom_parameter' function
@@ -106,4 +107,58 @@ public function isExtensionInstalled()
106107
{
107108
return extension_loaded('newrelic');
108109
}
110+
111+
/**
112+
* Checks whether automatic injection of the browser monitoring is enabled
113+
*
114+
* @return bool
115+
*/
116+
public function isAutoInstrumentEnabled(): bool
117+
{
118+
return $this->isExtensionInstalled() && ini_get(self::NEWRELIC_AUTO_INSTRUMENT);
119+
}
120+
121+
/**
122+
* Wrapper for 'newrelic_disable_autorum'
123+
*
124+
* @return bool|null
125+
*/
126+
public function disableAutorum(): ?bool
127+
{
128+
if (!$this->isExtensionInstalled()) {
129+
return null;
130+
}
131+
132+
return newrelic_disable_autorum();
133+
}
134+
135+
/**
136+
* Wrapper for 'newrelic_get_browser_timing_header'
137+
*
138+
* @param bool $includeTags
139+
* @return string|null
140+
*/
141+
public function getBrowserTimingHeader(bool $includeTags = true): ?string
142+
{
143+
if (!$this->isExtensionInstalled()) {
144+
return null;
145+
}
146+
147+
return newrelic_get_browser_timing_header($includeTags);
148+
}
149+
150+
/**
151+
* Wrapper for 'newrelic_get_browser_timing_footer'
152+
*
153+
* @param bool $includeTags
154+
* @return string|null
155+
*/
156+
public function getBrowserTimingFooter(bool $includeTags = true): ?string
157+
{
158+
if (!$this->isExtensionInstalled()) {
159+
return null;
160+
}
161+
162+
return newrelic_get_browser_timing_footer($includeTags);
163+
}
109164
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
/**
3+
* Copyright 2024 Adobe
4+
* All Rights Reserved.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\NewRelicReporting\Observer;
9+
10+
use Magento\Framework\Event\Observer;
11+
use Magento\Framework\Event\ObserverInterface;
12+
use Magento\NewRelicReporting\Model\NewRelicWrapper;
13+
14+
class DisableBrowserMonitoringAutomaticInjection implements ObserverInterface
15+
{
16+
/**
17+
* @param NewRelicWrapper $newRelicWrapper
18+
*/
19+
public function __construct(
20+
private readonly NewRelicWrapper $newRelicWrapper
21+
) {
22+
}
23+
24+
/**
25+
* Disables PHP agent's automatic injection of the browser monitoring in favor of manual injection
26+
*
27+
* New Relic's PHP agent does not support adding nonce attribute to the auto-injected scripts.
28+
* Thus, these scripts are now included out of box in the Http Response for compliance with CSP.
29+
*
30+
* @param Observer $observer
31+
* @return void
32+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
33+
*/
34+
public function execute(Observer $observer)
35+
{
36+
if ($this->newRelicWrapper->isAutoInstrumentEnabled()) {
37+
$this->newRelicWrapper->disableAutorum();
38+
}
39+
}
40+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
/**
3+
* Copyright 2024 Adobe
4+
* All Rights Reserved.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\NewRelicReporting\ViewModel;
9+
10+
use Magento\Framework\View\Element\Block\ArgumentInterface;
11+
use Magento\NewRelicReporting\Model\NewRelicWrapper;
12+
13+
class BrowserMonitoringFooterJs implements ArgumentInterface, ContentProviderInterface
14+
{
15+
/**
16+
* @param NewRelicWrapper $newRelicWrapper
17+
*/
18+
public function __construct(
19+
private readonly NewRelicWrapper $newRelicWrapper
20+
) {
21+
}
22+
23+
/**
24+
* @inheritDoc
25+
*/
26+
public function getContent(): ?string
27+
{
28+
return $this->newRelicWrapper->isAutoInstrumentEnabled()
29+
? $this->newRelicWrapper->getBrowserTimingFooter(false)
30+
: null;
31+
}
32+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
/**
3+
* Copyright 2024 Adobe
4+
* All Rights Reserved.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\NewRelicReporting\ViewModel;
9+
10+
use Magento\Framework\View\Element\Block\ArgumentInterface;
11+
use Magento\NewRelicReporting\Model\NewRelicWrapper;
12+
13+
class BrowserMonitoringHeaderJs implements ArgumentInterface, ContentProviderInterface
14+
{
15+
/**
16+
* @param NewRelicWrapper $newRelicWrapper
17+
*/
18+
public function __construct(
19+
private readonly NewRelicWrapper $newRelicWrapper
20+
) {
21+
}
22+
23+
/**
24+
* @inheritDoc
25+
*/
26+
public function getContent(): ?string
27+
{
28+
return $this->newRelicWrapper->isAutoInstrumentEnabled()
29+
? $this->newRelicWrapper->getBrowserTimingHeader(false)
30+
: null;
31+
}
32+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
/**
3+
* Copyright 2024 Adobe
4+
* All Rights Reserved.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\NewRelicReporting\ViewModel;
9+
10+
interface ContentProviderInterface
11+
{
12+
/**
13+
* Get content
14+
*
15+
* @return string|null
16+
*/
17+
public function getContent(): ?string;
18+
}

app/code/Magento/NewRelicReporting/etc/adminhtml/events.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,7 @@
2525
<event name="admin_system_config_changed_section_newrelicreporting">
2626
<observer name="newrelicreporting_observer_check_config" instance="Magento\NewRelicReporting\Model\Observer\CheckConfig"/>
2727
</event>
28+
<event name="controller_front_send_response_before">
29+
<observer name="newrelicreporting_observer_controller_front_send_response_before" instance="Magento\NewRelicReporting\Observer\DisableBrowserMonitoringAutomaticInjection" />
30+
</event>
2831
</config>

app/code/Magento/NewRelicReporting/etc/frontend/events.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,7 @@
1010
<observer name="newrelicreporting_observer_report_concurrent_users" instance="Magento\NewRelicReporting\Model\Observer\ReportConcurrentUsers" />
1111
<observer name="newrelicreporting_newrelic_report_concurrent_users" instance="Magento\NewRelicReporting\Model\Observer\ReportConcurrentUsersToNewRelic" />
1212
</event>
13+
<event name="controller_front_send_response_before">
14+
<observer name="newrelicreporting_observer_controller_front_send_response_before" instance="Magento\NewRelicReporting\Observer\DisableBrowserMonitoringAutomaticInjection" />
15+
</event>
1316
</config>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
/**
4+
* Copyright 2024 Adobe
5+
* All Rights Reserved.
6+
*/
7+
-->
8+
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
9+
<body>
10+
<referenceBlock name="head.critical">
11+
<block class="Magento\Framework\View\Element\Template" name="newrelicreporting.browser_monitoring.script.header" before="-" template="Magento_NewRelicReporting::html/inline_js.phtml">
12+
<arguments>
13+
<argument name="content_provider" xsi:type="object">
14+
Magento\NewRelicReporting\ViewModel\BrowserMonitoringHeaderJs
15+
</argument>
16+
</arguments>
17+
</block>
18+
</referenceBlock>
19+
<referenceContainer name="root">
20+
<block class="Magento\Framework\View\Element\Template" name="newrelicreporting.browser_monitoring.script.footer" after="-" template="Magento_NewRelicReporting::html/inline_js.phtml">
21+
<arguments>
22+
<argument name="content_provider" xsi:type="object">
23+
Magento\NewRelicReporting\ViewModel\BrowserMonitoringFooterJs
24+
</argument>
25+
</arguments>
26+
</block>
27+
</referenceContainer>
28+
</body>
29+
</page>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
/**
3+
* Copyright 2024 Adobe
4+
* All Rights Reserved.
5+
*/
6+
7+
/**
8+
* @var \Magento\Framework\View\Element\Template $block
9+
* @var \Magento\Framework\View\Helper\SecureHtmlRenderer $secureRenderer
10+
* @var \Magento\NewRelicReporting\ViewModel\ContentProviderInterface $contentProvider
11+
*/
12+
13+
$contentProvider = $block->getContentProvider();
14+
$content = $contentProvider->getContent();
15+
?>
16+
<?php if ($content): ?>
17+
<?= /* @noEscape */ $secureRenderer->renderTag('script', [], $content, false) ?>
18+
<?php endif; ?>

0 commit comments

Comments
 (0)