Skip to content

Commit

Permalink
Merge pull request #5 from Becklyn/extract-trackjs
Browse files Browse the repository at this point in the history
Extract trackjs embed
  • Loading branch information
keichinger authored Jan 17, 2019
2 parents 105fa0c + 34fa9ce commit ddacb12
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 81 deletions.
3 changes: 1 addition & 2 deletions src/Resources/config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ services:
Becklyn\Hosting\Project\ProjectVersion:
$cachePool: '@cache.app'


Becklyn\Hosting\Twig\MonitoringTwigExtension:
Becklyn\Hosting\TrackJS\TrackJSEmbed:
$assetHelper: '@?Becklyn\AssetsBundle\Helper\AssetHelper'
$packages: '@?Symfony\Component\Asset\Packages'
$environment: '%kernel.environment%'
Expand Down
100 changes: 100 additions & 0 deletions src/TrackJS/TrackJSEmbed.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?php declare(strict_types=1);

namespace Becklyn\Hosting\TrackJS;

use Becklyn\AssetsBundle\Helper\AssetHelper;
use Becklyn\Hosting\Config\HostingConfig;
use Becklyn\Hosting\Exception\AssetIntegrationFailedException;
use Symfony\Component\Asset\Packages;


class TrackJSEmbed
{
/**
* @var HostingConfig
*/
private $hostingConfig;

/**
* @var string
*/
private $environment;

/**
* @var string
*/
private $isDebug;

/**
* @var AssetHelper|null
*/
private $assetHelper;

/**
* @var Packages|null
*/
private $packages;


/**
* @param HostingConfig $hostingConfig
* @param AssetHelper|null $assetHelper
* @param Packages|null $packages
* @param string $environment
* @param string $isDebug
*/
public function __construct (
HostingConfig $hostingConfig,
?AssetHelper $assetHelper,
?Packages $packages,
string $environment,
string $isDebug
)
{
$this->hostingConfig = $hostingConfig;
$this->assetHelper = $assetHelper;
$this->packages = $packages;
$this->environment = $environment;
$this->isDebug = $isDebug;
}


/**
* Returns the embed HTML for TrackJS
*
* @return string
*/
public function getEmbedHtml () : string
{
if (null === $this->assetHelper && null === $this->packages)
{
throw new AssetIntegrationFailedException("No asset integration extension found. Please either install `becklyn/assets-bundle` or `symfony/asset` to use this bundle.");
}

$trackJsToken = $this->hostingConfig->getTrackJsToken();

// only embed if token is set, in production and not in debug
if (null === $trackJsToken || $this->isDebug || "prod" !== $this->environment)
{
return "";
}

$assetUrl = null !== $this->assetHelper
? $this->assetHelper->getUrl("@hosting/js/trackjs.js")
: $this->packages->getUrl("bundles/becklynhosting/js/trackjs.js");

return \sprintf(
'<script src="%s"></script><script>window.TrackJS && TrackJS.install(%s)</script>',
$assetUrl,
\json_encode([
"token" => $trackJsToken,
"application" => $this->hostingConfig->getProjectName(),
"version" => $this->hostingConfig->getGitCommit(),
"console" => [
"display" => false,
],
])
);

}
}
86 changes: 7 additions & 79 deletions src/Twig/MonitoringTwigExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,105 +2,33 @@

namespace Becklyn\Hosting\Twig;

use Becklyn\AssetsBundle\Helper\AssetHelper;
use Becklyn\Hosting\Config\HostingConfig;
use Symfony\Component\Asset\Packages;
use Becklyn\Hosting\TrackJS\TrackJSEmbed;


class MonitoringTwigExtension extends \Twig_Extension
{
/**
* @var HostingConfig
* @var TrackJSEmbed
*/
private $hostingConfig;
private $trackJSEmbed;

/**
* @var string
*/
private $environment;

/**
* @var string
*/
private $isDebug;

/**
* @var AssetHelper|null
* @param TrackJSEmbed $trackJSEmbed
*/
private $assetHelper;

/**
* @var Packages|null
*/
private $packages;


/**
* @param HostingConfig $hostingConfig
* @param AssetHelper|null $assetHelper
* @param Packages|null $packages
* @param string $environment
* @param string $isDebug
*/
public function __construct (
HostingConfig $hostingConfig,
?AssetHelper $assetHelper,
?Packages $packages,
string $environment,
string $isDebug
)
public function __construct (TrackJSEmbed $trackJSEmbed)
{
$this->hostingConfig = $hostingConfig;
$this->assetHelper = $assetHelper;
$this->packages = $packages;
$this->environment = $environment;
$this->isDebug = $isDebug;
$this->trackJSEmbed = $trackJSEmbed;
}


/**
* @return string
*/
public function embedMonitoring () : string
{
if (null === $this->assetHelper && null === $this->packages)
{
throw new AssetIntegrationFailedException("No asset integration extension found. Please either install `becklyn/assets-bundle` or `symfony/asset` to use this bundle.");
}

$trackJsToken = $this->hostingConfig->getTrackJsToken();

// only embed if token is set, in production and not in debug
if (null === $trackJsToken || $this->isDebug || "prod" !== $this->environment)
{
return "";
}

$assetUrl = null !== $this->assetHelper
? $this->assetHelper->getUrl("@hosting/js/trackjs.js")
: $this->packages->getUrl("bundles/becklynhosting/js/trackjs.js");

return \sprintf(
'<script src="%s"></script><script>window.TrackJS && TrackJS.install(%s)</script>',
$assetUrl,
\json_encode([
"token" => $trackJsToken,
"application" => $this->hostingConfig->getProjectName(),
"version" => $this->hostingConfig->getGitCommit(),
"console" => [
"display" => false,
],
])
);
}

/**
* @inheritdoc
*/
public function getFunctions () : iterable
{
return [
new \Twig_Function("hosting_embed_monitoring", [$this, "embedMonitoring"], ["is_safe" => ["html"]]),
new \Twig_Function("hosting_embed_monitoring", [$this->trackJSEmbed, "getEmbedHtml"], ["is_safe" => ["html"]]),
];
}
}

0 comments on commit ddacb12

Please sign in to comment.