Created
March 23, 2015 12:30
-
-
Save httpspace/d363437d66ec70b91115 to your computer and use it in GitHub Desktop.
This file contains 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 Su; | |
class CooRoute | |
{ | |
private static $_instance = null, | |
$noFoundHandle = null, | |
$_complete = false; | |
public static $cookie = false, | |
$RouterKey = 'CooRouterPath', | |
$baseRouter = null, | |
$thisRouter = '', | |
$RouterHandles = [], | |
$laravelCookie = false; | |
public function __construct(){} | |
public static function make ( $key, $value, $minutes=3600 ) | |
{ | |
// self::$cookie = \Cookie::make( $key, $value, $minutes ); | |
setCookie( $key, $value, time() + $minutes ); | |
return self::returnSelf(); | |
} | |
public static function get ( $key ) | |
{ | |
return $_COOKIE[$key]; //\Cookie::get( $key ); | |
} | |
public static function forever ( $key, $value ) | |
{ | |
// self::$cookie = \Cookie::forever( $key, $value ); | |
setCookie( $key, $value, time() + (10 * 365 * 24 * 60 * 60) ); | |
return self::returnSelf(); | |
} | |
public static function forget ( $key ) | |
{ | |
// \Cookie::forget( $key ); | |
setcookie($key, "", time()-3600); | |
return self::returnSelf(); | |
} | |
private static function queue ( $key, $value, $minutes=3600 ) | |
{ | |
self::$cookie = \Cookie::queue( $key, $value, $minutes ); | |
return self::returnSelf(); | |
} | |
public static function has ( $key ) | |
{ | |
return isset( $_COOKIE[$key] ); | |
} | |
public static function setRouter ($router) | |
{ | |
self::forever( self::$RouterKey, $router ); | |
self::$thisRouter = $router; | |
return self::returnSelf(); | |
} | |
public static function getRouter () | |
{ | |
if( self::has( self::$RouterKey ) ) | |
{ | |
self::$thisRouter = self::get( self::$RouterKey ); | |
} | |
else | |
{ | |
/*thisRouter init*/ | |
self::forever( self::$RouterKey, self::$baseRouter ); | |
self::$thisRouter = self::$baseRouter; | |
} | |
return self::$thisRouter; | |
} | |
public static function nextHome() | |
{ | |
self::setNext(self::$baseRouter); | |
return self::returnSelf(); | |
} | |
public static function setNext ($routerTxt) | |
{ | |
return self::forever( self::$RouterKey, $routerTxt ); | |
} | |
public static function flash () | |
{ | |
return self::forever( self::$RouterKey, self::$baseRouter ); | |
} | |
public static function withClass ($class) | |
{ | |
$class->afterFilter(function ( $route, $request, $response ) { | |
self::withResponse( $response ); | |
}); | |
return self::returnSelf(); | |
} | |
public static function withResponse ( $response ) | |
{ | |
if(self::$cookie) $response->withCookie( self::$cookie ); | |
return self::returnSelf(); | |
} | |
public static function callHome () | |
{ | |
return self::callRouter(self::$baseRouter); | |
} | |
/* 跳到指定的router 如果沒有就使用baseRouter */ | |
public static function callRouter ( $routerTxt , $param=[]) | |
{ | |
try{ | |
if( !isset( self::$RouterHandles[$routerTxt] ) ) throw new \Exception( "CooRoute::callRouter => can't find route \" $routerTxt \"。" ); | |
$fn = self::$RouterHandles[$routerTxt]; | |
return call_user_func_array($fn, $param); | |
} | |
catch(Exception $e) | |
{ | |
die( $e->getMessage() ); | |
} | |
} | |
public static function setBase ($router) | |
{ | |
self::$baseRouter = $router; | |
return self::returnSelf(); | |
} | |
public static function home ($router, $callback) | |
{ | |
self::setBase($router); | |
return self::set($router, $callback); | |
} | |
/* 設定router名稱跟 callback unction */ | |
public static function set ( $router, $callback) | |
{ | |
try | |
{ | |
if(is_callable($callback) or is_string($callback)) | |
{ | |
self::$RouterHandles[$router] = $callback; | |
} | |
else throw new \Exception("CooRoute::set => the second param must be anonymous function or string。"); | |
} | |
catch(Exception $e) | |
{ | |
die($e->getMessage()); | |
} | |
return self::returnSelf(); | |
} | |
public static function start ($param=[]) | |
{ | |
if(self::$_complete === false) | |
{ | |
$handle = isset(self::$RouterHandles[self::$thisRouter]) ? self::$RouterHandles[self::$thisRouter] : self::$noFoundHandle; | |
return is_callable($handle) ? call_user_func_array($handle, $param) : false; | |
} | |
return false; | |
} | |
/* 設定noFound處理 */ | |
public static function noFound ($callback) | |
{ | |
try | |
{ | |
if(is_callable($callback) or is_string($callback)) | |
self::$noFoundHandle = $callback; | |
else | |
throw new \Exception("CooRoute::noFound => the noFound method only param must be anonymous function or string。"); | |
} | |
catch(Exception $e) | |
{ | |
die($e->getMessage()); | |
} | |
return self::returnSelf(); | |
} | |
/* 初始化 */ | |
private static function init () | |
{ | |
self::$_instance = new self(); | |
self::getRouter(); | |
return self::$_instance; | |
} | |
/**/ | |
private static function returnSelf () | |
{ | |
return isset( self::$_instance ) ? self::$_instance : ( self::$_instance = self::init() ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment