|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the Cecil/Cecil package. |
|
4
|
|
|
* |
|
5
|
|
|
* Copyright (c) Arnaud Ligny <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Cecil\Step; |
|
12
|
|
|
|
|
13
|
|
|
use Cecil\Assets\Cache; |
|
14
|
|
|
use Cecil\Exception\Exception; |
|
15
|
|
|
use Symfony\Component\Finder\Finder; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Post Processing. |
|
19
|
|
|
*/ |
|
20
|
|
|
abstract class AbstractPostProcess extends AbstractStep |
|
21
|
|
|
{ |
|
22
|
|
|
/** @var string File type (ie: 'css') */ |
|
23
|
|
|
protected $type = 'type'; |
|
24
|
|
|
/** @var mixed File processor */ |
|
25
|
|
|
protected $processor; |
|
26
|
|
|
|
|
27
|
|
|
const CACHE_FILES = 'postprocess/files'; |
|
28
|
|
|
const CACHE_HASH = 'postprocess/hash'; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* {@inheritdoc} |
|
32
|
|
|
*/ |
|
33
|
|
|
public function init($options) |
|
34
|
|
|
{ |
|
35
|
|
|
if ($options['dry-run']) { |
|
36
|
|
|
$this->canProcess = false; |
|
37
|
|
|
|
|
38
|
|
|
return; |
|
39
|
|
|
} |
|
40
|
|
|
if (false === $this->builder->getConfig()->get(sprintf('postprocess.%s.enabled', $this->type))) { |
|
41
|
|
|
$this->canProcess = false; |
|
42
|
|
|
|
|
43
|
|
|
return; |
|
44
|
|
|
} |
|
45
|
|
|
if (true === $this->builder->getConfig()->get('postprocess.enabled')) { |
|
46
|
|
|
$this->canProcess = true; |
|
47
|
|
|
} |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* {@inheritdoc} |
|
52
|
|
|
*/ |
|
53
|
|
|
public function process() |
|
54
|
|
|
{ |
|
55
|
|
|
$this->setProcessor(); |
|
56
|
|
|
|
|
57
|
|
|
$extensions = $this->builder->getConfig()->get(sprintf('postprocess.%s.ext', $this->type)); |
|
58
|
|
|
if (empty($extensions)) { |
|
59
|
|
|
throw new Exception(sprintf('The config key "postprocess.%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
|
|
|
$this->builder->getLogger()->info('No files'); |
|
72
|
|
|
|
|
73
|
|
|
return; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
$count = 0; |
|
77
|
|
|
$postprocessed = 0; |
|
78
|
|
|
$cache = new Cache($this->builder, 'postprocess', $this->config->getOutputPath()); |
|
79
|
|
|
|
|
80
|
|
|
/** @var \Symfony\Component\Finder\SplFileInfo $file */ |
|
81
|
|
|
foreach ($files as $file) { |
|
82
|
|
|
$count++; |
|
83
|
|
|
$sizeBefore = $file->getSize(); |
|
84
|
|
|
$message = $file->getRelativePathname(); |
|
85
|
|
|
$fileContent = file_get_contents($file->getPathname()); |
|
86
|
|
|
|
|
87
|
|
|
if (!$cache->hasHash($file->getRelativePathname(), $cache->createHash($fileContent))) { |
|
88
|
|
|
$this->processFile($file); |
|
89
|
|
|
$fileProcessedContent = file_get_contents($file->getPathname()); |
|
90
|
|
|
$postprocessed++; |
|
91
|
|
|
|
|
92
|
|
|
$sizeAfter = $file->getSize(); |
|
93
|
|
|
if ($sizeAfter < $sizeBefore) { |
|
94
|
|
|
$message = sprintf( |
|
95
|
|
|
'%s (%s Ko -> %s Ko)', |
|
96
|
|
|
$file->getRelativePathname(), |
|
97
|
|
|
ceil($sizeBefore / 1000), |
|
98
|
|
|
ceil($sizeAfter / 1000) |
|
99
|
|
|
); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
$cache->set($file->getRelativePathname(), $fileProcessedContent); |
|
103
|
|
|
|
|
104
|
|
|
$this->builder->getLogger()->info($message, ['progress' => [$count, $max]]); |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
if ($postprocessed == 0) { |
|
108
|
|
|
$this->builder->getLogger()->info('Nothing to do'); |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Set file processor object. |
|
114
|
|
|
* |
|
115
|
|
|
* @return void |
|
116
|
|
|
*/ |
|
117
|
|
|
abstract public function setProcessor(); |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* Process a file. |
|
121
|
|
|
* |
|
122
|
|
|
* @param \Symfony\Component\Finder\SplFileInfo $file |
|
123
|
|
|
* |
|
124
|
|
|
* @return void |
|
125
|
|
|
*/ |
|
126
|
|
|
abstract public function processFile(\Symfony\Component\Finder\SplFileInfo $file); |
|
127
|
|
|
} |
|
128
|
|
|
|