You are here

class SassCompiler in Sassy 7

Hierarchy

Expanded class hierarchy of SassCompiler

File

phamlp/sassc.php, line 45

View source
class SassCompiler {
  private $_sass;
  private $_path;
  private $_updates;
  public function __construct() {
    require_once dirname(__FILE__) . '/sass/SassParser.php';
    $this->_sass = new SassParser(array(
      'extensions' => array(
        'compass' => array(),
      ),
      'style' => SassRenderer::STYLE_EXPANDED,
      'syntax' => SassFile::SCSS,
    ));
  }
  public function loop() {
    while (1) {
      $this
        ->compile();
      sleep(1);
    }
  }
  public function compile() {
    try {
      clearstatcache();
      foreach ($this
        ->getFiles() as $i => $file) {
        $fname = $file['filename'];
        if (!isset($this->_updates[$fname]) || filemtime($file['filename']) > $this->_updates[$fname]) {
          $c = file_get_contents($file['filename']);
          if (empty($c)) {
            continue;
          }
          $result = $this->_sass
            ->toCss($c, false);
          $newfile = substr($fname, 0, -5) . '.css';
          file_put_contents($newfile, $result);
          $this->_updates[$fname] = filemtime($file['filename']);
          echo "save file {$newfile}\n";
        }
      }
    } catch (Exception $e) {
      $result = $e
        ->getMessage();
      echo "Error: " . $result;
    }
  }
  public function setPath($path) {
    $this->_path = $path;
  }
  public function getFiles() {
    $files = array();
    if (is_dir($this->_path)) {
      if ($handle = opendir($this->_path)) {
        while (($file = readdir($handle)) !== false) {
          if (substr($file, -5) === '.scss' && is_file($this->_path . DIRECTORY_SEPARATOR . $file)) {
            $files[] = array(
              'filename' => $this->_path . DIRECTORY_SEPARATOR . $file,
            );
          }
        }
      }
    }
    else {
      if (is_file($this->_path)) {
        $files[] = array(
          'filename' => $file,
        );
      }
    }
    return $files;
  }
  public function addDir($dir) {
    if (is_dir($dir)) {
      if ($handle = opendir($dir)) {
        while (($file = readdir($handle)) !== false) {
          if (substr($file, -5) === '.scss' && is_file($dir . DIRECTORY_SEPARATOR . $file)) {
            $this->_files[] = array(
              'filename' => $dir . DIRECTORY_SEPARATOR . $file,
            );
          }
        }
      }
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
SassCompiler::$_path private property
SassCompiler::$_sass private property
SassCompiler::$_updates private property
SassCompiler::addDir public function
SassCompiler::compile public function
SassCompiler::getFiles public function
SassCompiler::loop public function
SassCompiler::setPath public function
SassCompiler::__construct public function