/
Router.php
74 lines (51 loc) · 1.99 KB
/
Router.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
<?php
use Symfony\Component\Yaml\Yaml;
use Tracy\Debugger;
use TYPO3Fluid\Fluid\View\TemplateView;
{
require_once 'Setup.inc';
$publicRoot = sprintf('%s%s%s', $workingDirectory, DIRECTORY_SEPARATOR, trim($_ENV['SAFES_PUBLIC_ROOT'], '/\\') ?? '_public');
$requestUri = trim($_SERVER['REQUEST_URI'], '/');
{
// Request to asset
$file = sprintf('%s/%s', $publicRoot, $requestUri);
if (is_file($file)) {
return false;
}
}
{
// Render page
$pageFile = sprintf('%s%s%s', $pagesRoot, DIRECTORY_SEPARATOR, str_replace(['/', '\\'], DIRECTORY_SEPARATOR, $requestUri));
if (is_file($pageFile)) {
/** @var TemplateView $view */
$templateName = pathinfo($pageFile, PATHINFO_FILENAME);
{
// Data source
$templateName = pathinfo($pageFile, PATHINFO_FILENAME);
$dataFile = sprintf('%s%s%s.html.yml', $pagesRoot, DIRECTORY_SEPARATOR, $templateName);
if (is_file($dataFile)) {
$data = Yaml::parseFile($dataFile);
Debugger::barDump($data);
if (!empty($data)) {
$view->assignMultiple($data);
}
}
}
$view->assign('debugger', $debugger);
echo $view->render($templateName);
return true;
}
}
{
// Overview of available pages
$files = glob(sprintf('%s/*.html', $pagesRoot));
$pages = [];
foreach ($files as $file) {
$filename = basename($file);
$name = pathinfo($filename, PATHINFO_FILENAME);
$pages[] = sprintf('<li><a href="/%s" target="%s">%s</a></li>', $filename, md5($file), ucfirst($name));
}
$template = file_get_contents(__DIR__ .DIRECTORY_SEPARATOR . 'Index.html');
echo sprintf($template, $_ENV['SAFES_PROJECT_NAME'], $debugger, implode("\n", $pages));
}
}