/** * Parse this node. * If the node is a CSS import return the CSS import rule. * Else returns the rendered tree for the file. * * @param Context $context the context in which this node is parsed * @return array the parsed node */ public function parse($context) { $imported = []; foreach ($this->files as $file) { if (preg_match(self::MATCH_CSS, $file, $matches)) { $file = isset($matches[2]) && $matches[2] == 'url' ? $matches[1] : "url('{$file}')"; return [new SassString("@import {$file};"), new SassString("\n")]; } $file = trim($file, '\'"'); $files = \PHPSass\File::get_file($file, $this->parser); $tree = []; if ($files) { if ($this->token->level > 0) { $tree = $this->parent; while (get_class($tree) != 'PHPSass\\Tree\\RuleNode' && get_class($tree) != 'PHPSass\\Tree\\RootNode' && isset($tree->parent)) { $tree = $tree->parent; } $tree = clone $tree; $tree->children = []; } else { $tree = new RootNode($this->parser); $tree->extend_parent = $this->parent; } foreach ($files as $subfile) { if (preg_match(self::MATCH_CSS, $subfile)) { $tree->addChild(new SassString("@import url('{$subfile}');")); } else { $this->parser->filename = $subfile; $subtree = \PHPSass\File::get_tree($subfile, $this->parser); foreach ($subtree->getChildren() as $child) { $tree->addChild($child); } } } } if (!empty($tree)) { # parent may be either RootNode (returns an object) or RuleNode (returns an array of nodes) # so we parse then try get the children. $parsed = $tree->parse($context); if (!is_array($parsed) && isset($parsed->children)) { $parsed = $parsed->children; } if (is_array($parsed)) { $imported = array_merge($imported, $parsed); } } } return $imported; }
/** * @param string $path * @param bool $only_path * @param bool $web_path * @return SassString * @throws Exception */ public static function compassUrl($path, $only_path = FALSE, $web_path = TRUE) { $opath = $path; if (!($path = \PHPSass\File::get_file($path, \PHPSass\Parser::$instance, FALSE))) { throw new Exception('File not found: ' . $opath); } $path = $path[0]; if ($web_path) { $webroot = realpath($_SERVER['DOCUMENT_ROOT']); $path = str_replace($webroot, '', $path); } if ($only_path) { return new SassString($path); } return new SassString("url('{$path}')"); }