1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* Copyright (c) Arnaud Ligny <[email protected]> |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Cecil\Step; |
10
|
|
|
|
11
|
|
|
use MatthiasMullie\Minify; |
12
|
|
|
use Symfony\Component\Finder\Finder; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* JS files Optimization. |
16
|
|
|
*/ |
17
|
|
View Code Duplication |
class OptimizeJs extends AbstractStep |
|
|
|
|
18
|
|
|
{ |
19
|
|
|
const TYPE = 'js'; |
20
|
|
|
|
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* {@inheritdoc} |
24
|
|
|
*/ |
25
|
|
|
public function init($options) |
26
|
|
|
{ |
27
|
|
|
if (false === $this->builder->getConfig()->get(sprintf('optimize.%s.enabled', self::TYPE)) |
28
|
|
|
|| false === $this->builder->getConfig()->get('optimize.enabled')) |
29
|
|
|
{ |
30
|
|
|
$this->process = false; |
31
|
|
|
|
32
|
|
|
return; |
33
|
|
|
} |
34
|
|
|
if ($options['dry-run']) { |
35
|
|
|
$this->process = false; |
36
|
|
|
|
37
|
|
|
return; |
38
|
|
|
} |
39
|
|
|
if (is_dir($this->builder->getConfig()->getOutputPath())) { |
40
|
|
|
$this->process = true; |
41
|
|
|
} |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* {@inheritdoc} |
46
|
|
|
*/ |
47
|
|
|
public function process() |
48
|
|
|
{ |
49
|
|
|
call_user_func_array($this->builder->getMessageCb(), ['OPTIMIZE', sprintf('Optimizing %s', self::TYPE)]); |
50
|
|
|
|
51
|
|
|
$extensions = $this->builder->getConfig()->get(sprintf('optimize.%s.ext', self::TYPE)); |
52
|
|
|
$files = Finder::create() |
53
|
|
|
->files() |
54
|
|
|
->in($this->builder->getConfig()->getOutputPath()) |
55
|
|
|
->name('/\.('.implode('|', $extensions).')$/') |
56
|
|
|
->notName('/\.min\.(' . implode('|', $extensions) . ')$/') |
57
|
|
|
->sortByName(true); |
58
|
|
|
$max = count($files); |
59
|
|
|
|
60
|
|
|
if ($max <= 0) { |
61
|
|
|
$message = 'No files'; |
62
|
|
|
call_user_func_array($this->builder->getMessageCb(), ['OPTIMIZE_PROGRESS', $message]); |
63
|
|
|
|
64
|
|
|
return; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
$count = 0; |
68
|
|
|
$optimized = 0; |
69
|
|
|
|
70
|
|
|
// setup processor |
71
|
|
|
// |
72
|
|
|
|
73
|
|
|
/* @var $file \Symfony\Component\Finder\SplFileInfo */ |
74
|
|
|
foreach ($files as $file) { |
75
|
|
|
$sizeBefore = 0; |
|
|
|
|
76
|
|
|
$sizeAfter = 0; |
|
|
|
|
77
|
|
|
$count++; |
78
|
|
|
|
79
|
|
|
$sizeBefore = $file->getSize(); |
80
|
|
|
|
81
|
|
|
// process file |
82
|
|
|
$minifier = new Minify\JS($file->getPathname()); |
83
|
|
|
$minified = $minifier->minify(); |
84
|
|
|
\Cecil\Util::getFS()->dumpFile($file->getPathname(), $minified); |
85
|
|
|
|
86
|
|
|
$sizeAfter = $file->getSize(); |
87
|
|
|
|
88
|
|
|
$subpath = \Cecil\Util::getFS()->makePathRelative( |
89
|
|
|
$file->getPath(), |
90
|
|
|
$this->builder->getConfig()->getOutputPath() |
91
|
|
|
); |
92
|
|
|
$subpath = trim($subpath, './'); |
93
|
|
|
$path = $subpath ? $subpath . '/' . $file->getFilename() : $file->getFilename(); |
94
|
|
|
|
95
|
|
|
$message = sprintf( |
96
|
|
|
'%s: %s Ko -> %s Ko', |
97
|
|
|
$path, |
98
|
|
|
ceil($sizeBefore / 1000), |
99
|
|
|
ceil($sizeAfter / 1000) |
100
|
|
|
); |
101
|
|
|
call_user_func_array($this->builder->getMessageCb(), ['OPTIMIZE_PROGRESS', $message, $count, $max]); |
102
|
|
|
if ($sizeAfter < $sizeBefore) { |
103
|
|
|
$optimized++; |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
if ($optimized == 0) { |
107
|
|
|
$message = 'Nothing to do'; |
108
|
|
|
call_user_func_array($this->builder->getMessageCb(), ['OPTIMIZE_PROGRESS', $message]); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.