]>
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          * @xxx compress can be clean, shrink, obfuscate or best 
  26         public function __construct(protected Source 
$fileName, protected int $line, protected string $bin = 'jpack', protected string $compress = 'best') { 
  28                 if (!empty($this->compress
)) { 
  29                         //Append clean parameter 
  30                         if ($this->compress 
== 'clean') { 
  31                                 //TODO: protect binary call by converting bin to array ? 
  32                                 $this->bin 
.= ' --clean'; 
  33                         //Append shrink parameter 
  34                         } elseif ($this->compress 
== 'shrink') { 
  35                                 //TODO: protect binary call by converting bin to array ? 
  36                                 $this->bin 
.= ' --shrink'; 
  37                         //Append obfuscate parameter 
  38                         } elseif ($this->compress 
== 'obfuscate') { 
  39                                 //TODO: protect binary call by converting bin to array ? 
  40                                 $this->bin 
.= ' --obfuscate'; 
  41                         //Unknown compress type 
  42                         //XXX: default compression is best 
  43                         } elseif ($this->compress 
!== 'best') { 
  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
); 
  53         public function process(string $content): string { 
  55                 $descriptorSpec = array( 
  56                         0 => array('pipe', 'r'), 
  57                         1 => array('pipe', 'w'), 
  58                         2 => array('pipe', 'w') 
  62                 if (is_resource($proc = proc_open($this->bin
, $descriptorSpec, $pipes))) { 
  63                         //Set stderr as non blocking 
  64                         stream_set_blocking($pipes[2], false); 
  66                         //Send content to stdin 
  67                         fwrite($pipes[0], $content); 
  72                         //Read content from stdout 
  73                         if ($stdout = stream_get_contents($pipes[1])) { 
  80                         //Read content from stderr 
  81                         if (($stderr = stream_get_contents($pipes[2]))) { 
  82                                 throw new Error(sprintf('Got unexpected strerr for %s: %s', $this->bin
, $stderr), $this->line
, $this->fileName
); 
  89                         if (($ret = proc_close($proc))) { 
  90                                 throw new Error(sprintf('Got unexpected non zero return code %s: %d', $this->bin
, $ret), $this->line
, $this->fileName
);