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
|
|
|
private $sourceDirectory; |
13
|
|
|
private $publicDirectory; |
14
|
|
|
private $sass; |
15
|
|
|
|
16
|
14 |
|
public function __construct( |
17
|
|
|
string $sourceDirectory, |
18
|
|
|
string $publicDirectory, |
19
|
|
|
Sass $sass |
20
|
|
|
) { |
21
|
14 |
|
$this->sourceDirectory = $sourceDirectory; |
22
|
14 |
|
$this->publicDirectory = $publicDirectory; |
23
|
14 |
|
$this->sass = $sass; |
24
|
14 |
|
} |
25
|
|
|
|
26
|
12 |
|
public function name(): string |
27
|
|
|
{ |
28
|
12 |
|
return 'css'; |
29
|
|
|
} |
30
|
|
|
|
31
|
7 |
|
public function inline(string $src): string |
32
|
|
|
{ |
33
|
7 |
|
[$url, $content] = $this->handle($src); |
|
|
|
|
34
|
|
|
|
35
|
7 |
|
return '<style>' . $content . '</style>'; |
36
|
|
|
} |
37
|
|
|
|
38
|
7 |
|
public function href(string $src): string |
39
|
|
|
{ |
40
|
7 |
|
[$url, $content] = $this->handle($src); |
|
|
|
|
41
|
|
|
|
42
|
7 |
|
return "<link rel=\"stylesheet\" href=\"{$url}\" />"; |
43
|
|
|
} |
44
|
|
|
|
45
|
9 |
|
public function handle(string $src): array |
46
|
|
|
{ |
47
|
9 |
|
$src = ltrim($src, '/'); |
48
|
|
|
|
49
|
9 |
|
['dirname' => $dirname, 'filename' => $filename, 'extension' => $extension] = pathinfo($src); |
|
|
|
|
50
|
|
|
|
51
|
9 |
|
$content = File::read("{$this->sourceDirectory}/{$src}"); |
52
|
|
|
|
53
|
9 |
|
if (in_array($extension, ['scss', 'sass'])) { |
|
|
|
|
54
|
8 |
|
$content = $this->sass->compile($content); |
55
|
8 |
|
$extension = 'css'; |
56
|
|
|
} |
57
|
|
|
|
58
|
9 |
|
$url = "{$dirname}/{$filename}.{$extension}"; |
|
|
|
|
59
|
|
|
|
60
|
9 |
|
$fs = new Filesystem(); |
61
|
|
|
|
62
|
9 |
|
$fs->dumpFile( |
63
|
9 |
|
File::path("{$this->publicDirectory}/{$url}"), |
64
|
9 |
|
$content |
65
|
|
|
); |
66
|
|
|
|
67
|
9 |
|
return ["/{$url}", $content]; |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
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.