Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
298 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
<?php | ||
/* | ||
* This file is part of the sfLESSPlugin. | ||
* (c) 2010 Konstantin Kudryashov <ever.zet@gmail.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
/** | ||
* sfLESSDependency checks for less dependencies | ||
* | ||
* @package sfLESSPlugin | ||
* @subpackage lib | ||
* @author Victor Berchet <victor@suumit.com> | ||
* @version 1.0.0 | ||
*/ | ||
class sfLESSDependency | ||
{ | ||
/** | ||
* @var string $path The base path | ||
*/ | ||
protected $path; | ||
|
||
/** | ||
* @var boolean $check Wether to check for dependency | ||
*/ | ||
protected $check = false; | ||
|
||
/** | ||
* @param string $path Base path (web root folder) | ||
* @param boolean $check Whether to check for dependency | ||
*/ | ||
public function __construct($path, $check) | ||
{ | ||
if (!sfLESSUtils::isPathAbsolute($path) || !is_dir($path)) | ||
{ | ||
throw new InvalidArgumentException("An existing absolute folder must be provided"); | ||
} | ||
else | ||
{ | ||
$this->path = preg_replace('/\/$/', '', $path); | ||
} | ||
$this->check = $check; | ||
} | ||
|
||
/** | ||
* Return the modification time of the file (optionally including its dependency) | ||
* | ||
* @param string $file Filename | ||
* @return integer|false The time the files was last modified (unix timestamp) | ||
*/ | ||
public function getMtime($lessFile) | ||
{ | ||
$mtime = filemtime($lessFile); | ||
|
||
if ($mtime !== false && $this->check) | ||
{ | ||
$deps = $this->computeDependencies($lessFile, array()); | ||
foreach ($deps as $file) | ||
{ | ||
if (is_file($file)) | ||
{ | ||
$mtime = max($mtime, filemtime($file)); | ||
} | ||
} | ||
} | ||
|
||
return $mtime; | ||
} | ||
|
||
/** | ||
* Compute the dependencies of the file | ||
* | ||
* @param file $lessFile A less file | ||
* @param array $deps An array of pre-existing dependencies | ||
* @return array The updated array of dependencies | ||
*/ | ||
protected function computeDependencies($lessFile, array $deps) | ||
{ | ||
if (!sfLESSUtils::isPathAbsolute($lessFile)) | ||
{ | ||
$lessFile = realpath($this->path . '/' . $lessFile); | ||
} | ||
|
||
if (is_file($lessFile)) | ||
{ | ||
$less = sfLESSUtils::stripLessComments(file_get_contents($lessFile)); | ||
if (preg_match_all("/@import\s+(?:url\s*\(\s*)?(['\"])(.*?)\\1\s*(?:\))?\s*;/", $less, $files)) | ||
{ | ||
foreach ($files[2] as $file) | ||
{ | ||
// Append the .less when the extension is omitted | ||
if (!preg_match('/\.(le?|c)ss$/', $file)) | ||
{ | ||
$file .= '.less'; | ||
} | ||
// Compute the canonical path | ||
if (sfLESSUtils::isPathAbsolute($file)) | ||
{ | ||
$file = realpath($this->path . $file); | ||
} | ||
else | ||
{ | ||
$file = realpath(dirname($lessFile) . '/' . $file); | ||
} | ||
if ($file !== false && !in_array($file, $deps) && !preg_match('/\.css$/', $file)) | ||
{ | ||
$deps[] = $file; | ||
// Recursively add dependencies | ||
$deps += $this->computeDependencies($file, $deps); | ||
} | ||
} | ||
} | ||
return $deps; | ||
} | ||
else | ||
{ | ||
return array(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
/* | ||
* This file is part of the sfLESSPlugin. | ||
* (c) 2010 Konstantin Kudryashov <ever.zet@gmail.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
/** | ||
* Various utility functions | ||
* | ||
* @package sfLESSPlugin | ||
* @subpackage lib | ||
* @author Victor Berchet <victor@suumit.com> | ||
* @version 1.0.0 | ||
*/ | ||
|
||
class sfLESSUtils | ||
{ | ||
/** | ||
* Determine if a filesystem path is absolute. | ||
* | ||
* @param path $path A filesystem path. | ||
* | ||
* @return bool true, if the path is absolute, otherwise false. | ||
*/ | ||
public static function isPathAbsolute($path) | ||
{ | ||
if ($path[0] == '/' || $path[0] == '\\' || | ||
(strlen($path) > 3 && ctype_alpha($path[0]) && | ||
$path[1] == ':' && | ||
($path[2] == '\\' || $path[2] == '/') | ||
) | ||
) | ||
{ | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
/** | ||
* Strip comments from less content | ||
* | ||
* @param string $less LESS code | ||
* @return string LESS code without comments | ||
*/ | ||
public static function stripLessComments($less) | ||
{ | ||
// strip /* */ style comments | ||
$less = preg_replace('#/\*.*?\*/#ms', '', $less); | ||
// stip // style comments | ||
$less = preg_replace('#//.*$#m', '', $less); | ||
return $less; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
// relative path, no extension, extra spaces, single quotes | ||
@import '../b/b' ; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
// basename with a '.' | ||
@import "../c/c.c"; | ||
@import "../a/a"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
// comments | ||
/* comment */@import /* comment */ url("../d/d"); // comment | ||
// @import "../not/not.less"; | ||
/* @import "../not/not.less"; */ | ||
@import "../a/a"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
// circular reference | ||
@import "../a/a"; | ||
@import "../e.css"; | ||
@import "../a/a"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
// css files should not be processed | ||
@import "../not/not"; |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
// absolute path, less extension, double quotes | ||
@import "/a/a.less"; |
Oops, something went wrong.