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 Cecil\Exception\Exception; |
12
|
|
|
use Symfony\Component\Finder\Finder; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Static Files Processing. |
16
|
|
|
*/ |
17
|
|
|
abstract class AbstractStaticProcess 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 ($options['dry-run']) { |
34
|
|
|
$this->process = false; |
35
|
|
|
|
36
|
|
|
return; |
37
|
|
|
} |
38
|
|
|
if (false === $this->builder->getConfig()->get(sprintf('optimize.%s.enabled', $this->type))) { |
39
|
|
|
$this->process = false; |
40
|
|
|
|
41
|
|
|
return; |
42
|
|
|
} |
43
|
|
|
if (true === $this->builder->getConfig()->get('optimize.enabled')) { |
44
|
|
|
$this->process = true; |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* {@inheritdoc} |
50
|
|
|
*/ |
51
|
|
|
public function process() |
52
|
|
|
{ |
53
|
|
|
$this->setProcessor(); |
54
|
|
|
|
55
|
|
|
call_user_func_array($this->builder->getMessageCb(), ['OPTIMIZE', sprintf('Optimizing %s', $this->type)]); |
56
|
|
|
|
57
|
|
|
$extensions = $this->builder->getConfig()->get(sprintf('optimize.%s.ext', $this->type)); |
58
|
|
|
if (empty($extensions)) { |
59
|
|
|
throw new Exception(sprintf('The config key "optimize.%s.ext" is empty', $this->type)); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
$files = Finder::create() |
63
|
|
|
->files() |
64
|
|
|
->in($this->builder->getConfig()->getOutputPath()) |
65
|
|
|
->name('/\.('.implode('|', $extensions).')$/') |
66
|
|
|
->notName('/\.min\.('.implode('|', $extensions).')$/') |
67
|
|
|
->sortByName(true); |
68
|
|
|
$max = count($files); |
69
|
|
|
|
70
|
|
|
if ($max <= 0) { |
71
|
|
|
$message = 'No files'; |
72
|
|
|
call_user_func_array($this->builder->getMessageCb(), ['OPTIMIZE_PROGRESS', $message]); |
73
|
|
|
|
74
|
|
|
return; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$count = 0; |
78
|
|
|
$optimized = 0; |
79
|
|
|
|
80
|
|
|
/* @var $file \Symfony\Component\Finder\SplFileInfo */ |
81
|
|
|
foreach ($files as $file) { |
82
|
|
|
$count++; |
83
|
|
|
|
84
|
|
|
$sizeBefore = $file->getSize(); |
85
|
|
|
|
86
|
|
|
$this->processFile($file); |
87
|
|
|
|
88
|
|
|
$sizeAfter = $file->getSize(); |
89
|
|
|
|
90
|
|
|
$subpath = \Cecil\Util::getFS()->makePathRelative( |
91
|
|
|
$file->getPath(), |
92
|
|
|
$this->builder->getConfig()->getOutputPath() |
93
|
|
|
); |
94
|
|
|
$subpath = trim($subpath, './'); |
95
|
|
|
$path = $subpath ? $subpath.'/'.$file->getFilename() : $file->getFilename(); |
96
|
|
|
|
97
|
|
|
$message = sprintf( |
98
|
|
|
'%s: %s Ko -> %s Ko', |
99
|
|
|
$path, |
100
|
|
|
ceil($sizeBefore / 1000), |
101
|
|
|
ceil($sizeAfter / 1000) |
102
|
|
|
); |
103
|
|
|
call_user_func_array($this->builder->getMessageCb(), ['OPTIMIZE_PROGRESS', $message, $count, $max]); |
104
|
|
|
if ($sizeAfter < $sizeBefore) { |
105
|
|
|
$optimized++; |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
if ($optimized == 0) { |
109
|
|
|
$message = 'Nothing to do'; |
110
|
|
|
call_user_func_array($this->builder->getMessageCb(), ['OPTIMIZE_PROGRESS', $message]); |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Set file processor object. |
116
|
|
|
* |
117
|
|
|
* @return void |
118
|
|
|
*/ |
119
|
|
|
abstract public function setProcessor(); |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Process a file. |
123
|
|
|
* |
124
|
|
|
* @param \Symfony\Component\Finder\SplFileInfo $file |
125
|
|
|
* |
126
|
|
|
* @return void |
127
|
|
|
*/ |
128
|
|
|
abstract public function processFile(\Symfony\Component\Finder\SplFileInfo $file); |
129
|
|
|
} |
130
|
|
|
|