Completed
Push — master ( ba39f4...421481 )
by Florin
01:50
created

Minify::isLocalhost()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A Minify::getDestinationExtension() 0 4 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
    /**
14
     * Cache bust file path
15
     *
16
     * @var string
17
     */
18
    protected $strCacheBustFile;
19
20
    /**
21
     * Public folder path
22
     *
23
     * @var string
24
     */
25
    protected $strPublicFolderPath;
26
27
    /**
28
     * File types supported
29
     *
30
     * @var array
31
     */
32
    protected $arrFileTypes;
33
34
    /**
35
     * Destination file extension
36
     *
37
     * @var string
38
     */
39
    protected $strDestinationExtension;
40
41
    /**
42
     * Destination folder
43
     *
44
     * @var
45
     */
46
    protected $strDestinationFolder;
47
48
    /**
49
     * Destination file
50
     *
51
     * @var
52
     */
53
    protected $strDestinationFile;
54
55
    /**
56
     * Set env to dev
57
     *
58
     * @var
59
     */
60
    protected $blnIsDev;
61
62
    /**
63
     * Create a new Minifier Instance
64
     *
65
     * Minify constructor.
66
     */
67
    public function __construct()
68
    {
69
        // set class variables
70
        $this->init();
71
72
        // set environment
73
        $this->enviroment = new Environment();
0 ignored issues
show
Bug introduced by
The property enviroment does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

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