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.

72 lines
1.7 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. */
  27. public function match($request)
  28. {
  29. $parts = explode('/', $this->route);
  30. $cnt = count($parts);
  31. if(count($request) != $cnt) {
  32. return false;
  33. }
  34. for ($i = 0; $i < $cnt; $i++) {
  35. if (substr($parts[$i], 0, 1) == ':') {
  36. $this->params[substr($parts[$i], 1)] = $request[$i];
  37. } elseif (substr($parts[$i], 0, 2) == '(?') {
  38. $match = array();
  39. if (!preg_match('#^' . $parts[$i] . '$#iu', $request[$i], $match)) {
  40. return false;
  41. }
  42. $start = strpos($parts[$i], '<') + 1;
  43. $key = substr($parts[$i], $start, strpos($parts[$i], '>', $start) - $start);
  44. $this->params[$key] = $match[$key];
  45. } elseif ($parts[$i] != $request[$i]) {
  46. return false;
  47. }
  48. }
  49. return true;
  50. }
  51. public function getAction()
  52. {
  53. return $this->action . 'Action';
  54. }
  55. public function getLayout()
  56. {
  57. return $this->layout . 'Layout';
  58. }
  59. public function getParams()
  60. {
  61. return $this->params;
  62. }
  63. }