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->markup
= function($page) use($pld) {
81 return $this->reference
->getFormatter()->markup($page, $pld);
84 $pld->refs
= $this->reference
;
85 $pld->baseUrl
= $this->baseUrl
;
91 * Redirect to canonical url
94 private function serveCanonical(string $cnn) : void
{
95 $this->response
->setHeader("Location", $this->baseUrl
->mod(["path" => $cnn]));
96 $this->response
->setResponseCode(301);
97 if (is_resource($this->output
)) {
98 $this->response
->send($this->output
);
100 $this->response
->send();
107 private function serveStylesheet() : void
{
108 $this->response
->setHeader("Content-Type", "text/css");
109 $this->response
->setBody(new Body(\fopen
(ROOT
."/public/index.css", "r")));
110 if (is_resource($this->output
)) {
111 $this->response
->send($this->output
);
113 $this->response
->send();
120 private function serveJavascript() : void
{
121 $this->response
->setHeader("Content-Type", "application/javascript");
122 $this->response
->setBody(new Body(\fopen
(ROOT
."/public/index.js", "r")));
123 if (is_resource($this->output
)) {
124 $this->response
->send($this->output
);
126 $this->response
->send();
135 private function serveStub() : void
{
136 $name = $this->request
->getQuery("ref", "s");
137 $repo = $this->reference
->getRepoForEntry($name);
138 if (!$repo->hasStub($stub)) {
139 throw new Exception(404, "Stub not found");
141 $this->response
->setHeader("Content-Type", "application/x-php");
142 $this->response
->setContentDisposition(["attachment" => ["filename" => "$name.stub.php"]]);
143 $this->response
->setBody(new Body(\fopen
($stub, "r")));
144 if (is_resource($this->output
)) {
145 $this->response
->send($this->output
);
147 $this->response
->send();
154 * @return true to continue serving the payload
157 private function servePreset(object $pld) : bool {
162 $pld->text
= file_get_contents(ROOT
."/$pld->ref");
165 $this->serveStylesheet();
168 $this->serveJavascript();
174 throw new Exception(404, "$pld->ref not found");
182 private function serve() : void
{
183 extract((array) func_get_arg(0));
184 include ROOT
."/views/layout.phtml";
185 $this->response
->addHeader("Link", "<" . $this->baseUrl
->path
. "index.css>; rel=preload; as=style");
186 $this->response
->addHeader("Link", "<" . $this->baseUrl
->path
. "index.js>; rel=preload; as=script");
187 if (isset($exception) && $exception->getCode()) {
188 $this->response
->setResponseCode($exception->getCode());
190 if (is_resource($this->output
)) {
191 $this->response
->send($this->output
);
193 $this->response
->send();
200 public function handle() : void
{
202 $pld = $this->createPayload();
204 if (isset($pld->ref
) && strlen($pld->ref
)) {
206 if (($repo = $this->reference
->getRepoForEntry($pld->ref
, $cnn))) {
207 if (isset($cnn) && strlen($cnn)) {
209 $this->serveCanonical($cnn);
213 $pld->entry
= $repo->getEntry($pld->ref
);
215 } elseif (!$this->servePreset($pld)) {
220 } catch (\Exception
$e) {
221 $pld->exception
= $e;