3 namespace Rapsys\PackBundle\Twig\Filter
;
5 use Rapsys\PackBundle\Twig\Filter\FilterInterface
;
8 class CPackFilter
implements FilterInterface
{
12 //Default compress type
15 //Twig template filename
21 //Configure the object
22 //XXX: compress can be minify or pretty
23 public function __construct($fileName, $line, $bin = 'cpack', $compress = 'minify') {
25 $this->fileName
= $fileName;
34 $this->compress
= $compress;
37 if (!empty($this->compress
)) {
38 //Append minify parameter
39 if ($this->compress
== 'minify') {
40 $this->bin
.= ' --minify';
41 //Unknown compress type
42 //XXX: default compression is pretty
43 } elseif ($this->compress
!== 'pretty') {
44 //Throw an error on unknown compress
45 throw new Error(sprintf('Got unexpected compress for %s: %s', $this->bin
, $this->compress
), $this->line
, $this->fileName
);
50 public function process($content) {
52 $descriptorSpec = array(
53 0 => array('pipe', 'r'),
54 1 => array('pipe', 'w'),
55 2 => array('pipe', 'w')
59 if (is_resource($proc = proc_open($this->bin
, $descriptorSpec, $pipes))) {
60 //Set stderr as non blocking
61 stream_set_blocking($pipes[2], 0);
63 //Send content to stdin
64 fwrite($pipes[0], $content);
69 //Read content from stdout
70 if ($stdout = stream_get_contents($pipes[1])) {
77 //Read content from stderr
78 if (($stderr = stream_get_contents($pipes[2]))) {
79 throw new Error(sprintf('Got unexpected strerr for %s: %s', $this->bin
, $stderr), $this->line
, $this->fileName
);
86 if (($ret = proc_close($proc))) {
87 throw new Error(sprintf('Got unexpected non zero return code %s: %d', $this->bin
, $ret), $this->line
, $this->fileName
);