-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.php
162 lines (127 loc) · 3.42 KB
/
build.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
<?php
/*
* Input: files matching src/pages/*.html
* Output: public/*.html
*
* Pages can extend a template by calling:
*
* extend(string $relativeTemplatePath, array $variables)
*
* at the start and:
*
* endExtend()
*
* at the end. Inside the template you can:
*
* echo $content;
*
* Pages and parts can use the following method to pass data down:
*
* includePart(string $relativePath, array $variables, bool $print)
*
* If environment variables are needed you can define these as PHP constants in:
*
* - env.php
* - env.prod.php
*
* Don't put anything secret in here. These are compiled into the built HTML.
*
* To run in prod mode, call `build.php --prod`
*/
$inPath = __DIR__ . '/src/pages';
$outPath = __DIR__ . '/public';
$savedTemplatePath = null;
$savedTemplateVariables = [];
function includePart(string $relativePath, array $variables = [], bool $print = true)
{
$calledBy = debug_backtrace()[0]['file'];
$actualPath = dirname($calledBy) . '/' . $relativePath;
$output = NULL;
if (! file_exists($actualPath)) {
echo("\n\nERROR: Part $actualPath doesn't exist\n\n");
die();
}
// Extract the variables to a local namespace
extract($variables);
// Start output buffering
ob_start();
// Include the template file
include $actualPath;
// End buffering and return its contents
$output = ob_get_clean();
if ($print) {
print $output;
}
return $output;
}
function extend(string $template, array $variables)
{
global $savedTemplatePath, $savedTemplateVariables;
$calledBy = debug_backtrace()[0]['file'];
$actualPath = dirname($calledBy) . '/' . $template;
$savedTemplatePath = $actualPath;
$savedTemplateVariables = $variables;
// Start output buffering
ob_start();
}
function endExtend()
{
global $savedTemplatePath, $savedTemplateVariables;
// End output buffering of content
$content = ob_get_clean();
extract($savedTemplateVariables);
ob_start();
include $savedTemplatePath;
$output = ob_get_clean();
echo $output;
}
function buildFile($filePath)
{
$output = NULL;
if (file_exists($filePath)) {
// Start output buffering
ob_start();
// Include the template file
include $filePath;
// End buffering and return its contents
$output = ob_get_clean();
}
return $output;
}
/*
* This checks for, creates, and deletes the HTML file contents of the path
*/
function clean($path) {
echo "CLEANING HTML from $path\n";
if (! file_exists($path)) {
echo "CREATING output path $path\n";
if (! mkdir($path, 0777, true)) {
echo "ERROR: Couldn't create output directory $path\n";
die();
};
}
$htmlFiles = glob($path . '/*.html');
foreach($htmlFiles as $htmlFile) {
echo "DELETING $htmlFile\n";
unlink($htmlFile);
}
}
function build()
{
global $inPath, $outPath;
clean($outPath);
$files = glob($inPath . '/*.html');
foreach ($files as $file) {
$outfile = str_replace($inPath, $outPath, $file);
print "BUILDING $file to $outfile\n";
$out = buildFile($file, false);
file_put_contents($outfile, $out);
}
}
if (isset($_SERVER['argv'][1]) && '--prod' === $_SERVER['argv'][1]) {
echo "Running production\n\n";
include('./env.prod.php');
} else {
include('./env.php');
}
build();