| [ Index ] |
PHP Cross Reference of GlotPress |
[Summary view] [Print] [Text view]
1 <?php 2 3 class GP_Router { 4 5 var $api_prefix = 'api'; 6 7 function __construct( $urls = null ) { 8 if ( is_null( $urls ) ) 9 $this->urls = $this->default_routes(); 10 else 11 $this->urls = $urls; 12 } 13 14 /** 15 * Returns the current request URI path, relative to 16 * the application URI and without the query string 17 */ 18 function request_uri() { 19 $subdir = rtrim( gp_url_path(), '/' ); 20 if ( preg_match( "@^$subdir(.*?)(\?.*)?$@", $_SERVER['REQUEST_URI'], $match ) ) 21 return urldecode( $match[1] ); 22 return false; 23 } 24 25 function request_method() { 26 return gp_array_get( $_SERVER, 'REQUEST_METHOD', 'GET' ); 27 } 28 29 function add( $re, $function, $method = 'get' ) { 30 $this->urls["$method:$re"] = $function; 31 } 32 33 function default_routes() { 34 $dir = '([^_/][^/]*)'; 35 $path = '(.+?)'; 36 $projects = 'projects'; 37 $project = $projects.'/'.$path; 38 $id = '(\d+)'; 39 $locale = '('.implode('|', array_map( create_function( '$x', 'return $x->slug;' ), GP_Locales::locales() ) ).')'; 40 $set = "$project/$locale/$dir"; 41 // overall structure 42 return apply_filters( 'routes', array( 43 '/' => array('GP_Route_Index', 'index'), 44 'get:/login' => array('GP_Route_Login', 'login_get'), 45 'post:/login' => array('GP_Route_Login', 'login_post'), 46 'get:/logout' => array('GP_Route_Login', 'logout'), 47 48 "get:/$project/import-originals" => array('GP_Route_Project', 'import_originals_get'), 49 "post:/$project/import-originals" => array('GP_Route_Project', 'import_originals_post'), 50 51 "get:/$project/-edit" => array('GP_Route_Project', 'edit_get'), 52 "post:/$project/-edit" => array('GP_Route_Project', 'edit_post'), 53 54 "get:/$project/-delete" => array('GP_Route_Project', 'delete_get'), 55 "post:/$project/-delete" => array('GP_Route_Project', 'delete_post'), 56 57 "post:/$project/-personal" => array('GP_Route_Project', 'personal_options_post'), 58 59 "get:/$project/-permissions" => array('GP_Route_Project', 'permissions_get'), 60 "post:/$project/-permissions" => array('GP_Route_Project', 'permissions_post'), 61 "get:/$project/-permissions/-delete/$dir" => array('GP_Route_Project', 'permissions_delete'), 62 63 "get:/$project/-mass-create-sets" => array('GP_Route_Project', 'mass_create_sets_get'), 64 "post:/$project/-mass-create-sets" => array('GP_Route_Project', 'mass_create_sets_post'), 65 "post:/$project/-mass-create-sets/preview" => array('GP_Route_Project', 'mass_create_sets_preview_post'), 66 67 68 "get:/$projects" => array('GP_Route_Project', 'index'), 69 "get:/$projects/-new" => array('GP_Route_Project', 'new_get'), 70 "post:/$projects/-new" => array('GP_Route_Project', 'new_post'), 71 72 "post:/$set/-bulk" => array('GP_Route_Translation', 'bulk_post'), 73 "get:/$set/import-translations" => array('GP_Route_Translation', 'import_translations_get'), 74 "post:/$set/import-translations" => array('GP_Route_Translation', 'import_translations_post'), 75 "post:/$set/-discard-warning" => array('GP_Route_Translation', 'discard_warning'), 76 "post:/$set/-set-status" => array('GP_Route_Translation', 'set_status'), 77 "/$set/export-translations" => array('GP_Route_Translation', 'export_translations_get'), 78 // keep this below all URLs ending with a literal string, because it may catch one of them 79 "get:/$set" => array('GP_Route_Translation', 'translations_get'), 80 "post:/$set" => array('GP_Route_Translation', 'translations_post'), 81 // keep this one at the bottom of the project, because it will catch anything starting with project 82 "/$project" => array('GP_Route_Project', 'single'), 83 84 "get:/sets/-new" => array('GP_Route_Translation_Set', 'new_get'), 85 "post:/sets/-new" => array('GP_Route_Translation_Set', 'new_post'), 86 "get:/sets/$id" => array('GP_Route_Translation_Set', 'single'), 87 "get:/sets/$id/-edit" => array('GP_Route_Translation_Set', 'edit_get'), 88 "post:/sets/$id/-edit" => array('GP_Route_Translation_Set', 'edit_post'), 89 90 "post:/originals/$id/set_priority" => array('GP_Route_Original', 'set_priority'), 91 ) ); 92 } 93 94 95 function route() { 96 $real_request_uri = $this->request_uri(); 97 $api_request_uri = $real_request_uri; 98 $request_method = strtolower( $this->request_method() ); 99 $api = gp_startswith( $real_request_uri, '/'.$this->api_prefix.'/' ); 100 if ( $api ) { 101 $real_request_uri = substr( $real_request_uri, strlen( $this->api_prefix ) + 1 ); 102 } 103 foreach( array( $api_request_uri, $real_request_uri ) as $request_uri ) { 104 foreach( $this->urls as $re => $func ) { 105 foreach (array('get', 'post', 'head', 'put', 'delete') as $http_method) { 106 if ( gp_startswith( $re, $http_method.':' ) ) { 107 108 if ( $http_method != $request_method ) continue; 109 $re = substr( $re, strlen( $http_method . ':' )); 110 break; 111 } 112 } 113 if ( preg_match("@^$re$@", $request_uri, $matches ) ) { 114 if ( is_array( $func ) ) { 115 list( $class, $method ) = $func; 116 $route = new $class; 117 $route->api = $api; 118 $route->last_method_called = $method; 119 $route->class_name = $class; 120 GP::$current_route = &$route; 121 $route->before_request(); 122 $route->request_running = true; 123 // make sure after_request() is called even if we $this->exit_() in the request 124 register_shutdown_function( array( &$route, 'after_request') ); 125 call_user_func_array( array( $route, $method ), array_slice( $matches, 1 ) ); 126 $route->after_request(); 127 do_action( 'after_request', $class, $method ); 128 $route->request_running = false; 129 } else { 130 call_user_func_array( $func, array_slice( $matches, 1 ) ); 131 } 132 return; 133 } 134 } 135 } 136 return gp_tmpl_404(); 137 } 138 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Thu May 24 03:59:35 2012 | Hosted by follow the white rabbit. |