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 | * @var Files |
||
29 | */ |
||
30 | protected $objFiles; |
||
31 | |||
32 | /** |
||
33 | * Cache bust file path |
||
34 | * |
||
35 | * @var string |
||
36 | */ |
||
37 | protected $strCacheBustFile; |
||
38 | |||
39 | /** |
||
40 | * Public folder path |
||
41 | * |
||
42 | * @var string |
||
43 | */ |
||
44 | protected $strPublicFolderPath; |
||
45 | |||
46 | /** |
||
47 | * File types supported |
||
48 | * |
||
49 | * @var array |
||
50 | */ |
||
51 | protected $arrFileTypes; |
||
52 | |||
53 | /** |
||
54 | * Destination file extension |
||
55 | * |
||
56 | * @var string |
||
57 | */ |
||
58 | protected $strDestinationExtension; |
||
59 | |||
60 | /** |
||
61 | * Destination folder |
||
62 | * |
||
63 | * @var |
||
64 | */ |
||
65 | protected $strDestinationFolder; |
||
66 | |||
67 | /** |
||
68 | * Destination file |
||
69 | * |
||
70 | * @var |
||
71 | */ |
||
72 | protected $strDestinationFile; |
||
73 | |||
74 | /** |
||
75 | * Set env to dev |
||
76 | * |
||
77 | * @var |
||
78 | */ |
||
79 | protected $blnIsDev; |
||
80 | |||
81 | /** |
||
82 | * Create a new Minifier Instance |
||
83 | * |
||
84 | * Minify constructor. |
||
85 | */ |
||
86 | public function __construct() |
||
87 | { |
||
88 | // set class variables |
||
89 | $this->init(); |
||
90 | |||
91 | // set environment |
||
92 | $this->objEnvironment = new Environment(); |
||
93 | |||
94 | // set files class |
||
95 | $this->objFiles = new Files(); |
||
96 | } |
||
97 | |||
98 | /** |
||
99 | * Set Class Variables |
||
100 | * |
||
101 | * @return null |
||
102 | */ |
||
103 | public function init() |
||
104 | { |
||
105 | // set public folder location |
||
106 | $this->setPublicFolder(); |
||
107 | |||
108 | // set file types we want minified |
||
109 | $this->arrFileTypes = [ |
||
110 | 'js' => ['js'], |
||
111 | 'css' => ['css', 'sass', 'scss'] |
||
112 | ]; |
||
113 | |||
114 | return null; |
||
115 | } |
||
116 | |||
117 | /** |
||
118 | * Build minified file |
||
119 | * |
||
120 | * @var $strFolder string |
||
121 | * @var $strFile string |
||
122 | * @return string |
||
123 | */ |
||
124 | public function js($strFolder = '/js', $strFile = 'app.min.js') |
||
125 | { |
||
126 | return $this->getMinifiedFile($strType = 'js', $strFolder, $strFile); |
||
127 | } |
||
128 | |||
129 | /** |
||
130 | * Build CSS minified file |
||
131 | * |
||
132 | * @var $strFolder string |
||
133 | * @var $strFile string |
||
134 | * @return bool|null|string |
||
135 | */ |
||
136 | public function css($strFolder = '/css', $strFile = 'app.min.css') |
||
137 | { |
||
138 | return $this->getMinifiedFile($strType = 'css', $strFolder, $strFile); |
||
139 | } |
||
140 | |||
141 | /** |
||
142 | * Generate minified file |
||
143 | * |
||
144 | * @param string $strType |
||
145 | * @param string $strFolder |
||
146 | * @param string $strFile |
||
147 | * @return string |
||
148 | */ |
||
149 | public function getMinifiedFile($strType = 'css', $strFolder = '/css', $strFile = 'app.min.css') |
||
150 | { |
||
151 | $this->setDestinationExtensionType($strType); |
||
152 | $this->setDestinationFolder($strFolder); |
||
153 | $this->setDestinationFile($strFile); |
||
154 | $this->setCacheBustFile(); |
||
155 | $this->loadConfig(); |
||
156 | return $strFolder . '/' . $strFile .'?' . $this->process(); |
||
157 | } |
||
158 | |||
159 | /** |
||
160 | * Process Built |
||
161 | * |
||
162 | * @return bool|null|string |
||
163 | */ |
||
164 | public function process() |
||
165 | { |
||
166 | // return last cache bust in non development environments |
||
167 | if (!$this->objEnvironment->isDevEnv()) { |
||
168 | return $this->objFiles->getCacheBust($this->strCacheBustFile); |
||
169 | } |
||
170 | |||
171 | // build application file |
||
172 | $strApplicationFileContents = $this->build(); |
||
173 | |||
174 | // save application file |
||
175 | file_put_contents($this->getAppFileName(), $strApplicationFileContents); |
||
176 | |||
177 | // save new cache bust |
||
178 | return $this->objFiles->saveCacheBust($strApplicationFileContents, $this->strCacheBustFile); |
||
179 | } |
||
180 | |||
181 | /** |
||
182 | * Build file |
||
183 | * |
||
184 | * @return string |
||
185 | */ |
||
186 | public function build() |
||
187 | { |
||
188 | // min file contents |
||
189 | $strMinifiedFileContents = ''; |
||
190 | |||
191 | // loop through all files |
||
192 | foreach ($this->getFiles() as $strFileName) { |
||
193 | // get minified content |
||
194 | $strFileContents = $this->getMinifiedContent($strFileName); |
||
195 | |||
196 | // don't include empty files |
||
197 | if (!$strFileContents) { |
||
198 | continue; |
||
199 | } |
||
200 | |||
201 | // add new minified file to concatenated version |
||
202 | $strMinifiedFileContents .= "\n/* $strFileName */\n" . $strFileContents; |
||
203 | } |
||
204 | |||
205 | // returned concatenated version of minifired files |
||
206 | return $strMinifiedFileContents; |
||
207 | } |
||
208 | |||
209 | /** |
||
210 | * @return array |
||
211 | */ |
||
212 | public function getFiles() |
||
213 | { |
||
214 | if (count($this->objConfig->files)) { |
||
215 | return $this->objConfig->files; |
||
216 | } |
||
217 | |||
218 | return $this->objFiles->getFiles($this->getFilesFolder()); |
||
219 | } |
||
220 | |||
221 | /** |
||
222 | * Get minified Content |
||
223 | * |
||
224 | * @param $strFileName |
||
225 | * @return bool|string |
||
226 | */ |
||
227 | public function getMinifiedContent($strFileName) |
||
245 | |||
246 | /** |
||
247 | * Determine if it's a valid file name |
||
248 | * |
||
249 | * @param $strFileName |
||
250 | * @return bool |
||
251 | */ |
||
252 | public function isValidFileName($strFileName) |
||
270 | |||
271 | /** |
||
272 | * Minify content |
||
273 | * |
||
274 | * @param $strFile |
||
275 | * @return bool|string |
||
276 | */ |
||
277 | public function minifyContent($strFile) |
||
289 | |||
290 | /** |
||
291 | * Load configuration |
||
292 | */ |
||
293 | public function loadConfig() |
||
294 | { |
||
295 | // set config |
||
296 | $this->objConfig = $this->objEnvironment->getConfig($this->getFilesFolder()); |
||
297 | |||
301 | |||
302 | /** |
||
303 | * Set env to Dev |
||
304 | * |
||
305 | * @param bool $bln |
||
306 | * @return $this |
||
307 | */ |
||
308 | public function setDev($bln = true) |
||
314 | |||
315 | /** |
||
316 | * Set Cache bust file |
||
317 | * |
||
318 | * @return $this |
||
319 | */ |
||
320 | public function setCacheBustFile() |
||
325 | |||
326 | /** |
||
327 | * Set public folder |
||
328 | * |
||
329 | * @param string $strFolder |
||
330 | * @return $this |
||
331 | */ |
||
332 | public function setPublicFolder($strFolder = '/../../../../public') |
||
338 | |||
339 | /** |
||
340 | * Set test folder |
||
341 | * |
||
342 | * @return $this |
||
343 | */ |
||
344 | public function setTest() |
||
354 | |||
355 | /** |
||
356 | * Set destination folder |
||
357 | * |
||
358 | * @param $strFolder |
||
359 | * @return $this |
||
360 | */ |
||
361 | public function setDestinationFolder($strFolder) |
||
366 | |||
367 | /** |
||
368 | * Set destination file |
||
369 | * |
||
370 | * @param $strFile |
||
371 | * @return $this |
||
372 | */ |
||
373 | public function setDestinationFile($strFile) |
||
378 | |||
379 | |||
380 | /** |
||
381 | * Set destination file extension |
||
382 | * |
||
383 | * @param $strDestinationExtension |
||
384 | * @return $this |
||
385 | */ |
||
386 | public function setDestinationExtensionType($strDestinationExtension) |
||
391 | |||
392 | /** |
||
393 | * Get Public folder |
||
394 | * |
||
395 | * @return string |
||
396 | */ |
||
397 | public function getPublicFolder() |
||
401 | |||
402 | /** |
||
403 | * Get destination folder |
||
404 | * |
||
405 | * @return mixed |
||
406 | */ |
||
407 | public function getDestinationFolder() |
||
411 | |||
412 | /** |
||
413 | * Set destination file |
||
414 | * |
||
415 | * @return mixed |
||
416 | */ |
||
417 | public function getDestinationFile() |
||
421 | |||
422 | /** |
||
423 | * Get application file name |
||
424 | * |
||
425 | * @return string |
||
426 | */ |
||
427 | public function getAppFileName() |
||
431 | |||
432 | /** |
||
433 | * Get destination file type extension |
||
434 | * |
||
435 | * @return mixed |
||
436 | */ |
||
437 | public function getDestinationExtension() |
||
441 | |||
442 | /** |
||
443 | * Get files folder |
||
444 | * |
||
445 | * @return string |
||
446 | */ |
||
447 | public function getFilesFolder() |
||
451 | } |
||
452 |