<?php /** * Register development routes which are available when * the development status is on */ namespace Application; use Application\Core\App; use Application\Core\Router; use Application\Core\HTMLHandler; use Application\Core\StatusCodeHandler; Router::restrict(function () { return App::environment() == 'dev' || App::environment() == 'development'; }, function () { Router::get('xtend/codes', function () { $table = HTMLHandler::createDocument()->createElement('table'); $codes = StatusCodeHandler::all(); foreach ($codes as $code) { $row = $table->createElement('tr'); $row->createElement('td')->addText($code->hex()); $row->createElement('td')->addText($code->name()); $row->createElement('td')->addText($code->readable()); } $table->write(true); }); });
/** * Checks whether the request uri is a match * * @param string $request * * @return boolean */ public function match($request) { if (is_array($this->_route) && isset($this->_route['environment']) && $this->_route['environment'] != App::environment()) { return false; } if (is_string($this->_handle)) { //split handle for multi handle $handles = explode('|', $this->_handle); foreach ($handles as $handle) { $handle_matched = true; //clear previous data Request::clear(); //ignore starting and trailing slashes $q_index = strpos($request, '?'); if ($q_index === false) { $ex_request = explode('/', trim($request, '/')); } else { $ex_request = explode('/', trim(substr($request, 0, $q_index), '/')); $ex_request[count($ex_request) - 1] .= substr($request, $q_index); } $ex_handle = explode('/', $handle); //if the amount of parts dont comply just, end if (count($ex_request) != count($ex_handle)) { continue; } //check all parts of the handle and see whether they match up to the request $ex_count = count($ex_handle); $rx_matches; $i = 0; while ($i < $ex_count) { //check $handle_part = $ex_handle[$i]; $request_part = $ex_request[$i]; //check {get} parameter first if ($i == $ex_count - 1) { //checking the last part of the handle //+{get} if (preg_match("/^(.*)(\\+{get})\$/i", $handle_part)) { //$handle_part ends with +{get} //thus get parameters are allowed //get rid of +{get} in the handle $handle_part = substr($handle_part, 0, strlen($handle_part) - 6); //get rid of anything after first question mark in the request part $qm_pos = strpos($request_part, '?'); if ($qm_pos !== false) { //remove GET part from URL $request_part = substr($request_part, 0, $qm_pos); } } } //check up //most complicated structure -> regexed URL variable if (preg_match("/^(rx)(\\{)([a-zA-Z0-9_]+)(\\})(\\{)(.*)(\\})\$/", $handle_part, $rx_matches) && preg_match("/^" . $rx_matches[6] . "\$/", $request_part)) { //regex for URL variable matches and handle is a regexed variable //setData on the UrlHandler to set URL parameter with name and value Request::set($rx_matches[3], $request_part); } elseif (preg_match("/^(\\{)([a-zA-Z0-9_]+)(\\})\$/", $handle_part, $rx_matches)) { //the handle is a non regex URL variable //just set whatever is in the URL to the variable Request::set($rx_matches[2], $request_part); } elseif (!(preg_match("/^(rx)(\\{)(.*)(\\})\$/", $handle_part, $rx_matches) && preg_match("/" . $rx_matches[3] . "/", $request_part) || preg_match("/^(\\*+)\$/", $handle_part) || $request_part == $handle_part)) { //if all of te above fails, return false $handle_matched = false; } ++$i; } if (!$handle_matched) { continue; } //set the route on the UrlHandler Request::route($this); return true; } } return false; }