1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of Cecil. |
7
|
|
|
* |
8
|
|
|
* Copyright (c) Arnaud Ligny <[email protected]> |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Cecil\Step\Assets; |
15
|
|
|
|
16
|
|
|
use Cecil\Assets\Cache; |
17
|
|
|
use Cecil\Exception\RuntimeException; |
18
|
|
|
use Cecil\Step\AbstractStep; |
19
|
|
|
use Cecil\Util; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Assets saving. |
23
|
|
|
*/ |
24
|
|
|
class Save extends AbstractStep |
25
|
|
|
{ |
26
|
|
|
protected Cache $cache; |
27
|
|
|
protected string $cacheKey; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* {@inheritdoc} |
31
|
|
|
*/ |
32
|
1 |
|
public function getName(): string |
33
|
|
|
{ |
34
|
1 |
|
return 'Saving assets'; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* {@inheritdoc} |
39
|
|
|
*/ |
40
|
1 |
|
public function init(array $options): void |
41
|
|
|
{ |
42
|
|
|
// last build step: should clear cache? |
43
|
1 |
|
$this->clearCacheIfDisabled(); |
44
|
|
|
|
45
|
1 |
|
if ($options['dry-run']) { |
46
|
|
|
return; |
47
|
|
|
} |
48
|
|
|
|
49
|
1 |
|
$this->cache = new \Cecil\Assets\Cache($this->builder, 'assets'); |
50
|
1 |
|
$this->cacheKey = \sprintf('_list__%s', $this->builder->getVersion()); |
51
|
1 |
|
if (empty($this->builder->getAssets()) && $this->cache->has($this->cacheKey)) { |
52
|
|
|
$this->builder->setAssets($this->cache->get($this->cacheKey)); |
53
|
|
|
} |
54
|
|
|
|
55
|
1 |
|
$this->canProcess = true; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* {@inheritdoc} |
60
|
|
|
* Note: a file from `static/` with the same name will NOT be overridden. |
61
|
|
|
* |
62
|
|
|
* @throws RuntimeException |
63
|
|
|
*/ |
64
|
1 |
|
public function process(): void |
65
|
|
|
{ |
66
|
1 |
|
$total = \count($this->builder->getAssets()); |
67
|
1 |
|
$count = 0; |
68
|
1 |
|
foreach ($this->builder->getAssets() as $path) { |
69
|
1 |
|
$count++; |
70
|
1 |
|
Util\File::getFS()->copy($this->cache->getContentFilePathname($path), Util::joinFile($this->config->getOutputPath(), $path), false); |
71
|
1 |
|
$message = \sprintf('Asset "%s" saved', $path); |
72
|
1 |
|
$this->builder->getLogger()->info($message, ['progress' => [$count, $total]]); |
73
|
|
|
} |
74
|
1 |
|
$this->cache->set($this->cacheKey, $this->builder->getAssets()); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Deletes cache directory, if cache is disabled. |
79
|
|
|
*/ |
80
|
1 |
|
private function clearCacheIfDisabled(): void |
81
|
|
|
{ |
82
|
1 |
|
if (!$this->config->isEnabled('cache')) { |
83
|
|
|
try { |
84
|
|
|
Util\File::getFS()->remove($this->config->getCachePath()); |
85
|
|
|
} catch (\Exception) { |
86
|
|
|
throw new RuntimeException(\sprintf('Can\'t remove cache directory "%s".', $this->config->getCachePath())); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|