3 namespace Rapsys\PackBundle\Twig\Filter
; 
   5 use Rapsys\PackBundle\Twig\Filter\FilterInterface
; 
   8 class JPackFilter 
implements FilterInterface 
{ 
  12         //Default compress type 
  15         //Twig template filename 
  21         //Configure the object 
  22         //XXX: can be clean, shrink, obfuscate or best 
  23         public function __construct($fileName, $line, $bin = 'jpack', $compress = 'best') { 
  25                 $this->fileName 
= $fileName; 
  34                 $this->compress 
= $compress; 
  37                 if (!empty($this->compress
)) { 
  38                         //Append clean parameter 
  39                         if ($this->compress 
== 'clean') { 
  40                                 $this->bin 
.= ' --clean'; 
  41                         //Append shrink parameter 
  42                         } elseif ($this->compress 
== 'shrink') { 
  43                                 $this->bin 
.= ' --shrink'; 
  44                         //Append obfuscate parameter 
  45                         } elseif ($this->compress 
== 'obfuscate') { 
  46                                 $this->bin 
.= ' --obfuscate'; 
  47                         //Unknown compress type 
  48                         //XXX: default compression is best 
  49                         } elseif ($this->compress 
!== 'best') { 
  50                                 //Throw an error on unknown compress 
  51                                 throw new Error(sprintf('Got unexpected compress for %s: %s', $this->bin
, $this->compress
), $this->line
, $this->fileName
); 
  56         public function process($content) { 
  58                 $descriptorSpec = array( 
  59                         0 => array('pipe', 'r'), 
  60                         1 => array('pipe', 'w'), 
  61                         2 => array('pipe', 'w') 
  65                 if (is_resource($proc = proc_open($this->bin
, $descriptorSpec, $pipes))) { 
  66                         //Set stderr as non blocking 
  67                         stream_set_blocking($pipes[2], 0); 
  69                         //Send content to stdin 
  70                         fwrite($pipes[0], $content); 
  75                         //Read content from stdout 
  76                         if ($stdout = stream_get_contents($pipes[1])) { 
  83                         //Read content from stderr 
  84                         if (($stderr = stream_get_contents($pipes[2]))) { 
  85                                 throw new Error(sprintf('Got unexpected strerr for %s: %s', $this->bin
, $stderr), $this->line
, $this->fileName
); 
  92                         if (($ret = proc_close($proc))) { 
  93                                 throw new Error(sprintf('Got unexpected non zero return code %s: %d', $this->bin
, $ret), $this->line
, $this->fileName
);