route.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php namespace Laravel\Routing;
  2. use Closure;
  3. class Route {
  4. /**
  5. * The route key, including request method and URI.
  6. *
  7. * @var string
  8. */
  9. public $key;
  10. /**
  11. * The URIs the route responds to.
  12. *
  13. * @var array
  14. */
  15. public $uris;
  16. /**
  17. * The route callback or array.
  18. *
  19. * @var mixed
  20. */
  21. public $callback;
  22. /**
  23. * The parameters that will passed to the route callback.
  24. *
  25. * @var array
  26. */
  27. public $parameters;
  28. /**
  29. * Create a new Route instance.
  30. *
  31. * @param string $key
  32. * @param mixed $callback
  33. * @param array $parameters
  34. * @return void
  35. */
  36. public function __construct($key, $callback, $parameters = array())
  37. {
  38. $this->key = $key;
  39. $this->callback = $callback;
  40. $this->parameters = $parameters;
  41. $this->uris = $this->parse_uris($key);
  42. }
  43. /**
  44. * Parse the route key and return an array of URIs the route responds to.
  45. *
  46. * @param string $key
  47. * @return array
  48. */
  49. protected function parse_uris($key)
  50. {
  51. if (strpos($key, ', ') === false) return array($this->extract_uri($key));
  52. // The extractor closure will retrieve the URI from a given route destination.
  53. // If the request is to the root of the application, a single forward slash
  54. // will be returned, otherwise the leading slash will be removed.
  55. $extractor = function($segment)
  56. {
  57. $segment = substr($segment, strpos($segment, ' ') + 1);
  58. return ($segment !== '/') ? trim($segment, '/') : $segment;
  59. };
  60. return array_map(function($segment) use ($extractor) { return $extractor($segment); }, explode(', ', $key));
  61. }
  62. /**
  63. * Call the route closure.
  64. *
  65. * If no closure is defined for the route, null will be returned.
  66. *
  67. * @return mixed
  68. */
  69. public function call()
  70. {
  71. return ( ! is_null($closure = $this->closure())) ? call_user_func_array($closure, $this->parameters) : null;
  72. }
  73. /**
  74. * Extract the route closure from the route.
  75. *
  76. * @return Closure|null
  77. */
  78. protected function closure()
  79. {
  80. if ($this->callback instanceof Closure) return $this->callback;
  81. foreach ($this->callback as $value) { if ($value instanceof Closure) return $value; }
  82. }
  83. /**
  84. * Get an array of filter names defined for the route.
  85. *
  86. * @param string $name
  87. * @return array
  88. */
  89. public function filters($name)
  90. {
  91. return (is_array($this->callback) and isset($this->callback[$name])) ? explode(', ', $this->callback[$name]) : array();
  92. }
  93. /**
  94. * Determine if the route has a given name.
  95. *
  96. * @param string $name
  97. * @return bool
  98. */
  99. public function is($name)
  100. {
  101. return (is_array($this->callback) and isset($this->callback['name'])) ? $this->callback['name'] === $name : false;
  102. }
  103. /**
  104. * Determine if the route handles a given URI.
  105. *
  106. * @param string $uri
  107. * @return bool
  108. */
  109. public function handles($uri)
  110. {
  111. return in_array($uri, $this->uris);
  112. }
  113. /**
  114. * Magic Method to handle dynamic method calls to determine the name of the route.
  115. */
  116. public function __call($method, $parameters)
  117. {
  118. if (strpos($method, 'is_') === 0) { return $this->is(substr($method, 3)); }
  119. }
  120. }