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