-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
132 lines (127 loc) · 4.92 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
<?php
namespace {
\lot('date', \lot('time', new \Time($_SERVER['REQUEST_TIME'] ?? \time())));
// Alias for `State`
\class_alias("\\State", "\\Site");
// Alias for `Time`
\class_alias("\\Time", "\\Date");
// Alias for `$state`
\lot('site', $site = $state);
// Default layout title
\lot('t', $t = new \Anemone([$state->title], ' · '));
}
namespace x\layout {
function content($content) {
if (false !== \strpos($content, '</html>')) {
return \preg_replace_callback('/<html(\s(?>"[^"]*"|\'[^\']*\'|[^>])*)?>/', static function ($m) {
if (
false !== \strpos($m[0], ' class=') ||
false !== \strpos($m[0], ' class ') ||
' class>' === \substr($m[0], -7)
) {
$r = new \HTML($m[0]);
$c = true === $r['class'] ? [] : \preg_split('/\s+/', $r['class'] ?? "");
$c = \array_unique(\array_merge($c, \array_keys(\array_filter((array) \State::get('[y]', true)))));
\sort($c); // Sort class name(s)
$r['class'] = \trim(\implode(' ', $c));
return $r;
}
return $m[0];
}, $content);
}
return $content;
}
function get() {
\extract(\lot());
$content = \Hook::fire('route', [null, $url->path, $url->query, $url->hash]);
if (\is_array($content) || \is_object($content)) {
if (!\error_get_last()) {
\type('application/json');
}
echo \To::JSON($content, true);
} else {
echo $content;
}
}
function route($content, $path) {
\ob_start();
\ob_start(!\error_get_last() ? "\\ob_gzhandler" : null);
// `$content = ['page', [], 200];`
if (\is_array($content) && isset($content[0]) && \is_string($content[0])) {
if ($r = \Layout::get(...$content)) {
$content = $r;
} else if (\defined("\\TEST") && \TEST && \function_exists("\\abort")) {
\status(403);
$content = \abort(\i('Requires at least one %s file.', ['<code>' . \strtr(\LOT, [\PATH . \D => '.' . \D]) . \D . 'y' . \D . '*' . \D . 'index.php</code>']));
}
}
echo \Hook::fire('content', [$content]);
\ob_end_flush();
// <https://www.php.net/manual/en/function.ob-get-length.php#59294>
\header('content-length: ' . \ob_get_length());
return \ob_get_clean();
}
\Hook::set('content', __NAMESPACE__ . "\\content", 20);
\Hook::set('get', __NAMESPACE__ . "\\get", 1000);
\Hook::set('route', __NAMESPACE__ . "\\route", 1000);
}
namespace x\layout\content {
function state() {
\State::set('[x]', []);
foreach (['are', 'as', 'can', 'has', 'is', 'not', 'of', 'with'] as $v) {
foreach ((array) \State::get($v, true) as $kk => $vv) {
\State::set('[y].' . $v . ':' . $kk, $vv);
}
}
if ($x = \State::get('is.error')) {
\State::set('[y].error:' . $x, true);
}
}
\Hook::set('content', __NAMESPACE__ . "\\state", 0);
}
namespace x\layout\get {
function asset() {
if (!\class_exists("\\Asset")) {
return;
}
foreach (\lot('Y')[1] ?? [] as $index) {
// Detect relative asset path to the `.\lot\y\*` folder
if ($assets = \Asset::get()) {
foreach ($assets as $k => $v) {
foreach ($v as $kk => $vv) {
// Full path, no change!
if (
0 === \strpos($kk, \PATH) ||
0 === \strpos($kk, '//') ||
false !== \strpos($kk, '://')
) {
continue;
}
if ($path = \Asset::path(\dirname($index) . \D . $kk)) {
\Asset::let($kk);
\Asset::set($path, $vv['stack'], $vv[2]);
}
}
}
}
}
}
\Hook::set('get', __NAMESPACE__ . "\\asset", 0);
}
namespace x\layout\route {
function page($content) {
if (\is_array($content) && \class_exists("\\Page")) {
$page = \lot('page') ?? new \Page;
if ($page && $page instanceof \Page && $page->exist() && ($layout = $page->layout)) {
// `$content = ['/lot/y/log/page/video.php', [], 200];`
if (0 === \strpos($layout, '/')) {
$layout = \stream_resolve_include_path(\PATH . \strtr($layout, '/', \D));
}
// `$content = ['page/video', [], 200];`
$content[0] = $layout;
}
}
return $content;
}
\Hook::set('route', __NAMESPACE__ . "\\page", 900);
}