vendor/armin/scssphp-bundle/src/Scss/Job.php line 7

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Armin\ScssphpBundle\Scss;
  3. use ScssPhp\ScssPhp\Compiler;
  4. use ScssPhp\ScssPhp\Formatter\Crunched;
  5. class Job
  6. {
  7.     /**
  8.      * @var string
  9.      */
  10.     private $assetName;
  11.     /**
  12.      * @var array
  13.      */
  14.     private $configuration;
  15.     /**
  16.      * @var string absolute path to project directory (given from Kernel)
  17.      */
  18.     private $projectDir;
  19.     /**
  20.      * @var string
  21.      */
  22.     private $sourceFilePath;
  23.     /**
  24.      * @var string
  25.      */
  26.     private $destinationPath;
  27.     public function __construct(string $assetName, array $configurationstring $projectDir)
  28.     {
  29.         $this->assetName $assetName;
  30.         $this->configuration $configuration;
  31.         $this->projectDir $projectDir;
  32.         $this->sourceFilePath $this->buildSourceFilePath();
  33.         $this->destinationPath $this->buildDestinationPath();
  34.     }
  35.     public function getSourceFilePath(): string
  36.     {
  37.         return $this->sourceFilePath;
  38.     }
  39.     public function getSourceFileName(): string
  40.     {
  41.         return basename($this->getSourceFilePath());
  42.     }
  43.     protected function buildSourceFilePath(): string
  44.     {
  45.         $src $this->projectDir '/' $this->configuration['src'];
  46.         if (!file_exists($src)) {
  47.             throw new \InvalidArgumentException(
  48.                 'Given src "' $src '" for asset "' $this->assetName '" not found!!'
  49.             );
  50.         }
  51.         return realpath($src);
  52.     }
  53.     public function getDestinationPath(): string
  54.     {
  55.         return $this->destinationPath;
  56.     }
  57.     protected function buildDestinationPath(): string
  58.     {
  59.         return $this->projectDir '/' . ($this->configuration['outputFolder'] ?? '') . '/' $this->assetName;
  60.     }
  61.     public function execute(): Result
  62.     {
  63.         $timeTrackingStart microtime(true);
  64.         $compiler = new Compiler();
  65.         $compiler->setImportPaths($this->getImportPaths());
  66.         $compiler->setVariables($this->configuration['variables'] ?? []);
  67.         $compiler->setFormatter($this->configuration['formatter'] ?? Crunched::class);
  68.         if ($this->configuration['sourceMap']) {
  69.             $compiler->setSourceMap(Compiler::SOURCE_MAP_INLINE);
  70.         }
  71.         $result = new Result($this);
  72.         try {
  73.             $css $compiler->compile(
  74.                 '@import "' basename($this->sourceFilePath) . '";',
  75.                 dirname($this->sourceFilePath)
  76.             );
  77.         } catch (\Exception $exception) {
  78.             return $result->markAsFailed($exceptionmicrotime(true) - $timeTrackingStart);
  79.         }
  80.         if (isset($css) && !empty($css)) {
  81.             $this->writeResultToFile($css);
  82.         }
  83.         return $result->markAsSuccessful(
  84.             $compiler,
  85.             microtime(true) - $timeTrackingStart,
  86.             strlen($css)
  87.         );
  88.     }
  89.     private function getImportPaths(): array
  90.     {
  91.         $importPaths = [];
  92.         foreach ($this->configuration['importPaths'] ?? [] as $importPath) {
  93.             $realImportPath realpath($this->projectDir '/' $importPath);
  94.             if ($realImportPath) {
  95.                 $importPaths[] = $realImportPath;
  96.             }
  97.         }
  98.         return array_merge(array_merge($importPaths ?? [], [dirname($this->sourceFilePath)]));
  99.     }
  100.     private function writeResultToFile(string $result): void
  101.     {
  102.         if (!is_dir(dirname($this->destinationPath))) {
  103.             if (!mkdir($dir dirname($this->destinationPath), 0777true) && !is_dir($dir)) {
  104.                 throw new \RuntimeException('Directory "' $dir '" was not created');
  105.             }
  106.         }
  107.         if (!is_writable(dirname($this->destinationPath))) {
  108.             throw new \RuntimeException('Can\'t write to "' dirname($this->destinationPath) . '"!');
  109.         }
  110.         $status file_put_contents($this->destinationPath$result);
  111.         if (!$status || !file_exists($this->destinationPath)) {
  112.             throw new \RuntimeException('Error while writing to "' $this->destinationPath '".');
  113.         }
  114.     }
  115.     public function getAssetName(): string
  116.     {
  117.         return $this->assetName;
  118.     }
  119.     public function getSanitizedAssetName(): string
  120.     {
  121.         return Parser::sanitizeAssetName($this->getAssetName());
  122.     }
  123.     public function getConfiguration(): array
  124.     {
  125.         return $this->configuration;
  126.     }
  127. }