You are here

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

Compiles single scss file into css.

Parameters

array $scss_file: An associative array with scss file info.

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

bool $flush: If TRUE ignore last modified time.

Overrides ScssCompilerInterface::compile

1 call to ScssCompilerService::compile()
ScssCompilerService::compileAll in src/ScssCompilerService.php
Compiles all scss files which was registered.

File

src/ScssCompilerService.php, line 428

Class

ScssCompilerService
Defines a class for scss compiler service.

Namespace

Drupal\scss_compiler

Code

public function compile(array $source_file, $flush = FALSE) {
  try {
    if (!file_exists($source_file['source_path'])) {
      $error_message = $this
        ->t('File @path not found', [
        '@path' => $source_file['source_path'],
      ]);
      throw new \Exception($error_message);
    }
    $extension = pathinfo($source_file['source_path'], PATHINFO_EXTENSION);
    $plugins = $this->config
      ->get('plugins');
    if (!empty($plugins[$extension])) {
      $compiler = \Drupal::service('plugin.manager.scss_compiler')
        ->getInstanceById($plugins[$extension]);
    }
    if (empty($compiler)) {
      $error_message = $this
        ->t('Compiler for @extension extension not found', [
        '@extension' => $extension,
      ]);
      throw new \Exception($error_message);
    }

    // Replace all local stream wrappers by real path.
    foreach ([
      &$source_file['source_path'],
      &$source_file['css_path'],
    ] as &$path) {
      if (\Drupal::service('stream_wrapper_manager')
        ->getScheme($path)) {
        $wrapper = \Drupal::service('stream_wrapper_manager')
          ->getViaUri($path);
        if ($wrapper instanceof LocalStream) {
          $host = $this->request
            ->getSchemeAndHttpHost();
          $wrapper_path = $wrapper
            ->getExternalUrl();
          $path = trim(str_replace($host, '', $wrapper_path), '/');
        }
      }
    }
    $source_content = file_get_contents($source_file['source_path']);
    if ($this->config
      ->get('check_modify_time') && !$flush && !$this
      ->checkLastModifyTime($source_file, $source_content)) {
      return;
    }
    $content = $compiler
      ->compile($source_file);
    if (!empty($content)) {
      $css_folder = dirname($source_file['css_path']);
      $this->fileSystem
        ->prepareDirectory($css_folder, FileSystemInterface::CREATE_DIRECTORY);
      file_put_contents($source_file['css_path'], trim($content));
    }
  } catch (\Exception $e) {

    // If error occurrence during compilation, reset last modified time of the
    // file.
    if (!empty($this->lastModifyList[$source_file['source_path']])) {
      $this->lastModifyList[$source_file['source_path']] = 0;
    }
    $this
      ->messenger()
      ->addError($e
      ->getMessage());
  }
}