SassParser::parse PHP Method

parse() public method

The file will be searched for in the directories specified by the load_paths option.
public parse ( string $source, boolean $isFile = true ) : SassRootNode
$source string name of source file or Sass source
$isFile boolean
return SassRootNode Root node of document tree
    public function parse($source, $isFile = true)
    {
        # Richard Lyon - 2011-10-25 - ignore unfound files
        # Richard Lyon - 2011-10-25 - add multiple files to load functions
        if (!$source) {
            return $this->toTree($source);
        }
        if (is_array($source)) {
            $return = null;
            foreach ($source as $key => $value) {
                if (is_numeric($key)) {
                    $code = $value;
                    $type = true;
                } else {
                    $code = $key;
                    $type = $value;
                }
                if ($return === null) {
                    $return = $this->parse($code, $type);
                } else {
                    /** @var SassNode $return */
                    $newNode = $this->parse($code, $type);
                    foreach ($newNode->children as $children) {
                        array_push($return->children, $children);
                    }
                }
            }
            return $return;
        }
        if ($isFile && ($files = SassFile::get_file($source, $this))) {
            $files_source = '';
            foreach ($files as $file) {
                $this->filename = $file;
                $this->syntax = substr(strrchr($file, '.'), 1);
                if ($this->syntax == SassFile::CSS) {
                    $this->property_syntax = "css";
                } elseif (!$this->property_syntax && $this->syntax == SassFile::SCSS) {
                    $this->property_syntax = "scss";
                }
                if ($this->syntax !== SassFile::SASS && $this->syntax !== SassFile::SCSS && $this->syntax !== SassFile::CSS) {
                    if ($this->debug) {
                        throw new SassException('Invalid {what}', array('{what}' => 'syntax option'));
                    }
                    return FALSE;
                }
                $files_source .= SassFile::get_file_contents($this->filename);
            }
            return $this->toTree($files_source);
        } else {
            return $this->toTree($source);
        }
    }

Usage Example

Example #1
0
 /**
  * Returns the parse tree for a file.
  * If caching is enabled a cached version will be used if possible; if not the
  * parsed file will be cached.
  * @param string filename to parse
  * @param array parse options
  * @return SassRootNode
  */
 public static function getTree($filename, $options)
 {
     if ($options['cache']) {
         $cached = self::getCachedFile($filename, $options);
         if ($cached !== false) {
             return $cached;
         }
     }
     $parser = new SassParser($options);
     $tree = $parser->parse($filename);
     if ($options['cache']) {
         self::setCachedFile($tree, $filename, $options);
     }
     return $tree;
 }
All Usage Examples Of SassParser::parse