Leafo\ScssPhp\Compiler::findImport PHP Method

findImport() public method

Return the file path for an import url if it exists
public findImport ( string $url ) : string | null
$url string
return string | null
    public function findImport($url)
    {
        $urls = [];
        // for "normal" scss imports (ignore vanilla css and external requests)
        if (!preg_match('/\\.css$|^https?:\\/\\//', $url)) {
            // try both normal and the _partial filename
            $urls = [$url, preg_replace('/[^\\/]+$/', '_\\0', $url)];
        }
        foreach ($this->importPaths as $dir) {
            if (is_string($dir)) {
                // check urls for normal import paths
                foreach ($urls as $full) {
                    $full = $dir . (!empty($dir) && substr($dir, -1) !== '/' ? '/' : '') . $full;
                    if ($this->fileExists($file = $full . '.scss') || $this->fileExists($file = $full)) {
                        return $file;
                    }
                }
            } elseif (is_callable($dir)) {
                // check custom callback for import path
                $file = call_user_func($dir, $url);
                if ($file !== null) {
                    return $file;
                }
            }
        }
        return null;
    }

Usage Example

 public function getChildren(AssetFactory $factory, $content, $loadPath = null)
 {
     $sc = new Compiler();
     $sc->addImportPath($loadPath);
     foreach ($this->importPaths as $path) {
         $sc->addImportPath($path);
     }
     $children = array();
     foreach (CssUtils::extractImports($content) as $match) {
         $file = $sc->findImport($match);
         if ($file) {
             $children[] = $child = $factory->createAsset($file, array(), array('root' => $loadPath));
             $child->load();
             $children = array_merge($children, $this->getChildren($factory, $child->getContent(), $loadPath));
         }
     }
     return $children;
 }
Compiler