From ec172f73e587cce297e68edd811fec28eceac2aa Mon Sep 17 00:00:00 2001 From: Matias Saggiorato Date: Fri, 6 Mar 2015 18:10:02 -0300 Subject: [PATCH 1/2] Add support for dyncamically generated imports. If the file doesn't exist in the filesystem, try to load it through an HTTP request using the uri root provided. --- lib/Less/Parser.php | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/lib/Less/Parser.php b/lib/Less/Parser.php index 891fe690..dd0e9d8e 100755 --- a/lib/Less/Parser.php +++ b/lib/Less/Parser.php @@ -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); + } } @@ -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($readFileName !== $filename) { //if we used a temp file, remove it + unlink($readFileName); + } return $return; } From ad92f1f9ede6f0cb3e7888bee1bf20a196dc3384 Mon Sep 17 00:00:00 2001 From: Matias Saggiorato Date: Mon, 9 Mar 2015 13:54:13 -0300 Subject: [PATCH 2/2] Fix for paths in windows It was removing the original less file in some cases. --- lib/Less/Parser.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Less/Parser.php b/lib/Less/Parser.php index dd0e9d8e..cbd2d347 100755 --- a/lib/Less/Parser.php +++ b/lib/Less/Parser.php @@ -364,7 +364,7 @@ public function parseFile( $filename, $uri_root = '', $returnRoot = false){ $this->env->currentFileInfo = $previousFileInfo; } - if($readFileName !== $filename) { //if we used a temp file, remove it + if(realpath($readFileName) !== realpath($filename)) { //if we used a temp file, remove it unlink($readFileName); }