1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Stitcher\Renderer\Extension; |
4
|
|
|
|
5
|
|
|
use Leafo\ScssPhp\Compiler as Sass; |
6
|
|
|
use Pageon\Html\Source; |
7
|
|
|
use Stitcher\File; |
8
|
|
|
use Stitcher\Renderer\Extension; |
9
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
10
|
|
|
use tubalmartin\CssMin\Minifier; |
11
|
|
|
|
12
|
|
|
class Css implements Extension |
13
|
|
|
{ |
14
|
|
|
/** @var string */ |
15
|
|
|
protected $publicDirectory; |
16
|
|
|
|
17
|
|
|
/** @var \Leafo\ScssPhp\Compiler */ |
18
|
|
|
protected $sass; |
19
|
|
|
|
20
|
|
|
/** @var \tubalmartin\CssMin\Minifier */ |
21
|
|
|
private $minifier; |
22
|
|
|
|
23
|
|
|
/** @var bool */ |
24
|
|
|
private $minify = false; |
25
|
|
|
|
26
|
9 |
|
public function __construct( |
27
|
|
|
string $publicDirectory, |
28
|
|
|
Sass $sass, |
29
|
|
|
Minifier $minifier |
30
|
|
|
) { |
31
|
9 |
|
$this->publicDirectory = $publicDirectory; |
32
|
9 |
|
$this->sass = $sass; |
33
|
9 |
|
$this->minifier = $minifier; |
34
|
9 |
|
} |
35
|
|
|
|
36
|
9 |
|
public function setMinify(bool $minify): self |
37
|
|
|
{ |
38
|
9 |
|
$this->minify = $minify; |
39
|
|
|
|
40
|
9 |
|
return $this; |
41
|
|
|
} |
42
|
|
|
|
43
|
5 |
|
public function name(): string |
44
|
|
|
{ |
45
|
5 |
|
return 'css'; |
46
|
|
|
} |
47
|
|
|
|
48
|
3 |
|
public function link(string $src): string |
49
|
|
|
{ |
50
|
3 |
|
$source = $this->parseSource($src); |
51
|
|
|
|
52
|
3 |
|
return "<link rel=\"stylesheet\" href=\"{$source->url()}\" />"; |
53
|
|
|
} |
54
|
|
|
|
55
|
3 |
|
public function inline(string $src): string |
56
|
|
|
{ |
57
|
3 |
|
$source = $this->parseSource($src); |
58
|
|
|
|
59
|
3 |
|
return '<style>' . $source->content() . '</style>'; |
60
|
|
|
} |
61
|
|
|
|
62
|
6 |
|
public function parseSource(string $src): Source |
63
|
|
|
{ |
64
|
6 |
|
$src = ltrim($src, '/'); |
65
|
|
|
|
66
|
6 |
|
['dirname' => $dirname, 'filename' => $filename, 'extension' => $extension] = pathinfo($src); |
|
|
|
|
67
|
|
|
|
68
|
6 |
|
$content = File::read($src); |
69
|
|
|
|
70
|
6 |
|
if (\in_array($extension, ['scss', 'sass'])) { |
|
|
|
|
71
|
4 |
|
$content = $this->sass->compile($content); |
72
|
4 |
|
$extension = 'css'; |
73
|
|
|
} |
74
|
|
|
|
75
|
6 |
|
if ($this->minify) { |
76
|
|
|
$content = $this->minifier->run($content); |
77
|
|
|
} |
78
|
|
|
|
79
|
6 |
|
$path = "{$dirname}/{$filename}.{$extension}"; |
|
|
|
|
80
|
|
|
|
81
|
6 |
|
$this->saveFile($path, $content); |
82
|
|
|
|
83
|
6 |
|
return new Source("/{$path}", $content); |
84
|
|
|
} |
85
|
|
|
|
86
|
6 |
|
protected function saveFile(string $path, string $content): void |
87
|
|
|
{ |
88
|
6 |
|
$fs = new Filesystem(); |
89
|
|
|
|
90
|
6 |
|
$fs->dumpFile( |
91
|
6 |
|
File::path("{$this->publicDirectory}/{$path}"), |
92
|
6 |
|
$content |
93
|
|
|
); |
94
|
6 |
|
} |
95
|
|
|
} |
96
|
|
|
|
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.