From e5714762168e9e87490bc311129877a50fed9d63 Mon Sep 17 00:00:00 2001 From: Rob Mills Date: Thu, 20 Jan 2022 15:22:02 +0000 Subject: [PATCH] Expose registerFunction method to Less filter --- src/Assetic/Filter/LessphpFilter.php | 13 ++++++++++++- .../Assetic/Test/Filter/LessphpFilterTest.php | 19 +++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/src/Assetic/Filter/LessphpFilter.php b/src/Assetic/Filter/LessphpFilter.php index 0e13e30..2494aa5 100644 --- a/src/Assetic/Filter/LessphpFilter.php +++ b/src/Assetic/Filter/LessphpFilter.php @@ -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; /** @@ -23,6 +24,7 @@ class LessphpFilter extends BaseFilter implements DependencyExtractorInterface private $options = [ 'compress' => true ]; + private $customFunctions = []; /** * Lessphp Load Paths @@ -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". */ @@ -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); } @@ -130,4 +141,4 @@ public function getChildren(AssetFactory $factory, $content, $loadPath = null) return $children; } -} +} \ No newline at end of file diff --git a/tests/Assetic/Test/Filter/LessphpFilterTest.php b/tests/Assetic/Test/Filter/LessphpFilterTest.php index 14f9774..0058265 100644 --- a/tests/Assetic/Test/Filter/LessphpFilterTest.php +++ b/tests/Assetic/Test/Filter/LessphpFilterTest.php @@ -4,6 +4,7 @@ use Assetic\Asset\StringAsset; use Assetic\Factory\AssetFactory; use Assetic\Filter\LessphpFilter; +use ScssPhp\ScssPhp\ValueConverter; /** * @property LessphpFilter $filter @@ -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'); + } }