X-Git-Url: https://git.rapsys.eu/packbundle/blobdiff_plain/59ae967e218457b2ab3d77cb621c0640345f5e9b..6bc3a323095049fba5ac8bb2b1a2cef1e82b8df5:/Filter/JPackFilter.php?ds=sidebyside diff --git a/Filter/JPackFilter.php b/Filter/JPackFilter.php new file mode 100644 index 0000000..ca4555d --- /dev/null +++ b/Filter/JPackFilter.php @@ -0,0 +1,100 @@ +fileName = $fileName; + + //Set line + $this->line = $line; + + //Set bin + $this->bin = $bin; + + //Set compress + $this->compress = $compress; + + //Deal with compress + if (!empty($this->compress)) { + //Append clean parameter + if ($this->compress == 'clean') { + $this->bin .= ' --clean'; + //Append shrink parameter + } elseif ($this->compress == 'shrink') { + $this->bin .= ' --shrink'; + //Append obfuscate parameter + } elseif ($this->compress == 'obfuscate') { + $this->bin .= ' --obfuscate'; + //Unknown compress type + //XXX: default compression is best + } elseif ($this->compress !== 'best') { + //Throw an error on unknown compress + throw new Error(sprintf('Got unexpected compress for %s: %s', $this->bin, $this->compress), $this->line, $this->fileName); + } + } + } + + public function process($content) { + //Create descriptors + $descriptorSpec = array( + 0 => array('pipe', 'r'), + 1 => array('pipe', 'w'), + 2 => array('pipe', 'w') + ); + + //Open process + if (is_resource($proc = proc_open($this->bin, $descriptorSpec, $pipes))) { + //Set stderr as non blocking + stream_set_blocking($pipes[2], 0); + + //Send content to stdin + fwrite($pipes[0], $content); + + //Close stdin + fclose($pipes[0]); + + //Read content from stdout + if ($stdout = stream_get_contents($pipes[1])) { + $content = $stdout; + } + + //Close stdout + fclose($pipes[1]); + + //Read content from stderr + if (($stderr = stream_get_contents($pipes[2]))) { + throw new Error(sprintf('Got unexpected strerr for %s: %s', $this->bin, $stderr), $this->line, $this->fileName); + } + + //Close stderr + fclose($pipes[2]); + + //Close process + if (($ret = proc_close($proc))) { + throw new Error(sprintf('Got unexpected non zero return code %s: %d', $this->bin, $ret), $this->line, $this->fileName); + } + } + + //Return content + return $content; + } +}