7e1307bffff70e85412e45a221a1589777cffa35
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
40 * Initialize the reference
42 public function __construct(Reference
$ref, Request
$req, Response
$res, BaseUrl
$baseUrl) {
43 $this->reference
= $ref;
44 $this->request
= $req;
45 $this->response
= $res;
46 $this->baseUrl
= $baseUrl;
51 * Shorthand for \htmlspecialchars()
55 function esc(string $txt) : string {
56 return htmlspecialchars($txt);
60 * Create the view payload
63 private function createPayload() : object {
66 $pld->esc
= "htmlspecialchars";
67 $pld->anchor
= [$this->reference
, "formatAnchor"];
68 $pld->quick
= [$this->reference
, "formatString"];
69 $pld->file
= [$this->reference
, "formatFile"];
71 $pld->ref
= $this->baseUrl
->pathinfo(
72 $this->baseUrl
->mod($this->request
->getRequestUrl()));
74 $pld->refs
= $this->reference
;
75 $pld->baseUrl
= $this->baseUrl
;
81 * Redirect to canonical url
84 private function serveCanonical(string $cnn) : void
{
85 $this->response
->setHeader("Location", $this->baseUrl
->mod(["path" => $cnn]));
86 $this->response
->setResponseCode(301);
87 $this->response
->send();
93 private function serveStylesheet() : void
{
94 $this->response
->setHeader("Content-Type", "text/css");
95 $this->response
->setBody(new Body(\fopen
(ROOT
."/public/index.css", "r")));
96 $this->response
->send();
102 private function serveJavascript() : void
{
103 $this->response
->setHeader("Content-Type", "application/javascript");
104 $this->response
->setBody(new Body(\fopen
(ROOT
."/public/index.js", "r")));
105 $this->response
->send();
113 private function serveStub() : void
{
114 $name = $this->request
->getQuery("ref", "s");
115 $repo = $this->reference
->getRepoForEntry($name);
116 if (!$repo->hasStub($stub)) {
117 throw new Exception(404, "Stub not found");
119 $this->response
->setHeader("Content-Type", "application/x-php");
120 $this->response
->setContentDisposition(["attachment" => ["filename" => "$name.stub.php"]]);
121 $this->response
->setBody(new Body(\fopen
($stub, "r")));
122 $this->response
->send();
128 * @return true to continue serving the payload
131 private function servePreset(object $pld) : bool {
136 $pld->text
= file_get_contents(ROOT
."/$pld->ref");
139 $this->serveStylesheet();
142 $this->serveJavascript();
148 throw new Exception(404, "$pld->ref not found");
156 private function serve() : void
{
157 extract((array) func_get_arg(0));
158 include ROOT
."/views/layout.phtml";
159 $this->response
->addHeader("Link", "<" . $this->baseUrl
->path
. "index.css>; rel=preload; as=style");
160 $this->response
->addHeader("Link", "<" . $this->baseUrl
->path
. "index.js>; rel=preload; as=script");
161 $this->response
->send();
167 public function handle() : void
{
169 $pld = $this->createPayload();
171 if (strlen($pld->ref
)) {
173 if (($repo = $this->reference
->getRepoForEntry($pld->ref
, $cnn))) {
176 $this->serveCanonical($cnn);
180 $pld->entry
= $repo->getEntry($pld->ref
);
182 } elseif (!$this->servePreset($pld)) {
187 } catch (\Exception
$e) {
188 $pld->exception
= $e;