Skip to content

Commit

Permalink
updated LessFilter to use the lessc binary rather than a node.js scri…
Browse files Browse the repository at this point in the history
…ptlet (requires latest less.js master)
  • Loading branch information
kriswallsmith committed Aug 28, 2011
1 parent 2f3a3fe commit c274e92
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 54 deletions.
1 change: 1 addition & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<!-- <server name="COFFEE_BIN" value="/path/to/coffee" /> -->
<!-- <server name="NODE_BIN" value="/path/to/node" /> -->
<!-- <server name="NODE_PATH" value="/path/to/node/lib" /> -->
<!-- <server name="LESS" value="/path/to/lessc" /> -->
<!-- <server name="LESSPHP" value="/path/to/lessphp/lessc.inc.php" /> -->
<!-- <server name="SASS_BIN" value="/path/to/sass" /> -->
<!-- <server name="COMPASS_BIN" value="/path/to/compass" /> -->
Expand Down
82 changes: 31 additions & 51 deletions src/Assetic/Filter/LessFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,17 @@
*/
class LessFilter implements FilterInterface
{
private $nodeBin;
private $nodePaths;
private $lesscBin;
private $compress;

/**
* Constructor.
*
* @param string $nodeBin The path to the node binary
* @param array $nodePaths An array of node paths
* @param string $lesscBin The path to the lessc binary
*/
public function __construct($nodeBin = '/usr/bin/node', array $nodePaths = array())
public function __construct($lesscBin = '/usr/bin/lessc')
{
$this->nodeBin = $nodeBin;
$this->nodePaths = $nodePaths;
$this->lesscBin = $lesscBin;
}

public function setCompress($compress)
Expand All @@ -44,66 +41,49 @@ public function setCompress($compress)

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));
} catch (e) {
less.writeError(e);
process.exit(3);
}
});

EOF;
$pb = new ProcessBuilder();
$pb
->inheritEnvironmentVariables()
->add($this->lesscBin)
;

// compress
if ($this->compress) {
$pb->add('--compress');
}

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

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

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

$pb = new ProcessBuilder();
$pb->inheritEnvironmentVariables();
// input
$input = tempnam(sys_get_temp_dir(), 'assetic_less');
$pb->add($input);
file_put_contents($input, $asset->getContent());

// 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)
));
// output
$output = $input.'.out';
$pb->add($output);

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

if (0 < $code) {
if (file_exists($output)) {
unlink($output);
}

throw new \RuntimeException($proc->getErrorOutput());
} elseif (!file_exists($output)) {
throw new \RuntimeException('Error creating output file.');
}

$asset->setContent($proc->getOutput());
$asset->setContent(file_get_contents($output));
unlink($output);
}

public function filterDump(AssetInterface $asset)
Expand Down
6 changes: 3 additions & 3 deletions tests/Assetic/Test/Filter/LessFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ class LessFilterTest extends \PHPUnit_Framework_TestCase

protected function setUp()
{
if (!isset($_SERVER['NODE_BIN']) || !isset($_SERVER['NODE_PATH'])) {
$this->markTestSkipped('No node.js configuration.');
if (!isset($_SERVER['LESS'])) {
$this->markTestSkipped('No less configuration.');
}

$this->filter = new LessFilter($_SERVER['NODE_BIN'], array($_SERVER['NODE_PATH']));
$this->filter = new LessFilter($_SERVER['LESS']);
}

public function testFilterLoad()
Expand Down

0 comments on commit c274e92

Please sign in to comment.