Skip to content
This repository has been archived by the owner on Dec 5, 2019. It is now read-only.

Add support for dyncamically generated imports. #201

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
28 changes: 25 additions & 3 deletions lib/Less/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -315,8 +315,26 @@ public function parse( $str, $file_uri = null ){
*/
public function parseFile( $filename, $uri_root = '', $returnRoot = false){

$readFileName = $filename;
if( !file_exists($filename) ){
$this->Error(sprintf('File `%s` not found.', $filename));
//if the file doesn't exist, maybe we should try to load it through HTTP, because it may be dynamically generated
$basename = basename($filename);
if($uri_root == $filename) {
$filePossibleURL = rtrim($this->env->currentFileInfo["uri_root"], "/")."/".$basename;
} else {
$filePossibleURL = rtrim($uri_root, "/")."/".$basename;
}

if(strpos($filePossibleURL, "//") === 0) {
$filePossibleURL = "http:".$filePossibleURL;
}
$cont = @file_get_contents($filePossibleURL);
if(empty($cont)) { // if we got either false or an empty string, then we throw the exception.
$this->Error(sprintf('File `%s` not found.', $filename));
} else { //otherwise we save the contents we got to a temp file
$readFileName = tempnam(sys_get_temp_dir(), 'Lessc');
file_put_contents($readFileName, $cont);
}
}


Expand All @@ -335,16 +353,20 @@ public function parseFile( $filename, $uri_root = '', $returnRoot = false){
self::AddParsedFile($filename);

if( $returnRoot ){
$rules = $this->GetRules( $filename );
$rules = $this->GetRules( $readFileName );
$return = new Less_Tree_Ruleset(array(), $rules );
}else{
$this->_parse( $filename );
$this->_parse( $readFileName );
$return = $this;
}

if( $previousFileInfo ){
$this->env->currentFileInfo = $previousFileInfo;
}

if(realpath($readFileName) !== realpath($filename)) { //if we used a temp file, remove it
unlink($readFileName);
}

return $return;
}
Expand Down