browser_development-8.x-1.x-dev/src/Processing/ScssCompiler.php

src/Processing/ScssCompiler.php
<?php

namespace Drupal\browser_development\Processing;

use ScssPhp\ScssPhp\Compiler;
use ScssPhp\ScssPhp\OutputStyle;

/**
 * Class ScssCompiler compiles SCSS to CSS.
 *
 * @package Drupal\browser_development\Processing
 */
class ScssCompiler extends FileSystemStructure {

  /**
   * Data that needs to be parsed to be compiled.
   *
   * @var array
   *   Adds data for SCSS deployment.
   */
  protected array $data;

  /**
   * Array that hold files names so they can be compiled.
   *
   * @var array
   *   Compile order data for SCSS deployment.
   */
  protected array $compileOrderArray = [];

  /**
   * End result after compiler is completed.
   *
   * @var string
   *    CSS string created by SCSS Compiler.
   */
  protected string $compiledScss;

  /**
   * Clears SCSS directory before compiling SCSS.
   */
  protected function clearDirectory(): void {
    $directory = $this->globalFilePathArray['scss_directory'];
    $path = $this->absolutePath . DIRECTORY_SEPARATOR . $directory . DIRECTORY_SEPARATOR . "*";

    $files = glob($path);
    foreach ($files as $file) {
      if (is_file($file)) {
        unlink($file);
      }
    }
  }

  /**
   * Creates files with data before it gets compiled.
   *
   * @param array $file
   *   Array that holds filename and data for the file.
   */
  protected function createFilesAndAddData(array $file): void {
    if (empty($file['title'])) {
      $filePlaceHolder = rand(1, 999);
    }
    else {
      $filePlaceHolder = $file['title'];
    }

    $fileName = str_replace(' ', '-', strtolower($filePlaceHolder) . ".scss");
    $this->compileOrderArray[] = $fileName;
    $directory = $this->globalFilePathArray['scss_directory'];
    $path = $this->absolutePath . DIRECTORY_SEPARATOR . $directory . DIRECTORY_SEPARATOR . $fileName;
    $fileOpen = fopen($path, "wb");
    fwrite($fileOpen, $file['code']);
    fclose($fileOpen);
    chmod($path, 0755);
  }

  /**
   * Creates main.scss import file and then compiles files.
   */
  protected function compileFiles() {
    $fileName = "main.scss";
    $directory = $this->globalFilePathArray['scss_directory'];
    $path = $this->absolutePath . DIRECTORY_SEPARATOR . $directory . DIRECTORY_SEPARATOR . $fileName;

    $fileOpen = fopen($path, "wb");
    foreach ($this->compileOrderArray as $fileOrder) {
      fwrite($fileOpen, "@import \"$fileOrder\"; ");
    }
    fclose($fileOpen);

    $compiler = new Compiler();
    $compiler->setOutputStyle(OutputStyle::COMPRESSED);
    $compiler->setImportPaths($this->absolutePath . DIRECTORY_SEPARATOR . $directory . DIRECTORY_SEPARATOR);
    $this->compiledScss = $compiler->compileString('@import "main.scss";')->getCss();
  }

  /**
   * Compiles SCSS to CSS.
   *
   * @param array $data
   *   Data to be compiled.
   *
   * @return array
   *   Returns compiled response to user.
   */
  public function compiler(array $data): array {
    $this->data = $data;
    $this->clearDirectory();

    foreach ($this->data['compiled'] as $file) {
      $this->createFilesAndAddData($file);
    }
    $this->compileFiles();

    new SavingCssToDisk($this->compiledScss);

    return [
      "compiled_response" => $this->compiledScss,
    ];
  }

}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc