Completed
Push — master ( d573c8...0f6a93 )
by Florin
13:34
created

Minify   B

Complexity

Total Complexity 38

Size/Duplication

Total Lines 460
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 38
lcom 1
cbo 3
dl 0
loc 460
rs 8.3999
c 0
b 0
f 0

26 Methods

Rating   Name   Duplication   Size   Complexity  
A saveCacheBust() 0 19 2
A getCacheBust() 0 10 2
A minifyContent() 0 12 3
A setDev() 0 6 1
A __construct() 0 8 1
A init() 0 13 1
A js() 0 4 1
A css() 0 4 1
A getMinifiedFile() 0 9 1
A process() 0 16 2
A build() 0 20 3
A getFiles() 0 15 3
B getMinifiedContent() 0 27 4
A loadConfig() 0 16 1
A setCacheBustFile() 0 5 1
A setPublicFolder() 0 6 1
A setTest() 0 10 1
A setDestinationFolder() 0 5 1
A setDestinationFile() 0 5 1
A setDestinationExtensionType() 0 4 1
A getPublicFolder() 0 4 1
A getDestinationFolder() 0 4 1
A getDestinationFile() 0 4 1
A getAppFileName() 0 4 1
A getDestinationExtension() 0 4 1
A getFilesFolder() 0 4 1
1
<?php
2
3
namespace Makehappen\AutoMinifier;
4
5
use MatthiasMullie\Minify as Minifier;
6
7
/**
8
 * Class Minify
9
 *
10
 * Automatically Minify and Concatenate your JS and CSS files and libraries into single files
11
 * for improved application performance.
12
 *
13
 * @package Makehappen\AutoMinifier
14
 */
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 = '')
276
    {
277
        // get contents signature
278
        $strNewCacheBust = md5($strApplicationFileContents);
279
280
        // get prior cache bust
281
        $strPriorCacheBust = $this->getCacheBust();
282
283
        // if unchanged, stop here
284
        if ($strPriorCacheBust == $strNewCacheBust) {
285
            return $strPriorCacheBust;
286
        }
287
288
        // set new cache bust
289
        file_put_contents($this->strCacheBustFile, $strNewCacheBust);
290
291
        // return new cache bust
292
        return $strNewCacheBust;
293
    }
294
295
    /**
296
     * Get Cache Bust
297
     *
298
     * @return bool|string
299
     */
300
    public function getCacheBust()
301
    {
302
        // abort if file not found
303
        if (!file_exists($this->strCacheBustFile)) {
304
            return '';
305
        }
306
307
        // get cache bust file contents
308
        return file_get_contents($this->strCacheBustFile);
309
    }
310
311
    /**
312
     * Set env to Dev
313
     *
314
     * @param bool $bln
315
     * @return $this
316
     */
317
    public function setDev($bln = true)
318
    {
319
        $this->objEnvironment->setDev($bln);
320
        $this->blnIsDev = $bln;
321
        return $this;
322
    }
323
324
    public function loadConfig()
325
    {
326
        // default config
327
        $arrDefaultConfig = [
328
            'files' => []
329
        ];
330
331
        // set config
332
        $this->objConfig = $this->objEnvironment->getSettings(
333
            $this->getFilesFolder() . '/' . Environment::CONFIG_FILE,
334
            $arrDefaultConfig
335
        );
336
337
        // set environment
338
        $this->objEnvironment->setEnvironment($this->getFilesFolder());
339
    }
340
341
    /**
342
     * Set Cache bust file
343
     *
344
     * @return $this
345
     */
346
    public function setCacheBustFile()
347
    {
348
        $this->strCacheBustFile = $this->getFilesFolder() . '/' . Environment::SIGNATURE_FILE;
349
        return $this;
350
    }
351
352
    /**
353
     * Set public folder
354
     *
355
     * @param string $strFolder
356
     * @return $this
357
     */
358
    public function setPublicFolder($strFolder = '/../../../../public')
359
    {
360
        // set application public path relative to package location
361
        $this->strPublicFolderPath = __DIR__ . $strFolder;
362
        return $this;
363
    }
364
365
    /**
366
     * Set test folder
367
     *
368
     * @return $this
369
     */
370
    public function setTest()
371
    {
372
        // set application public path relative to package location
373
        $this->setPublicFolder('/../build');
374
375
        // cache bust file
376
        $this->setCacheBustFile();
377
378
        return $this;
379
    }
380
381
    /**
382
     * Set destination folder
383
     *
384
     * @param $strFolder
385
     * @return $this
386
     */
387
    public function setDestinationFolder($strFolder)
388
    {
389
        $this->strDestinationFolder = $strFolder;
390
        return $this;
391
    }
392
393
    /**
394
     * Set destination file
395
     *
396
     * @param $strFile
397
     * @return $this
398
     */
399
    public function setDestinationFile($strFile)
400
    {
401
        $this->strDestinationFile = $strFile;
402
        return $this;
403
    }
404
405
    /**
406
     * Set destination file extension
407
     *
408
     * @param $strDestinationExtension
409
     */
410
    public function setDestinationExtensionType($strDestinationExtension)
411
    {
412
        $this->strDestinationExtension = $strDestinationExtension;
413
    }
414
415
    /**
416
     * Get Public folder
417
     *
418
     * @return string
419
     */
420
    public function getPublicFolder()
421
    {
422
        return $this->strPublicFolderPath;
423
    }
424
425
    /**
426
     * Get destination folder
427
     *
428
     * @return mixed
429
     */
430
    public function getDestinationFolder()
431
    {
432
        return $this->strDestinationFolder;
433
    }
434
435
    /**
436
     * Set destination file
437
     *
438
     * @return mixed
439
     */
440
    public function getDestinationFile()
441
    {
442
        return $this->strDestinationFile;
443
    }
444
445
    /**
446
     * Get application file name
447
     *
448
     * @return string
449
     */
450
    public function getAppFileName()
451
    {
452
        return $this->getFilesFolder() . '/' . $this->getDestinationFile();
453
    }
454
455
    /**
456
     * Get destination file type extension
457
     *
458
     * @return mixed
459
     */
460
    public function getDestinationExtension()
461
    {
462
        return $this->strDestinationExtension;
463
    }
464
465
    /**
466
     * Get files folder
467
     *
468
     * @return string
469
     */
470
    public function getFilesFolder()
471
    {
472
        return $this->getPublicFolder() . '/' . $this->getDestinationFolder();
473
    }
474
}
475