1 | <?php |
||
15 | class Minify |
||
16 | { |
||
17 | /** |
||
18 | * @var Environment |
||
19 | */ |
||
20 | protected $objEnvironment; |
||
21 | |||
22 | /** |
||
23 | * Cache bust file path |
||
24 | * |
||
25 | * @var string |
||
26 | */ |
||
27 | protected $strCacheBustFile; |
||
28 | |||
29 | /** |
||
30 | * Public folder path |
||
31 | * |
||
32 | * @var string |
||
33 | */ |
||
34 | protected $strPublicFolderPath; |
||
35 | |||
36 | /** |
||
37 | * File types supported |
||
38 | * |
||
39 | * @var array |
||
40 | */ |
||
41 | protected $arrFileTypes; |
||
42 | |||
43 | /** |
||
44 | * Destination file extension |
||
45 | * |
||
46 | * @var string |
||
47 | */ |
||
48 | protected $strDestinationExtension; |
||
49 | |||
50 | /** |
||
51 | * Destination folder |
||
52 | * |
||
53 | * @var |
||
54 | */ |
||
55 | protected $strDestinationFolder; |
||
56 | |||
57 | /** |
||
58 | * Destination file |
||
59 | * |
||
60 | * @var |
||
61 | */ |
||
62 | protected $strDestinationFile; |
||
63 | |||
64 | /** |
||
65 | * Set env to dev |
||
66 | * |
||
67 | * @var |
||
68 | */ |
||
69 | protected $blnIsDev; |
||
70 | |||
71 | /** |
||
72 | * Create a new Minifier Instance |
||
73 | * |
||
74 | * Minify constructor. |
||
75 | */ |
||
76 | public function __construct() |
||
77 | { |
||
78 | // set class variables |
||
79 | $this->init(); |
||
80 | |||
81 | // set environment |
||
82 | $this->objEnvironment = new Environment(); |
||
83 | } |
||
84 | |||
85 | |||
86 | /** |
||
87 | * Set Class Variables |
||
88 | * |
||
89 | * @return null |
||
90 | */ |
||
91 | public function init() |
||
92 | { |
||
93 | // set public folder location |
||
94 | $this->setPublicFolder(); |
||
95 | |||
96 | // set file types we want minified |
||
97 | $this->arrFileTypes = [ |
||
98 | 'js' => ['js'], |
||
99 | 'css' => ['css', 'sass', 'scss'] |
||
100 | ]; |
||
101 | |||
102 | return null; |
||
103 | } |
||
104 | |||
105 | /** |
||
106 | * Build minified file |
||
107 | * |
||
108 | * @var $strFolder string |
||
109 | * @var $strFile string |
||
110 | * @return string |
||
111 | */ |
||
112 | public function js($strFolder = '/js', $strFile = 'app.min.js') |
||
113 | { |
||
114 | return $this->getMinifiedFile($strType = 'js', $strFolder, $strFile); |
||
115 | } |
||
116 | |||
117 | /** |
||
118 | * Build CSS minified file |
||
119 | * |
||
120 | * @var $strFolder string |
||
121 | * @var $strFile string |
||
122 | * @return bool|null|string |
||
123 | */ |
||
124 | public function css($strFolder = '/css', $strFile = 'app.min.css') |
||
125 | { |
||
126 | return $this->getMinifiedFile($strType = 'css', $strFolder, $strFile); |
||
127 | } |
||
128 | |||
129 | /** |
||
130 | * Generate minified file |
||
131 | * |
||
132 | * @param string $strType |
||
133 | * @param string $strFolder |
||
134 | * @param string $strFile |
||
135 | * @return string |
||
136 | */ |
||
137 | public function getMinifiedFile($strType = 'css', $strFolder = '/css', $strFile = 'app.min.css') |
||
138 | { |
||
139 | $this->setDestinationExtensionType($strType); |
||
140 | $this->setDestinationFolder($strFolder); |
||
141 | $this->setDestinationFile($strFile); |
||
142 | $this->setCacheBustFile(); |
||
143 | return $strFolder . '/' . $strFile .'?' . $this->process(); |
||
144 | } |
||
145 | |||
146 | /** |
||
147 | * Process Built |
||
148 | * |
||
149 | * @return bool|null|string |
||
150 | */ |
||
151 | public function process() |
||
152 | { |
||
153 | // return last cache bust in non development environments |
||
154 | if (!$this->objEnvironment->isDevEnv()) { |
||
155 | return $this->getCacheBust(); |
||
156 | } |
||
157 | |||
158 | // build application file |
||
159 | $strApplicationFileContents = $this->build(); |
||
160 | |||
161 | // save application file |
||
162 | file_put_contents($this->getAppFileName(), $strApplicationFileContents); |
||
163 | |||
164 | // save new cache bust |
||
165 | return $this->saveCacheBust($strApplicationFileContents); |
||
166 | } |
||
167 | |||
168 | /** |
||
169 | * Build file |
||
170 | * |
||
171 | * @return string |
||
172 | */ |
||
173 | public function build() |
||
174 | { |
||
175 | // files folder |
||
176 | $strFilesFolder = $this->getFilesFolder(); |
||
177 | |||
178 | // abort if folder not found |
||
179 | if (!is_dir($strFilesFolder)) { |
||
180 | return $strFilesFolder . ' folder not found'; |
||
181 | } |
||
182 | |||
183 | // get all files |
||
184 | $arrFiles = scandir($strFilesFolder); |
||
185 | |||
186 | // loop through all files |
||
187 | $strMinifiedFileContents = ''; |
||
188 | foreach ($arrFiles as $strFileName) { |
||
189 | // get minified content |
||
190 | $strFileContents = $this->getMinifiedContent($strFileName); |
||
191 | |||
192 | // don't include empty files |
||
193 | if (!$strFileContents) { |
||
194 | continue; |
||
195 | } |
||
196 | |||
197 | // add new minified file to concatenated version |
||
198 | $strMinifiedFileContents .= "\n/* $strFileName */\n" . $strFileContents; |
||
199 | } |
||
200 | |||
201 | // returned concatenated version of minifired files |
||
202 | return $strMinifiedFileContents; |
||
203 | } |
||
204 | |||
205 | /** |
||
206 | * Get minified Content |
||
207 | * |
||
208 | * @param $strFileName |
||
209 | * @return null|string |
||
210 | */ |
||
211 | public function getMinifiedContent($strFileName) |
||
212 | { |
||
213 | // get file extension |
||
214 | $strFileExtension = array_pop(explode('.', $strFileName)); |
||
|
|||
215 | |||
216 | // must be an accepted file type |
||
217 | if (!$this->isAcceptedExtension($strFileExtension)) { |
||
218 | return null; |
||
219 | } |
||
220 | |||
221 | // must not be the app file |
||
222 | if ($strFileName == $this->getDestinationFile()) { |
||
223 | return null; |
||
224 | } |
||
225 | |||
226 | // build file path and name |
||
227 | $strFile = $this->getFilesFolder() . '/' . $strFileName; |
||
228 | |||
229 | // if it's minified already return content |
||
230 | if (preg_match('/\.min\./', $strFile)) { |
||
231 | return file_get_contents($strFile); |
||
232 | } |
||
233 | |||
234 | // return minified content |
||
235 | return $this->minifyContent($strFile); |
||
236 | } |
||
237 | |||
238 | /** |
||
239 | * Determine if it's an accepted extension |
||
240 | * |
||
241 | * @param $strFileExtension |
||
242 | * @return bool |
||
243 | */ |
||
244 | public function isAcceptedExtension($strFileExtension) |
||
245 | { |
||
246 | return in_array($strFileExtension, $this->arrFileTypes[$this->getDestinationExtension()]); |
||
247 | } |
||
248 | |||
249 | /** |
||
250 | * Minify content |
||
251 | * |
||
252 | * @param $strFile |
||
253 | * @return bool|string |
||
254 | */ |
||
255 | public function minifyContent($strFile) |
||
256 | { |
||
257 | // minify based on file type |
||
258 | switch ($this->getDestinationExtension()) { |
||
259 | case 'js': |
||
260 | return (new Minifier\JS($strFile))->minify(); |
||
261 | case 'css': |
||
262 | return (new Minifier\CSS($strFile))->minify(); |
||
263 | default: |
||
264 | return ''; |
||
265 | } |
||
266 | } |
||
267 | |||
268 | /** |
||
269 | * Save Cache Bust |
||
270 | * |
||
271 | * @var string $strApplicationFileContents |
||
272 | * @return null |
||
273 | */ |
||
274 | public function saveCacheBust($strApplicationFileContents = '') |
||
293 | |||
294 | /** |
||
295 | * Get Cache Bust |
||
296 | * |
||
297 | * @return bool|string |
||
298 | */ |
||
299 | public function getCacheBust() |
||
309 | |||
310 | /** |
||
311 | * Set env to Dev |
||
312 | * |
||
313 | * @param bool $bln |
||
314 | * @return $this |
||
315 | */ |
||
316 | public function setDev($bln = true) |
||
317 | { |
||
318 | $this->objEnvironment->setDev($bln); |
||
319 | $this->blnIsDev = $bln; |
||
322 | |||
323 | /** |
||
324 | * Set Cache bust file |
||
325 | * |
||
326 | * @param string $strFile |
||
327 | * @return $this |
||
328 | */ |
||
329 | public function setCacheBustFile($strFile = 'autominifier-cache-bust.txt') |
||
334 | |||
335 | /** |
||
336 | * Set public folder |
||
337 | * |
||
338 | * @param string $strFolder |
||
339 | * @return $this |
||
340 | */ |
||
341 | public function setPublicFolder($strFolder = '/../../../../public') |
||
347 | |||
348 | /** |
||
349 | * Set test folder |
||
350 | * |
||
351 | * @return $this |
||
352 | */ |
||
353 | public function setTest() |
||
363 | |||
364 | /** |
||
365 | * Set destination folder |
||
366 | * |
||
367 | * @param $strFolder |
||
368 | * @return $this |
||
369 | */ |
||
370 | public function setDestinationFolder($strFolder) |
||
375 | |||
376 | /** |
||
377 | * Set destination file |
||
378 | * |
||
379 | * @param $strFile |
||
380 | * @return $this |
||
381 | */ |
||
382 | public function setDestinationFile($strFile) |
||
387 | |||
388 | /** |
||
389 | * Set destination file extension |
||
390 | * |
||
391 | * @param $strDestinationExtension |
||
392 | */ |
||
393 | public function setDestinationExtensionType($strDestinationExtension) |
||
397 | |||
398 | /** |
||
399 | * Get Public folder |
||
400 | * |
||
401 | * @return string |
||
402 | */ |
||
403 | public function getPublicFolder() |
||
407 | |||
408 | /** |
||
409 | * Get destination folder |
||
410 | * |
||
411 | * @return mixed |
||
412 | */ |
||
413 | public function getDestinationFolder() |
||
417 | |||
418 | /** |
||
419 | * Set destination file |
||
420 | * |
||
421 | * @return mixed |
||
422 | */ |
||
423 | public function getDestinationFile() |
||
427 | |||
428 | /** |
||
429 | * Get application file name |
||
430 | * |
||
431 | * @return string |
||
432 | */ |
||
433 | public function getAppFileName() |
||
437 | |||
438 | /** |
||
439 | * Get destination file type extension |
||
440 | * |
||
441 | * @return mixed |
||
442 | */ |
||
443 | public function getDestinationExtension() |
||
447 | |||
448 | /** |
||
449 | * Get files folder |
||
450 | * |
||
451 | * @return string |
||
452 | */ |
||
453 | public function getFilesFolder() |
||
457 | } |
||
458 |