more refactoring and dependency injection.
This commit is contained in:
@@ -1,5 +1,8 @@
|
||||
<?php namespace Laravel\Routing;
|
||||
|
||||
use Closure;
|
||||
use Laravel\Container;
|
||||
|
||||
class Route {
|
||||
|
||||
/**
|
||||
@@ -30,13 +33,6 @@ class Route {
|
||||
*/
|
||||
public $parameters;
|
||||
|
||||
/**
|
||||
* The route filters for the application.
|
||||
*
|
||||
* @param array $filters
|
||||
*/
|
||||
public $filters = array();
|
||||
|
||||
/**
|
||||
* Create a new Route instance.
|
||||
*
|
||||
@@ -45,51 +41,120 @@ class Route {
|
||||
* @param array $parameters
|
||||
* @return void
|
||||
*/
|
||||
public function __construct($key, $callback, $parameters)
|
||||
public function __construct($key, $callback, $parameters = array())
|
||||
{
|
||||
$this->key = $key;
|
||||
$this->callback = $callback;
|
||||
$this->parameters = $parameters;
|
||||
|
||||
// Extract each URI handled by the URI. These will be used to find the route by
|
||||
// URI when requested. The leading slash will be removed for convenience.
|
||||
foreach (explode(', ', $key) as $segment)
|
||||
{
|
||||
$segment = substr($segment, strpos($segment, ' ') + 1);
|
||||
|
||||
$this->uris[] = ($segment !== '/') ? trim($segment, '/') : $segment;
|
||||
}
|
||||
$this->uris = $this->parse($key);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all of the "before" filters defined for the route.
|
||||
* Call the route closure.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function before()
|
||||
{
|
||||
return $this->filters('before');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all of the "after" filters defined for the route.
|
||||
* If no closure is defined for the route, null will be returned. The IoC container instance will be
|
||||
* passed to the route closure so it has access to all of the framework components.
|
||||
*
|
||||
* @return array
|
||||
* @param Container $container
|
||||
* @return mixed
|
||||
*/
|
||||
public function after()
|
||||
public function call(Container $container)
|
||||
{
|
||||
return $this->filters('after');
|
||||
if (is_null($closure = $this->find_closure())) return;
|
||||
|
||||
return call_user_func_array($closure, array_merge($this->parameters, array($container)));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get an array of filters defined for the route.
|
||||
* Extract the route closure from the route.
|
||||
*
|
||||
* @return Closure|null
|
||||
*/
|
||||
protected function find_closure()
|
||||
{
|
||||
if ($this->callback instanceof Closure) return $this->callback;
|
||||
|
||||
if (isset($this->callback['do'])) return $this->callback['do'];
|
||||
|
||||
foreach ($this->callback as $value) { if ($value instanceof Closure) return $value; }
|
||||
}
|
||||
|
||||
/**
|
||||
* Get an array of filter names defined for a route.
|
||||
*
|
||||
* @param string $name
|
||||
* @return array
|
||||
*/
|
||||
private function filters($name)
|
||||
public function filters($name)
|
||||
{
|
||||
return (is_array($this->callback) and isset($this->callback[$name])) ? explode(', ', $this->callback[$name]) : array();
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if the route handling has a given name.
|
||||
*
|
||||
* @param string $name
|
||||
* @return bool
|
||||
*/
|
||||
public function is($name)
|
||||
{
|
||||
if ( ! is_array($this->callback) or ! isset($this->callback['name'])) return false;
|
||||
|
||||
return $this->callback['name'] === $name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if the route handles a given URI.
|
||||
*
|
||||
* @param string $uri
|
||||
* @return bool
|
||||
*/
|
||||
public function handles($uri)
|
||||
{
|
||||
return in_array($uri, $this->uris);
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse the route key and return an array of URIs the route responds to.
|
||||
*
|
||||
* @param string $key
|
||||
* @return array
|
||||
*/
|
||||
protected function parse($key)
|
||||
{
|
||||
if (strpos($key, ', ') === false) return array($this->extract($key));
|
||||
|
||||
foreach (explode(', ', $key) as $segment)
|
||||
{
|
||||
$uris[] = $this->extract($segment);
|
||||
}
|
||||
|
||||
return $uris;
|
||||
}
|
||||
|
||||
/**
|
||||
* Extract the URI from a route destination.
|
||||
*
|
||||
* Route destinations include the request method the route responds to, so this method
|
||||
* will only remove it from the string. Unless the URI is root, the forward slash will
|
||||
* be removed to make searching the URIs more convenient.
|
||||
*
|
||||
* @param string $segment
|
||||
* @return string
|
||||
*/
|
||||
protected function extract($segment)
|
||||
{
|
||||
$segment = substr($segment, strpos($segment, ' ') + 1);
|
||||
|
||||
return ($segment !== '/') ? trim($segment, '/') : $segment;
|
||||
}
|
||||
|
||||
/**
|
||||
* Magic Method to handle dynamic method calls to determine the name of the route.
|
||||
*/
|
||||
public function __call($method, $parameters)
|
||||
{
|
||||
if (strpos($method, 'is_') === 0) { return $this->is(substr($method, 3)); }
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user