You are here

public function ScssCompilerService::buildCompilationFileInfo in SCSS/Less Compiler 8

Returns compile file info.

Parameters

array $info: An associative array containing:

  • namespace: theme/module name. Required.
  • data: source file path. Required.
  • css_path: custom destination css path. Optional.

Return value

array|null Compile file info:

  • name: filename.
  • namespace: theme/module name.
  • source_path: source file path.
  • css_path: css file destination path.

or NULL if source data is incorrect.

Overrides ScssCompilerInterface::buildCompilationFileInfo

File

src/ScssCompilerService.php, line 280

Class

ScssCompilerService
Defines a class for scss compiler service.

Namespace

Drupal\scss_compiler

Code

public function buildCompilationFileInfo(array $info) {
  try {
    if (empty($info['data']) || empty($info['namespace'])) {
      $error_message = $this
        ->t('Compilation file info build is failed. Required parameters are missing.');
      throw new \Exception($error_message);
    }
    $namespace_path = $this
      ->getNamespacePath($info['namespace']);
    $assets_path = '';
    if (isset($info['assets_path'])) {
      if (substr($info['assets_path'], 0, 1) === '@') {
        $assets_path = '/' . trim($this
          ->replaceTokens($info['assets_path']), '/. ') . '/';
      }
    }
    elseif (!empty($namespace_path)) {
      $assets_path = '/' . trim($namespace_path, '/') . '/';
    }
    $name = pathinfo($info['data'], PATHINFO_FILENAME);
    if (!empty($info['css_path'])) {
      if (substr($info['css_path'], 0, 1) === '@') {
        $css_path = trim($this
          ->replaceTokens($info['css_path']), '/. ') . '/' . $name . '.css';
      }
      elseif (!empty($namespace_path)) {
        $css_path = $namespace_path . '/' . trim($info['css_path'], '/. ') . '/' . $name . '.css';
      }
    }
    if (!isset($css_path)) {

      // Get source file path relative to theme/module and add it to css path
      // to prevent overwriting files when two source files with the same name
      // defined in different folders.
      $source_folder = dirname($info['data']);
      if (substr($source_folder, 0, strlen($namespace_path)) === $namespace_path) {
        $internal_folder = substr($source_folder, strlen($namespace_path));
        $css_path = $this
          ->getCacheFolder() . '/' . $info['namespace'] . '/' . trim($internal_folder, '/ ') . '/' . $name . '.css';
      }
      else {
        $css_path = $this
          ->getCacheFolder() . '/' . $info['namespace'] . '/' . $name . '.css';
      }
    }
    return [
      'name' => $name,
      'namespace' => $info['namespace'],
      'assets_path' => $assets_path,
      'source_path' => $info['data'],
      'css_path' => $css_path,
    ];
  } catch (\Exception $e) {
    $this
      ->messenger()
      ->addError($e
      ->getMessage());
  }
}