1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Brendt\Stitcher\Template; |
4
|
|
|
|
5
|
|
|
use Brendt\Image\ResponsiveFactory; |
6
|
|
|
use Brendt\Stitcher\Config; |
7
|
|
|
use Brendt\Stitcher\Factory\ParserFactory; |
8
|
|
|
use CSSmin; |
9
|
|
|
use JSMin; |
10
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
11
|
|
|
use Symfony\Component\Finder\Finder; |
12
|
|
|
use Symfony\Component\Finder\SplFileInfo; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* This class provides functionality which can be used by template plugins/functions. |
16
|
|
|
*/ |
17
|
|
|
class TemplatePlugin |
18
|
|
|
{ |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
private $publicDir; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
private $srcDir; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var ParserFactory |
32
|
|
|
*/ |
33
|
|
|
private $parserFactory; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var ResponsiveFactory |
37
|
|
|
*/ |
38
|
|
|
private $responsiveFactory; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var CSSmin |
42
|
|
|
*/ |
43
|
|
|
private $cssMinifier; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var array |
47
|
|
|
*/ |
48
|
|
|
private $meta; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @var bool |
52
|
|
|
*/ |
53
|
|
|
private $minify; |
54
|
|
|
|
55
|
|
|
public function __construct( |
56
|
|
|
ParserFactory $parserFactory, |
57
|
|
|
ResponsiveFactory $responsiveFactory, |
58
|
|
|
CSSmin $cssMinifier, |
59
|
|
|
string $publicDir, |
60
|
|
|
string $srcDir, |
61
|
|
|
$minify, |
62
|
|
|
$meta |
63
|
|
|
) { |
64
|
|
|
$this->parserFactory = $parserFactory; |
65
|
|
|
$this->responsiveFactory = $responsiveFactory; |
66
|
|
|
$this->cssMinifier = $cssMinifier; |
67
|
|
|
$this->publicDir = $publicDir; |
68
|
|
|
$this->srcDir = $srcDir; |
69
|
|
|
$this->minify = $minify; |
70
|
|
|
|
71
|
|
|
$this->meta = is_array($meta) ? $meta : [$meta]; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* This function will read meta configuration from `meta` and output the corresponding meta tags. |
76
|
|
|
* |
77
|
|
|
* @param array $meta |
78
|
|
|
* |
79
|
|
|
* @return string |
80
|
|
|
*/ |
81
|
|
|
public function meta(array $meta = []) : string { |
82
|
|
|
$result = []; |
83
|
|
|
|
84
|
|
|
$meta = array_merge($this->meta, $meta); |
85
|
|
|
|
86
|
|
|
foreach ($meta as $name => $content) { |
87
|
|
|
if (!is_string($content)) { |
88
|
|
|
continue; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
$result[] = "<meta name=\"{$name}\" content=\"{$content}\">"; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
return implode("\n", $result); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* This function will take a source path and an optional inline parameter. |
99
|
|
|
* The CSS file will be copied from the source path to the public directory. |
100
|
|
|
* If the `minify` option is set to true in config.yml, the output will be minified. |
101
|
|
|
* |
102
|
|
|
* If the inline parameter is set, the output won't be copied to a public file, |
103
|
|
|
* but instead be outputted to an HTML string which can be included in a template. |
104
|
|
|
* |
105
|
|
|
* Files with the .scss and .sass extensions will be compiled to normal CSS files. |
106
|
|
|
* |
107
|
|
|
* @param string $src |
108
|
|
|
* @param bool $inline |
109
|
|
|
* |
110
|
|
|
* @return string |
111
|
|
|
*/ |
112
|
|
|
public function css($src, $inline = false) { |
113
|
|
|
$parser = $this->parserFactory->getByFileName($src); |
114
|
|
|
$data = $parser->parse($src); |
115
|
|
|
|
116
|
|
|
if ($this->minify) { |
117
|
|
|
$data = $this->cssMinifier->run($data); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
if ($inline) { |
121
|
|
|
return "<style>{$data}</style>"; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
$srcParsed = preg_replace('/\.scss|\.sass/', '.css', $src); |
125
|
|
|
$fs = new Filesystem(); |
126
|
|
|
$dst = "{$this->publicDir}/$srcParsed"; |
127
|
|
|
|
128
|
|
|
if ($fs->exists($dst)) { |
129
|
|
|
$fs->remove($dst); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
$fs->dumpFile($dst, $data); |
133
|
|
|
|
134
|
|
|
return "<link rel=\"stylesheet\" type=\"text/css\" href=\"{$srcParsed}\">"; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* This function will take a source path and an optional inline parameter. |
139
|
|
|
* The JS file will be copied from the source path to the public directory. |
140
|
|
|
* If the `minify` option is set to true in config.yml, the output will be minified. |
141
|
|
|
* |
142
|
|
|
* If the inline parameter is set, the output won't be copied to a public file, |
143
|
|
|
* but instead be outputted to an HTML string which can be included in a template. |
144
|
|
|
* |
145
|
|
|
* @param string $src |
146
|
|
|
* @param bool $inline |
147
|
|
|
* @param bool $async |
148
|
|
|
* |
149
|
|
|
* @return string |
150
|
|
|
*/ |
151
|
|
|
public function js($src, $inline = false, $async = false) { |
152
|
|
|
$parser = $this->parserFactory->getByFileName($src); |
153
|
|
|
$data = $parser->parse($src); |
154
|
|
|
|
155
|
|
|
if ($this->minify) { |
156
|
|
|
$data = JSMin::minify($data); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
if ($inline) { |
160
|
|
|
return "<script>{$data}</script>"; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
$fs = new Filesystem(); |
164
|
|
|
$dst = "{$this->publicDir}/$src"; |
165
|
|
|
|
166
|
|
|
if ($fs->exists($dst)) { |
167
|
|
|
$fs->remove($dst); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
$fs->dumpFile($dst, $data); |
171
|
|
|
$result = "<script src=\"{$src}\""; |
172
|
|
|
|
173
|
|
|
if ($async) { |
174
|
|
|
$result .= ' async'; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
$result .= "></script>"; |
178
|
|
|
|
179
|
|
|
return $result; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Create a responsive image using brendt\responsive-images. |
184
|
|
|
* |
185
|
|
|
* @param $src |
186
|
|
|
* |
187
|
|
|
* @return array |
188
|
|
|
* |
189
|
|
|
* @see \Brendt\Image\ResponsiveFactory |
190
|
|
|
*/ |
191
|
|
|
public function image($src) { |
192
|
|
|
$image = $this->responsiveFactory->create($src); |
193
|
|
|
|
194
|
|
|
if (!$image) { |
195
|
|
|
return ['src' => null, 'srcset' => null, 'sizes' => null]; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
return [ |
199
|
|
|
'src' => $image->src(), |
200
|
|
|
'srcset' => $image->srcset(), |
201
|
|
|
'sizes' => $image->sizes(), |
202
|
|
|
]; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* Create a public file from the src directory and return its path. |
207
|
|
|
* |
208
|
|
|
* @param $src |
209
|
|
|
* |
210
|
|
|
* @return null|string |
211
|
|
|
*/ |
212
|
|
|
public function file($src) { |
213
|
|
|
$src = trim($src, '/'); |
214
|
|
|
$files = Finder::create()->in($this->srcDir)->path($src)->getIterator(); |
215
|
|
|
$files->rewind(); |
216
|
|
|
/** @var SplFileInfo $file */ |
217
|
|
|
$file = $files->current(); |
218
|
|
|
|
219
|
|
|
if (!$file) { |
220
|
|
|
return null; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
$fs = new Filesystem(); |
224
|
|
|
$dst = "{$this->publicDir}/{$src}"; |
225
|
|
|
|
226
|
|
|
if ($fs->exists($dst)) { |
227
|
|
|
$fs->remove($dst); |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
$fs->dumpFile($dst, $file->getContents()); |
231
|
|
|
|
232
|
|
|
return "/{$src}"; |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
} |
236
|
|
|
|