SassFile.php in Sassy 7
File
phamlp/sass/SassFile.php
View source
<?php
class SassFile {
const SASS = 'sass';
const SCSS = 'scss';
const SASSC = 'sassc';
private static $extensions = array(
self::SASS,
self::SCSS,
);
public static function getTree($filename, $parser) {
if ($parser->cache) {
$cached = self::getCachedFile($filename, $parser->cache_location);
if ($cached !== false) {
return $cached;
}
}
$sassParser = new SassParser(array_merge($parser->options, array(
'line' => 1,
)));
$tree = $sassParser
->parse($filename);
if ($parser->cache) {
self::setCachedFile($tree, $filename, $parser->cache_location);
}
return $tree;
}
public static function getFile($filename, $parser) {
$ext = substr($filename, -5);
foreach (self::$extensions as $i => $extension) {
if ($ext !== '.' . self::SASS && $ext !== '.' . self::SCSS) {
if ($i === 0) {
$_filename = "{$filename}.{$parser->syntax}";
}
else {
$_filename = $filename . '.' . ($parser->syntax === self::SASS ? self::SCSS : self::SASS);
}
}
else {
$_filename = $filename;
}
if (file_exists($_filename)) {
return $_filename;
}
$paths = $parser->load_paths;
if (!empty($parser->filename)) {
$paths[] = dirname($parser->filename);
}
foreach ($paths as $loadPath) {
$path = self::findFile($_filename, realpath($loadPath));
if ($path !== false) {
return $path;
}
}
if (!empty($parser->template_location)) {
$path = self::findFile($_filename, realpath($parser->template_location));
if ($path !== false) {
return $path;
}
}
}
throw new SassException('Unable to find {what}: {filename}', array(
'{what}' => 'import file',
'{filename}' => $filename,
));
}
public static function findFile($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);
}
}
$files = array_slice(scandir($dir), 2);
foreach ($files as $file) {
if (is_dir($dir . DIRECTORY_SEPARATOR . $file)) {
$path = self::findFile($filename, $dir . DIRECTORY_SEPARATOR . $file);
if ($path !== false) {
return $path;
}
}
}
return false;
}
public static function getCachedFile($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 setCachedFile($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));
}
}
Classes
Name |
Description |
SassFile |
SassFile class.
@package PHamlP
@subpackage Sass |