1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
use html_go\exceptions\InternalException; |
4
|
|
|
use html_go\model\Config; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* The main entry point. Called from <code>index.php</code> in the application |
8
|
|
|
* root. The parameters are provided for testing thus they have default values. |
9
|
|
|
* @param string $uri |
10
|
|
|
* @param string $method |
11
|
|
|
* @return string The html to be rendered. |
12
|
|
|
*/ |
13
|
|
|
function dispatch(string $uri = null, string $method = HTTP_GET): string { |
14
|
|
|
if ($uri === null) { |
15
|
|
|
$uri = $_SERVER['REQUEST_URI']; // @codeCoverageIgnore |
16
|
|
|
$method = \strtoupper($_SERVER['REQUEST_METHOD']); // @codeCoverageIgnore |
17
|
|
|
} |
18
|
|
|
$uri = strip_url_parameters($uri); |
19
|
|
|
$uri = \trim($uri, FWD_SLASH); |
20
|
|
|
if (empty($uri)) { |
21
|
|
|
$uri = HOME_INDEX_KEY; |
22
|
|
|
} |
23
|
|
|
return route($uri, $method); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Route the given HTTP request. |
28
|
|
|
* @param string $uri The requested URI |
29
|
|
|
* @param string $method the HTTP method |
30
|
|
|
* @throws InternalException |
31
|
|
|
* @return string |
32
|
|
|
*/ |
33
|
|
|
function route(string $uri, string $method): string { |
34
|
|
|
if ($method === HTTP_POST) { |
35
|
|
|
return not_found(); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
$result = \preg_match(POST_REQ_REGEX, $uri); |
39
|
|
|
if ($result === false) { |
40
|
|
|
throw new InternalException("preg_match() failed checking [$uri]"); // @codeCoverageIgnore |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
if ($result === 1) { |
44
|
|
|
$content = get_content_object($uri); |
45
|
|
|
} else { |
46
|
|
|
$content = process_request($uri, get_pagination_pagenumber(), get_config()->getInt(Config::KEY_POSTS_PERPAGE)); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
if ($content === null) { |
50
|
|
|
return not_found(); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
return render(vars: get_template_context($content)); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Process a request for a single blog-post. |
58
|
|
|
* @param string $uri |
59
|
|
|
* @param int $pageNum |
60
|
|
|
* @param int $perPage |
61
|
|
|
* @return \stdClass|NULL |
62
|
|
|
*/ |
63
|
|
|
function process_request(string $uri, int $pageNum, int $perPage): ?\stdClass { |
64
|
|
|
$template = LIST_TEMPLATE; |
65
|
|
|
|
66
|
|
|
switch (true) { |
67
|
|
|
case \str_starts_with($uri, POST_INDEX_KEY.FWD_SLASH): |
68
|
|
|
$list = get_posts($pageNum, $perPage); |
69
|
|
|
break; |
70
|
|
|
case \str_starts_with($uri, CAT_INDEX_KEY.FWD_SLASH): |
71
|
|
|
$list = get_categories($pageNum, $perPage); |
72
|
|
|
break; |
73
|
|
|
case \str_starts_with($uri, TAG_INDEX_KEY.FWD_SLASH): |
74
|
|
|
$list = get_tags($pageNum, $perPage); |
75
|
|
|
break; |
76
|
|
|
default: // home page or static page |
77
|
|
|
$list = []; |
78
|
|
|
$template = SINGLE_TEMPLATE; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
if ($uri === HOME_INDEX_KEY) { |
82
|
|
|
$content = get_homepage($pageNum, $perPage); |
83
|
|
|
} else { |
84
|
|
|
$content = get_content_object($uri, $list, $template); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return $content; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
function get_homepage(int $pageNum, int $perPage): \stdClass { |
91
|
|
|
if (get_config()->getBool(Config::KEY_STATIC_INDEX)) { |
92
|
|
|
if (($content = get_content_object(HOME_INDEX_KEY, template: SINGLE_TEMPLATE)) === null) { |
|
|
|
|
93
|
|
|
throw new InternalException("Unable to find the home index page!"); |
94
|
|
|
} |
95
|
|
|
} else { |
96
|
|
|
if (($content = get_content_object(HOME_INDEX_KEY, get_posts($pageNum, $perPage), LIST_TEMPLATE)) === null) { |
97
|
|
|
throw new InternalException("Unable to find the home index page!"); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
return $content; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Strips parameters from the given URL. |
105
|
|
|
* @param string $url |
106
|
|
|
* @return string returns the URL without the parameters. |
107
|
|
|
*/ |
108
|
|
|
function strip_url_parameters(string $url): string { |
109
|
|
|
if (($pos = \strpos($url, '?')) === false) { |
110
|
|
|
return $url; |
111
|
|
|
} |
112
|
|
|
parse_query(\substr($url, $pos + 1)); |
113
|
|
|
return \substr($url, 0, $pos); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @param string $query (Optional). Calling with this parameter set will populate the returned data array. |
118
|
|
|
* @return array<string, string> |
119
|
|
|
*/ |
120
|
|
|
function parse_query(string $query = null): array { |
121
|
|
|
static $data = []; |
122
|
|
|
if (empty($data) && $query !== null) { |
123
|
|
|
\parse_str($query, $data); |
124
|
|
|
} |
125
|
|
|
return $data; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Return the query string value for the given key. |
130
|
|
|
* @param string $key |
131
|
|
|
* @return string|NULL <code>null</code> if the key is not found |
132
|
|
|
*/ |
133
|
|
|
function get_query_parameter(string $key): ?string { |
134
|
|
|
$params = parse_query(); |
135
|
|
|
if (isset($params[$key])) { |
136
|
|
|
return $params[$key]; |
137
|
|
|
} |
138
|
|
|
return null; |
139
|
|
|
} |
140
|
|
|
|