Completed
Push — master ( b09679...8729da )
by Florin
11:38
created

Minify::build()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 22
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 22
rs 9.2
c 0
b 0
f 0
cc 3
eloc 8
nc 3
nop 0
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
    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)
228
    {
229
        // make sure the file name it's valid
230
        if (!$this->isValidFileName($strFileName)) {
231
            return '';
232
        }
233
234
        // build file path and name
235
        $strFile = $this->getFilesFolder() . '/' . $strFileName;
236
237
        // if it's minified already return content
238
        if (preg_match('/\.min\./', $strFile)) {
239
            return file_get_contents($strFile);
240
        }
241
242
        // return minified content
243
        return $this->minifyContent($strFile);
244
    }
245
246
    /**
247
     * Determine if it's a valid file name
248
     *
249
     * @param $strFileName
250
     * @return bool
251
     */
252
    public function isValidFileName($strFileName)
253
    {
254
        // get file extension
255
        $arrFileName = explode('.', $strFileName);
256
        $strFileExtension = array_pop($arrFileName);
257
258
        // must be a listed file type
259
        if (!in_array($strFileExtension, $this->arrFileTypes[$this->getDestinationExtension()])) {
260
            return false;
261
        }
262
263
        // must not be the app file
264
        if ($strFileName == $this->getDestinationFile()) {
265
            return false;
266
        }
267
268
        return true;
269
    }
270
271
    /**
272
     * Minify content
273
     *
274
     * @param  $strFile
275
     * @return bool|string
276
     */
277
    public function minifyContent($strFile)
278
    {
279
        // minify based on file type
280
        switch ($this->getDestinationExtension()) {
281
            case 'js':
282
                return (new Minifier\JS($strFile))->minify();
283
            case 'css':
284
                return (new Minifier\CSS($strFile))->minify();
285
            default:
286
                return '';
287
        }
288
    }
289
290
    /**
291
     * Load configuration
292
     */
293
    public function loadConfig()
294
    {
295
        // set config
296
        $this->objConfig = $this->objEnvironment->getConfig($this->getFilesFolder());
297
298
        // set environment
299
        $this->objEnvironment->setEnvironment($this->getFilesFolder());
300
    }
301
302
    /**
303
     * Set env to Dev
304
     *
305
     * @param bool $bln
306
     * @return $this
307
     */
308
    public function setDev($bln = true)
309
    {
310
        $this->objEnvironment->setDev($bln);
311
        $this->blnIsDev = $bln;
312
        return $this;
313
    }
314
315
    /**
316
     * Set Cache bust file
317
     *
318
     * @return $this
319
     */
320
    public function setCacheBustFile()
321
    {
322
        $this->strCacheBustFile = $this->getFilesFolder() . '/' . Environment::SIGNATURE_FILE;
323
        return $this;
324
    }
325
326
    /**
327
     * Set public folder
328
     *
329
     * @param string $strFolder
330
     * @return $this
331
     */
332
    public function setPublicFolder($strFolder = '/../../../../public')
333
    {
334
        // set application public path relative to package location
335
        $this->strPublicFolderPath = __DIR__ . $strFolder;
336
        return $this;
337
    }
338
339
    /**
340
     * Set test folder
341
     *
342
     * @return $this
343
     */
344
    public function setTest()
345
    {
346
        // set application public path relative to package location
347
        $this->setPublicFolder('/../build');
348
349
        // cache bust file
350
        $this->setCacheBustFile();
351
352
        return $this;
353
    }
354
355
    /**
356
     * Set destination folder
357
     *
358
     * @param $strFolder
359
     * @return $this
360
     */
361
    public function setDestinationFolder($strFolder)
362
    {
363
        $this->strDestinationFolder = $strFolder;
364
        return $this;
365
    }
366
367
    /**
368
     * Set destination file
369
     *
370
     * @param $strFile
371
     * @return $this
372
     */
373
    public function setDestinationFile($strFile)
374
    {
375
        $this->strDestinationFile = $strFile;
376
        return $this;
377
    }
378
379
380
    /**
381
     * Set destination file extension
382
     *
383
     * @param $strDestinationExtension
384
     * @return $this
385
     */
386
    public function setDestinationExtensionType($strDestinationExtension)
387
    {
388
        $this->strDestinationExtension = $strDestinationExtension;
389
        return $this;
390
    }
391
392
    /**
393
     * Get Public folder
394
     *
395
     * @return string
396
     */
397
    public function getPublicFolder()
398
    {
399
        return $this->strPublicFolderPath;
400
    }
401
402
    /**
403
     * Get destination folder
404
     *
405
     * @return mixed
406
     */
407
    public function getDestinationFolder()
408
    {
409
        return $this->strDestinationFolder;
410
    }
411
412
    /**
413
     * Set destination file
414
     *
415
     * @return mixed
416
     */
417
    public function getDestinationFile()
418
    {
419
        return $this->strDestinationFile;
420
    }
421
422
    /**
423
     * Get application file name
424
     *
425
     * @return string
426
     */
427
    public function getAppFileName()
428
    {
429
        return $this->getFilesFolder() . '/' . $this->getDestinationFile();
430
    }
431
432
    /**
433
     * Get destination file type extension
434
     *
435
     * @return mixed
436
     */
437
    public function getDestinationExtension()
438
    {
439
        return $this->strDestinationExtension;
440
    }
441
442
    /**
443
     * Get files folder
444
     *
445
     * @return string
446
     */
447
    public function getFilesFolder()
448
    {
449
        return $this->getPublicFolder() . '/' . $this->getDestinationFolder();
450
    }
451
}
452