Completed
Push — master ( 9d47dc...029617 )
by Florin
14:58
created

Minify::setCacheBustFile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
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 set in config file, return them
215
        if (count($this->objConfig->files)) {
216
            return $this->objConfig->files;
217
        }
218
219
        // find files in folder
220
        return $this->objFiles->getFiles($this->getFilesFolder());
221
    }
222
223
    /**
224
     * Get minified Content
225
     *
226
     * @param $strFileName
227
     * @return bool|string
228
     */
229
    public function getMinifiedContent($strFileName)
230
    {
231
        // make sure the file name it's valid
232
        if (!$this->isValidFileName($strFileName)) {
233
            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
    public function isValidFileName($strFileName)
255
    {
256
        // get file extension
257
        $arrFileName = explode('.', $strFileName);
258
        $strFileExtension = array_pop($arrFileName);
259
260
        // must be a listed file type
261
        if (!in_array($strFileExtension, $this->arrFileTypes[$this->getDestinationExtension()])) {
262
            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
    public function loadConfig()
296
    {
297
        // set config
298
        $this->objConfig = $this->objEnvironment->getConfig($this->getFilesFolder());
299
300
        // set environment
301
        $this->objEnvironment->setEnvironment($this->getFilesFolder());
302
    }
303
304
    /**
305
     * Set env to Dev
306
     *
307
     * @param bool $bln
308
     * @return $this
309
     */
310
    public function setDev($bln = true)
311
    {
312
        $this->objEnvironment->setDev($bln);
313
        $this->blnIsDev = $bln;
314
        return $this;
315
    }
316
317
    /**
318
     * Set Cache bust file
319
     *
320
     * @return $this
321
     */
322
    public function setCacheBustFile()
323
    {
324
        $this->strCacheBustFile = $this->getFilesFolder() . '/' . Environment::SIGNATURE_FILE;
325
        return $this;
326
    }
327
328
    /**
329
     * Set public folder
330
     *
331
     * @param string $strFolder
332
     * @return $this
333
     */
334
    public function setPublicFolder($strFolder = '/../../../../public')
335
    {
336
        // set application public path relative to package location
337
        $this->strPublicFolderPath = __DIR__ . $strFolder;
338
        return $this;
339
    }
340
341
    /**
342
     * Set test folder
343
     *
344
     * @return $this
345
     */
346
    public function setTest()
347
    {
348
        // set application public path relative to package location
349
        $this->setPublicFolder('/../build');
350
351
        // cache bust file
352
        $this->setCacheBustFile();
353
354
        return $this;
355
    }
356
357
    /**
358
     * Set destination folder
359
     *
360
     * @param $strFolder
361
     * @return $this
362
     */
363
    public function setDestinationFolder($strFolder)
364
    {
365
        $this->strDestinationFolder = $strFolder;
366
        return $this;
367
    }
368
369
    /**
370
     * Set destination file
371
     *
372
     * @param $strFile
373
     * @return $this
374
     */
375
    public function setDestinationFile($strFile)
376
    {
377
        $this->strDestinationFile = $strFile;
378
        return $this;
379
    }
380
381
382
    /**
383
     * Set destination file extension
384
     *
385
     * @param $strDestinationExtension
386
     * @return $this
387
     */
388
    public function setDestinationExtensionType($strDestinationExtension)
389
    {
390
        $this->strDestinationExtension = $strDestinationExtension;
391
        return $this;
392
    }
393
394
    /**
395
     * Get Public folder
396
     *
397
     * @return string
398
     */
399
    public function getPublicFolder()
400
    {
401
        return $this->strPublicFolderPath;
402
    }
403
404
    /**
405
     * Get destination folder
406
     *
407
     * @return mixed
408
     */
409
    public function getDestinationFolder()
410
    {
411
        return $this->strDestinationFolder;
412
    }
413
414
    /**
415
     * Set destination file
416
     *
417
     * @return mixed
418
     */
419
    public function getDestinationFile()
420
    {
421
        return $this->strDestinationFile;
422
    }
423
424
    /**
425
     * Get application file name
426
     *
427
     * @return string
428
     */
429
    public function getAppFileName()
430
    {
431
        return $this->getFilesFolder() . '/' . $this->getDestinationFile();
432
    }
433
434
    /**
435
     * Get destination file type extension
436
     *
437
     * @return mixed
438
     */
439
    public function getDestinationExtension()
440
    {
441
        return $this->strDestinationExtension;
442
    }
443
444
    /**
445
     * Get files folder
446
     *
447
     * @return string
448
     */
449
    public function getFilesFolder()
450
    {
451
        return $this->getPublicFolder() . '/' . $this->getDestinationFolder();
452
    }
453
}
454