refactoring routing and class comments.

This commit is contained in:
Taylor Otwell 2011-10-13 21:32:11 -05:00
parent cff90b52ab
commit 9fa69e0844
27 changed files with 465 additions and 435 deletions

View File

@ -40,7 +40,7 @@
|
*/
'key' => 'some_secret_key',
'key' => '',
/*
|--------------------------------------------------------------------------
@ -137,7 +137,7 @@
'Benchmark' => 'Laravel\\Benchmark',
'Cache' => 'Laravel\\Cache',
'Config' => 'Laravel\\Config',
'Controller' => 'Laravel\\Controller',
'Controller' => 'Laravel\\Routing\\Controller',
'Cookie' => 'Laravel\\Cookie',
'Crypter' => 'Laravel\\Security\\Crypter',
'DB' => 'Laravel\\Database\\Manager',

View File

@ -21,7 +21,11 @@ public static function parse($path)
*/
public static function parse_string($value)
{
return static::closings(static::openings(static::echos($value)));
$value = static::echos($value);
$value = static::openings($value);
$value = static::closings($value);
return $value;
}
/**

View File

@ -39,9 +39,13 @@ public function has($key)
*/
protected function retrieve($key)
{
if ( ! \Laravel\File::exists($this->path.$key)) return null;
if ( ! file_exists($this->path.$key)) return null;
if (time() >= substr($cache = \Laravel\File::get($this->path.$key), 0, 10))
// File based caches store have the expiration timestamp stored in
// UNIX format prepended to their contents. This timestamp is then
// extracted and removed when the cache is read to determine if
// the file is still valid.
if (time() >= substr($cache = file_get_contents($this->path.$key), 0, 10))
{
return $this->forget($key);
}
@ -64,7 +68,7 @@ protected function retrieve($key)
*/
public function put($key, $value, $minutes)
{
\Laravel\File::put($this->path.$key, (time() + ($minutes * 60)).serialize($value));
file_put_contents($this->path.$key, (time() + ($minutes * 60)).serialize($value), LOCK_EX);
}
/**
@ -75,7 +79,10 @@ public function put($key, $value, $minutes)
*/
public function forget($key)
{
\Laravel\File::delete($this->path.$key);
if (file_exists($this->path.$key))
{
@unlink($this->path.$key);
}
}
}

View File

@ -89,12 +89,24 @@ public static function set($key, $value)
static::load($file);
(is_null($key)) ? Arr::set(static::$items, $file, $value) : Arr::set(static::$items[$file], $key, $value);
if (is_null($key))
{
Arr::set(static::$items, $file, $value);
}
else
{
Arr::set(static::$items[$file], $key, $value);
}
}
/**
* Parse a configuration key and return its file and key segments.
*
* The first segment of a configuration key represents the configuration
* file, while the remaining segments represent an item within that file.
* If no item segment is present, null will be returned for the item value
* indicating that the entire configuration array should be returned.
*
* @param string $key
* @return array
*/
@ -102,8 +114,6 @@ protected static function parse($key)
{
$segments = explode('.', $key);
// If there is only one segment after exploding on dots, we will return NULL
// as the key value, causing the entire configuration array to be returned.
$key = (count($segments) > 1) ? implode('.', array_slice($segments, 1)) : null;
return array($segments[0], $key);
@ -121,9 +131,9 @@ protected static function load($file)
$config = array();
// Configuration files cascade. Typically, the system configuration array is loaded
// first, followed by the application array, providing the convenient cascading
// of configuration options from system to application.
// Configuration files cascade. Typically, the system configuration array is
// loaded first, followed by the application array, providing the convenient
// cascading of configuration options from system to application.
foreach (static::$paths as $directory)
{
if (file_exists($path = $directory.$file.EXT))

View File

@ -1,104 +0,0 @@
<?php namespace Laravel;
abstract class Controller {
/**
* The "before" filters defined for the controller.
*
* @var array
*/
public $before = array();
/**
* The "after" filters defined for the controller.
*
* @var array
*/
public $after = array();
/**
* Get an array of filter names defined for the destination.
*
* @param string $name
* @return array
*/
public function filters($name)
{
return (array) $this->$name;
}
/**
* Resolve a controller name to a controller instance.
*
* @param Container $container
* @param string $controller
* @param string $path
* @return Controller
*/
public static function resolve(Container $container, $controller, $path)
{
if ( ! static::load($controller, $path)) return;
// If the controller is registered in the IoC container, we will resolve it out
// of the container. Using constructor injection on controllers via the container
// allows more flexible and testable development of applications.
if ($container->registered('controllers.'.$controller))
{
return $container->resolve('controllers.'.$controller);
}
$controller = str_replace(' ', '_', ucwords(str_replace('.', ' ', $controller))).'_Controller';
return new $controller;
}
/**
* Load the file for a given controller.
*
* @param string $controller
* @param string $path
* @return bool
*/
protected static function load($controller, $path)
{
if (file_exists($path = $path.strtolower(str_replace('.', '/', $controller)).EXT))
{
require $path;
return true;
}
return false;
}
/**
* Magic Method to handle calls to undefined functions on the controller.
*
* By default, the 404 response will be returned for an calls to undefined
* methods on the controller. However, this method may also be overridden
* and used as a pseudo-router by the controller.
*/
public function __call($method, $parameters)
{
return Response::error('404');
}
/**
* Dynamically resolve items from the application IoC container.
*
* <code>
* // Retrieve an object registered in the container as "mailer"
* $mailer = $this->mailer;
*
* // Equivalent call using the IoC container instance
* $mailer = IoC::container()->resolve('mailer');
* </code>
*/
public function __get($key)
{
if (IoC::container()->registered($key)) return IoC::container()->resolve($key);
throw new \Exception("Attempting to access undefined property [$key] on controller.");
}
}

View File

@ -157,7 +157,7 @@ public static function raw_token()
throw new \Exception("A session driver must be specified before using CSRF tokens.");
}
return Session\Manager::$payload->get('csrf_token');
return Session\Manager::get('csrf_token');
}
/**

View File

@ -12,7 +12,7 @@ class HTML {
*/
public static function entities($value)
{
return htmlentities($value, ENT_QUOTES, Config::get('application.encoding'), false);
return htmlentities($value, ENT_QUOTES, Config::$items['application']['encoding'], false);
}
/**
@ -60,7 +60,10 @@ public static function style($url, $attributes = array())
foreach ($defaults as $attribute => $default)
{
if ( ! array_key_exists($attribute, $attributes)) $attributes[$attribute] = $default;
if ( ! array_key_exists($attribute, $attributes))
{
$attributes[$attribute] = $default;
}
}
return '<link href="'.static::entities(URL::to_asset($url)).'"'.static::attributes($attributes).'>'.PHP_EOL;

View File

@ -107,7 +107,7 @@ public static function old($key = null, $default = null)
throw new \Exception('A session driver must be specified in order to access old input.');
}
return Arr::get(Session\Manager::$payload->get(Input::old_input, array()), $key, $default);
return Arr::get(Session\Manager::get(Input::old_input, array()), $key, $default);
}
/**

View File

@ -37,7 +37,7 @@ class Lang {
*
* @var array
*/
protected $paths;
protected $paths = array(SYS_LANG_PATH, LANG_PATH);
/**
* Create a new Lang instance.
@ -45,13 +45,11 @@ class Lang {
* @param string $key
* @param array $replacements
* @param string $language
* @param array $paths
* @return void
*/
protected function __construct($key, $replacements = array(), $language = null, $paths = array())
protected function __construct($key, $replacements = array(), $language = null)
{
$this->key = $key;
$this->paths = $paths;
$this->language = $language;
$this->replacements = $replacements;
}
@ -70,23 +68,20 @@ protected function __construct($key, $replacements = array(), $language = null,
* @param string $key
* @param array $replacements
* @param string $language
* @param array $paths
* @return Lang
*/
public static function line($key, $replacements = array(), $language = null, $paths = array())
public static function line($key, $replacements = array(), $language = null)
{
if (count($paths) == 0) $paths = array(SYS_LANG_PATH, LANG_PATH);
if (is_null($language)) $language = Config::get('application.language');
return new static($key, $replacements, $language, $paths);
return new static($key, $replacements, $language);
}
/**
* Get the language line as a string.
*
* If a language is specified, it should correspond to a directory within
* your application language directory.
* If a language is specified, it should correspond to a directory
* within your application language directory.
*
* <code>
* // Get a language line
@ -105,7 +100,6 @@ public static function line($key, $replacements = array(), $language = null, $pa
*/
public function get($language = null, $default = null)
{
// If a language was passed to the method, we will let it override the default
if ( ! is_null($language)) $this->language = $language;
list($file, $line) = $this->parse($this->key);
@ -115,8 +109,21 @@ public function get($language = null, $default = null)
return ($default instanceof Closure) ? call_user_func($default) : $default;
}
$line = Arr::get(static::$lines[$this->language.$file], $line, $default);
return $this->replace(Arr::get(static::$lines[$this->language.$file], $line, $default));
}
/**
* Make all necessary replacements on a language line.
*
* Replacements place-holder are prefixed with a colon, and are replaced
* with the appropriate value based on the replacement array set for the
* language line instance.
*
* @param string $line
* @return string
*/
protected function replace($line)
{
foreach ($this->replacements as $key => $value)
{
$line = str_replace(':'.$key, $value, $line);
@ -128,6 +135,10 @@ public function get($language = null, $default = null)
/**
* Parse a language key into its file and line segments.
*
* Language keys are formatted similarly to configuration keys. The first
* segment represents the language file, while the second segment
* represents a language line within that file.
*
* @param string $key
* @return array
*/
@ -153,9 +164,9 @@ protected function load($file)
$language = array();
// Language files cascade. Typically, the system language array is loaded first,
// followed by the application array. This allows the convenient overriding of the
// system language files (like validation) by the application developer.
// Language files cascade. Typically, the system language array is
// loaded first, followed by the application array. This allows the
// convenient overriding of the system language files.
foreach ($this->paths as $directory)
{
if (file_exists($path = $directory.$this->language.'/'.$file.EXT))
@ -164,9 +175,9 @@ protected function load($file)
}
}
// If language lines were actually found, they will be loaded into the array
// containing all of the lines for all languages and files. The array is
// keyed by the language and the file name.
// If language lines were actually found, they will be loaded into
// the array containing all of the lines for all languages and files.
// The array is keyed by the language and the file name.
if (count($language) > 0) static::$lines[$this->language.$file] = $language;
return isset(static::$lines[$this->language.$file]);
@ -175,6 +186,9 @@ protected function load($file)
/**
* Get the string content of the language line.
*/
public function __toString() { return $this->get(); }
public function __toString()
{
return $this->get();
}
}

View File

@ -41,7 +41,7 @@
require SYS_PATH.'routing/route'.EXT;
require SYS_PATH.'routing/router'.EXT;
require SYS_PATH.'routing/loader'.EXT;
require SYS_PATH.'routing/caller'.EXT;
require SYS_PATH.'routing/filter'.EXT;
/**
* Gather the input to the application for the current request.
@ -72,6 +72,10 @@
}
}
/**
* The spoofed request method is removed from the input so it is
* not unexpectedly included in Input::all() or Input::get().s
*/
unset($input[Request::spoofer]);
Input::set($input);
@ -82,19 +86,31 @@
* instance. If no route is found, the 404 response will be returned
* to the browser.
*/
Routing\Filter::register(require APP_PATH.'filters'.EXT);
list($method, $uri) = array(Request::method(), Request::uri());
$route = IoC::container()->core('routing.router')->route($method, $uri);
if ( ! is_null($route))
{
$response = IoC::container()->core('routing.caller')->call($route);
$response = $route->call();
}
else
{
$response = Response::error('404');
}
if ($response instanceof Routing\Delegate)
{
$response = Routing\Controller::call($response, $route->parameters);
}
if ( ! $response instanceof Response)
{
$response = new Response($response);
}
/**
* Stringify the response. We need to force the response to be
* stringed before closing the session, since the developer may

View File

@ -280,7 +280,6 @@ protected function appendage($element, $page)
public function appends($values)
{
$this->appends = $values;
return $this;
}
@ -295,7 +294,6 @@ public function appends($values)
public function elements($elements)
{
$this->elements = $elements;
return $this;
}

View File

@ -61,7 +61,7 @@ public function with($key, $value)
throw new \Exception('A session driver must be set before setting flash data.');
}
Session\Manager::$payload->flash($key, $value);
Session\Manager::flash($key, $value);
return $this;
}

View File

@ -197,6 +197,9 @@ public static function ajax()
*
* @return Route
*/
public static function route() { return static::$route; }
public static function route()
{
return static::$route;
}
}

View File

@ -235,7 +235,10 @@ public function render()
*/
public function send()
{
if ( ! isset($this->headers['Content-Type'])) $this->header('Content-Type', 'text/html; charset=utf-8');
if ( ! isset($this->headers['Content-Type']))
{
$this->header('Content-Type', 'text/html; charset=utf-8');
}
if ( ! headers_sent()) $this->send_headers();
@ -274,7 +277,6 @@ public function send_headers()
public function header($name, $value)
{
$this->headers[$name] = $value;
return $this;
}
@ -287,7 +289,6 @@ public function header($name, $value)
public function status($status)
{
$this->status = $status;
return $this;
}

View File

@ -1,194 +0,0 @@
<?php namespace Laravel\Routing;
use Closure;
use Laravel\Response;
use Laravel\Container;
use Laravel\Controller;
class Caller {
/**
* The IoC container instance.
*
* @var Container
*/
protected $container;
/**
* The route filters defined for the application.
*
* @var array
*/
protected $filters;
/**
* The path to the application's controllers.
*
* @var string
*/
protected $path;
/**
* Create a new route caller instance.
*
* @param Container $container
* @param Delegator $delegator
* @param array $filters
* @return void
*/
public function __construct(Container $container, $filters, $path)
{
$this->path = $path;
$this->filters = $filters;
$this->container = $container;
}
/**
* Call a given route and return the route's response.
*
* @param Route $route
* @return Response
*/
public function call(Route $route)
{
// Since "before" filters can halt the request cycle, we will return any response
// from the before filters. Allowing the filters to halt the request cycle makes
// common tasks like authorization convenient to implement.
$before = array_merge(array('before'), $route->filters('before'));
if ( ! is_null($response = $this->filter($before, array(), true)))
{
return $this->finish($route, $response);
}
// If a route returns a Delegate, it means the route is delegating the handling
// of the request to a controller method. We will pass the Delegate instance
// to the "delegate" method which will call the controller.
if ($route->delegates())
{
return $this->delegate($route, $route->call());
}
// If no before filters returned a response and the route is not delegating
// execution to a controller, we will call the route like normal and return
// the response. If the no response is given by the route, we will return
// the 404 error view.
elseif ( ! is_null($response = $route->call()))
{
return $this->finish($route, $response);
}
else
{
return $this->finish($route, Response::error('404'));
}
}
/**
* Handle the delegation of a route to a controller method.
*
* @param Route $route
* @param Delegate $delegate
* @return mixed
*/
protected function delegate(Route $route, Delegate $delegate)
{
// Route delegates follow a {controller}@{method} naming convention. For example,
// to delegate to the "home" controller's "index" method, the delegate should be
// formatted like "home@index". Nested controllers may be delegated to using dot
// syntax, like so: "user.profile@show".
if (strpos($delegate->destination, '@') === false)
{
throw new \Exception("Route delegate [{$delegate->destination}] has an invalid format.");
}
list($controller, $method) = explode('@', $delegate->destination);
$controller = Controller::resolve($this->container, $controller, $this->path);
// If the controller doesn't exist or the request is to an invalid method, we will
// return the 404 error response. The "before" method and any method beginning with
// an underscore are not publicly available.
if (is_null($controller) or ! $this->callable($method))
{
return Response::error('404');
}
$controller->container = $this->container;
// Again, as was the case with route closures, if the controller "before" filters
// return a response, it will be considered the response to the request and the
// controller method will not be used to handle the request to the application.
$response = $this->filter($controller->filters('before'), array(), true);
if (is_null($response))
{
$response = call_user_func_array(array($controller, $method), $route->parameters);
}
return $this->finish($controller, $response);
}
/**
* Determine if a given controller method is callable.
*
* @param string $method
* @return bool
*/
protected function callable($method)
{
return $method !== 'before' and $method !== 'after' and strncmp($method, '_', 1) !== 0;
}
/**
* Finish the route handling for the request.
*
* The route response will be converted to a Response instance and the "after" filters will be run.
*
* @param Route|Controller $destination
* @param mixed $response
* @return Response
*/
protected function finish($destination, $response)
{
if ( ! $response instanceof Response) $response = new Response($response);
$this->filter(array_merge($destination->filters('after'), array('after')), array($response));
return $response;
}
/**
* Call a filter or set of filters.
*
* @param array|string $filters
* @param array $parameters
* @param bool $override
* @return mixed
*/
protected function filter($filters, $parameters = array(), $override = false)
{
if (is_string($filters)) $filters = explode('|', $filters);
foreach ((array) $filters as $filter)
{
// Parameters may be passed into routes by specifying the list of parameters after
// a colon. If parameters are present, we will merge them into the parameter array
// that was passed to the method and slice the parameters off of the filter string.
if (($colon = strpos($filter, ':')) !== false)
{
$parameters = array_merge($parameters, explode(',', substr($filter, $colon + 1)));
$filter = substr($filter, 0, $colon);
}
if ( ! isset($this->filters[$filter])) continue;
$response = call_user_func_array($this->filters[$filter], $parameters);
// "Before" filters may override the request cycle. For example, an authentication
// filter may redirect a user to a login view if they are not logged in. Because of
// this, we will return the first filter response if overriding is enabled.
if ( ! is_null($response) and $override) return $response;
}
}
}

View File

@ -0,0 +1,170 @@
<?php namespace Laravel\Routing;
use Laravel\IoC;
use Laravel\Response;
abstract class Controller {
/**
* The "before" filters defined for the controller.
*
* @var array
*/
public $before = array();
/**
* The "after" filters defined for the controller.
*
* @var array
*/
public $after = array();
/**
* Handle the delegation of a route to a controller method.
*
* The controller destination should follow a {controller}@{method} convention.
* Nested controllers may be delegated to using dot syntax.
*
* For example, a destination of "user.profile@show" would call the User_Profile
* controller's show method with the given parameters.
*
* @param string $destination
* @param array $parameters
* @return mixed
*/
public static function call($destination, $parameters)
{
if (strpos($destination, '@') === false)
{
throw new \Exception("Route delegate [{$destination}] has an invalid format.");
}
list($controller, $method) = explode('@', $destination);
$controller = static::resolve($controller);
if (is_null($controller) or static::hidden($method))
{
return Response::error('404');
}
// Again, as was the case with route closures, if the controller
// "before" filters return a response, it will be considered the
// response to the request and the controller method will not be
// used to handle the request to the application.
$response = Filter::run($controller->filters('before'), array(), true);
if (is_null($response))
{
$response = call_user_func_array(array($controller, $method), $parameters);
}
$filters = array_merge($controller->filters('after'), array('after'));
Filter::run($filters, array($response));
return $response;
}
/**
* Determine if a given controller method is callable.
*
* @param string $method
* @return bool
*/
protected static function hidden($method)
{
return $method == 'before' or $method == 'after' or strncmp($method, '_', 1) == 0;
}
/**
* Resolve a controller name to a controller instance.
*
* @param Container $container
* @param string $controller
* @return Controller
*/
public static function resolve($controller)
{
if ( ! static::load($controller)) return;
// If the controller is registered in the IoC container, we will
// resolve it out of the container. Using constructor injection
// on controllers via the container allows more flexible and
// testable development of applications.
if (IoC::container()->registered('controllers.'.$controller))
{
return IoC::container()->resolve('controllers.'.$controller);
}
$controller = str_replace(' ', '_', ucwords(str_replace('.', ' ', $controller))).'_Controller';
return new $controller;
}
/**
* Load the file for a given controller.
*
* @param string $controller
* @return bool
*/
protected static function load($controller)
{
$controller = strtolower(str_replace('.', '/', $controller));
if (file_exists($path = CONTROLLER_PATH.$controller.EXT))
{
require $path;
return true;
}
return false;
}
/**
* Get an array of filter names defined for the destination.
*
* @param string $name
* @return array
*/
public function filters($name)
{
return (array) $this->$name;
}
/**
* Magic Method to handle calls to undefined functions on the controller.
*
* By default, the 404 response will be returned for an calls to undefined
* methods on the controller. However, this method may also be overridden
* and used as a pseudo-router by the controller.
*/
public function __call($method, $parameters)
{
return Response::error('404');
}
/**
* Dynamically resolve items from the application IoC container.
*
* <code>
* // Retrieve an object registered in the container as "mailer"
* $mailer = $this->mailer;
*
* // Equivalent call using the IoC container instance
* $mailer = IoC::container()->resolve('mailer');
* </code>
*/
public function __get($key)
{
if (IoC::container()->registered($key))
{
return IoC::container()->resolve($key);
}
throw new \Exception("Attempting to access undefined property [$key] on controller.");
}
}

View File

@ -0,0 +1,60 @@
<?php namespace Laravel\Routing;
class Filter {
/**
* The route filters for the application.
*
* @var array
*/
protected static $filters = array();
/**
* Register an array of route filters.
*
* @param array $filters
* @return void
*/
public static function register($filters)
{
static::$filters = array_merge(static::$filters, $filters);
}
/**
* Call a filter or set of filters.
*
* @param array|string $filters
* @param array $parameters
* @param bool $override
* @return mixed
*/
public static function run($filters, $parameters = array(), $override = false)
{
if (is_string($filters)) $filters = explode('|', $filters);
foreach ((array) $filters as $filter)
{
// Parameters may be passed into routes by specifying the list of
// parameters after a colon. If parameters are present, we will
// merge them into the parameter array that was passed to the
// method and slice the parameters off of the filter string.
if (($colon = strpos($filter, ':')) !== false)
{
$parameters = array_merge($parameters, explode(',', substr($filter, $colon + 1)));
$filter = substr($filter, 0, $colon);
}
if ( ! isset(static::$filters[$filter])) continue;
$response = call_user_func_array(static::$filters[$filter], $parameters);
// "Before" filters may override the request cycle. For example,
// an authentication filter may redirect a user to a login view
// if they are not logged in. Because of this, we will return
// the first filter response if overriding is enabled.
if ( ! is_null($response) and $override) return $response;
}
}
}

View File

@ -90,15 +90,16 @@ public function everything()
$routes = array_merge($routes, require $path);
}
// Since route files can be nested deep within the route directory, we need to
// recursively spin through each directory to find every file.
// Since route files can be nested deep within the route directory,
// we need to recursively spin through each directory
$iterator = new Iterator(new DirectoryIterator($this->nest), Iterator::SELF_FIRST);
foreach ($iterator as $file)
{
// Since some Laravel developers may place HTML files in the route directories, we will
// check for the PHP extension before merging the file. Typically, the HTML files are
// present in installations that are not using mod_rewrite and the public directory.
// Since some Laravel developers may place HTML files in the route
// directories, we will check for the PHP extension before merging
// the file. Typically, the HTML files are present in installations
// that are not using mod_rewrite and the public directory.
if (filetype($file) === 'file' and strpos($file, EXT) !== false)
{
$routes = array_merge(require $file, $routes);

View File

@ -1,4 +1,8 @@
<?php namespace Laravel\Routing; use Closure, Laravel\Arr;
<?php namespace Laravel\Routing;
use Closure;
use Laravel\Arr;
use Laravel\Response;
class Route {
@ -44,56 +48,96 @@ public function __construct($key, $callback, $parameters = array())
$this->callback = $callback;
$this->parameters = $parameters;
// The extractor closure will retrieve the URI from a given route destination.
// If the request is to the root of the application, a single forward slash
// will be returned, otherwise the leading slash will be removed.
$extractor = function($segment)
{
$segment = substr($segment, strpos($segment, ' ') + 1);
return ($segment !== '/') ? trim($segment, '/') : $segment;
};
// Extract each URI out of the route key. Since the route key has the request
// method, we will extract the method off of the string. If the URI points to
// the root of the application, a single forward slash will be returned.
// Otherwise, the leading slash will be removed.
// Extract each URI from the route key. Since the route key has the
// request method, we will extract that from the string. If the URI
// points to the root of the application, a single forward slash
// will be returned.
if (strpos($key, ', ') === false)
{
$this->uris = array($extractor($this->key));
$this->uris = array($this->extract($this->key));
}
else
{
$this->uris = array_map(function($segment) use ($extractor) { return $extractor($segment); }, explode(', ', $key));
$this->uris = array_map(array($this, 'extract'), explode(', ', $key));
}
// The route callback must be either a Closure, an array, or a string. Closures
// obviously handle the requests to the route. An array can contain filters, as
// well as a Closure to handle requests to the route. A string, delegates control
// of the request to a controller method.
if ( ! $this->callback instanceof Closure and ! is_array($this->callback) and ! is_string($this->callback))
if ( ! $callback instanceof Closure and ! is_array($callback) and ! is_string($callback))
{
throw new \Exception('Invalid route defined for URI ['.$this->key.']');
}
}
/**
* Call the closure defined for the route, or get the route delegator.
* Retrieve the URI from a given route destination.
*
* @return mixed
* If the request is to the root of the application, a single slash
* will be returned, otherwise the leading slash will be removed.
*
* @param string $segment
* @return string
*/
protected function extract($segment)
{
$segment = substr($segment, strpos($segment, ' ') + 1);
return ($segment !== '/') ? trim($segment, '/') : $segment;
}
/**
* Call a given route and return the route's response.
*
* @return Response
*/
public function call()
{
// If the value defined for a route is a Closure, we simply call the closure with the
// route's parameters and return the response.
// Since "before" filters can halt the request cycle, we will return
// any response from the before filters. Allowing filters to halt the
// request cycle makes tasks like authorization convenient.
$before = array_merge(array('before'), $this->filters('before'));
if ( ! is_null($response = $this->filter($before, array(), true)))
{
return $response;
}
if ( ! is_null($response = $this->response()))
{
if ($response instanceof Delegate)
{
return $response;
}
$filters = array_merge($this->filters('after'), array('after'));
Filter::run($filters, array($response));
return $response;
}
else
{
return Response::error('404');
}
}
/**
* Call the closure defined for the route, or get the route delegator.
*
* Note that this method differs from the "call" method in that it does
* not resolve the controller or prepare the response. Delegating to
* controller's is handled by the "call" method.
*
* @return mixed
*/
protected function response()
{
if ($this->callback instanceof Closure)
{
return call_user_func_array($this->callback, $this->parameters);
}
// Otherwise, we will assume the route is an array and will return the first value with
// a key of "delegate", or the first instance of a Closure. If the value is a string, the
// route is delegating the responsibility for handling the request to a controller.
// If the route is an array we will return the first value with a
// key of "delegate", or the first instance of a Closure. If the
// value is a string, the route is delegating the responsibility
// for handling the request to a controller.
elseif (is_array($this->callback))
{
$callback = Arr::first($this->callback, function($key, $value)
@ -101,12 +145,15 @@ public function call()
return $key == 'delegate' or $value instanceof Closure;
});
return ($callback instanceof Closure) ? call_user_func_array($callback, $this->parameters) : new Delegate($callback);
if ($callback instanceof Closure)
{
return call_user_func_array($callback, $this->parameters);
}
else
{
return new Delegate($callback);
}
}
// If a value defined for a route is a string, it means the route is delegating control
// of the request to a controller. If that is the case, we will simply return the string
// for the route caller to parse and delegate.
elseif (is_string($this->callback))
{
return new Delegate($this->callback);
@ -129,16 +176,6 @@ public function filters($name)
return array();
}
/**
* Deteremine if the route delegates to a controller.
*
* @return bool
*/
public function delegates()
{
return is_string($this->callback) or (is_array($this->callback) and isset($this->callback['delegate']));
}
/**
* Determine if the route has a given name.
*
@ -147,7 +184,7 @@ public function delegates()
*/
public function is($name)
{
return (is_array($this->callback) and isset($this->callback['name'])) ? $this->callback['name'] === $name : false;
return is_array($this->callback) and Arr::get($this->callback, 'name') === $name;
}
/**
@ -166,7 +203,7 @@ public function handles($uri)
*/
public function __call($method, $parameters)
{
if (strpos($method, 'is_') === 0) { return $this->is(substr($method, 3)); }
if (strpos($method, 'is_') === 0) return $this->is(substr($method, 3));
}
}

View File

@ -100,13 +100,14 @@ protected static function recall($cookie)
/**
* Attempt to log a user into the application.
*
* If the given credentials are valid, the user will be logged into the application
* and their user ID will be stored in the session via the "login" method.
* If the given credentials are valid, the user will be logged into
* the application and their user ID will be stored in the session
* via the "login" method.
*
* The user may also be "remembered". When this option is set, the user will be
* automatically logged into the application for one year via an encrypted cookie
* containing their ID. Of course, if the user logs out of the application,
* they will no longer be remembered.
* The user may also be "remembered". When this option is set, the user
* will be automatically logged into the application for one year via
* an encrypted cookie containing their ID. Of course, if the user logs
* out of the application, they will no longer be remembered.
*
* @param string $username
* @param string $password
@ -130,8 +131,6 @@ public static function attempt($username, $password = null, $remember = false)
/**
* Log a user into the application.
*
* The user ID will be stored in the session so it is available on subsequent requests.
*
* @param object $user
* @param bool $remember
* @return void
@ -156,9 +155,10 @@ protected static function remember($id, $username)
{
$cookie = Crypter::encrypt($id.'|'.$username.'|'.Str::random(40));
// This method assumes the "remember me" cookie should have the same configuration
// as the session cookie. Since this cookie, like the session cookie, should be
// kept very secure, it's probably safe to assume the settings are the same.
// This method assumes the "remember me" cookie should have the
// same configuration as the session cookie. Since this cookie,
// like the session cookie, should be kept very secure, it's
// probably safe to assume the settings are the same.
$config = Config::get('session');
Cookie::forever(Auth::remember_key, $cookie, $config['path'], $config['domain'], $config['secure']);
@ -167,9 +167,9 @@ protected static function remember($id, $username)
/**
* Log the current user out of the application.
*
* The "logout" closure in the authenciation configuration file will be called.
* All authentication cookies will be deleted and the user ID will be removed
* from the session.
* The "logout" closure in the authenciation configuration file
* will be called. All authentication cookies will be deleted
* and the user ID will be removed from the session.
*
* @return void
*/

View File

@ -24,8 +24,9 @@ class Crypter {
/**
* Encrypt a string using Mcrypt.
*
* The string will be encrypted using the cipher and mode specified when the crypter
* instance was created, and the final result will be base64 encoded.
* The string will be encrypted using the cipher and mode specified
* when the crypter instance was created, and the final result will
* be base64 encoded.
*
* <code>
* // Encrypt a string using the Mcrypt PHP extension
@ -70,8 +71,9 @@ public static function encrypt($value)
*/
public static function decrypt($value)
{
// Since all encrypted strings generated by this class are base64 encoded, we will
// first attempt to base64 decode the string. If we can't do it, we'll bail out.
// Since all encrypted strings generated by this class are base64
// encoded, we will first attempt to base64 decode the string.
// If we can't do it, we'll bail out.
if ( ! is_string($value = base64_decode($value, true)))
{
throw new \Exception('Decryption error. Input value is not valid base64 data.');

View File

@ -5,10 +5,10 @@ class Hasher {
/**
* Hash a password using the Bcrypt hashing scheme.
*
* Bcrypt provides a future-proof hashing algorithm by allowing the number of "rounds"
* to be increased, thus increasing the time is takes to generate the hashed value.
* The longer is takes to generate the hash, the more impractical a rainbow table
* attack against the hashes becomes.
* Bcrypt provides a future-proof hashing algorithm by allowing the number
* of "rounds" to be increased, thus increasing the time is takes to generate
* the hashed value. The longer is takes to generate the hash, the more
* impractical a rainbow table attack against the hashes becomes.
*
* <code>
* // Create a Bcrypt hash of a value
@ -30,9 +30,6 @@ public static function hash($value, $rounds = 8)
/**
* Determine if an unhashed value matches a given Bcrypt hash.
*
* Since the number of rounds is included in the Bcrypt hash, it is not
* necessary to specify the rounds when calling this method.
*
* @param string $value
* @param string $hash
* @return bool

View File

@ -101,7 +101,7 @@ public function sweep($expiration)
*/
private function table()
{
return $this->connection->table(Config::get('session.table'));
return $this->connection->table(Config::$items['session']['table']);
}
}

View File

@ -1,7 +1,5 @@
<?php namespace Laravel\Session\Drivers;
use Laravel\File as F;
class File implements Driver, Sweeper {
/**
@ -32,7 +30,10 @@ public function __construct($path)
*/
public function load($id)
{
if (F::exists($path = $this->path.$id)) return unserialize(F::get($path));
if (file_exists($path = $this->path.$id))
{
return unserialize(file_get_contents($path));
}
}
/**
@ -45,7 +46,7 @@ public function load($id)
*/
public function save($session, $config, $exists)
{
F::put($this->path.$session['id'], serialize($session), LOCK_EX);
file_put_contents($this->path.$session['id'], serialize($session), LOCK_EX);
}
/**
@ -56,7 +57,7 @@ public function save($session, $config, $exists)
*/
public function delete($id)
{
F::delete($this->path.$id);
if (file_exists($this->path.$id)) @unlink($this->path.$id);
}
/**
@ -69,9 +70,9 @@ public function sweep($expiration)
{
foreach (glob($this->path.'*') as $file)
{
if (F::type($file) == 'file' and F::modified($file) < $expiration)
if (filetype($file) == 'file' and filemtime($file) < $expiration)
{
F::delete($file);
@unlink($file);
}
}
}

View File

@ -168,7 +168,10 @@ public static function reflash()
*/
public static function keep($key)
{
if (is_array($key)) return array_map(array('Laravel\\Session\\Manager', 'keep'), $key);
if (is_array($key))
{
return array_map(array('Laravel\\Session\\Manager', 'keep'), $key);
}
static::flash($key, static::get($key));
@ -242,7 +245,9 @@ public static function age()
*/
protected static function replace($search, $replace, $keys)
{
static::$session['data'] = array_combine(str_replace($search, $replace, $keys), array_values(static::$session['data']));
$keys = str_replace($search, $replace, $keys);
static::$session['data'] = array_combine($keys, array_values(static::$session['data']));
}
/**

View File

@ -29,9 +29,9 @@ public function get($config)
*/
public function put($id, $config)
{
// Session cookies may be set to expire on close, which means we
// will need to pass "0" into the cookie manager. This will cause
// the cookie to not be deleted until the user closes their browser.
// Session cookies may be set to expire on close, which means we will
// need to pass "0" into the cookie manager. This will cause the
// cookie to not be deleted until the user closes their browser.
$minutes = ( ! $config['expire_on_close']) ? $config['lifetime'] : 0;
\Laravel\Cookie::put(Cookie::key, $id, $minutes, $config['path'], $config['domain'], $config['secure']);

View File

@ -252,7 +252,6 @@ public function nest($key, $view, $data = array())
public function with($key, $value)
{
$this->data[$key] = $value;
return $this;
}