public function testScalar() { //Arrange $_GET["foo"] = 5; //Act $php = \BTRAutoMap::Query(); //Assert $this->assertTrue($php->get->foo == 5); }
/** * Will map $_POST, $_GET $_FILES $_SERVER $_SESSION (if started) $_COOKIE $_REQUEST and $_ENV to the $php object * @global \stdClass $php * @return \stdClass $php */ public static function Query() { global $php; $php = new \stdClass(); $globals = array("post" => $_POST, "get" => $_GET, "files" => $_FILES, "server" => $_SERVER, "session" => isset($_SESSION) ? $_SESSION : null, "cookie" => $_COOKIE, "request" => $_REQUEST, "env" => $_ENV); try { foreach ($globals as $key => $value) { $php->{$key} = \BTRAutoMap::ArrayToObject($value); } } catch (\Exception $ex) { if (error_reporting() == E_ALL) { error_log($ex->getTrace()); } } return $php; }
/** * Execute the route */ public function Execute() { $path = $this->GetQueryString(); if (!isset($this->routes["/"])) { return false; } if (defined("__BUTTERPHP_FILTER__") || defined("__BUTTERPHP_ALL__")) { $_GET = \BTRAutoFilter::Filter($_GET); $_POST = \BTRAutoFilter::Filter($_POST); } $p = !is_null($path) ? $path : '/'; $php = \BTRAutoMap::Query(); $php->router = $this; $php->HTTPCode = !isset($this->routes[$p]) || !isset($this->routes[$p . "/"]) ? 404 : 200; foreach ($this->routes as $key => $value) { $pattern = $key; if (!empty($this->parameterMap[$key])) { foreach ($this->parameterMap[$key] as $param) { $pattern .= "((?<" . $param . ">[^/]+)/)?"; } $result = "/" . addcslashes($pattern, "/") . "/"; $matches = array(); $matched = preg_match($result, $path, $matches); if ($matched === 1) { if (empty($php->get)) { $php->get = new \stdClass(); } foreach ($matches as $matchKey => $matchValue) { if (!is_numeric($matchKey)) { $php->get->{$matchKey} = $matchValue; } } return $this->routes[$key]($php); } } } if (isset($this->routes[$p])) { return $this->routes[$p]($php); } else { //return the base route if no correction is possible return $this->routes["/"]($php); } }