/
RevisionCompiler.php
200 lines (159 loc) · 4.85 KB
/
RevisionCompiler.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
<?php
/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/
namespace Flarum\Frontend\Compiler;
use Flarum\Frontend\Compiler\Source\SourceCollector;
use Flarum\Frontend\Compiler\Source\SourceInterface;
use Illuminate\Contracts\Filesystem\Cloud;
/**
* @internal
*/
class RevisionCompiler implements CompilerInterface
{
const EMPTY_REVISION = 'empty';
/**
* @var Cloud
*/
protected $assetsDir;
/**
* @var VersionerInterface
*/
protected $versioner;
/**
* @var string
*/
protected $filename;
/**
* @var callable[]
*/
protected $sourcesCallbacks = [];
/**
* @param Cloud $assetsDir
* @param string $filename
* @param VersionerInterface|null $versioner @deprecated nullable will be removed at v2.0
*/
public function __construct(Cloud $assetsDir, string $filename, VersionerInterface $versioner = null)
{
$this->assetsDir = $assetsDir;
$this->filename = $filename;
$this->versioner = $versioner ?: new FileVersioner($assetsDir);
}
public function getFilename(): string
{
return $this->filename;
}
public function setFilename(string $filename)
{
$this->filename = $filename;
}
public function commit(bool $force = false)
{
$sources = $this->getSources();
$oldRevision = $this->versioner->getRevision($this->filename);
$newRevision = $this->calculateRevision($sources);
// In case the previous and current revisions do not match
// Or no file was written yet, let's save the file to disk.
if ($force || $oldRevision !== $newRevision || ! $this->assetsDir->exists($this->filename)) {
if (! $this->save($this->filename, $sources)) {
// If no file was written (because the sources were empty), we
// will set the revision to a special value so that we can tell
// that this file does not have a URL.
$newRevision = static::EMPTY_REVISION;
}
$this->versioner->putRevision($this->filename, $newRevision);
}
}
public function addSources(callable $callback)
{
$this->sourcesCallbacks[] = $callback;
}
/**
* @return SourceInterface[]
*/
protected function getSources(): array
{
$sources = new SourceCollector;
foreach ($this->sourcesCallbacks as $callback) {
$callback($sources);
}
return $sources->getSources();
}
public function getUrl(): ?string
{
$revision = $this->versioner->getRevision($this->filename);
if (! $revision) {
$this->commit();
$revision = $this->versioner->getRevision($this->filename);
if (! $revision) {
return null;
}
}
if ($revision === static::EMPTY_REVISION) {
return null;
}
$url = $this->assetsDir->url($this->filename);
// Append revision as GET param to signify that there's been
// a change to the file and it should be refreshed.
return "$url?v=$revision";
}
/**
* @param string $file
* @param SourceInterface[] $sources
* @return bool true if the file was written, false if there was nothing to write
*/
protected function save(string $file, array $sources): bool
{
if ($content = $this->compile($sources)) {
$this->assetsDir->put($file, $content);
return true;
}
return false;
}
/**
* @param SourceInterface[] $sources
*/
protected function compile(array $sources): string
{
$output = '';
foreach ($sources as $source) {
$output .= $this->format($source->getContent());
}
return $output;
}
protected function format(string $string): string
{
return $string;
}
/**
* @param SourceInterface[] $sources
*/
protected function calculateRevision(array $sources): string
{
$cacheDifferentiator = [$this->getCacheDifferentiator()];
foreach ($sources as $source) {
$cacheDifferentiator[] = $source->getCacheDifferentiator();
}
return hash('crc32b', serialize($cacheDifferentiator));
}
protected function getCacheDifferentiator(): ?array
{
return null;
}
public function flush()
{
if ($this->versioner->getRevision($this->filename) !== null) {
$this->delete($this->filename);
$this->versioner->putRevision($this->filename, null);
}
}
protected function delete(string $file)
{
if ($this->assetsDir->exists($file)) {
$this->assetsDir->delete($file);
}
}
}