View source
<?php
class SassFile {
const CSS = 'css';
const SASS = 'sass';
const SCSS = 'scss';
const SASSC = 'sassc';
private static $extensions = array(
self::SASS,
self::SCSS,
);
public static $path = FALSE;
public static $parser = FALSE;
public static function get_tree($filename, $parser) {
if ($parser->cache) {
$cached = self::get_cached_file($filename, $parser->cache_location);
if ($cached !== false) {
return $cached;
}
}
$contents = file_get_contents($filename) . "\n\n ";
SassFile::$parser = $parser;
SassFile::$path = $filename;
$contents = preg_replace_callback('/url\\(\\s*[\'"]?(?![a-z]+:|\\/+)([^\'")]+)[\'"]?\\s*\\)/i', 'SassFile::resolve_paths', $contents);
$sassParser = new SassParser(array_merge($parser->options, array(
'line' => 1,
)));
$tree = $sassParser
->parse($contents, FALSE);
if ($parser->cache) {
self::set_cached_file($tree, $filename, $parser->cache_location);
}
return $tree;
}
public static function resolve_paths($matches, $inc_url = TRUE) {
$path = (self::$parser ? self::$parser->basepath : '') . self::$path;
$path = substr($path, 0, strrpos($path, '/')) . '/';
$path = $path . $matches[1];
$last = '';
while ($path != $last) {
$last = $path;
$path = preg_replace('`(^|/)(?!\\.\\./)([^/]+)/\\.\\./`', '$1', $path);
}
return $inc_url ? 'url("' . $path . '")' : $path;
}
public static function get_file($filename, $parser, $repass = TRUE) {
$ext = substr($filename, strrpos($filename, '.') + 1);
if (substr($filename, -1) != '*' && $ext !== self::SASS && $ext !== self::SCSS && $ext !== self::CSS) {
return self::get_file($filename . '.' . $parser->syntax, $parser);
}
if (file_exists($filename)) {
return array(
$filename,
);
}
$paths = $parser->load_paths;
if (is_string($parser->filename) && strlen($parser->filename)) {
$paths[] = dirname($parser->filename);
}
foreach ($paths as $path) {
$filepath = self::find_file($filename, realpath($path));
if ($filepath !== false) {
return array(
$filepath,
);
}
}
foreach ($parser->load_path_functions as $function) {
if (function_exists($function) && ($paths = call_user_func($function, $filename, $parser))) {
return $paths;
}
}
if ($repass) {
return self::get_file(self::resolve_paths(array(
1 => $filename,
), FALSE), $parser, FALSE);
}
return FALSE;
}
public static function find_file($filename, $dir) {
$partialname = dirname($filename) . DIRECTORY_SEPARATOR . '_' . basename($filename);
foreach (array(
$filename,
$partialname,
) as $file) {
if (file_exists($dir . DIRECTORY_SEPARATOR . $file)) {
return realpath($dir . DIRECTORY_SEPARATOR . $file);
}
}
if (is_dir($dir)) {
$files = array_slice(scandir($dir), 2);
foreach ($files as $file) {
if (is_dir($dir . DIRECTORY_SEPARATOR . $file)) {
$path = self::find_file($filename, $dir . DIRECTORY_SEPARATOR . $file);
if ($path !== false) {
return $path;
}
}
}
}
return false;
}
public static function get_cached_file($filename, $cacheLocation) {
$cached = realpath($cacheLocation) . DIRECTORY_SEPARATOR . md5($filename) . '.' . self::SASSC;
if ($cached && file_exists($cached) && filemtime($cached) >= filemtime($filename)) {
return unserialize(file_get_contents($cached));
}
return false;
}
public static function set_cached_file($sassc, $filename, $cacheLocation) {
$cacheDir = realpath($cacheLocation);
if (!$cacheDir) {
mkdir($cacheLocation);
@chmod($cacheLocation, 0777);
$cacheDir = realpath($cacheLocation);
}
$cached = $cacheDir . DIRECTORY_SEPARATOR . md5($filename) . '.' . self::SASSC;
return file_put_contents($cached, serialize($sassc));
}
}