9 use function file_get_contents
;
10 use function htmlspecialchars
;
11 use function ob_start
;
20 * @var \mdref\Reference
25 * @var \http\Env\Request
30 * @var \http\Env\Response
45 * Initialize the reference
47 public function __construct(Reference
$ref, Request
$req, Response
$res, BaseUrl
$baseUrl, $output = null) {
48 $this->reference
= $ref;
49 $this->request
= $req;
50 $this->response
= $res;
51 $this->baseUrl
= $baseUrl;
52 $this->output
= $output;
57 * Shorthand for \htmlspecialchars()
61 function esc(string $txt) : string {
62 return htmlspecialchars($txt);
66 * Create the view payload
69 private function createPayload() : object {
72 $pld->esc
= "htmlspecialchars";
73 $pld->anchor
= [$this->reference
, "formatAnchor"];
74 $pld->quick
= [$this->reference
, "formatString"];
75 $pld->file
= [$this->reference
, "formatFile"];
77 $pld->ref
= $this->baseUrl
->pathinfo(
78 $this->baseUrl
->mod($this->request
->getRequestUrl()));
80 $pld->refs
= $this->reference
;
81 $pld->baseUrl
= $this->baseUrl
;
87 * Redirect to canonical url
90 private function serveCanonical(string $cnn) : void
{
91 $this->response
->setHeader("Location", $this->baseUrl
->mod(["path" => $cnn]));
92 $this->response
->setResponseCode(301);
93 if (is_resource($this->output
)) {
94 $this->response
->send($this->output
);
96 $this->response
->send();
103 private function serveStylesheet() : void
{
104 $this->response
->setHeader("Content-Type", "text/css");
105 $this->response
->setBody(new Body(\fopen
(ROOT
."/public/index.css", "r")));
106 if (is_resource($this->output
)) {
107 $this->response
->send($this->output
);
109 $this->response
->send();
116 private function serveJavascript() : void
{
117 $this->response
->setHeader("Content-Type", "application/javascript");
118 $this->response
->setBody(new Body(\fopen
(ROOT
."/public/index.js", "r")));
119 if (is_resource($this->output
)) {
120 $this->response
->send($this->output
);
122 $this->response
->send();
131 private function serveStub() : void
{
132 $name = $this->request
->getQuery("ref", "s");
133 $repo = $this->reference
->getRepoForEntry($name);
134 if (!$repo->hasStub($stub)) {
135 throw new Exception(404, "Stub not found");
137 $this->response
->setHeader("Content-Type", "application/x-php");
138 $this->response
->setContentDisposition(["attachment" => ["filename" => "$name.stub.php"]]);
139 $this->response
->setBody(new Body(\fopen
($stub, "r")));
140 if (is_resource($this->output
)) {
141 $this->response
->send($this->output
);
143 $this->response
->send();
150 * @return true to continue serving the payload
153 private function servePreset(object $pld) : bool {
158 $pld->text
= file_get_contents(ROOT
."/$pld->ref");
161 $this->serveStylesheet();
164 $this->serveJavascript();
170 throw new Exception(404, "$pld->ref not found");
178 private function serve() : void
{
179 extract((array) func_get_arg(0));
180 include ROOT
."/views/layout.phtml";
181 $this->response
->addHeader("Link", "<" . $this->baseUrl
->path
. "index.css>; rel=preload; as=style");
182 $this->response
->addHeader("Link", "<" . $this->baseUrl
->path
. "index.js>; rel=preload; as=script");
183 if (isset($exception) && $exception->getCode()) {
184 $this->response
->setResponseCode($exception->getCode());
186 if (is_resource($this->output
)) {
187 $this->response
->send($this->output
);
189 $this->response
->send();
196 public function handle() : void
{
198 $pld = $this->createPayload();
200 if (isset($pld->ref
) && strlen($pld->ref
)) {
202 if (($repo = $this->reference
->getRepoForEntry($pld->ref
, $cnn))) {
203 if (isset($cnn) && strlen($cnn)) {
205 $this->serveCanonical($cnn);
209 $pld->entry
= $repo->getEntry($pld->ref
);
211 } elseif (!$this->servePreset($pld)) {
216 } catch (\Exception
$e) {
217 $pld->exception
= $e;