Last active
October 5, 2016 07:19
-
-
Save potato4d/7f427169934126257cae45620838c551 to your computer and use it in GitHub Desktop.
雑Router(ちゃんとしたい)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| use \Utilitys\Response; | |
| $router->match(['greeting', ':id'], "GET", function($attrs){ | |
| Response::response(200, ["message"=>"Hello, ".$attrs["id"]); | |
| }); | |
| Response::NotFound("Endpoint"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| namespace Utilitys; | |
| class Response | |
| { | |
| private static $instance; | |
| /** | |
| * getInstance | |
| * | |
| **/ | |
| public static function getInstance() | |
| { | |
| if (!self::$instance) { | |
| self::$instance = new Response(); | |
| } | |
| return self::$instance; | |
| } | |
| /** | |
| * response | |
| * Basic response system. | |
| * | |
| * int $statusCode 200, 201, 400, 401, etc... | |
| * array $response array data can covert json. | |
| * | |
| **/ | |
| public static function response(int $statusCode, array $response){ | |
| header("Content-Type: application/json"); | |
| header("Access-Control-Allow-Origin: *"); | |
| header("Access-Control-Allow-Methods: GET, POST, PUT, DELETE"); | |
| header("Access-Control-Allow-Headers: *"); | |
| http_response_code($statusCode); | |
| echo json_encode($response); | |
| exit; | |
| } | |
| /** | |
| * NotFound | |
| * Return 404 JSON. | |
| * Format: `Error $objName Not Found.` | |
| * | |
| * string $objName | |
| **/ | |
| public static function NotFound(string $objName){ | |
| header("Content-Type: application/json"); | |
| header("Access-Control-Allow-Origin: *"); | |
| header("Access-Control-Allow-Methods: GET, POST, PUT, DELETE"); | |
| header("Access-Control-Allow-Headers: *"); | |
| http_response_code(404); | |
| echo json_encode(["message"=>"Error, {$objName} Not Found."]); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| namespace Utilitys; | |
| class Router{ | |
| private $routes = []; | |
| private $attrs = []; | |
| public function __construct($routes){ | |
| $this->routes = $routes; | |
| } | |
| public function match(array $pattern, string $method, $callback){ | |
| $isMatch = true; | |
| if($method != $_SERVER["REQUEST_METHOD"]) $isMatch = false; | |
| if(in_array(":any", $pattern)){ | |
| if(count($pattern) > count($this->routes)-1) $isMatch = false; | |
| }else{ | |
| if(count($pattern) != count($this->routes)-1) $isMatch = false; | |
| } | |
| for ($i=0; $i < count($pattern); $i++) { | |
| if(strpos($pattern[$i], ":") === 0) continue; | |
| if($pattern[$i] != $this->routes[$i+1]) $isMatch = false; | |
| } | |
| if(!$isMatch) return false; | |
| $this->makeAttrs($pattern); | |
| call_user_func($callback, $this->attrs); | |
| } | |
| private function makeAttrs(array $pattern){ | |
| for ($i=0; $i < count($pattern); $i++) { | |
| if(strpos($pattern[$i], ":") === false) continue; | |
| if($pattern[$i] == ":any"){ | |
| for ($index=0; $index < count($this->routes); $index++) { | |
| if($index <= $i) continue; | |
| $this->attrs["any"][] = $this->routes[$index]; | |
| } | |
| break; | |
| } | |
| $this->attrs[mb_substr($pattern[$i], 1)] = $this->routes[$i+1]; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment