Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion src/Assetic/Filter/LessphpFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use Assetic\Contracts\Asset\AssetInterface;
use Assetic\Contracts\Filter\DependencyExtractorInterface;
use Assetic\Factory\AssetFactory;
use Assetic\Filter\BaseFilter;
use Assetic\Util\LessUtils;

/**
Expand All @@ -23,6 +24,7 @@ class LessphpFilter extends BaseFilter implements DependencyExtractorInterface
private $options = [
'compress' => true
];
private $customFunctions = [];

/**
* Lessphp Load Paths
Expand Down Expand Up @@ -61,6 +63,11 @@ public function setOptions(array $options)
$this->options = $options;
}

public function registerFunction($name, $callable)
{
$this->customFunctions[$name] = $callable;
}

/**
* @param string $formatter One of "lessjs", "compressed", or "classic".
*/
Expand All @@ -80,6 +87,10 @@ public function filterLoad(AssetInterface $asset)
$lc->addImportDir($loadPath);
}

foreach ($this->customFunctions as $name => $callable) {
$lc->registerFunction($name, $callable);
}

if ($this->formatter) {
$lc->setFormatter($this->formatter);
}
Expand Down Expand Up @@ -130,4 +141,4 @@ public function getChildren(AssetFactory $factory, $content, $loadPath = null)

return $children;
}
}
}
19 changes: 19 additions & 0 deletions tests/Assetic/Test/Filter/LessphpFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use Assetic\Asset\StringAsset;
use Assetic\Factory\AssetFactory;
use Assetic\Filter\LessphpFilter;
use ScssPhp\ScssPhp\ValueConverter;

/**
* @property LessphpFilter $filter
Expand Down Expand Up @@ -163,4 +164,22 @@ public function provideImports()
array('@import-once url(main);'),
);
}

/**
* @group integration
*/
public function testRegisterFunction()
{
$asset = new StringAsset('.foo { color: bar(); }');
$asset->load();

$this->filter->registerFunction('bar', function () { return 'red';});
$this->filter->filterLoad($asset);

$expected = new StringAsset('.foo { color: red; }');
$expected->load();
$this->filter->filterLoad($expected);

$this->assertEquals($expected->getContent(), $asset->getContent(), 'custom function can be registered');
}
}