-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathSite.php
More file actions
115 lines (104 loc) · 3.35 KB
/
Site.php
File metadata and controls
115 lines (104 loc) · 3.35 KB
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
<?php
namespace Shaack\Reboot;
use Shaack\Logger;
use Shaack\Utils\HttpUtils;
use Symfony\Component\Yaml\Exception\ParseException;
use Symfony\Component\Yaml\Yaml;
class Site
{
protected Reboot $reboot;
protected string $fsPath; // The path to the `site` folder, relative to $baseFsPath
protected string $webPath; // The web path $domain . $baseUrl . $webPath
protected array $config = []; // Global values for all pages in a site folder (`/site/config.yml`)
protected array $addOns = []; // Site addons
/**
* @param $reboot Reboot
* @param string $sitePath
* @param string $siteWebPath
*/
public function __construct(Reboot $reboot, string $sitePath, string $siteWebPath)
{
$this->reboot = $reboot;
$this->fsPath = $this->reboot->getBaseFsPath() . $sitePath;
$this->webPath = $this->reboot->getBaseWebPath() . $siteWebPath;
$file = $this->fsPath . '/config.yml';
try {
$this->config = Yaml::parseFile($file);
} catch (ParseException $e) {
error_log("parse exception " . $file);
}
// addOns
if (@$this->config["addons"]) {
foreach ($this->config["addons"] as $addOnName) {
// Validate addon name to prevent path traversal
if (!preg_match('/^[a-zA-Z0-9_]+$/', $addOnName)) {
Logger::error("Invalid addon name rejected: " . $addOnName);
continue;
}
$addOnPath = $this->getFsPath() . "/addons/" . $addOnName . ".php";
if (!file_exists($addOnPath)) {
Logger::error("Addon file not found: " . $addOnPath);
continue;
}
require_once $addOnPath;
$className = "\Shaack\Reboot\\" . $addOnName;
$this->addOns[$addOnName] = new $className($this->reboot, $this);
}
}
Logger::debug("site->webPath: " . $this->webPath);
Logger::debug("site->fsPath: " . $this->fsPath);
}
/**
* @param $request Request
* @return string
*/
public function render(Request $request): string
{
/** @var AddOn $addOn */
foreach ($this->addOns as $addOn) {
if (!$addOn->preRender($request)) {
return "";
}
}
$page = new Page($this->reboot, $this);
$content = renderPage($this, $page, $request);
foreach ($this->addOns as $addOn) {
$content = $addOn->postRender($request, $content);
}
return $content;
}
/**
* @return string
*/
public function getFsPath(): string
{
return $this->fsPath;
}
/**
* @return string
*/
public function getWebPath(): string
{
return $this->webPath;
}
/**
* @return mixed
*/
public function getConfig()
{
return $this->config;
}
public function getAddOn($addOnName)
{
return $this->addOns[$addOnName];
}
}
/** @noinspection PhpUnusedParameterInspection */
function renderPage(Site $site, Page $page, Request $request): string
{
ob_start();
include $site->getFsPath() . '/template.php'; // The name is hardcoded for now. Maybe configurable in the future.
$contents = ob_get_contents();
ob_end_clean();
return $contents;
}