Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add loader system for @import #521

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
24 changes: 24 additions & 0 deletions docs/docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -1003,6 +1003,8 @@ Methods:

* [`addImportDir($dir)`](#import_directory) -- Append directory to search path for imports

* [`setLoader($callback)`](#loader) -- Register a callback use to fetch imported files


### Compiling

Expand Down Expand Up @@ -1277,6 +1279,28 @@ overwriting the whole thing.
$less->addImportDir("public/stylesheets");
```

### Loader

It is possible to register a callback that will be called to fetch imported files (using `@import`).
This can be useful when LESS files are not in a directory on the filesystem.

The callback takes one parameter, which is the name of the file (as specified in the `@import` directive).

It will return either `false` or a string of LESS code to be parsed.
If it returns `false`, the `@import` directive will stay.

```php
$less->loader(function($name) {
// Use $name to do whatever you want to fetch your LESS code

// return $content; <- will use the $content you just fetched
// return false; <- will let the `@import` directive untouched
// return ''; <- will import nothing and remove the `@import` directive
});
```

If no callback is registered, the `@import` directives will be resolved using the classic behaviour (see **Import Directory**).

### Custom Functions

**lessphp** has a simple extension interface where you can implement user
Expand Down
29 changes: 21 additions & 8 deletions lessc.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ class lessc {
public $importDisabled = false;
public $importDir = '';

public $loader = null;

protected $numberPrecision = null;

protected $allParsedFiles = array();
Expand Down Expand Up @@ -90,6 +92,10 @@ static public function preg_quote($what) {
return preg_quote($what, '/');
}

public function setLoader($callback) {
$this->loader = $callback;
}

protected function tryImport($importPath, $parentBlock, $out) {
if ($importPath[0] == "function" && $importPath[1] == "url") {
$importPath = $this->flattenList($importPath[2]);
Expand All @@ -100,13 +106,21 @@ protected function tryImport($importPath, $parentBlock, $out) {

$url = $this->compileValue($this->lib_e($str));

// don't import if it ends in css
if (substr_compare($url, '.css', -4, 4) === 0) return false;

$realPath = $this->findImport($url);

if ($realPath === null) return false;
if (is_callable($this->loader)) {
$realPath = $url;
$content = call_user_func($this->loader, $realPath);
if ($content === false) return false;
}
else {
// don't import if it ends in css
if (substr_compare($url, '.css', -4, 4) === 0) return false;

$realPath = $this->findImport($url);
if ($realPath === null) return false;
$content = file_get_contents($realPath);
$this->addParsedFile($realPath);
}

if ($this->importDisabled) {
return array(false, "/* import disabled */");
}
Expand All @@ -115,9 +129,8 @@ protected function tryImport($importPath, $parentBlock, $out) {
return array(false, null);
}

$this->addParsedFile($realPath);
$parser = $this->makeParser($realPath);
$root = $parser->parse(file_get_contents($realPath));
$root = $parser->parse($content);

// set the parents of all the block props
foreach ($root->props as $prop) {
Expand Down