PageRenderTime 42ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/sample/core/classes/Route.php

https://github.com/caferrari/vortice_book
PHP | 60 lines | 25 code | 3 blank | 32 comment | 2 complexity | 50183ddfb404c8d2e7e0dbc7057237a9 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /*
  3. * Copyright (c) 2008, Carlos André Ferrari <[carlos@]ferrari.eti.br>; Luan Almeida <[luan@]luan.eti.br>
  4. * All rights reserved.
  5. */
  6. /**
  7. * Route Class, decode routed requests
  8. *
  9. * @version 1
  10. * @package Framework
  11. * @author Carlos André Ferrari <carlos@ferrari.eti.br>
  12. */
  13. class Route{
  14. /**
  15. * Rotutes conteiner
  16. *
  17. * @staticvar array
  18. * @access private
  19. */
  20. private static $routes = array();
  21. /**
  22. * Add a route to the conteiner
  23. *
  24. * @param string $er Regular Expression for te route
  25. * @param string $ac Action to redirect to [module]+[controller:[action]]
  26. * @param string $pars Route parameters
  27. * @return string
  28. * @static
  29. */
  30. public static function add($er, $ac, $pars=''){
  31. $er = "/" . addcslashes($er, '/') . '/';
  32. self::$routes[] = array('er' => $er, 'ac' => $ac, 'pars' => $pars);
  33. }
  34. /**
  35. * Test requested URI and return the json encoded url to the Link class
  36. *
  37. * @return string
  38. * @static
  39. */
  40. public static function exec(){
  41. foreach (self::$routes as $r){
  42. if (preg_match($r['er'], uri, $match)){
  43. $p = $r['pars'];
  44. for ($x=1; $x<count($match); $x++) $p = str_replace('%' . $x, $match[$x], $p);
  45. $p = preg_replace('@%[0-9]+@', '', $p);
  46. parse_str($p, $pars);
  47. define ('routed', true);
  48. return array(
  49. 'url' => $r['ac'],
  50. 'pars' => $pars
  51. );
  52. }
  53. }
  54. define ('routed', false);
  55. return false;
  56. }
  57. }