1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Stitcher\Renderer\Extension; |
4
|
|
|
|
5
|
|
|
use Leafo\ScssPhp\Compiler as Sass; |
6
|
|
|
use Stitcher\File; |
7
|
|
|
use Stitcher\Renderer\Extension; |
8
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
9
|
|
|
|
10
|
|
|
class Css implements Extension |
11
|
|
|
{ |
12
|
|
|
protected $sourceDirectory; |
13
|
|
|
protected $publicDirectory; |
14
|
|
|
protected $sass; |
15
|
|
|
|
16
|
16 |
|
public function __construct( |
17
|
|
|
string $sourceDirectory, |
18
|
|
|
string $publicDirectory, |
19
|
|
|
Sass $sass |
20
|
|
|
) { |
21
|
16 |
|
$this->sourceDirectory = $sourceDirectory; |
22
|
16 |
|
$this->publicDirectory = $publicDirectory; |
23
|
16 |
|
$this->sass = $sass; |
24
|
16 |
|
} |
25
|
|
|
|
26
|
12 |
|
public function name(): string |
27
|
|
|
{ |
28
|
12 |
|
return 'css'; |
29
|
|
|
} |
30
|
|
|
|
31
|
8 |
|
public function link(string $src): string |
32
|
|
|
{ |
33
|
8 |
|
[$url, $content] = $this->parseSource($src); |
|
|
|
|
34
|
|
|
|
35
|
8 |
|
return "<link rel=\"stylesheet\" href=\"{$url}\" />"; |
36
|
|
|
} |
37
|
|
|
|
38
|
8 |
|
public function inline(string $src): string |
39
|
|
|
{ |
40
|
8 |
|
[$url, $content] = $this->parseSource($src); |
|
|
|
|
41
|
|
|
|
42
|
8 |
|
return '<style>' . $content . '</style>'; |
43
|
|
|
} |
44
|
|
|
|
45
|
11 |
|
public function parseSource(string $src): array |
46
|
|
|
{ |
47
|
11 |
|
$src = ltrim($src, '/'); |
48
|
|
|
|
49
|
11 |
|
['dirname' => $dirname, 'filename' => $filename, 'extension' => $extension] = pathinfo($src); |
|
|
|
|
50
|
|
|
|
51
|
11 |
|
$content = File::read("{$this->sourceDirectory}/{$src}"); |
52
|
|
|
|
53
|
11 |
|
if (in_array($extension, ['scss', 'sass'])) { |
|
|
|
|
54
|
9 |
|
$content = $this->sass->compile($content); |
55
|
9 |
|
$extension = 'css'; |
56
|
|
|
} |
57
|
|
|
|
58
|
11 |
|
$path = "{$dirname}/{$filename}.{$extension}"; |
|
|
|
|
59
|
|
|
|
60
|
11 |
|
$this->saveFile($path, $content); |
61
|
|
|
|
62
|
11 |
|
return ["/{$path}", $content]; |
63
|
|
|
} |
64
|
|
|
|
65
|
11 |
|
protected function saveFile(string $path, string $content): void |
66
|
|
|
{ |
67
|
11 |
|
$fs = new Filesystem(); |
68
|
|
|
|
69
|
11 |
|
$fs->dumpFile( |
70
|
11 |
|
File::path("{$this->publicDirectory}/{$path}"), |
71
|
11 |
|
$content |
72
|
|
|
); |
73
|
11 |
|
} |
74
|
|
|
} |
75
|
|
|
|
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.