123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221 |
- <?php namespace Laravel\Routing;
- use Closure;
- use Laravel\Bundle;
- use Laravel\Response;
- class Route {
-
- public $key;
-
- public $uris;
-
- public $bundle;
-
- public $action;
-
- public $parameters;
-
- public function __construct($key, $action, $parameters = array())
- {
- $this->key = $key;
- $this->action = $action;
-
-
-
- $uris = array_get($action, 'handles', array($key));
- $this->uris = array_map(array($this, 'extract'), $uris);
-
-
-
- $this->bundle = Bundle::handles($this->uris[0]);
- $this->parameters = array_map('urldecode', $parameters);
- }
-
- protected static function extract($segment)
- {
- $uri = substr($segment, strpos($segment, ' ') + 1);
- return ($uri !== '/') ? trim($uri, '/') : $uri;
- }
-
- public function call()
- {
-
-
-
-
- $response = Filter::run($this->filters('before'), array(), true);
- if (is_null($response))
- {
- $response = $this->response();
- }
- $response = Response::prepare($response);
- Filter::run($this->filters('after'), array($response));
- return $response;
- }
-
- public function response()
- {
-
-
-
-
- if ( ! is_null($delegate = $this->delegate()))
- {
- return Controller::call($delegate, $this->parameters);
- }
-
-
-
- elseif ( ! is_null($handler = $this->handler()))
- {
- return call_user_func_array($handler, $this->parameters);
- }
- }
-
- protected function filters($event)
- {
- $filters = array_unique(array($event, Bundle::prefix($this->bundle).$event));
-
-
-
- if (isset($this->action[$event]))
- {
- $filters = array_merge($filters, Filter::parse($this->action[$event]));
- }
- return array(new Filter_Collection($filters));
- }
-
- protected function delegate()
- {
- return array_get($this->action, 'uses');
- }
-
- protected function handler()
- {
- return array_first($this->action, function($key, $value)
- {
- return $value instanceof Closure;
- });
- }
-
- public function is($name)
- {
- return is_array($this->action) and array_get($this->action, 'name') === $name;
- }
-
- public function handles($uri)
- {
- $pattern = ($uri !== '/') ? str_replace('*', '(.*)', $uri).'\z' : '^/$';
- return ! is_null(array_first($this->uris, function($key, $uri) use ($pattern)
- {
- return preg_match('#'.$pattern.'#', $uri);
- }));
- }
- }
|