Assetic\Filter\LessFilter::filterLoad PHP Метод

filterLoad() публичный Метод

public filterLoad ( Assetic\Asset\AssetInterface $asset )
$asset Assetic\Asset\AssetInterface
    public function filterLoad(AssetInterface $asset)
    {
        static $format = <<<'EOF'
var less = require('less');
var sys  = require('sys');

new(less.Parser)(%s).parse(%s, function(e, tree) {
    if (e) {
        less.writeError(e);
        process.exit(2);
    }

    try {
        sys.print(tree.toCSS(%s));
        process.exit(0);
    } catch (e) {
        less.writeError(e);
        process.exit(3);
    }
});

EOF;

        $root = $asset->getSourceRoot();
        $path = $asset->getSourcePath();

        // parser options
        $parserOptions = array();
        if ($root && $path) {
            $parserOptions['paths'] = array(dirname($root.'/'.$path));
            $parserOptions['filename'] = basename($path);
        }

        // tree options
        $treeOptions = array();
        if (null !== $this->compress) {
            $treeOptions['compress'] = $this->compress;
        }

        $pb = new ProcessBuilder();
        $pb->inheritEnvironmentVariables();

        // node.js configuration
        if (0 < count($this->nodePaths)) {
            $pb->setEnv('NODE_PATH', implode(':', $this->nodePaths));
        }

        $pb->add($this->nodeBin)->add($input = tempnam(sys_get_temp_dir(), 'assetic_less'));
        file_put_contents($input, sprintf($format,
            json_encode($parserOptions),
            json_encode($asset->getContent()),
            json_encode($treeOptions)
        ));

        $proc = $pb->getProcess();
        $code = $proc->run();
        unlink($input);

        if (0 < $code) {
            throw new \RuntimeException($proc->getErrorOutput());
        }

        $asset->setContent($proc->getOutput());
    }

Usage Example

    public function testSettingLoadPaths()
    {
        $expected = <<<EOF
.foo {
  color: blue;
}
.foo {
  color: red;
}
.bar {
  color: #ff0000;
}

EOF;
        $this->filter->setLoadPaths(array(__DIR__ . '/fixtures/less', __DIR__ . '/fixtures/less/import_path'));
        $asset = new StringAsset('@import "main"; @import "_import"; .bar {color: @red}');
        $asset->load();
        $this->filter->filterLoad($asset);
        $this->assertEquals($expected, $asset->getContent(), '->filterLoad() sets load paths to include paths');
    }
All Usage Examples Of Assetic\Filter\LessFilter::filterLoad