Skip to content

Instantly share code, notes, and snippets.

@stof
Created July 3, 2018 14:21
Show Gist options
  • Save stof/c57666855c540e9f85ec04bdad76fa66 to your computer and use it in GitHub Desktop.
Save stof/c57666855c540e9f85ec04bdad76fa66 to your computer and use it in GitHub Desktop.
Newrelic with CSP
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<{# lots of stuff #}
</head>
<body>
{% block body %}{% endblock %}
{% if has_newrelic() %}
<script nonce="{{ csp_nonce('script') }}">{{ get_newrelic_js() }}</script>
{% endif %}
{% block javascripts '' %}
</body>
</html>
<?php
namespace Incenteev\WebBundle\Twig;
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;
class NewrelicExtension extends AbstractExtension
{
public function getFunctions()
{
$checkerFunction = new TwigFunction('has_newrelic', 'extension_loaded');
$checkerFunction->setArguments(array('newrelic'));
return array(
$checkerFunction,
new TwigFunction('get_newrelic_js', array($this, 'getNewrelicJs'), array('is_safe' => array('html'))),
);
}
public function getNewrelicJs()
{
if (!\extension_loaded('newrelic')) {
return '';
}
newrelic_disable_autorum();
return newrelic_get_browser_timing_header(false).newrelic_get_browser_timing_footer(false);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment