Completed
Push — master ( 9d78f0...b09679 )
by Florin
14:38 queued 44s
created

Minify::build()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 29
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

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