-
Notifications
You must be signed in to change notification settings - Fork 148
Expand file tree
/
Copy pathCreateManifest.class.php
More file actions
80 lines (72 loc) · 2.75 KB
/
CreateManifest.class.php
File metadata and controls
80 lines (72 loc) · 2.75 KB
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
<?php
namespace wcf\command\style;
use wcf\data\page\PageCache;
use wcf\data\style\Style;
use wcf\system\io\AtomicWriter;
use wcf\system\language\LanguageFactory;
use wcf\system\WCF;
/**
* Generate then `manifest-*.json` files for a style.
*
* @author Olaf Braun
* @copyright 2001-2024 WoltLab GmbH
* @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
* @since 6.2
*/
final class CreateManifest
{
public function __construct(
private readonly Style $style
) {}
public function __invoke(): void
{
$this->style->loadVariables();
$headerColor = $this->style->getVariable('wcfHeaderBackground', true);
$backgroundColor = $this->style->getVariable('wcfContentBackground', true);
$landingPage = PageCache::getInstance()->getLandingPage();
$icons = [];
foreach ([192, 256, 512] as $iconSize) {
$icons[] = [
"src" => \sprintf(
"%sandroid-chrome-%dx%d.png",
$this->style->hasFavicon ? "" : "../favicon/default.",
$iconSize,
$iconSize
),
"sizes" => "{$iconSize}x{$iconSize}",
"type" => "image/png"
];
}
$icons = \json_encode($icons, \JSON_THROW_ON_ERROR);
$originalLanguage = WCF::getLanguage();
try {
foreach (LanguageFactory::getInstance()->getLanguages() as $language) {
// To get the correct landing page url, we need to change the language.
WCF::setLanguage($language->languageID);
$title = \json_encode($language->get(PAGE_TITLE), \JSON_THROW_ON_ERROR);
$startUrl = \json_encode($landingPage->getLink(), \JSON_THROW_ON_ERROR);
// update manifest.json
$manifest = <<<MANIFEST
{
"name": {$title},
"start_url": {$startUrl},
"icons": {$icons},
"theme_color": "{$headerColor}",
"background_color": "{$backgroundColor}",
"display": "standalone"
}
MANIFEST;
$manifestPath = $this->style->getAssetPath() . "manifest-{$language->languageID}.json";
if (\file_exists($manifestPath) && \hash_equals(\sha1_file($manifestPath), \sha1($manifest))) {
continue;
}
$writer = new AtomicWriter($manifestPath);
$writer->write($manifest);
$writer->flush();
$writer->close();
}
} finally {
WCF::setLanguage($originalLanguage->languageID);
}
}
}