refactoring for dependency injection and testability.
This commit is contained in:
@@ -1,8 +1,5 @@
|
||||
<?php namespace Laravel\Routing;
|
||||
|
||||
use Laravel\Package;
|
||||
use Laravel\Response;
|
||||
|
||||
class Route {
|
||||
|
||||
/**
|
||||
@@ -42,64 +39,39 @@ class Route {
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the route function.
|
||||
* Get all of the "before" filters defined for the route.
|
||||
*
|
||||
* @param mixed $route
|
||||
* @param array $parameters
|
||||
* @return Response
|
||||
* @return array
|
||||
*/
|
||||
public function call()
|
||||
public function before()
|
||||
{
|
||||
$response = null;
|
||||
|
||||
// The callback may be in array form, meaning it has attached filters or is named and we
|
||||
// will need to evaluate it further to determine what to do. If the callback is just a
|
||||
// closure, we can execute it now and return the result.
|
||||
if (is_callable($this->callback))
|
||||
{
|
||||
$response = call_user_func_array($this->callback, $this->parameters);
|
||||
}
|
||||
elseif (is_array($this->callback))
|
||||
{
|
||||
if (isset($this->callback['needs']))
|
||||
{
|
||||
Package::load(explode(', ', $this->callback['needs']));
|
||||
}
|
||||
|
||||
$response = isset($this->callback['before']) ? Filter::call($this->callback['before'], array(), true) : null;
|
||||
|
||||
if (is_null($response) and ! is_null($handler = $this->find_route_function()))
|
||||
{
|
||||
$response = call_user_func_array($handler, $this->parameters);
|
||||
}
|
||||
}
|
||||
|
||||
$response = Response::prepare($response);
|
||||
|
||||
if (is_array($this->callback) and isset($this->callback['after']))
|
||||
{
|
||||
Filter::call($this->callback['after'], array($response));
|
||||
}
|
||||
|
||||
return $response;
|
||||
return $this->filters('before');
|
||||
}
|
||||
|
||||
/**
|
||||
* Extract the route function from the route.
|
||||
* Get all of the "after" filters defined for the route.
|
||||
*
|
||||
* If a "do" index is specified on the callback, that is the handler.
|
||||
* Otherwise, we will return the first callable array value.
|
||||
*
|
||||
* @return Closure
|
||||
* @return array
|
||||
*/
|
||||
private function find_route_function()
|
||||
public function after()
|
||||
{
|
||||
if (isset($this->callback['do'])) return $this->callback['do'];
|
||||
return $this->filters('after');
|
||||
}
|
||||
|
||||
foreach ($this->callback as $value)
|
||||
{
|
||||
if (is_callable($value)) return $value;
|
||||
}
|
||||
/**
|
||||
* Get an array of filters defined for the route.
|
||||
*
|
||||
* <code>
|
||||
* // Get all of the "before" filters defined for the route.
|
||||
* $filters = $route->filters('before');
|
||||
* </code>
|
||||
*
|
||||
* @param string $name
|
||||
* @return array
|
||||
*/
|
||||
private function filters($name)
|
||||
{
|
||||
return (is_array($this->callback) and isset($this->callback[$name])) ? explode(', ', $this->callback[$name]) : array();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user