]>
Raphaël G. Git Repositories - packbundle/blob - Filter/JPackFilter.php
   1 <?php 
declare(strict_types
=1); 
   4  * This file is part of the Rapsys PackBundle package. 
   6  * (c) Raphaël Gertz <symfony@rapsys.eu> 
   8  * For the full copyright and license information, please view the LICENSE 
   9  * file that was distributed with this source code. 
  12 namespace Rapsys\PackBundle\Filter
; 
  20 class JPackFilter 
implements FilterInterface 
{ 
  24         //Default compress type 
  27         //Twig template filename 
  36          * @xxx compress can be clean, shrink, obfuscate or best 
  38         public function __construct(Source 
$fileName, int $line, string $bin = 'jpack', string $compress = 'best') { 
  40                 $this->fileName 
= $fileName; 
  49                 $this->compress 
= $compress; 
  52                 if (!empty($this->compress
)) { 
  53                         //Append clean parameter 
  54                         if ($this->compress 
== 'clean') { 
  55                                 $this->bin 
.= ' --clean'; 
  56                         //Append shrink parameter 
  57                         } elseif ($this->compress 
== 'shrink') { 
  58                                 $this->bin 
.= ' --shrink'; 
  59                         //Append obfuscate parameter 
  60                         } elseif ($this->compress 
== 'obfuscate') { 
  61                                 $this->bin 
.= ' --obfuscate'; 
  62                         //Unknown compress type 
  63                         //XXX: default compression is best 
  64                         } elseif ($this->compress 
!== 'best') { 
  65                                 //Throw an error on unknown compress 
  66                                 throw new Error(sprintf('Got unexpected compress for %s: %s', $this->bin
, $this->compress
), $this->line
, $this->fileName
); 
  74         public function process(string $content): string { 
  76                 $descriptorSpec = array( 
  77                         0 => array('pipe', 'r'), 
  78                         1 => array('pipe', 'w'), 
  79                         2 => array('pipe', 'w') 
  83                 if (is_resource($proc = proc_open($this->bin
, $descriptorSpec, $pipes))) { 
  84                         //Set stderr as non blocking 
  85                         stream_set_blocking($pipes[2], false); 
  87                         //Send content to stdin 
  88                         fwrite($pipes[0], $content); 
  93                         //Read content from stdout 
  94                         if ($stdout = stream_get_contents($pipes[1])) { 
 101                         //Read content from stderr 
 102                         if (($stderr = stream_get_contents($pipes[2]))) { 
 103                                 throw new Error(sprintf('Got unexpected strerr for %s: %s', $this->bin
, $stderr), $this->line
, $this->fileName
); 
 110                         if (($ret = proc_close($proc))) { 
 111                                 throw new Error(sprintf('Got unexpected non zero return code %s: %d', $this->bin
, $ret), $this->line
, $this->fileName
);