]>
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 //Default compress type
27 //Twig template filename
36 * @xxx compress can be minify or pretty
38 public function __construct(Source
$fileName, int $line, string $bin = 'cpack', string $compress = 'minify') {
40 $this->fileName
= $fileName;
49 $this->compress
= $compress;
52 if (!empty($this->compress
)) {
53 //Append minify parameter
54 if ($this->compress
== 'minify') {
55 $this->bin
.= ' --minify';
56 //Unknown compress type
57 //XXX: default compression is pretty
58 } elseif ($this->compress
!== 'pretty') {
59 //Throw an error on unknown compress
60 throw new Error(sprintf('Got unexpected compress for %s: %s', $this->bin
, $this->compress
), $this->line
, $this->fileName
);
68 public function process(string $content): string {
70 $descriptorSpec = array(
71 0 => array('pipe', 'r'),
72 1 => array('pipe', 'w'),
73 2 => array('pipe', 'w')
77 if (is_resource($proc = proc_open($this->bin
, $descriptorSpec, $pipes))) {
78 //Set stderr as non blocking
79 stream_set_blocking($pipes[2], false);
81 //Send content to stdin
82 fwrite($pipes[0], $content);
87 //Read content from stdout
88 if ($stdout = stream_get_contents($pipes[1])) {
95 //Read content from stderr
96 if (($stderr = stream_get_contents($pipes[2]))) {
97 throw new Error(sprintf('Got unexpected strerr for %s: %s', $this->bin
, $stderr), $this->line
, $this->fileName
);
104 if (($ret = proc_close($proc))) {
105 throw new Error(sprintf('Got unexpected non zero return code %s: %d', $this->bin
, $ret), $this->line
, $this->fileName
);