|
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 Exception; |
|
12
|
|
|
use Symfony\Component\Finder\Finder; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Files Optimization. |
|
16
|
|
|
*/ |
|
17
|
|
|
abstract class AbstractStepOptimize extends AbstractStep |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* File type (ie: 'css'). |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $type = 'type'; |
|
23
|
|
|
/** |
|
24
|
|
|
* File processor. |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $processor; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* {@inheritdoc} |
|
30
|
|
|
*/ |
|
31
|
|
|
public function init($options) |
|
32
|
|
|
{ |
|
33
|
|
|
if (false === $this->builder->getConfig()->get(sprintf('optimize.%s.enabled', $this->type)) |
|
34
|
|
|
|| false === $this->builder->getConfig()->get('optimize.enabled')) { |
|
35
|
|
|
$this->process = false; |
|
36
|
|
|
|
|
37
|
|
|
return; |
|
38
|
|
|
} |
|
39
|
|
|
if ($options['dry-run']) { |
|
40
|
|
|
$this->process = false; |
|
41
|
|
|
|
|
42
|
|
|
return; |
|
43
|
|
|
} |
|
44
|
|
|
if (is_dir($this->builder->getConfig()->getOutputPath())) { |
|
45
|
|
|
$this->process = true; |
|
46
|
|
|
} |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* {@inheritdoc} |
|
51
|
|
|
*/ |
|
52
|
|
|
public function process() |
|
53
|
|
|
{ |
|
54
|
|
|
$this->setProcessor(); |
|
55
|
|
|
|
|
56
|
|
|
call_user_func_array($this->builder->getMessageCb(), ['OPTIMIZE', sprintf('Optimizing %s', $this->type)]); |
|
57
|
|
|
|
|
58
|
|
|
$extensions = $this->builder->getConfig()->get(sprintf('optimize.%s.ext', $this->type)); |
|
59
|
|
|
if (empty($extensions)) { |
|
60
|
|
|
throw new \Exception(sprintf('The config key "optimize.%s.ext" is empty', $this->type)); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
$files = Finder::create() |
|
64
|
|
|
->files() |
|
65
|
|
|
->in($this->builder->getConfig()->getOutputPath()) |
|
66
|
|
|
->name('/\.('.implode('|', $extensions).')$/') |
|
67
|
|
|
->notName('/\.min\.('.implode('|', $extensions).')$/') |
|
68
|
|
|
->sortByName(true); |
|
69
|
|
|
$max = count($files); |
|
70
|
|
|
|
|
71
|
|
|
if ($max <= 0) { |
|
72
|
|
|
$message = 'No files'; |
|
73
|
|
|
call_user_func_array($this->builder->getMessageCb(), ['OPTIMIZE_PROGRESS', $message]); |
|
74
|
|
|
|
|
75
|
|
|
return; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
$count = 0; |
|
79
|
|
|
$optimized = 0; |
|
80
|
|
|
|
|
81
|
|
|
/* @var $file \Symfony\Component\Finder\SplFileInfo */ |
|
82
|
|
|
foreach ($files as $file) { |
|
83
|
|
|
$count++; |
|
84
|
|
|
|
|
85
|
|
|
$sizeBefore = $file->getSize(); |
|
86
|
|
|
|
|
87
|
|
|
$this->processFile($file); |
|
88
|
|
|
|
|
89
|
|
|
$sizeAfter = $file->getSize(); |
|
90
|
|
|
|
|
91
|
|
|
$subpath = \Cecil\Util::getFS()->makePathRelative( |
|
92
|
|
|
$file->getPath(), |
|
93
|
|
|
$this->builder->getConfig()->getOutputPath() |
|
94
|
|
|
); |
|
95
|
|
|
$subpath = trim($subpath, './'); |
|
96
|
|
|
$path = $subpath ? $subpath.'/'.$file->getFilename() : $file->getFilename(); |
|
97
|
|
|
|
|
98
|
|
|
$message = sprintf( |
|
99
|
|
|
'%s: %s Ko -> %s Ko', |
|
100
|
|
|
$path, |
|
101
|
|
|
ceil($sizeBefore / 1000), |
|
102
|
|
|
ceil($sizeAfter / 1000) |
|
103
|
|
|
); |
|
104
|
|
|
call_user_func_array($this->builder->getMessageCb(), ['OPTIMIZE_PROGRESS', $message, $count, $max]); |
|
105
|
|
|
if ($sizeAfter < $sizeBefore) { |
|
106
|
|
|
$optimized++; |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
if ($optimized == 0) { |
|
110
|
|
|
$message = 'Nothing to do'; |
|
111
|
|
|
call_user_func_array($this->builder->getMessageCb(), ['OPTIMIZE_PROGRESS', $message]); |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Set file processor object. |
|
117
|
|
|
*/ |
|
118
|
|
|
abstract public function setProcessor(); |
|
|
|
|
|
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Process a file. |
|
122
|
|
|
* |
|
123
|
|
|
* @param \Symfony\Component\Finder\SplFileInfo $file |
|
124
|
|
|
*/ |
|
125
|
|
|
abstract public function processFile(\Symfony\Component\Finder\SplFileInfo $file); |
|
|
|
|
|
|
126
|
|
|
} |
|
127
|
|
|
|
For interface and abstract methods, it is impossible to infer the return type from the immediate code. In these cases, it is generally advisible to explicitly annotate these methods with a
@returndoc comment to communicate to implementors of these methods what they are expected to return.