|
| 1 | +<?php declare(strict_types=1); |
| 2 | + |
| 3 | +namespace Bugo\Sass; |
| 4 | + |
| 5 | +use Symfony\Component\Process\Process; |
| 6 | + |
| 7 | +use function array_merge; |
| 8 | +use function basename; |
| 9 | +use function file_exists; |
| 10 | +use function file_get_contents; |
| 11 | +use function file_put_contents; |
| 12 | +use function getcwd; |
| 13 | +use function is_array; |
| 14 | +use function is_dir; |
| 15 | +use function json_decode; |
| 16 | +use function json_encode; |
| 17 | +use function realpath; |
| 18 | +use function strtoupper; |
| 19 | +use function substr; |
| 20 | +use function sys_get_temp_dir; |
| 21 | +use function tempnam; |
| 22 | +use function trim; |
| 23 | + |
| 24 | +class Compiler implements CompilerInterface |
| 25 | +{ |
| 26 | + protected array $options = []; |
| 27 | + |
| 28 | + public function __construct(protected ?string $bridgePath = null, protected ?string $nodePath = null) |
| 29 | + { |
| 30 | + $this->bridgePath = $bridgePath ?? __DIR__ . '/../bin/bridge.js'; |
| 31 | + $this->nodePath = $nodePath ?? $this->findNode(); |
| 32 | + $this->checkEnvironment(); |
| 33 | + } |
| 34 | + |
| 35 | + public function setOptions(array $options): static |
| 36 | + { |
| 37 | + $this->options = $options; |
| 38 | + |
| 39 | + return $this; |
| 40 | + } |
| 41 | + |
| 42 | + public function getOptions(): array |
| 43 | + { |
| 44 | + return $this->options; |
| 45 | + } |
| 46 | + |
| 47 | + public function compileString(string $source, array $options = []): string |
| 48 | + { |
| 49 | + if (trim($source) === '') { |
| 50 | + return ''; |
| 51 | + } |
| 52 | + |
| 53 | + $options = array_merge($this->options, $options); |
| 54 | + |
| 55 | + if (! isset($merged['url'])) { |
| 56 | + $options['url'] = 'file://' . getcwd() . '/string.scss'; |
| 57 | + } |
| 58 | + |
| 59 | + return $this->compileSource($source, $options); |
| 60 | + } |
| 61 | + |
| 62 | + public function compileFile(string $filePath, array $options = []): string |
| 63 | + { |
| 64 | + if (! file_exists($filePath)) { |
| 65 | + throw new Exception("File not found: $filePath"); |
| 66 | + } |
| 67 | + |
| 68 | + $content = $this->readFile($filePath); |
| 69 | + if ($content === false) { |
| 70 | + throw new Exception("Unable to read file: $filePath"); |
| 71 | + } |
| 72 | + |
| 73 | + if (trim($content) === '') { |
| 74 | + return ''; |
| 75 | + } |
| 76 | + |
| 77 | + $options = array_merge($this->options, $options); |
| 78 | + |
| 79 | + if (! isset($options['url'])) { |
| 80 | + $options['url'] = 'file://' . realpath($filePath); |
| 81 | + } |
| 82 | + |
| 83 | + return $this->compileSource($content, $options); |
| 84 | + } |
| 85 | + |
| 86 | + protected function compileSource(string $source, array $options): string |
| 87 | + { |
| 88 | + $payload = [ |
| 89 | + 'source' => $source, |
| 90 | + 'options' => $options, |
| 91 | + 'url' => $options['url'], |
| 92 | + ]; |
| 93 | + |
| 94 | + return $this->runCompile($payload); |
| 95 | + } |
| 96 | + |
| 97 | + protected function runCompile(array $payload): string |
| 98 | + { |
| 99 | + $cmd = [$this->nodePath, $this->bridgePath, '--stdin']; |
| 100 | + |
| 101 | + $process = $this->createProcess($cmd); |
| 102 | + $process->setInput(json_encode($payload)); |
| 103 | + $process->run(); |
| 104 | + |
| 105 | + $out = trim($process->getOutput()); |
| 106 | + if ($out === '') { |
| 107 | + $err = trim($process->getErrorOutput()); |
| 108 | + throw new Exception('Sass process failed: ' . ($err ?: 'unknown error')); |
| 109 | + } |
| 110 | + |
| 111 | + $data = json_decode($out, true); |
| 112 | + if (! is_array($data)) { |
| 113 | + throw new Exception('Invalid response from sass bridge'); |
| 114 | + } |
| 115 | + |
| 116 | + if (! empty($data['error'])) { |
| 117 | + throw new Exception('Sass parsing error: ' . $data['error']); |
| 118 | + } |
| 119 | + |
| 120 | + $css = $data['css'] ?? ''; |
| 121 | + |
| 122 | + if (! empty($payload['options']['sourceMap']) && ! empty($data['sourceMap'])) { |
| 123 | + $mapFile = tempnam(sys_get_temp_dir(), 'sass_') . '.map'; |
| 124 | + file_put_contents($mapFile, json_encode($data['sourceMap'])); |
| 125 | + $css .= "\n/*# sourceMappingURL=" . basename($mapFile) . " */"; |
| 126 | + } |
| 127 | + |
| 128 | + return $css; |
| 129 | + } |
| 130 | + |
| 131 | + protected function readFile(string $path): string|false |
| 132 | + { |
| 133 | + return file_get_contents($path); |
| 134 | + } |
| 135 | + |
| 136 | + protected function createProcess(array $command): Process |
| 137 | + { |
| 138 | + return new Process($command); |
| 139 | + } |
| 140 | + |
| 141 | + protected function findNode(): string |
| 142 | + { |
| 143 | + $candidates = ['node']; |
| 144 | + if ($this->isWindows()) { |
| 145 | + $candidates[] = 'C:\\Program Files\\nodejs\\node.exe'; |
| 146 | + $candidates[] = 'C:\\Program Files (x86)\\nodejs\\node.exe'; |
| 147 | + } else { |
| 148 | + $candidates[] = '/usr/local/bin/node'; |
| 149 | + $candidates[] = '/usr/bin/node'; |
| 150 | + $candidates[] = '/opt/homebrew/bin/node'; |
| 151 | + } |
| 152 | + |
| 153 | + foreach ($candidates as $node) { |
| 154 | + $process = $this->createProcess([$node, '--version']); |
| 155 | + $process->run(); |
| 156 | + if ($process->isSuccessful()) { |
| 157 | + return $node; |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + throw new Exception( |
| 162 | + "Node.js not found. Please install Node.js >= 18 and make sure it's in PATH, " . |
| 163 | + "or pass its full path to your Compiler constructor." |
| 164 | + ); |
| 165 | + } |
| 166 | + |
| 167 | + protected function isWindows(): bool |
| 168 | + { |
| 169 | + return strtoupper(substr(PHP_OS, 0, 3)) === 'WIN'; |
| 170 | + } |
| 171 | + |
| 172 | + protected function checkEnvironment(): void |
| 173 | + { |
| 174 | + if (! file_exists($this->bridgePath)) { |
| 175 | + throw new Exception("bridge.js not found at $this->bridgePath"); |
| 176 | + } |
| 177 | + |
| 178 | + $nodeModules = $this->getPackageRoot() . '/node_modules/sass-embedded'; |
| 179 | + if (! is_dir($nodeModules)) { |
| 180 | + throw new Exception("sass-embedded not found. Run `npm install` in {$this->getPackageRoot()}."); |
| 181 | + } |
| 182 | + } |
| 183 | + |
| 184 | + protected function getPackageRoot(): string |
| 185 | + { |
| 186 | + return realpath(__DIR__ . '/../'); |
| 187 | + } |
| 188 | +} |
0 commit comments