|
|
@@ -0,0 +1,102 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+require_once("Flammenkuchen.php");
|
|
|
+
|
|
|
+class Router
|
|
|
+{
|
|
|
+ protected $request;
|
|
|
+ protected $requestingDate;
|
|
|
+ protected $data;
|
|
|
+
|
|
|
+ protected $LATEST = -1;
|
|
|
+
|
|
|
+ function __construct()
|
|
|
+ {
|
|
|
+ $this->request = $_SERVER["REQUEST_URI"];
|
|
|
+ $this->data = null;
|
|
|
+
|
|
|
+ if ($this->request[0] == '/')
|
|
|
+ $this->request = substr($this->request, 1);
|
|
|
+
|
|
|
+ if ($this->request == "")
|
|
|
+ $this->requestingDate = $this->checkFileExists($this->LATEST);
|
|
|
+ else if (strlen($this->request) == 6)
|
|
|
+ {
|
|
|
+ $isNumeric = true;
|
|
|
+
|
|
|
+ for ($i =0; $i < 6; $i++)
|
|
|
+ {
|
|
|
+ if (!is_numeric($this->request[$i]))
|
|
|
+ {
|
|
|
+ $isNumeric = false;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if ($isNumeric)
|
|
|
+ $this->requestingDate = $this->checkFileExists($this->request);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private function checkFileExists($yymmdd)
|
|
|
+ {
|
|
|
+ if ($yymmdd != $this->LATEST)
|
|
|
+ {
|
|
|
+ // TODO use index cache
|
|
|
+ if (file_exists("../data/${yymmdd}.json"))
|
|
|
+ return $yymmdd;
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ $now = (int) (new \Datetime())->format("ymd");
|
|
|
+ $min = 0;
|
|
|
+
|
|
|
+ // obvious today-is-the-day
|
|
|
+ if ($this->checkFileExists($now))
|
|
|
+ return $now;
|
|
|
+
|
|
|
+ //TODO use index cache
|
|
|
+ $datadir = opendir("../data/");
|
|
|
+ while (($file = readdir($datadir)) !== false)
|
|
|
+ {
|
|
|
+ if (is_dir("../data/".$file)
|
|
|
+ || strlen($file) != 11
|
|
|
+ || substr($file, 6) !== ".json")
|
|
|
+ continue;
|
|
|
+
|
|
|
+ $current = (int) substr($file, 0, 6);
|
|
|
+
|
|
|
+ if ($current > $now)
|
|
|
+ continue;
|
|
|
+ if ($now - $current < $now - $min)
|
|
|
+ $min = $current;
|
|
|
+ }
|
|
|
+ closedir($datadir);
|
|
|
+ return $min === 0 ? null : $min;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public function isLost()
|
|
|
+ {
|
|
|
+ if ($this->requestingDate === null)
|
|
|
+ return true;
|
|
|
+ if ($this->data === null)
|
|
|
+ $this->readData();
|
|
|
+ return ($this->data === false);
|
|
|
+ }
|
|
|
+
|
|
|
+ public function getData()
|
|
|
+ {
|
|
|
+ if ($this->data === null)
|
|
|
+ $this->data = $this->readData();
|
|
|
+ return $this->data;
|
|
|
+ }
|
|
|
+
|
|
|
+ private function readData()
|
|
|
+ {
|
|
|
+ $req = $this->requestingDate;
|
|
|
+
|
|
|
+ return new Flammenkuchen(json_decode(file_get_contents("../data/${req}.json")));
|
|
|
+ }
|
|
|
+}
|
|
|
+
|