1<?php
2
3/*
4 * This file is part of the Assetic package, an OpenSky project.
5 *
6 * (c) 2010-2014 OpenSky Project Inc
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12namespace Assetic\Filter;
13
14use Assetic\Asset\AssetInterface;
15use Assetic\Exception\FilterException;
16use Assetic\Factory\AssetFactory;
17use Assetic\Util\FilesystemUtils;
18
19/**
20 * Loads Roole files.
21 *
22 * @link http://roole.org
23 * @author Marcin Chwedziak <tiraeth@gmail.com>
24 */
25class RooleFilter extends BaseNodeFilter implements DependencyExtractorInterface
26{
27    private $rooleBin;
28    private $nodeBin;
29
30    /**
31     * Constructor
32     *
33     * @param string $rooleBin The path to the roole binary
34     * @param string $nodeBin  The path to the node binary
35     */
36    public function __construct($rooleBin = '/usr/bin/roole', $nodeBin = null)
37    {
38        $this->rooleBin = $rooleBin;
39        $this->nodeBin = $nodeBin;
40    }
41
42    public function filterLoad(AssetInterface $asset)
43    {
44        $input = FilesystemUtils::createTemporaryFile('roole');
45        $output = $input.'.css';
46
47        file_put_contents($input, $asset->getContent());
48
49        $pb = $this->createProcessBuilder($this->nodeBin
50            ? array($this->nodeBin, $this->rooleBin)
51            : array($this->rooleBin));
52
53        $pb->add($input);
54
55        $proc = $pb->getProcess();
56        $code = $proc->run();
57        unlink($input);
58
59        if (0 !== $code) {
60            if (file_exists($output)) {
61                unlink($output);
62            }
63
64            throw FilterException::fromProcess($proc);
65        }
66
67        if (!file_exists($output)) {
68            throw new \RuntimeException('Error creating output file.');
69        }
70
71        $asset->setContent(file_get_contents($output));
72        unlink($output);
73    }
74
75    public function filterDump(AssetInterface $asset)
76    {
77    }
78
79    public function getChildren(AssetFactory $factory, $content, $loadPath = null)
80    {
81        // todo
82        return array();
83    }
84}
85