-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
197 lines (153 loc) · 6.64 KB
/
index.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
<?php
declare(strict_types=1);
ini_set('log_errors', "0");
ini_set('display_errors', "1");
require __DIR__ . '/vendor/autoload.php';
$latte = new Latte\Engine;
$file = $argv[1] ?? null;
$config = $argv[2] ?? null;
if ($config) {
try {
$config = json_decode($config, false, 512, JSON_THROW_ON_ERROR);
} catch (JsonException $e) {
throw new Error($e->getMessage());
}
}
$isDocker = str_starts_with($config->bin, 'docker');
if ($isDocker) {
define("ROOT_DIR", '/usr/src/app/');
define("TEMPLATES_DIR", '/usr/src/app/src/templates/');
} else {
define("ROOT_DIR", $config->cwd . '/');
define("TEMPLATES_DIR", $config->cwd . '/src/templates/');
}
define("PACKAGE_DIR", str_replace($config->cwd, ROOT_DIR, $config->packageRoot));
/**
* @throws JsonException
*/
function NodeHandler($name, $type, ...$params) : string {
$params = array_map(static function($value) { return base64_encode((string)$value); }, $params);
exec('node '. PACKAGE_DIR .'/handler.js ' . $name . ' ' . $type .' ' . json_encode(json_encode($params, JSON_HEX_QUOT | JSON_HEX_TAG), JSON_THROW_ON_ERROR), $output);
return base64_decode($output[0] ?? '') ?? $params[1];
}
function NodeHandlerSingle($path, ...$params) : string {
$params = array_map(static function($value) { return base64_encode((string)$value); }, $params);
exec('node '. PACKAGE_DIR .'/handler-single.js ' . $path . ' ' . json_encode(json_encode($params, JSON_HEX_QUOT | JSON_HEX_TAG), JSON_THROW_ON_ERROR), $output);
return base64_decode($output[0] ?? '') ?? $params[1];
}
if (!file_exists(__DIR__ . '/temp') && !mkdir($concurrentDirectory = __DIR__ . '/temp') && !is_dir($concurrentDirectory)) {
throw new Error(sprintf('Directory "%s" was not created', $concurrentDirectory));
}
$latte->setTempDirectory(__DIR__ . '/temp');
$params = [(array)$config->globals];
foreach ($config->data as $dataPath) {
$dataPath = str_replace($config->cwd, ROOT_DIR, $dataPath);
if (!file_exists($dataPath)) {
throw new Error('File not found ' . $dataPath);
}
try {
$params[] = json_decode(file_get_contents($dataPath), true, 512, JSON_THROW_ON_ERROR);
} catch (JsonException $e) {
throw new Error('Error parsing params');
}
}
$file = str_replace($config->cwd, ROOT_DIR, $file);
$withoutTemplate = false;
if (!file_exists($file)) {
throw new Error('File not found ' . $file);
}
if (!str_ends_with($file, '.json.html') && !str_ends_with($file, '.json') && !str_ends_with($file, '.latte.html') && !str_ends_with($file, '.latte')) {
preg_match('/<script\b[^>]*>([\s\S]+)<\/script>/', file_get_contents($file), $fileContents);
$fileContents = $fileContents[1];
} else if (str_ends_with($file, '.latte.html')) {
if (file_exists(str_replace('.latte.html', '.latte.json', $file))) {
$fileContents = file_get_contents(str_replace('.latte.html', '.latte.json', $file));
}
$withoutTemplate = true;
} else if (str_ends_with($file, '.latte')) {
if (file_exists(str_replace('.latte', '.latte.json', $file))) {
$fileContents = file_get_contents(str_replace('.latte', '.latte.json', $file));
}
$withoutTemplate = true;
} else {
$fileContents = file_get_contents($file);
}
if (isset($fileContents)) {
try {
$params[] = json_decode($fileContents, true, 512, JSON_THROW_ON_ERROR);
} catch (JsonException $e) {
throw new Error('Error parsing params');
}
}
$params = array_replace_recursive(...$params);
try {
$params = json_encode($params, JSON_THROW_ON_ERROR);
$params = json_decode($params, false, 512, JSON_THROW_ON_ERROR);
} catch (JsonException $e) {
throw new Error('Error parsing params');
}
if ($withoutTemplate) {
$params->template = $file;
}
foreach (['tel', 'asset'] as $filter) {
require PACKAGE_DIR . '/latte/' . ucfirst($filter) . 'Filter.php';
$latte->addFilter($filter, 'App\Latte\\' . ucfirst($filter) . 'Filter::execute');
}
foreach (['fetch', 'placeholder', 'randomColor'] as $function) {
require PACKAGE_DIR . '/latte/' . ucfirst($function) . 'Function.php';
$latte->addFunction($function, 'App\Latte\\' . ucfirst($function) . 'Function::execute');
}
foreach ($config->filters as $filter => $path) {
if (is_string($path) && str_ends_with($path, '.php')) {
require ROOT_DIR . $path;
$latte->addFilter($filter, 'App\Latte\\' . ucfirst($filter) . 'Filter::execute');
} elseif (!$isDocker && is_string($path) && str_ends_with($path, '.js')) {
$latte->addFilter($filter, function (...$params) use ($path) : string {
return NodeHandlerSingle($path, ...$params);
});
} elseif (!$isDocker) {
$latte->addFilter($filter, function (...$params) use ($filter) : string {
return NodeHandler($filter, 'filters', ...$params);
});
}
}
foreach ($config->functions as $function => $path) {
if (is_string($path) && str_ends_with($path, '.php')) {
require ROOT_DIR . $path;
$latte->addFunction($function, 'App\Latte\\' . ucfirst($function) . 'Function::execute');
} elseif (!$isDocker && is_string($path) && str_ends_with($path, '.js')) {
$latte->addFunction($function, function (...$params) use ($path) : string {
return NodeHandlerSingle($path, ...$params);
});
} elseif (!$isDocker) {
$latte->addFunction($function, function (...$params) use ($function) : string {
return NodeHandler($function, 'functions', ...$params);
});
}
}
$translator = static function (string $original): string {
return $original;
};
$latte->addExtension(new Latte\Essential\TranslatorExtension($translator));
$tag = 'json';
require PACKAGE_DIR . '/latte/' . ucfirst($tag) . 'Tag.php';
eval('$latte->addExtension(new App\Latte\\' . ucfirst($tag) . 'Extension);');
foreach ($config->tags as $tag => $path) {
require ROOT_DIR . $path;
eval('$latte->addExtension(new App\Latte\\' . ucfirst($tag) . 'Extension);');
}
if (!file_exists(str_replace($config->cwd, ROOT_DIR, $params->template))) {
throw new Error('File not found ' . str_replace($config->cwd, ROOT_DIR, $params->template));
}
if (isset($config->isRenderTransformedHtml) && $config->isRenderTransformedHtml) {
if (isset($config->contentTimestamp)) {
$content = file_get_contents(__DIR__ . '/temp/' . $config->contentTimestamp . '.html');
unlink(__DIR__ . '/temp/' . $config->contentTimestamp . '.html');
} else {
$content = '';
}
$latte->setLoader(new Latte\Loaders\StringLoader([
str_replace($config->cwd, ROOT_DIR, $params->template) => $content
]));
}
echo $latte->renderToString(str_replace($config->cwd, ROOT_DIR, $params->template), $params);