Created
July 26, 2011 21:38
-
-
Save timewasted/1108126 to your computer and use it in GitHub Desktop.
Assetic gzip filter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Assetic\Filter; | |
use Assetic\Asset\AssetInterface; | |
use Assetic\Util\ProcessBuilder; | |
/** | |
* Runs assets through gzip. | |
*/ | |
class GzipFilter implements FilterInterface | |
{ | |
private $gzipBin; | |
private $compression; | |
/** | |
* Constructor. | |
* | |
* @param string $gzipBin Path to the gzip binary | |
*/ | |
public function __construct($gzipBin = '/usr/local/bin') | |
{ | |
$this->gzipBin = $gzipBin; | |
} | |
public function setCompression($compression) | |
{ | |
if( !is_numeric($compression) || (int)$compression < 1 || (int)$compression > 9 ) | |
throw new \RuntimeException('Compression must be an integer between 1 and 9, inclusive.'); | |
$this->compression = $compression; | |
} | |
public function filterLoad(AssetInterface $asset) | |
{ | |
} | |
public function filterDump(AssetInterface $asset) | |
{ | |
$pb = new ProcessBuilder(); | |
$pb | |
->inheritEnvironmentVariables() | |
->add($this->gzipBin); | |
; | |
if (null !== $this->compression) { | |
$pb->add('-' . $this->compression); | |
} | |
$pb->add('--no-name'); | |
$pb->add($input = tempnam(sys_get_temp_dir(), 'assetic_gzip')); | |
file_put_contents($input, $asset->getContent()); | |
$proc = $pb->getProcess(); | |
$code = $proc->run(); | |
if (0 < $code) { | |
unlink($input); | |
throw new \RuntimeException($proc->getErrorOutput()); | |
} | |
$asset->setContent(file_get_contents($input . '.gz')); | |
unlink($input . '.gz'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment