Completed
Push — master ( 98c4bc...2e8965 )
by Florin
11:04
created

Minify::getMinifiedContent()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 27
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 27
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 11
nc 4
nop 1
1
<?php
2
3
namespace Makehappen\AutoMinifier;
4
5
use MatthiasMullie\Minify as Minifier;
6
7
/**
8
 * Class Minify
9
 * @package Makehappen\AutoMinifier
10
 */
11
class Minify
12
{
13
    protected $objEnvironment;
14
15
    /**
16
     * Cache bust file path
17
     *
18
     * @var string
19
     */
20
    protected $strCacheBustFile;
21
22
    /**
23
     * Public folder path
24
     *
25
     * @var string
26
     */
27
    protected $strPublicFolderPath;
28
29
    /**
30
     * File types supported
31
     *
32
     * @var array
33
     */
34
    protected $arrFileTypes;
35
36
    /**
37
     * Destination file extension
38
     *
39
     * @var string
40
     */
41
    protected $strDestinationExtension;
42
43
    /**
44
     * Destination folder
45
     *
46
     * @var
47
     */
48
    protected $strDestinationFolder;
49
50
    /**
51
     * Destination file
52
     *
53
     * @var
54
     */
55
    protected $strDestinationFile;
56
57
    /**
58
     * Set env to dev
59
     *
60
     * @var
61
     */
62
    protected $blnIsDev;
63
64
    /**
65
     * Create a new Minifier Instance
66
     *
67
     * Minify constructor.
68
     */
69
    public function __construct()
70
    {
71
        // set class variables
72
        $this->init();
73
74
        // set environment
75
        $this->objEnvironment = new Environment();
76
    }
77
78
79
    /**
80
     * Set Class Variables
81
     *
82
     * @return null
83
     */
84
    public function init()
85
    {
86
        // set public folder location
87
        $this->setPublicFolder();
88
89
        // set file types we want minified
90
        $this->arrFileTypes = [
91
            'js' => ['js'],
92
            'css' => ['css', 'sass', 'scss']
93
        ];
94
95
        return null;
96
    }
97
98
    /**
99
     * Build minified file
100
     *
101
     * @var $strFolder string
102
     * @var $strFile string
103
     * @return string
104
     */
105
    public function js($strFolder = '/js', $strFile = 'app.min.js')
106
    {
107
        return $this->getMinifiedFile($strType = 'js', $strFolder, $strFile);
108
    }
109
110
    /**
111
     * Build CSS minified file
112
     *
113
     * @var $strFolder string
114
     * @var $strFile string
115
     * @return bool|null|string
116
     */
117
    public function css($strFolder = '/css', $strFile = 'app.min.css')
118
    {
119
        return $this->getMinifiedFile($strType = 'css', $strFolder, $strFile);
120
    }
121
    
122
    /**
123
     * Generate minified file
124
     *
125
     * @param string $strType
126
     * @param string $strFolder
127
     * @param string $strFile
128
     * @return string
129
     */
130
    public function getMinifiedFile($strType = 'css', $strFolder = '/css', $strFile = 'app.min.css')
131
    {
132
        $this->setDestinationExtensionType($strType);
133
        $this->setDestinationFolder($strFolder);
134
        $this->setDestinationFile($strFile);
135
        $this->setCacheBustFile();
136
        return $strFolder . '/' . $strFile .'?' . $this->process();
137
    }
138
139
    /**
140
     * Process Built
141
     *
142
     * @return bool|null|string
143
     */
144
    public function process()
145
    {
146
        // return last cache bust in non development environments
147
        if (!$this->objEnvironment->isDevEnv()) {
148
            return $this->getCacheBust();
149
        }
150
151
        // build application file
152
        $strApplicationFileContents = $this->build();
153
154
        // save application file
155
        file_put_contents($this->getAppFileName(), $strApplicationFileContents);
156
157
        // save new cache bust
158
        return $this->saveCacheBust($strApplicationFileContents);
159
    }
160
161
    /**
162
     * Build file
163
     *
164
     * @return string
165
     */
166
    public function build()
167
    {
168
        // files folder
169
        $strFilesFolder = $this->getFilesFolder();
170
171
        // abort if folder not found
172
        if (!is_dir($strFilesFolder)) {
173
            return $strFilesFolder . ' folder not found';
174
        }
175
176
        // get all files
177
        $arrFiles = scandir($strFilesFolder);
178
179
        // loop through all files
180
        $strMinifiedFileContents = '';
181
        foreach ($arrFiles as $strFileName) {
182
            // get minified content
183
            $strFileContents = $this->getMinifiedContent($strFileName);
184
185
            // don't include empty files
186
            if (!$strFileContents) {
187
                continue;
188
            }
189
190
            // add new minified file to concatenated version
191
            $strMinifiedFileContents .= "\n/* $strFileName */\n" . $strFileContents;
192
        }
193
194
        // returned concatenated version of minifired files
195
        return $strMinifiedFileContents;
196
    }
197
198
    /**
199
     * Get minified Content
200
     *
201
     * @param $strFileName
202
     * @return bool|string
203
     */
204
    public function getMinifiedContent($strFileName)
205
    {
206
        // get file extension
207
        $arrFileName = explode('.', $strFileName);
208
        $strFileExtension = array_pop($arrFileName);
209
210
        // must be a listed file type
211
        if (!in_array($strFileExtension, $this->arrFileTypes[$this->getDestinationExtension()])) {
212
            return '';
213
        }
214
215
        // must not be the app file
216
        if ($strFileName == $this->getDestinationFile()) {
217
            return '';
218
        }
219
220
        // build file path and name
221
        $strFile = $this->getFilesFolder() . '/' . $strFileName;
222
223
        // if it's minified already return content
224
        if (preg_match('/\.min\./', $strFile)) {
225
            return file_get_contents($strFile);
226
        }
227
228
        // return minified content
229
        return $this->minifyContent($strFile);
230
    }
231
232
    /**
233
     * Minify content
234
     *
235
     * @param  $strFile
236
     * @return bool|string
237
     */
238
    public function minifyContent($strFile)
239
    {
240
        // minify based on file type
241
        switch ($this->getDestinationExtension()) {
242
            case 'js':
243
                return (new Minifier\JS($strFile))->minify();
244
            case 'css':
245
                return (new Minifier\CSS($strFile))->minify();
246
            default:
247
                return '';
248
        }
249
    }
250
251
    /**
252
     * Save Cache Bust
253
     *
254
     * @var string $strApplicationFileContents
255
     * @return null
256
     */
257
    public function saveCacheBust($strApplicationFileContents = '')
258
    {
259
        // get contents signature
260
        $strNewCacheBust = md5($strApplicationFileContents);
261
262
        // get prior cache bust
263
        $strPriorCacheBust = $this->getCacheBust();
264
265
        // if unchanged, stop here
266
        if ($strPriorCacheBust == $strNewCacheBust) {
267
            return $strPriorCacheBust;
268
        }
269
270
        // set new cache bust
271
        file_put_contents($this->strCacheBustFile, $strNewCacheBust);
272
273
        // return new cache bust
274
        return $strNewCacheBust;
275
    }
276
277
    /**
278
     * Get Cache Bust
279
     *
280
     * @return bool|string
281
     */
282
    public function getCacheBust()
283
    {
284
        // abort if file not found
285
        if (!file_exists($this->strCacheBustFile)) {
286
            return '';
287
        }
288
289
        // get cache bust file contents
290
        return file_get_contents($this->strCacheBustFile);
291
    }
292
293
    /**
294
     * Set env to Dev
295
     *
296
     * @param bool $bln
297
     * @return $this
298
     */
299
    public function setDev($bln = true)
300
    {
301
        $this->objEnvironment->setDev($bln);
302
        $this->blnIsDev = $bln;
303
        return $this;
304
    }
305
306
    /**
307
     * Set Cache bust file
308
     *
309
     * @param string $strFile
310
     * @return $this
311
     */
312
    public function setCacheBustFile($strFile = 'autominifier-cache-bust.txt')
313
    {
314
        $this->strCacheBustFile = $this->getFilesFolder() . "/$strFile";
315
        return $this;
316
    }
317
318
    /**
319
     * Set public folder
320
     *
321
     * @param string $strFolder
322
     * @return $this
323
     */
324
    public function setPublicFolder($strFolder = '/../../../../public')
325
    {
326
        // set application public path relative to package location
327
        $this->strPublicFolderPath = __DIR__ . $strFolder;
328
        return $this;
329
    }
330
331
    /**
332
     * Set test folder
333
     *
334
     * @return $this
335
     */
336
    public function setTest()
337
    {
338
        // set application public path relative to package location
339
        $this->setPublicFolder('/../build');
340
341
        // cache bust file
342
        $this->setCacheBustFile();
343
344
        return $this;
345
    }
346
347
    /**
348
     * Set destination folder
349
     *
350
     * @param $strFolder
351
     * @return $this
352
     */
353
    public function setDestinationFolder($strFolder)
354
    {
355
        $this->strDestinationFolder = $strFolder;
356
        return $this;
357
    }
358
359
    /**
360
     * Set destination file
361
     *
362
     * @param $strFile
363
     * @return $this
364
     */
365
    public function setDestinationFile($strFile)
366
    {
367
        $this->strDestinationFile = $strFile;
368
        return $this;
369
    }
370
371
    /**
372
     * Set destination file extension
373
     *
374
     * @param $strDestinationExtension
375
     */
376
    public function setDestinationExtensionType($strDestinationExtension)
377
    {
378
        $this->strDestinationExtension = $strDestinationExtension;
379
    }
380
381
    /**
382
     * Get Public folder
383
     *
384
     * @return string
385
     */
386
    public function getPublicFolder()
387
    {
388
        return $this->strPublicFolderPath;
389
    }
390
391
    /**
392
     * Get destination folder
393
     *
394
     * @return mixed
395
     */
396
    public function getDestinationFolder()
397
    {
398
        return $this->strDestinationFolder;
399
    }
400
401
    /**
402
     * Set destination file
403
     *
404
     * @return mixed
405
     */
406
    public function getDestinationFile()
407
    {
408
        return $this->strDestinationFile;
409
    }
410
411
    /**
412
     * Get application file name
413
     *
414
     * @return string
415
     */
416
    public function getAppFileName()
417
    {
418
        return $this->getFilesFolder() . '/' . $this->getDestinationFile();
419
    }
420
421
    /**
422
     * Get destination file type extension
423
     *
424
     * @return mixed
425
     */
426
    public function getDestinationExtension()
427
    {
428
        return $this->strDestinationExtension;
429
    }
430
431
    public function getFilesFolder()
432
    {
433
        return $this->getPublicFolder() . '/' . $this->getDestinationFolder();
434
    }
435
}
436