3 // src/Rapsys/PackBundle/Twig/Filter/CPackFilter.php
4 namespace Rapsys\PackBundle\Twig\Filter
;
6 use Rapsys\PackBundle\Twig\Filter\FilterInterface
;
7 use Symfony\Component\DependencyInjection\ContainerInterface
;
9 class CPackFilter
implements FilterInterface
{
11 private $bin = 'cpack';
13 //Default compress type
14 //XXX: can be minify or pretty
15 private $compress = 'minify';
17 //Twig template filename
23 //Configure the object
24 public function __construct(ContainerInterface
$containerInterface, $fileName, $line) {
26 if ($containerInterface->hasParameter('rapsys_pack.cpackfilter')) {
27 if ($parameters = $containerInterface->getParameter('rapsys_pack.cpackfilter')) {
28 foreach($parameters as $k => $v) {
29 if (isset($this->$k)) {
37 $this->fileName
= $fileName;
43 if (!empty($this->compress
)) {
44 //Append minify parameter
45 if ($this->compress
== 'minify') {
46 $this->bin
.= ' --minify';
47 //Unknown compress type
48 #XXX: default compression is pretty
49 } elseif ($this->compress
!== 'pretty') {
50 //Throw an error on unknown compress
51 throw new \
Twig_Error(sprintf('Got unexpected compress for %s: %s', $this->bin
, $this->compress
), $this->line
, $this->fileName
);
56 public function process($content) {
58 $descriptorSpec = array(
59 0 => array('pipe', 'r'),
60 1 => array('pipe', 'w'),
61 2 => array('pipe', 'w')
65 if (is_resource($proc = proc_open($this->bin
, $descriptorSpec, $pipes))) {
66 //Set stderr as non blocking
67 stream_set_blocking($pipes[2], 0);
69 //Send content to stdin
70 fwrite($pipes[0], $content);
75 //Read content from stdout
76 if ($stdout = stream_get_contents($pipes[1])) {
83 //Read content from stderr
84 if (($stderr = stream_get_contents($pipes[2]))) {
85 throw new \
Twig_Error(sprintf('Got unexpected strerr for %s: %s', $this->bin
, $stderr), $this->line
, $this->fileName
);
92 if (($ret = proc_close($proc))) {
93 throw new \
Twig_Error(sprintf('Got unexpected non zero return code %s: %d', $this->bin
, $ret), $this->line
, $this->fileName
);