|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Stitcher\Renderer\Extension; |
|
4
|
|
|
|
|
5
|
|
|
use JSMin\JSMin; |
|
6
|
|
|
use Pageon\Html\Source; |
|
7
|
|
|
use Stitcher\Exception\InvalidConfiguration; |
|
8
|
|
|
use Stitcher\File; |
|
9
|
|
|
use Stitcher\Renderer\Extension; |
|
10
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
|
11
|
|
|
|
|
12
|
|
|
class Js implements Extension |
|
13
|
|
|
{ |
|
14
|
|
|
/** @var string */ |
|
15
|
|
|
protected $publicDirectory; |
|
16
|
|
|
|
|
17
|
|
|
/** @var bool */ |
|
18
|
|
|
protected $defer = false; |
|
19
|
|
|
|
|
20
|
|
|
/** @var bool */ |
|
21
|
|
|
protected $async = false; |
|
22
|
|
|
|
|
23
|
|
|
/** @var bool */ |
|
24
|
|
|
protected $minify = false; |
|
25
|
|
|
|
|
26
|
10 |
|
public function __construct( |
|
27
|
|
|
string $publicDirectory |
|
28
|
|
|
) { |
|
29
|
10 |
|
$this->publicDirectory = $publicDirectory; |
|
30
|
10 |
|
} |
|
31
|
|
|
|
|
32
|
5 |
|
public function setMinify(bool $minify): self |
|
33
|
|
|
{ |
|
34
|
5 |
|
$this->minify = $minify; |
|
35
|
|
|
|
|
36
|
5 |
|
return $this; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
5 |
|
public function name(): string |
|
40
|
|
|
{ |
|
41
|
5 |
|
return 'js'; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
5 |
|
public function link(string $src): string |
|
45
|
|
|
{ |
|
46
|
5 |
|
$source = $this->parseSource($src); |
|
47
|
|
|
|
|
48
|
5 |
|
$script = "<script src=\"{$source->url()}\""; |
|
49
|
|
|
|
|
50
|
5 |
|
if ($this->defer) { |
|
51
|
3 |
|
$script .= ' defer'; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
5 |
|
if ($this->async) { |
|
55
|
1 |
|
$script .= ' async'; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
5 |
|
$script .= '></script>'; |
|
59
|
|
|
|
|
60
|
5 |
|
return $script; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
1 |
|
public function inline(string $src): string |
|
64
|
|
|
{ |
|
65
|
1 |
|
$source = $this->parseSource($src); |
|
66
|
|
|
|
|
67
|
1 |
|
return '<script>' . $source->content() . '</script>'; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
3 |
|
public function defer(): Js |
|
71
|
|
|
{ |
|
72
|
3 |
|
$this->defer = true; |
|
73
|
|
|
|
|
74
|
3 |
|
return $this; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
1 |
|
public function async(): Js |
|
78
|
|
|
{ |
|
79
|
1 |
|
$this->async = true; |
|
80
|
|
|
|
|
81
|
1 |
|
return $this; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
7 |
|
public function parseSource(string $src): Source |
|
85
|
|
|
{ |
|
86
|
7 |
|
$src = ltrim($src, '/'); |
|
87
|
|
|
|
|
88
|
7 |
|
['dirname' => $dirname, 'filename' => $filename, 'extension' => $extension] = pathinfo($src); |
|
|
|
|
|
|
89
|
|
|
|
|
90
|
7 |
|
$content = File::read($src); |
|
91
|
|
|
|
|
92
|
7 |
|
if (!$content) { |
|
93
|
|
|
throw InvalidConfiguration::fileNotFound(File::path($src)); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
7 |
|
if ($this->minify) { |
|
97
|
|
|
$content = JSMin::minify($content); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
7 |
|
$path = "{$dirname}/{$filename}.{$extension}"; |
|
101
|
|
|
|
|
102
|
7 |
|
$this->saveFile($path, $content); |
|
103
|
|
|
|
|
104
|
7 |
|
return new Source("/{$path}", $content); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
7 |
|
protected function saveFile(string $path, string $content): void |
|
108
|
|
|
{ |
|
109
|
7 |
|
$fs = new Filesystem(); |
|
110
|
|
|
|
|
111
|
7 |
|
$fs->dumpFile( |
|
112
|
7 |
|
File::path("{$this->publicDirectory}/{$path}"), |
|
113
|
7 |
|
$content |
|
114
|
|
|
); |
|
115
|
7 |
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|
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.