]> Raphaël G. Git Repositories - packbundle/blob - Filter/JPackFilter.php
Update composer requires
[packbundle] / Filter / JPackFilter.php
1 <?php declare(strict_types=1);
2
3 /*
4 * This file is part of the Rapsys PackBundle package.
5 *
6 * (c) Raphaël Gertz <symfony@rapsys.eu>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12 namespace Rapsys\PackBundle\Filter;
13
14 use Twig\Error\Error;
15 use Twig\Source;
16
17 /**
18 * {@inheritdoc}
19 */
20 class JPackFilter implements FilterInterface {
21 //Default bin
22 private $bin;
23
24 //Default compress type
25 private $compress;
26
27 //Twig template filename
28 private $fileName;
29
30 //Twig template line
31 private $line;
32
33 /**
34 * Setup jpack filter
35 *
36 * @xxx compress can be clean, shrink, obfuscate or best
37 */
38 public function __construct(Source $fileName, int $line, string $bin = 'jpack', string $compress = 'best') {
39 //Set fileName
40 $this->fileName = $fileName;
41
42 //Set line
43 $this->line = $line;
44
45 //Set bin
46 $this->bin = $bin;
47
48 //Set compress
49 $this->compress = $compress;
50
51 //Deal with 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);
67 }
68 }
69 }
70
71 /**
72 * {@inheritdoc}
73 */
74 public function process(string $content): string {
75 //Create descriptors
76 $descriptorSpec = array(
77 0 => array('pipe', 'r'),
78 1 => array('pipe', 'w'),
79 2 => array('pipe', 'w')
80 );
81
82 //Open process
83 if (is_resource($proc = proc_open($this->bin, $descriptorSpec, $pipes))) {
84 //Set stderr as non blocking
85 stream_set_blocking($pipes[2], false);
86
87 //Send content to stdin
88 fwrite($pipes[0], $content);
89
90 //Close stdin
91 fclose($pipes[0]);
92
93 //Read content from stdout
94 if ($stdout = stream_get_contents($pipes[1])) {
95 $content = $stdout;
96 }
97
98 //Close stdout
99 fclose($pipes[1]);
100
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);
104 }
105
106 //Close stderr
107 fclose($pipes[2]);
108
109 //Close process
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);
112 }
113 }
114
115 //Return content
116 return $content;
117 }
118 }