Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move dependency checking in its own class
- Loading branch information
Showing
3 changed files
with
126 additions
and
34 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
<?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 | ||
{ | ||
/** | ||
* Base path | ||
*/ | ||
protected $path; | ||
|
||
public function __construct($path) | ||
{ | ||
if (!sfLESSUtils::isPathAbsolute($path) || !is_dir($path)) | ||
{ | ||
throw new InvalidArgumentException("An existing absolute folder must be provided"); | ||
} | ||
else | ||
{ | ||
$this->path = $path; | ||
} | ||
} | ||
|
||
/** | ||
* 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 | ||
*/ | ||
public function computeDependencies($lessFile, array $deps) | ||
{ | ||
if (!sfLESSUtils::isPathAbsolute($lessFile)) | ||
{ | ||
$lessFile = realpath($this->path + $lessFile); | ||
} | ||
|
||
if (is_file($lessFile)) | ||
{ | ||
$less = file_get_contents($lessFile); | ||
if (preg_match_all("/\s*@import\s+(['\"])(.*?)\\1\s*;/", $less, $files)) | ||
{ | ||
foreach ($files[2] as $file) | ||
{ | ||
// Append the .less extension when omitted | ||
if (strpos('.', $file) === false) | ||
{ | ||
$file .= '.less'; | ||
} | ||
// Compute the canonical path | ||
$file = realpath(dirname($lessFile) . '/' . $file); | ||
if ($file !== false && !in_array($file, $deps)) | ||
{ | ||
$deps[] = $file; | ||
// Recursively add dependencies | ||
$deps = array_merge($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,43 @@ | ||
<?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; | ||
} | ||
} |