Advertisement
Guest User

Untitled

a guest
Jul 6th, 2015
432
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.74 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4. *
  5. * @author rajeev jha (jha dot rajeev at gmail)
  6. * Django style URL routing.
  7. * Pattern matching logic of this code is copied from cobweb framework
  8. * @see http://code.google.com/p/cobweb/source/browse/trunk/dispatch/url_resolver.class.php
  9. *
  10. */
  11.  
  12. class Gloo_Core_Router {
  13.  
  14. private $table ;
  15.  
  16.  
  17. function __construct() {
  18. //initialize routing table
  19. $this->table = new Gloo_Core_RoutingTable();
  20. }
  21.  
  22. function getRoute($path,$domain=NULL){
  23.  
  24. if(empty($path)) {
  25. $message = sprintf("Please supply a valid path to match :: got [%s] ", $path);
  26. trigger_error($message,E_USER_ERROR);
  27. }
  28.  
  29. //all rules for this domain
  30. $rules = $this->table->getRules($domain);
  31. $route = NULL ;
  32.  
  33. if($path == '/')
  34. $route = $this->matchHome($rules);
  35. else
  36. $route = $this->match($rules,$path);
  37.  
  38. return $route ;
  39.  
  40. }
  41.  
  42. private function matchHome($rules) {
  43. $route = NULL ;
  44.  
  45. foreach($rules as $rule) {
  46. if($rule["pattern"] == '/') {
  47. $route = $this->createRoute($rule,array());
  48. }
  49. }
  50.  
  51. return $route ;
  52.  
  53. }
  54.  
  55. private function match($rules,$path) {
  56. $path = ltrim($path, '/');
  57. $matches = array();
  58. $route = NULL ;
  59.  
  60. foreach($rules as $rule) {
  61. if(preg_match($this->patternize($rule["pattern"]),$path,$matches) != 0 ) {
  62. //match happened
  63. $matches = $this->sanitizeMatches($matches);
  64. $route = $this->createRoute($rule,$matches);
  65. }
  66. }
  67.  
  68. return $route ;
  69.  
  70. }
  71.  
  72. private function createRoute($rule,$matches) {
  73.  
  74. $route = $rule ;
  75. //add parameters
  76. $route["params"] = $matches ;
  77. return $route ;
  78. }
  79.  
  80. private function sanitizeMatches($matches){
  81. //discard the first one
  82. if (count($matches) >= 1)
  83. $matches = array_splice($matches,1);
  84.  
  85. $unset_next = false;
  86.  
  87. //group name match will create a string key as well as int key
  88. // like match["token"] = soemthing and match[1] = something
  89. // remove int key when string key is present for same value
  90.  
  91. foreach ($matches as $key => $value) {
  92. if (is_string($key)){
  93. $unset_next = true;
  94. } else if (is_int($key) && $unset_next) {
  95. unset($matches[$key]);
  96. $unset_next = false;
  97. }
  98. }
  99.  
  100. return array_merge($matches);
  101.  
  102. }
  103.  
  104. private function patternize($pattern) {
  105. //http://www.php.net/manual/en/reference.pcre.pattern.modifiers.php
  106. //treat pattern as UTF-8
  107. return '{'.$pattern.'}u' ;
  108. }
  109.  
  110. }
  111.  
  112. ?>
  113.  
  114.  
  115. <?php
  116.  
  117. /**
  118. *
  119. * @author rajeev jha (jha dot rajeev at gmail)
  120. * Django style URL routing.
  121. * URL routing table
  122. *
  123. */
  124.  
  125.  
  126. class Gloo_Core_RoutingTable {
  127.  
  128. private $rules ;
  129. private $splrules ;
  130. const ANY_DOMAIN = '__ANY__' ;
  131.  
  132.  
  133. function __construct() {
  134. $this->rules = array();
  135. $this->splrules = array();
  136.  
  137. //@todo inject from outside
  138. $this->createRule(self::ANY_DOMAIN, '/', 'Gloo_Controller_Home');
  139. //match alphanumeric + dashes
  140. //a pcre word (w) does not contain dashes
  141. $this->createRule(self::ANY_DOMAIN, '^(?P<token>[-w]+)$','Gloo_Controller_Post');
  142. $this->createRule(self::ANY_DOMAIN, '^page/(?P<pagenum>d+)$','Gloo_Controller_Home');
  143. $this->createRule(self::ANY_DOMAIN, '^(?P<token>w+)/page/(?P<pagenum>d+)$','Gloo_Controller_Post');
  144. $this->createRule(self::ANY_DOMAIN, '^category/(?P<name>w+)$','Gloo_Controller_Category');
  145. $this->createRule(self::ANY_DOMAIN, '^category/(?P<name>w+)/page/(?P<pagenum>d+)$','Gloo_Controller_Category');
  146.  
  147. //special rules
  148. $this->createRule('www.test1.com','^(?P<token>w+)$','Gloo_Controller_File', array("template" => "post.php"));
  149.  
  150. }
  151.  
  152. function createRule($domain,$pattern,$action,$options=NULL) {
  153.  
  154. if(empty($domain)) {
  155. trigger_error("No domain supplied for rule" ,E_USER_ERROR);
  156. }
  157.  
  158.  
  159. $rule = array();
  160.  
  161. $rule["pattern"] = $pattern;
  162. $rule["action"] = $action ;
  163.  
  164. //Add options
  165.  
  166. if(is_null($options))
  167. $rule["options"] = array();
  168. else
  169. $rule["options"] = $options ;
  170.  
  171. $rule["domain"] = $domain ;
  172.  
  173. //add to generic or domain specific rules
  174. if($domain == self::ANY_DOMAIN)
  175. $this->rules[] = $rule ;
  176. else
  177. $this->splrules[$domain][] = $rule ;
  178.  
  179. }
  180.  
  181. function getRules($domain) {
  182. if(empty($domain))
  183. return $this->rules ;
  184.  
  185. //valid domain - rules as well
  186. // add to existing rules
  187.  
  188. if(array_key_exists($domain,$this->splrules)) {
  189. $splrules = $this->splrules[$domain];
  190. $rules = array_merge($this->rules,$splrules);
  191. return $rules ;
  192.  
  193. } else {
  194. return $this->rules ;
  195. }
  196.  
  197. }
  198.  
  199. }
  200.  
  201.  
  202. ?>
  203.  
  204. $path = 'category/news/page/3' ;
  205. printf("Now matching path [%s]" , $path);
  206. $route = $router->getRoute($path);
  207. print_r($route);
  208.  
  209. $path = '/category/Health/page' ;
  210. printf("Now matching path [%s]" , $path);
  211. $route = $router->getRoute($path);
  212. print_r($route);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement