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 Cecil\Util; |
16
|
|
|
use Symfony\Component\Finder\Finder; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Post Processing. |
20
|
|
|
*/ |
21
|
|
|
abstract class AbstractPostProcess extends AbstractStep |
22
|
|
|
{ |
23
|
|
|
/** @var string File type (ie: 'css') */ |
24
|
|
|
protected $type; |
25
|
|
|
|
26
|
|
|
/** @var mixed File processor */ |
27
|
|
|
protected $processor; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* {@inheritdoc} |
31
|
|
|
*/ |
32
|
|
|
public function init($options) |
33
|
|
|
{ |
34
|
|
|
if ($options['dry-run']) { |
35
|
|
|
$this->canProcess = false; |
36
|
|
|
|
37
|
|
|
return; |
38
|
|
|
} |
39
|
|
|
if (false === $this->builder->getConfig()->get(sprintf('postprocess.%s.enabled', $this->type))) { |
40
|
|
|
$this->canProcess = false; |
41
|
|
|
|
42
|
|
|
return; |
43
|
|
|
} |
44
|
|
|
if (true === $this->builder->getConfig()->get('postprocess.enabled')) { |
45
|
|
|
$this->canProcess = true; |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* {@inheritdoc} |
51
|
|
|
*/ |
52
|
|
|
public function process() |
53
|
|
|
{ |
54
|
|
|
$this->setProcessor(); |
55
|
|
|
|
56
|
|
|
$extensions = $this->builder->getConfig()->get(sprintf('postprocess.%s.ext', $this->type)); |
57
|
|
|
if (empty($extensions)) { |
58
|
|
|
throw new Exception(sprintf('The config key "postprocess.%s.ext" is empty', $this->type)); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
$files = Finder::create() |
62
|
|
|
->files() |
63
|
|
|
->in($this->builder->getConfig()->getOutputPath()) |
64
|
|
|
->name('/\.('.implode('|', $extensions).')$/') |
65
|
|
|
->notName('/\.min\.('.implode('|', $extensions).')$/') |
66
|
|
|
->sortByName(true); |
67
|
|
|
$max = count($files); |
68
|
|
|
|
69
|
|
|
if ($max <= 0) { |
70
|
|
|
$this->builder->getLogger()->info('No files'); |
71
|
|
|
|
72
|
|
|
return; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
$count = 0; |
76
|
|
|
$postprocessed = 0; |
77
|
|
|
$cache = new Cache($this->builder, 'postprocess'); |
78
|
|
|
|
79
|
|
|
/** @var \Symfony\Component\Finder\SplFileInfo $file */ |
80
|
|
|
foreach ($files as $file) { |
81
|
|
|
$count++; |
82
|
|
|
$sizeBefore = $file->getSize(); |
83
|
|
|
$message = $file->getRelativePathname(); |
84
|
|
|
|
85
|
|
|
$cacheKey = $cache->createKeyFromPath($file->getPathname(), $file->getRelativePathname()); |
86
|
|
|
if (!$cache->has($cacheKey)) { |
87
|
|
|
$processed = $this->processFile($file); |
88
|
|
|
$sizeAfter = $file->getSize(); |
89
|
|
|
if ($sizeAfter < $sizeBefore) { |
90
|
|
|
$message = sprintf( |
91
|
|
|
'%s (%s Ko -> %s Ko)', |
92
|
|
|
$file->getRelativePathname(), |
93
|
|
|
ceil($sizeBefore / 1000), |
94
|
|
|
ceil($sizeAfter / 1000) |
95
|
|
|
); |
96
|
|
|
} |
97
|
|
|
$postprocessed++; |
98
|
|
|
$processed = $this->encode($processed); |
99
|
|
|
$cache->set($cacheKey, $processed); |
100
|
|
|
$this->builder->getLogger()->info($message, ['progress' => [$count, $max]]); |
101
|
|
|
} |
102
|
|
|
$processed = $cache->get($cacheKey); |
103
|
|
|
$processed = $this->decode($processed); |
|
|
|
|
104
|
|
|
Util\File::getFS()->dumpFile($file->getPathname(), $processed); |
105
|
|
|
} |
106
|
|
|
if ($postprocessed == 0) { |
107
|
|
|
$this->builder->getLogger()->info('Nothing to do'); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Set file processor object. |
113
|
|
|
*/ |
114
|
|
|
abstract public function setProcessor(): void; |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Process a file. |
118
|
|
|
*/ |
119
|
|
|
abstract public function processFile(\Symfony\Component\Finder\SplFileInfo $file): string; |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Encode file content. |
123
|
|
|
*/ |
124
|
|
|
public function encode(string $content): string |
125
|
|
|
{ |
126
|
|
|
return $content; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Decode file content. |
131
|
|
|
*/ |
132
|
|
|
public function decode(string $content): string |
133
|
|
|
{ |
134
|
|
|
return $content; |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|