Skip to content

Instantly share code, notes, and snippets.

@bwiggs
Last active December 18, 2015 15:49
Show Gist options
  • Save bwiggs/5806810 to your computer and use it in GitHub Desktop.
Save bwiggs/5806810 to your computer and use it in GitHub Desktop.
<?php
namespace CI\Twig;
use Silex\Application;
class AssetTwigExtension extends \Twig_Extension
{
private $CDN = null;
private $VERSION = null;
/**
* Asset_Twig_Extension Constructor
*
* @param String $version The version to use for assets
* @param String $cdnHost The CDN hostname to use, do not include the protocol.
*/
public function __construct($version = null, $cdnHost = null)
{
if($version) {
$this->VERSION = $version;
}
if($cdnHost) {
$this->CDN = $cdnHost;
}
}
/**
* Returns the Twig Extension name
*
* @return string
*/
public function getName()
{
return 'asset';
}
/**
* Returns the functions for the Twig Extension
* @return array
*/
public function getFunctions()
{
return array(
'asset' => new \Twig_Function_Method($this, 'getAssetURL'),
'assets_version' => new \Twig_Function_Method($this, 'getAssetsVersion'),
);
}
/**
* Returns the asset with either the CDN, query string, or both attached
* @param $asset
*
* @return string
*/
public function getAssetURL($asset, $setCDN = true)
{
if(! empty($this->VERSION)) {
$parts = explode('.', $asset);
$extension = array_pop($parts);
$filename = implode('.', $parts);
// set the cache busting query string
$asset = "{$filename}.{$this->VERSION}.{$extension}";
}
// set the CDN host
if($setCDN && !empty($this->CDN)) {
$asset = "//{$this->CDN}{$asset}";
}
return $asset;
}
}
<?php
namespace CI\Tests;
use CI\Twig\AssetTwigExtension as TwigAssetExtension;
use PHPUnit_Framework_TestCase;
class AssetTwigExtension_Test extends PHPUnit_Framework_TestCase
{
/**
* Tests that the Version is set correctly when passed into the constructor
* without a CDN.
* @group Twig
*/
public function testConstructorSetVersionOnly()
{
$version = 12345;
$extension = new TwigAssetExtension($version);
$this->assertAttributeEquals(
$version,
'VERSION',
$extension
);
$this->assertAttributeEquals(
null,
'CDN',
$extension
);
}
/**
* Tests that the CDN is set correctly when passed into the constructor
* without the version.
* @group Twig
*/
public function testConstructorSetCDNOnly()
{
$cdn = 'cdn.example.com';
$extension = new TwigAssetExtension(null, $cdn);
$this->assertAttributeEquals(
$cdn,
'CDN',
$extension
);
$this->assertAttributeEquals(
null,
'VERSION',
$extension
);
}
/**
* Tests that the constructor sets the Version and CDN when provided.
* @group Twig
*/
public function testConstructorSetVersionAndCDN()
{
$version = 12345;
$cdn = 'cdn.example.com';
$extension = new TwigAssetExtension($version, $cdn);
$this->assertAttributeEquals(
$cdn,
'CDN',
$extension
);
$this->assertAttributeEquals(
$version,
'VERSION',
$extension
);
}
/**
* Tests if the getAssetUrl method doesn't modify the asset if no CDN or version.
* @group Twig
*/
public function testGetAssetURLWithoutVersionOrCDN()
{
$extension = new TwigAssetExtension();
$asset = '/path/to/script.js';
$this->assertEquals(
"/path/to/script.js",
$extension->getAssetUrl($asset)
);
}
/**
* Tests if the getAssetUrl method adds a version to the asset.
* @group Twig
*/
public function testGetAssetURLGeneratesVersion()
{
$version = 12345;
$extension = new TwigAssetExtension($version, null);
$asset = '/path/to/script.js';
$this->assertEquals(
"/path/to/script.{$version}.js",
$extension->getAssetUrl($asset)
);
$asset = '/path/to/jQuery.selectBox.min.js';
$this->assertEquals(
"/path/to/jQuery.selectBox.min.{$version}.js",
$extension->getAssetUrl($asset)
);
}
/**
* Tests if the getAssetUrl method adds a CDN to the asset.
* @group Twig
*/
public function testGetAssetURLGeneratesCDN()
{
$cdn = 'cdn.example.com';
$extension = new TwigAssetExtension(null, $cdn);
$asset = '/path/to/script.js';
$this->assertEquals(
"//{$cdn}/path/to/script.js",
$extension->getAssetUrl($asset)
);
}
/**
* Tests if the getAssetUrl method will add a CDN and Version to the asset
* @group Twig
*/
public function testGetAssetURLGeneratesVersionAndCDN()
{
$version = 12345;
$cdn = 'cdn.example.com';
$extension = new TwigAssetExtension($version, $cdn);
$asset = '/path/to/script.js';
$this->assertEquals(
"//{$cdn}/path/to/script.{$version}.js",
$extension->getAssetUrl($asset)
);
}
/**
* Tests if the getAssetUrl can disable the use of a CDN.
* @group Twig
*/
public function testGetAssetURLCanOmitCDN()
{
$version = 12345;
$cdn = 'cdn.example.com';
$extension = new TwigAssetExtension($version, $cdn);
$asset = '/path/to/script.js';
$this->assertEquals(
"/path/to/script.{$version}.js",
$extension->getAssetUrl($asset, false)
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment