Minify   A
last analyzed

Complexity

Total Complexity 35

Size/Duplication

Total Lines 439
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 81.89%

Importance

Changes 0
Metric Value
wmc 35
lcom 1
cbo 4
dl 0
loc 439
ccs 86
cts 105
cp 0.8189
rs 9
c 0
b 0
f 0

25 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 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 22 3
A getFiles() 0 10 2
A getMinifiedContent() 0 18 3
A isValidFileName() 0 18 3
A minifyContent() 0 12 3
A loadConfig() 0 8 1
A setDev() 0 6 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 5 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
     * @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 3
    public function __construct()
87
    {
88
        // set class variables
89 3
        $this->init();
90
91
        // set environment
92 3
        $this->objEnvironment = new Environment();
93
94
        // set files class
95 3
        $this->objFiles = new Files();
96 3
    }
97
98
    /**
99
     * Set Class Variables
100
     *
101
     * @return null
102
     */
103 3
    public function init()
104
    {
105
        // set public folder location
106 3
        $this->setPublicFolder();
107
108
        // set file types we want minified
109 3
        $this->arrFileTypes = [
110 3
            'js' => ['js'],
111 3
            'css' => ['css', 'sass', 'scss']
112 3
        ];
113
114 3
        return null;
115
    }
116
117
    /**
118
     * Build minified file
119
     *
120
     * @var $strFolder string
121
     * @var $strFile string
122
     * @return string
123
     */
124 3
    public function js($strFolder = '/js', $strFile = 'app.min.js')
125
    {
126 3
        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 3
    public function css($strFolder = '/css', $strFile = 'app.min.css')
137
    {
138 3
        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 3
    public function getMinifiedFile($strType = 'css', $strFolder = '/css', $strFile = 'app.min.css')
150
    {
151 3
        $this->setDestinationExtensionType($strType);
152 3
        $this->setDestinationFolder($strFolder);
153 3
        $this->setDestinationFile($strFile);
154 3
        $this->setCacheBustFile();
155 3
        $this->loadConfig();
156 3
        return $strFolder . '/' . $strFile .'?' . $this->process();
157
    }
158
159
    /**
160
     * Process Built
161
     *
162
     * @return bool|null|string
163
     */
164 3
    public function process()
165
    {
166
        // return last cache bust in non development environments
167 3
        if (!$this->objEnvironment->isDevEnv()) {
168
            return $this->objFiles->getCacheBust($this->strCacheBustFile);
169
        }
170
171
        // build application file
172 3
        $strApplicationFileContents = $this->build();
173
174
        // save application file
175 3
        file_put_contents($this->getAppFileName(), $strApplicationFileContents);
176
177
        // save new cache bust
178 3
        return $this->objFiles->saveCacheBust($strApplicationFileContents, $this->strCacheBustFile);
179
    }
180
181
    /**
182
     * Build file
183
     *
184
     * @return string
185
     */
186 3
    public function build()
187
    {
188
        // min file contents
189 3
        $strMinifiedFileContents = '';
190
191
        // loop through all files
192 3
        foreach ($this->getFiles() as $strFileName) {
193
            // get minified content
194 3
            $strFileContents = $this->getMinifiedContent($strFileName);
195
196
            // don't include empty files
197 3
            if (!$strFileContents) {
198 3
                continue;
199
            }
200
201
            // add new minified file to concatenated version
202
            $strMinifiedFileContents .= "\n/* $strFileName */\n" . $strFileContents;
203 3
        }
204
205
        // returned concatenated version of minifired files
206 3
        return $strMinifiedFileContents;
207
    }
208
209
    /**
210
     * @return array
211
     */
212 3
    public function getFiles()
213
    {
214
        // if set in config file, return them
215 3
        if (count($this->objConfig->files)) {
216
            return $this->objConfig->files;
217
        }
218
219
        // find files in folder
220 3
        return $this->objFiles->getFiles($this->getFilesFolder());
221
    }
222
223
    /**
224
     * Get minified Content
225
     *
226
     * @param $strFileName
227
     * @return bool|string
228
     */
229 3
    public function getMinifiedContent($strFileName)
230
    {
231
        // make sure the file name it's valid
232 3
        if (!$this->isValidFileName($strFileName)) {
233 3
            return '';
234
        }
235
236
        // build file path and name
237
        $strFile = $this->getFilesFolder() . '/' . $strFileName;
238
239
        // if it's minified already return content
240
        if (preg_match('/\.min\./', $strFile)) {
241
            return file_get_contents($strFile);
242
        }
243
244
        // return minified content
245
        return $this->minifyContent($strFile);
246
    }
247
248
    /**
249
     * Determine if it's a valid file name
250
     *
251
     * @param $strFileName
252
     * @return bool
253
     */
254 3
    public function isValidFileName($strFileName)
255
    {
256
        // get file extension
257 3
        $arrFileName = explode('.', $strFileName);
258 3
        $strFileExtension = array_pop($arrFileName);
259
260
        // must be a listed file type
261 3
        if (!in_array($strFileExtension, $this->arrFileTypes[$this->getDestinationExtension()])) {
262 3
            return false;
263
        }
264
265
        // must not be the app file
266
        if ($strFileName == $this->getDestinationFile()) {
267
            return false;
268
        }
269
270
        return true;
271
    }
272
273
    /**
274
     * Minify content
275
     *
276
     * @param  $strFile
277
     * @return bool|string
278
     */
279
    public function minifyContent($strFile)
280
    {
281
        // minify based on file type
282
        switch ($this->getDestinationExtension()) {
283
            case 'js':
284
                return (new Minifier\JS($strFile))->minify();
285
            case 'css':
286
                return (new Minifier\CSS($strFile))->minify();
287
            default:
288
                return '';
289
        }
290
    }
291
292
    /**
293
     * Load configuration
294
     */
295 3
    public function loadConfig()
296
    {
297
        // set config
298 3
        $this->objConfig = $this->objEnvironment->getConfig($this->getFilesFolder());
299
300
        // set environment
301 3
        $this->objEnvironment->setEnvironment($this->getFilesFolder());
302 3
    }
303
304
    /**
305
     * Set env to Dev
306
     *
307
     * @param bool $bln
308
     * @return $this
309
     */
310 3
    public function setDev($bln = true)
311
    {
312 3
        $this->objEnvironment->setDev($bln);
313 3
        $this->blnIsDev = $bln;
314 3
        return $this;
315
    }
316
317
    /**
318
     * Set Cache bust file
319
     *
320
     * @return $this
321
     */
322 3
    public function setCacheBustFile()
323
    {
324 3
        $this->strCacheBustFile = $this->getFilesFolder() . '/' . Environment::SIGNATURE_FILE;
325 3
        return $this;
326
    }
327
328
    /**
329
     * Set public folder
330
     *
331
     * @param string $strFolder
332
     * @return $this
333
     */
334 3
    public function setPublicFolder($strFolder = '/../../../../public')
335
    {
336
        // set application public path relative to package location
337 3
        $this->strPublicFolderPath = __DIR__ . $strFolder;
338 3
        return $this;
339
    }
340
341
    /**
342
     * Set test folder
343
     *
344
     * @return $this
345
     */
346 3
    public function setTest()
347
    {
348
        // set application public path relative to package location
349 3
        $this->setPublicFolder('/../build');
350
351
        // cache bust file
352 3
        $this->setCacheBustFile();
353
354 3
        return $this;
355
    }
356
357
    /**
358
     * Set destination folder
359
     *
360
     * @param $strFolder
361
     * @return $this
362
     */
363 3
    public function setDestinationFolder($strFolder)
364
    {
365 3
        $this->strDestinationFolder = $strFolder;
366 3
        return $this;
367
    }
368
369
    /**
370
     * Set destination file
371
     *
372
     * @param $strFile
373
     * @return $this
374
     */
375 3
    public function setDestinationFile($strFile)
376
    {
377 3
        $this->strDestinationFile = $strFile;
378 3
        return $this;
379
    }
380
381
382
    /**
383
     * Set destination file extension
384
     *
385
     * @param $strDestinationExtension
386
     * @return $this
387
     */
388 3
    public function setDestinationExtensionType($strDestinationExtension)
389
    {
390 3
        $this->strDestinationExtension = $strDestinationExtension;
391 3
        return $this;
392
    }
393
394
    /**
395
     * Get Public folder
396
     *
397
     * @return string
398
     */
399 3
    public function getPublicFolder()
400
    {
401 3
        return $this->strPublicFolderPath;
402
    }
403
404
    /**
405
     * Get destination folder
406
     *
407
     * @return mixed
408
     */
409 3
    public function getDestinationFolder()
410
    {
411 3
        return $this->strDestinationFolder;
412
    }
413
414
    /**
415
     * Set destination file
416
     *
417
     * @return mixed
418
     */
419 3
    public function getDestinationFile()
420
    {
421 3
        return $this->strDestinationFile;
422
    }
423
424
    /**
425
     * Get application file name
426
     *
427
     * @return string
428
     */
429 3
    public function getAppFileName()
430
    {
431 3
        return $this->getFilesFolder() . '/' . $this->getDestinationFile();
432
    }
433
434
    /**
435
     * Get destination file type extension
436
     *
437
     * @return mixed
438
     */
439 3
    public function getDestinationExtension()
440
    {
441 3
        return $this->strDestinationExtension;
442
    }
443
444
    /**
445
     * Get files folder
446
     *
447
     * @return string
448
     */
449 3
    public function getFilesFolder()
450
    {
451 3
        return $this->getPublicFolder() . '/' . $this->getDestinationFolder();
452
    }
453
}
454