You are here

public function ScssphpCompiler::getImportNamespace in SCSS/Less Compiler 8

Processes the import paths using prefixed module/theme.

Parameters

string $path: The import path to process.

Return value

string|null Path to file or NULL if path not found.

File

src/Plugin/ScssCompiler/ScssphpCompiler.php, line 169

Class

ScssphpCompiler
Plugin implementation of the Scss compiler.

Namespace

Drupal\scss_compiler\Plugin\ScssCompiler

Code

public function getImportNamespace($path) {
  if (!preg_match('#([^/]+)/#', $path, $match)) {
    return NULL;
  }
  $namespace = $match[1];

  // Prevent name conflicts when module/theme name same as subfolder name,
  // use @module to import from module.
  if (substr($namespace, 0, 1) === '@') {
    $namespace = substr($namespace, 1);
  }
  $namespace_path = substr($path, strlen($match[0]));
  $type = 'theme';
  if ($this->moduleHandler
    ->moduleExists($namespace)) {
    $type = 'module';
  }
  $path = @drupal_get_path($type, $namespace);
  if (empty($path)) {
    return NULL;
  }

  // Try different extensions to allow for import from the usual scss sources.
  $base_path = DRUPAL_ROOT . '/' . $path . '/' . $namespace_path;
  foreach ([
    '',
    '.scss',
    '.css',
  ] as $extension) {
    if ($path = realpath($base_path . $extension)) {
      return $path;
    }
  }
  return NULL;
}