You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

78 lines
1.8 KiB

  1. <?php
  2. /**
  3. * @copyright NetMonsters <team@netmonsters.ru>
  4. * @link http://netmonsters.ru
  5. * @package Majestic
  6. * @subpackage app
  7. * @since 2010-02-25
  8. * @version SVN: $Id$
  9. * @filesource $URL$
  10. */
  11. class Route
  12. {
  13. protected $route;
  14. protected $action;
  15. protected $params;
  16. protected $layout;
  17. public function __construct($route, $action, $params = array(), $layout = null)
  18. {
  19. $this->route = $route;
  20. $this->action = $action;
  21. $this->params = $params;
  22. $this->layout = $layout;
  23. }
  24. /**
  25. * @param array $request
  26. * @return bool
  27. */
  28. public function match($request)
  29. {
  30. $parts = explode('/', $this->route);
  31. $cnt = count($parts);
  32. if(count($request) != $cnt) {
  33. return false;
  34. }
  35. for ($i = 0; $i < $cnt; $i++) {
  36. if (substr($parts[$i], 0, 1) == ':') {
  37. $this->params[substr($parts[$i], 1)] = urldecode($request[$i]);
  38. } elseif (substr($parts[$i], 0, 2) == '(?') {
  39. $match = array();
  40. if (!preg_match('#^' . $parts[$i] . '$#iu', $request[$i], $match)) {
  41. return false;
  42. }
  43. $start = strpos($parts[$i], '<') + 1;
  44. $key = substr($parts[$i], $start, strpos($parts[$i], '>', $start) - $start);
  45. $this->params[$key] = urldecode($match[$key]);
  46. } elseif ($parts[$i] != $request[$i]) {
  47. return false;
  48. }
  49. }
  50. return true;
  51. }
  52. public function getUri()
  53. {
  54. return '/' . $this->route;
  55. }
  56. public function getAction()
  57. {
  58. return $this->action . 'Action';
  59. }
  60. public function getLayout()
  61. {
  62. return $this->layout . 'Layout';
  63. }
  64. public function getParams()
  65. {
  66. return $this->params;
  67. }
  68. }