Skip to content

Commit

Permalink
Move dependency checking in its own class
Browse files Browse the repository at this point in the history
  • Loading branch information
vicb committed Jul 3, 2010
1 parent c37670b commit c00bb54
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 34 deletions.
36 changes: 2 additions & 34 deletions lib/sfLESS.class.php
Expand Up @@ -390,7 +390,8 @@ protected function checkDatesAndDependencies($lessFile, $checkDeps)
if ($checkDeps)
{
// Compute the less file dependencies and the date of the last modified file
$deps = $this->computeDependencies($lessFile, $deps);
$d = new sfLESSDependency(self::getLessPaths());
$deps = $d->computeDependencies($lessFile, $deps);
foreach ($deps as $file)
{
if (is_file($file))
Expand All @@ -411,39 +412,6 @@ protected function checkDatesAndDependencies($lessFile, $checkDeps)
}
}

/**
* 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)
{
$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 (is_file($file) && !in_array($file, $deps))
{
$deps[] = $file;
// Recursively add dependencies
$deps = array_merge($deps, $this->computeDependencies($file, $deps));
}
}
}
return $deps;
}

/**
* Compress CSS by removing whitespaces, tabs, newlines, etc.
*
Expand Down
81 changes: 81 additions & 0 deletions lib/sfLESSDependency.class.php
@@ -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();
}
}
}
43 changes: 43 additions & 0 deletions lib/sfLESSUtils.class.php
@@ -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;
}
}

0 comments on commit c00bb54

Please sign in to comment.