]>
Raphaël G. Git Repositories - packbundle/blob - Filter/CPackFilter.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 CPackFilter
implements FilterInterface
{
24 * @xxx compress can be minify or pretty
26 public function __construct(protected Source
$fileName, protected int $line, protected string $bin = 'cpack', protected string $compress = 'minify') {
28 if (!empty($this->compress
)) {
29 //Append minify parameter
30 if ($this->compress
== 'minify') {
31 //TODO: protect binary call by converting bin to array ?
32 $this->bin
.= ' --minify';
33 //Unknown compress type
34 //XXX: default compression is pretty
35 } elseif ($this->compress
!== 'pretty') {
36 //Throw an error on unknown compress
37 throw new Error(sprintf('Got unexpected compress for %s: %s', $this->bin
, $this->compress
), $this->line
, $this->fileName
);
45 public function process(string $content): string {
47 $descriptorSpec = array(
48 0 => array('pipe', 'r'),
49 1 => array('pipe', 'w'),
50 2 => array('pipe', 'w')
54 if (is_resource($proc = proc_open($this->bin
, $descriptorSpec, $pipes))) {
55 //Set stderr as non blocking
56 stream_set_blocking($pipes[2], false);
58 //Send content to stdin
59 fwrite($pipes[0], $content);
64 //Read content from stdout
65 if ($stdout = stream_get_contents($pipes[1])) {
72 //Read content from stderr
73 if (($stderr = stream_get_contents($pipes[2]))) {
74 throw new Error(sprintf('Got unexpected strerr for %s: %s', $this->bin
, $stderr), $this->line
, $this->fileName
);
81 if (($ret = proc_close($proc))) {
82 throw new Error(sprintf('Got unexpected non zero return code %s: %d', $this->bin
, $ret), $this->line
, $this->fileName
);