Skip to content

Commit 7e7d14a

Browse files
committed
Move manifest-generating code to manifest.inc
This allows generating the manifest during deployment without needing all the requirements in base.inc.
1 parent 3c2b2e9 commit 7e7d14a

3 files changed

Lines changed: 63 additions & 62 deletions

File tree

SETUP/generate_manifest_cache.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,11 @@
77
throw new RuntimeException("Script is meant to be run via CLI");
88
}
99

10-
if (! is_file("$relPath/base.inc")) {
10+
if (! is_file("$relPath/manifest.inc")) {
1111
throw new RuntimeException("First argument '$relPath' does not point to `pinc/` directory");
1212
}
1313

14-
include_once($relPath."base.inc");
15-
include_once($relPath."html_page_common.inc");
14+
include_once($relPath."manifest.inc");
1615

1716
$manifest = get_js_manifest($basedir);
1817
if (!$manifest) {

pinc/html_page_common.inc

Lines changed: 2 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php
2-
include_once($relPath."languages.inc"); // html_lang_header();
2+
include_once($relPath."languages.inc"); // lang_html_header();
3+
include_once($relPath."manifest.inc");
34

45
define('NO_STATSBAR', false);
56
define('SHOW_STATSBAR', true);
@@ -199,61 +200,3 @@ function get_local_file_browser_cache_key($url)
199200

200201
return $cache_fixer;
201202
}
202-
203-
/**
204-
* @return string[]
205-
*/
206-
function get_js_manifest(string $basedir = ""): array
207-
{
208-
global $code_dir;
209-
210-
if (!$basedir) {
211-
$basedir = $code_dir;
212-
}
213-
214-
// first, see if the PHP version of our manifest file exists, and if so
215-
// load it and get the opcache-cached version without hitting the disk
216-
if (is_file("$basedir/dist/manifest.php")) {
217-
require_once("$basedir/dist/manifest.php");
218-
return get_cached_js_manifest(); // @phpstan-ignore function.notFound
219-
}
220-
221-
// if it doesn't exist, see if the JSON version exists and if so load
222-
// and process it
223-
if (is_file("$basedir/dist/manifest.json")) {
224-
$raw_manifest = json_decode(file_get_contents("$basedir/dist/manifest.json"), true) ?? [];
225-
// webpack puts "auto" in the pathname, no idea why
226-
foreach ($raw_manifest as $filename => $path) {
227-
$manifest[$filename] = str_replace("auto", "dist", $path);
228-
}
229-
return $manifest;
230-
}
231-
232-
// otherwise, just return an empty manifest
233-
return [];
234-
}
235-
236-
/**
237-
* @param string[] $manifest
238-
*/
239-
function generate_cached_js_manifest(string $basedir, array $manifest): void
240-
{
241-
if (!$manifest) {
242-
return;
243-
}
244-
245-
if (!is_dir("$basedir/dist")) {
246-
throw new RuntimeException("Directory '$basedir/dist' does not exist.");
247-
}
248-
249-
$serialized_manifest = serialize($manifest);
250-
$cached_php_file = <<<EOF
251-
<?php
252-
function get_cached_js_manifest(): array
253-
{
254-
return unserialize('$serialized_manifest');
255-
}
256-
EOF;
257-
258-
file_put_contents("$basedir/dist/manifest.php", $cached_php_file);
259-
}

pinc/manifest.inc

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
/**
4+
* @return string[]
5+
*/
6+
function get_js_manifest(string $basedir = ""): array
7+
{
8+
global $code_dir;
9+
10+
if (!$basedir) {
11+
$basedir = $code_dir;
12+
}
13+
14+
// first, see if the PHP version of our manifest file exists, and if so
15+
// load it and get the opcache-cached version without hitting the disk
16+
if (is_file("$basedir/dist/manifest.php")) {
17+
require_once("$basedir/dist/manifest.php");
18+
return get_cached_js_manifest(); // @phpstan-ignore function.notFound
19+
}
20+
21+
// if it doesn't exist, see if the JSON version exists and if so load
22+
// and process it
23+
if (is_file("$basedir/dist/manifest.json")) {
24+
$raw_manifest = json_decode(file_get_contents("$basedir/dist/manifest.json"), true) ?? [];
25+
// webpack puts "auto" in the pathname, no idea why
26+
foreach ($raw_manifest as $filename => $path) {
27+
$manifest[$filename] = str_replace("auto", "dist", $path);
28+
}
29+
return $manifest;
30+
}
31+
32+
// otherwise, just return an empty manifest
33+
return [];
34+
}
35+
36+
/**
37+
* @param string[] $manifest
38+
*/
39+
function generate_cached_js_manifest(string $basedir, array $manifest): void
40+
{
41+
if (!$manifest) {
42+
return;
43+
}
44+
45+
if (!is_dir("$basedir/dist")) {
46+
throw new RuntimeException("Directory '$basedir/dist' does not exist.");
47+
}
48+
49+
$serialized_manifest = serialize($manifest);
50+
$cached_php_file = <<<EOF
51+
<?php
52+
function get_cached_js_manifest(): array
53+
{
54+
return unserialize('$serialized_manifest');
55+
}
56+
EOF;
57+
58+
file_put_contents("$basedir/dist/manifest.php", $cached_php_file);
59+
}

0 commit comments

Comments
 (0)