Commit 52b68c06 authored by Taylor Otwell's avatar Taylor Otwell

refactoring. adding back pagination.

parent 34452f5f
...@@ -39,6 +39,18 @@ return array( ...@@ -39,6 +39,18 @@ return array(
'language' => 'en', 'language' => 'en',
/*
|--------------------------------------------------------------------------
| Application Timezone
|--------------------------------------------------------------------------
|
| The default timezone of your application. This timezone will be used when
| Laravel needs a date, such as when writing to a log file.
|
*/
'timezone' => 'UTC',
/* /*
|-------------------------------------------------------------------------- |--------------------------------------------------------------------------
| Application Character Encoding | Application Character Encoding
...@@ -53,46 +65,51 @@ return array( ...@@ -53,46 +65,51 @@ return array(
/* /*
|-------------------------------------------------------------------------- |--------------------------------------------------------------------------
| Application Timezone | Application Key
|-------------------------------------------------------------------------- |--------------------------------------------------------------------------
| |
| The default timezone of your application. This timezone will be used when | Your application key should be a 32 character string that is totally
| Laravel needs a date, such as when writing to a log file. | random and secret. This key is used by the encryption class to generate
| secure, encrypted strings.
| |
*/ */
'timezone' => 'UTC', 'key' => '',
/* /*
|-------------------------------------------------------------------------- |--------------------------------------------------------------------------
| Auto-Loaded Packages | SSL Link Generation
|-------------------------------------------------------------------------- |--------------------------------------------------------------------------
| |
| The packages that should be auto-loaded each time Laravel handles | Many sites use SSL to protect their users data. However, you may not
| a request. These should generally be packages that you use on almost | always be able to use SSL on your development machine, meaning all HTTPS
| every request to your application. | will be broken during development.
| |
| Each package specified here will be bootstrapped and can be conveniently | For this reason, you may wish to disable the generation of HTTPS links
| used by your application's routes, models, and libraries. | throughout your application. This option does just that. All attempts to
| | generate HTTPS links will generate regular HTTP links instead.
| Note: The package names in this array should correspond to a package
| directory in application/packages.
| |
*/ */
'packages' => array(), 'ssl' => true,
/* /*
|-------------------------------------------------------------------------- |--------------------------------------------------------------------------
| Application Key | Auto-Loaded Packages
|-------------------------------------------------------------------------- |--------------------------------------------------------------------------
| |
| Your application key should be a 32 character string that is totally | The packages that should be auto-loaded each time Laravel handles a
| random and secret. This key is used by the encryption class to generate | request. These should generally be packages that you use on almost every
| secure, encrypted strings. | request to your application.
|
| Each package specified here will be bootstrapped and can be conveniently
| used by your application's routes, models, and libraries.
|
| Note: The package names in this array should correspond to a package
| directory in application/packages.
| |
*/ */
'key' => '', 'packages' => array(),
); );
\ No newline at end of file
<?php namespace Laravel; use Laravel\Routing\Destination; <?php namespace Laravel;
abstract class Controller implements Destination { abstract class Controller {
/** /**
* The "before" filters defined for the controller. * The "before" filters defined for the controller.
...@@ -27,6 +27,54 @@ abstract class Controller implements Destination { ...@@ -27,6 +27,54 @@ abstract class Controller implements Destination {
return $this->$name; return $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);
}
// If the controller was not registered in the container, we will instantiate
// an instance of the controller manually. All controllers are suffixed with
// "_Controller" to avoid namespacing. Allowing controllers to exist in the
// global namespace gives the developer a convenient API for using the framework.
$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. * Magic Method to handle calls to undefined functions on the controller.
* *
......
<?php namespace Laravel\Database; <?php namespace Laravel\Database; use Laravel\Paginator;
class Query { class Query {
...@@ -430,16 +430,12 @@ class Query { ...@@ -430,16 +430,12 @@ class Query {
/** /**
* Set the query limit and offset for a given page and item per page count. * Set the query limit and offset for a given page and item per page count.
* *
* If the given page is not an integer or is less than one, one will be used.
*
* @param int $page * @param int $page
* @param int $per_page * @param int $per_page
* @return Query * @return Query
*/ */
public function for_page($page, $per_page) public function for_page($page, $per_page)
{ {
if ($page < 1 or filter_var($page, FILTER_VALIDATE_INT) === false) $page = 1;
return $this->skip(($page - 1) * $per_page)->take($per_page); return $this->skip(($page - 1) * $per_page)->take($per_page);
} }
...@@ -522,6 +518,22 @@ class Query { ...@@ -522,6 +518,22 @@ class Query {
return $result; return $result;
} }
/**
* Get the paginated query results as a Paginator instance.
*
* @param int $per_page
* @param array $columns
* @return Paginator
*/
public function paginate($per_page, $columns = array('*'))
{
// Calculate the current page for the request. The page number will be validated
// and adjusted by the Paginator class, so we can assume it is valid.
$page = Paginator::page($total = $this->count(), $per_page);
return Paginator::make($this->for_page($page, $per_page)->get($columns), $total, $per_page);
}
/** /**
* Insert an array of values into the database table. * Insert an array of values into the database table.
* *
......
...@@ -77,6 +77,8 @@ class Lang { ...@@ -77,6 +77,8 @@ class Lang {
{ {
if (count($paths) == 0) $paths = array(SYS_LANG_PATH, LANG_PATH); 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, $paths);
} }
...@@ -147,7 +149,7 @@ class Lang { ...@@ -147,7 +149,7 @@ class Lang {
*/ */
protected function load($file) protected function load($file)
{ {
if (isset(static::$lines[$this->language.$file])) return; if (isset(static::$lines[$this->language.$file])) return true;
$language = array(); $language = array();
......
<?php return array(
'previous' => '&larr; Previous',
'first' => 'First',
'next' => 'Next &rarr;',
'last' => 'Last',
'status' => 'Page :current of :last',
);
\ No newline at end of file
...@@ -2,15 +2,19 @@ ...@@ -2,15 +2,19 @@
return array( return array(
/** /*
* The validation error messages. |--------------------------------------------------------------------------
* | Validation Error Messages
* These error messages will be used by the Validator class if no |--------------------------------------------------------------------------
* other messages are provided by the developer. |
*/ | These error messages will be used by the Validator class if no other
| messages are provided by the developer. They may be overriden by the
| developer in the application language directory.
|
*/
"accepted" => "The :attribute must be accepted.", "accepted" => "The :attribute must be accepted.",
"active_url" => "The :attribute does not exist.", "active_url" => "The :attribute is not an active URL.",
"alpha" => "The :attribute may only contain letters.", "alpha" => "The :attribute may only contain letters.",
"alpha_dash" => "The :attribute may only contain letters, numbers, dashes, and underscores.", "alpha_dash" => "The :attribute may only contain letters, numbers, dashes, and underscores.",
"alpha_num" => "The :attribute may only contain letters and numbers.", "alpha_num" => "The :attribute may only contain letters and numbers.",
...@@ -30,11 +34,15 @@ return array( ...@@ -30,11 +34,15 @@ return array(
"unique" => "The :attribute has already been taken.", "unique" => "The :attribute has already been taken.",
"url" => "The :attribute format is invalid.", "url" => "The :attribute format is invalid.",
/** /*
* The following words are appended to the "size" messages when |--------------------------------------------------------------------------
* applicable, such as when validating string lengths or the | Validation Units
* size of file uploads. |--------------------------------------------------------------------------
*/ |
| The following words are appended to the "size" messages when applicable,
| such as when validating string lengths or the size of file uploads.
|
*/
"characters" => "characters", "characters" => "characters",
"kilobytes" => "kilobytes", "kilobytes" => "kilobytes",
......
...@@ -39,8 +39,8 @@ if (Config::get('session.driver') !== '') ...@@ -39,8 +39,8 @@ if (Config::get('session.driver') !== '')
* be returned to the browser. * be returned to the browser.
*/ */
require SYS_PATH.'request'.EXT; require SYS_PATH.'request'.EXT;
require SYS_PATH.'routing/router'.EXT;
require SYS_PATH.'routing/route'.EXT; require SYS_PATH.'routing/route'.EXT;
require SYS_PATH.'routing/router'.EXT;
require SYS_PATH.'routing/loader'.EXT; require SYS_PATH.'routing/loader'.EXT;
require SYS_PATH.'routing/caller'.EXT; require SYS_PATH.'routing/caller'.EXT;
......
This diff is collapsed.
<?php namespace Laravel; <?php namespace Laravel; use Closure;
class Request { class Request {
...@@ -132,7 +132,7 @@ class Request { ...@@ -132,7 +132,7 @@ class Request {
return $this->server['REMOTE_ADDR']; return $this->server['REMOTE_ADDR'];
} }
return ($default instanceof \Closure) ? call_user_func($default) : $default; return ($default instanceof Closure) ? call_user_func($default) : $default;
} }
/** /**
......
...@@ -141,17 +141,17 @@ class Response { ...@@ -141,17 +141,17 @@ class Response {
* *
* <code> * <code>
* // Create a response with the "layout" named view * // Create a response with the "layout" named view
* return Response::with('layout'); * return Response::of('layout');
* *
* // Create a response with the "layout" named view and data * // Create a response with the "layout" named view and data
* return Response::with('layout', array('name' => 'Taylor')); * return Response::of('layout', array('name' => 'Taylor'));
* </code> * </code>
* *
* @param string $name * @param string $name
* @param array $data * @param array $data
* @return Response * @return Response
*/ */
public static function with($name, $data = array()) public static function of($name, $data = array())
{ {
return new static(IoC::container()->core('view')->of($name, $data)); return new static(IoC::container()->core('view')->of($name, $data));
} }
...@@ -296,17 +296,17 @@ class Response { ...@@ -296,17 +296,17 @@ class Response {
* *
* <code> * <code>
* // Create a response instance with the "layout" named view * // Create a response instance with the "layout" named view
* return Response::with_layout(); * return Response::of_layout();
* *
* // Create a response instance with a named view and data * // Create a response instance with a named view and data
* return Response::with_layout(array('name' => 'Taylor')); * return Response::of_layout(array('name' => 'Taylor'));
* </code> * </code>
*/ */
public static function __callStatic($method, $parameters) public static function __callStatic($method, $parameters)
{ {
if (strpos($method, 'with_') === 0) if (strpos($method, 'of_') === 0)
{ {
return static::with(substr($method, 5), Arr::get($parameters, 0, array())); return static::with(substr($method, 3), Arr::get($parameters, 0, array()));
} }
} }
......
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
use Closure; use Closure;
use Laravel\Response; use Laravel\Response;
use Laravel\Container; use Laravel\Container;
use Laravel\Controller;
class Caller { class Caller {
...@@ -45,7 +46,7 @@ class Caller { ...@@ -45,7 +46,7 @@ class Caller {
/** /**
* Call a given route and return the route's response. * Call a given route and return the route's response.
* *
* @param Route $route * @param Route $route
* @return Response * @return Response
*/ */
public function call(Route $route) public function call(Route $route)
...@@ -53,58 +54,70 @@ class Caller { ...@@ -53,58 +54,70 @@ class Caller {
// Since "before" filters can halt the request cycle, we will return any response // 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 // from the before filters. Allowing the filters to halt the request cycle makes
// common tasks like authorization convenient to implement. // common tasks like authorization convenient to implement.
if ( ! is_null($response = $this->before($route))) $before = array_merge(array('before'), $route->filters('before'));
if ( ! is_null($response = $this->filter($before, array(), true)))
{ {
return $this->finish($route, $response); return $this->finish($route, $response);
} }
if ( ! is_null($response = $route->call())) // 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()))
{ {
// If a route returns a Delegate, it also means the route is delegating the
// handling of the request to a controller method. So, we will pass the string
// to the route delegator, exploding on "@".
if ($response instanceof Delegate) return $this->delegate($route, $response->destination);
return $this->finish($route, $response); return $this->finish($route, $response);
} }
else
// If we get to this point, no response was returned from the filters or the route. {
// The 404 response will be returned to the browser instead of a blank screen. return $this->finish($route, Response::error('404'));
return $this->finish($route, Response::error('404')); }
} }
/** /**
* Handle the delegation of a route to a controller method. * Handle the delegation of a route to a controller method.
* *
* @param Route $route * @param Route $route
* @param string $delegate * @param Delegate $delegate
* @return mixed * @return mixed
*/ */
protected function delegate(Route $route, $delegate) protected function delegate(Route $route, Delegate $delegate)
{ {
if (strpos($delegate, '@') === false) // 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] has an invalid format."); throw new \Exception("Route delegate [{$delegate->destination}] has an invalid format.");
} }
list($controller, $method) = explode('@', $delegate); list($controller, $method) = explode('@', $delegate->destination);
$controller = $this->resolve($controller); $controller = Controller::resolve($this->container, $controller, $this->path);
// If the controller doesn't exist or the request is to an invalid method, we will // 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 // return the 404 error response. The "before" method and any method beginning with
// an underscore are not publicly available. // an underscore are not publicly available.
if (is_null($controller) or ($method == 'before' or strncmp($method, '_', 1) === 0)) if (is_null($controller) or ! $this->callable($method))
{ {
return Response::error('404'); return Response::error('404');
} }
$controller->container = $this->container; $controller->container = $this->container;
// Again, as was the case with route closures, if the controller "before" filters return // Again, as was the case with route closures, if the controller "before" filters
// a response, it will be considered the response to the request and the controller method // return a response, it will be considered the response to the request and the
// will not be used to handle the request to the application. // controller method will not be used to handle the request to the application.
$response = $this->before($controller); $response = $this->filter($controller->filters('before'), array(), true);
if (is_null($response)) if (is_null($response))
{ {
...@@ -115,48 +128,14 @@ class Caller { ...@@ -115,48 +128,14 @@ class Caller {
} }
/** /**
* Resolve a controller name to a controller instance. * Determine if a given controller method is callable.
* *
* @param string $controller * @param string $method
* @return Controller
*/
protected function resolve($controller)
{
if ( ! $this->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 ($this->container->registered('controllers.'.$controller))
{
return $this->container->resolve('controllers.'.$controller);
}
// If the controller was not registered in the container, we will instantiate
// an instance of the controller manually. All controllers are suffixed with
// "_Controller" to avoid namespacing. Allowing controllers to exist in the
// global namespace gives the developer a convenient API for using the framework.
$controller = str_replace(' ', '_', ucwords(str_replace('.', ' ', $controller))).'_Controller';
return new $controller;
}
/**
* Load the file for a given controller.
*
* @param string $controller
* @return bool * @return bool
*/ */
protected function load($controller) protected function callable($method)
{ {
if (file_exists($path = $this->path.strtolower(str_replace('.', '/', $controller)).EXT)) return $method == 'before' or $method == 'after' or strncmp($method, '_', 1) === 0;
{
require $path;
return true;
}
return false;
} }
/** /**
...@@ -164,11 +143,11 @@ class Caller { ...@@ -164,11 +143,11 @@ class Caller {
* *
* The route response will be converted to a Response instance and the "after" filters will be run. * The route response will be converted to a Response instance and the "after" filters will be run.
* *
* @param Destination $route * @param Route|Controller $destination
* @param mixed $response * @param mixed $response
* @return Response * @return Response
*/ */
protected function finish(Destination $destination, $response) protected function finish($destination, $response)
{ {
if ( ! $response instanceof Response) $response = new Response($response); if ( ! $response instanceof Response) $response = new Response($response);
...@@ -177,22 +156,6 @@ class Caller { ...@@ -177,22 +156,6 @@ class Caller {
return $response; return $response;
} }
/**
* Run the "before" filters for the routing destination.
*
* If a before filter returns a value, that value will be considered the response to the
* request and the route function / controller will not be used to handle the request.
*
* @param Route $route
* @return mixed
*/
protected function before(Destination $destination)
{
$before = array_merge(array('before'), $destination->filters('before'));
return $this->filter($before, array(), true);
}
/** /**
* Call a filter or set of filters. * Call a filter or set of filters.
* *
......
<?php namespace Laravel\Routing;
interface Destination {
/**
* Get an array of filter names defined for the destination.
*
* @param string $name
* @return array
*/
public function filters($name);
}
\ No newline at end of file
<?php namespace Laravel\Routing; <?php namespace Laravel\Routing; use Closure, Laravel\Arr;
use Closure; class Route {
use Laravel\Arr;
class Route implements Destination {
/** /**
* The route key, including request method and URI. * The route key, including request method and URI.
...@@ -132,6 +129,16 @@ class Route implements Destination { ...@@ -132,6 +129,16 @@ class Route implements Destination {
return array(); return array();
} }
/**
* Deteremine if the route delegates to a controller.
*
* @return bool
*/
public function delegates()
{
return is_array($this->callback) and isset($this->callback['delegate']);
}
/** /**
* Determine if the route has a given name. * Determine if the route has a given name.
* *
......
<?php namespace Laravel\Routing; <?php namespace Laravel\Routing; use Laravel\Request;
use Laravel\Request;
interface Destination {
/**
* Get an array of filter names defined for the destination.
*
* @param string $name
* @return array
*/
public function filters($name);
}
class Delegate { class Delegate {
......
...@@ -50,7 +50,16 @@ class Auth { ...@@ -50,7 +50,16 @@ class Auth {
/** /**
* Get the current user of the application. * Get the current user of the application.
* *
* If the current user is not authenticated, NULL will be returned. * If the current user is not authenticated, null will be returned. This method
* will call the "user" closure in the authentication configuration file.
*
* <code>
* // Get the current user of the application
* $user = Auth::user();
*
* // Access a property on the current user of the application
* $email = Auth::user()->email;
* </code>
* *
* @return object * @return object
*/ */
...@@ -64,8 +73,8 @@ class Auth { ...@@ -64,8 +73,8 @@ class Auth {
/** /**
* Attempt to log a user into the application. * Attempt to log a user into the application.
* *
* If the given credentials are valid, the user will be considered logged into the * If the given credentials are valid, the user will be considered logged into
* application and their user ID will be stored in the session data. * the application and their user ID will be stored in the session data.
* *
* @param string $username * @param string $username
* @param string $password * @param string $password
...@@ -101,6 +110,8 @@ class Auth { ...@@ -101,6 +110,8 @@ class Auth {
/** /**
* Log the current user out of the application. * Log the current user out of the application.
* *
* The "logout" closure in the authenciation configuration file will be called.
*
* @return void * @return void
*/ */
public function logout() public function logout()
......
<?php namespace Laravel\Security\Hashing; <?php namespace Laravel\Security\Hashing;
# //
# Portable PHP password hashing framework. // Portable PHP password hashing framework.
# //
# Version 0.3 / genuine. // Version 0.3 / genuine.
# //
# Written by Solar Designer <solar at openwall.com> in 2004-2006 and placed in // Written by Solar Designer <solar at openwall.com> in 2004-2006 and placed in
# the public domain. Revised in subsequent years, still public domain. // the public domain. Revised in subsequent years, still public domain.
# //
# There's absolutely no warranty. // There's absolutely no warranty.
# //
# The homepage URL for this framework is: // The homepage URL for this framework is:
# //
# http://www.openwall.com/phpass/ // http://www.openwall.com/phpass/
# //
# Please be sure to update the Version line if you edit this file in any way. // Please be sure to update the Version line if you edit this file in any way.
# It is suggested that you leave the main version number intact, but indicate // It is suggested that you leave the main version number intact, but indicate
# your project name (after the slash) and add your own revision information. // your project name (after the slash) and add your own revision information.
# //
# Please do not change the "private" password hashing method implemented in // Please do not change the "private" password hashing method implemented in
# here, thereby making your hashes incompatible. However, if you must, please // here, thereby making your hashes incompatible. However, if you must, please
# change the hash type identifier (the "$P$") to something different. // change the hash type identifier (the "$P$") to something different.
# //
# Obviously, since this code is in the public domain, the above are not // Obviously, since this code is in the public domain, the above are not
# requirements (there can be none), but merely suggestions. // requirements (there can be none), but merely suggestions.
# //
class Bcrypt implements Engine { class Bcrypt implements Engine {
var $itoa64; var $itoa64;
var $iteration_count_log2; var $iteration_count_log2;
...@@ -123,12 +123,12 @@ class Bcrypt implements Engine { ...@@ -123,12 +123,12 @@ class Bcrypt implements Engine {
if (strlen($salt) != 8) if (strlen($salt) != 8)
return $output; return $output;
# We're kind of forced to use MD5 here since it's the only // We're kind of forced to use MD5 here since it's the only
# cryptographic primitive available in all versions of PHP // cryptographic primitive available in all versions of PHP
# currently in use. To implement our own low-level crypto // currently in use. To implement our own low-level crypto
# in PHP would result in much worse performance and // in PHP would result in much worse performance and
# consequently in lower iteration counts and hashes that are // consequently in lower iteration counts and hashes that are
# quicker to crack (by non-PHP code). // quicker to crack (by non-PHP code).
if (PHP_VERSION >= '5') { if (PHP_VERSION >= '5') {
$hash = md5($salt . $password, TRUE); $hash = md5($salt . $password, TRUE);
do { do {
...@@ -150,8 +150,8 @@ class Bcrypt implements Engine { ...@@ -150,8 +150,8 @@ class Bcrypt implements Engine {
protected function gensalt_extended($input) protected function gensalt_extended($input)
{ {
$count_log2 = min($this->iteration_count_log2 + 8, 24); $count_log2 = min($this->iteration_count_log2 + 8, 24);
# This should be odd to not reveal weak DES keys, and the // This should be odd to not reveal weak DES keys, and the
# maximum valid value is (2**24 - 1) which is odd anyway. // maximum valid value is (2**24 - 1) which is odd anyway.
$count = (1 << $count_log2) - 1; $count = (1 << $count_log2) - 1;
$output = '_'; $output = '_';
...@@ -167,14 +167,14 @@ class Bcrypt implements Engine { ...@@ -167,14 +167,14 @@ class Bcrypt implements Engine {
protected function gensalt_blowfish($input) protected function gensalt_blowfish($input)
{ {
# This one needs to use a different order of characters and a // This one needs to use a different order of characters and a
# different encoding scheme from the one in encode64() above. // different encoding scheme from the one in encode64() above.
# We care because the last character in our encoded string will // We care because the last character in our encoded string will
# only represent 2 bits. While two known implementations of // only represent 2 bits. While two known implementations of
# bcrypt will happily accept and correct a salt string which // bcrypt will happily accept and correct a salt string which
# has the 4 unused bits set to non-zero, we do not want to take // has the 4 unused bits set to non-zero, we do not want to take
# chances and we also do not want to waste an additional byte // chances and we also do not want to waste an additional byte
# of entropy. // of entropy.
$itoa64 = './ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; $itoa64 = './ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
$output = '$2a$'; $output = '$2a$';
...@@ -235,9 +235,9 @@ class Bcrypt implements Engine { ...@@ -235,9 +235,9 @@ class Bcrypt implements Engine {
if (strlen($hash) == 34) if (strlen($hash) == 34)
return $hash; return $hash;
# Returning '*' on error is safe here, but would _not_ be safe // Returning '*' on error is safe here, but would _not_ be safe
# in a crypt(3)-like function used _both_ for generating new // in a crypt(3)-like function used _both_ for generating new
# hashes and for validating passwords against existing hashes. // hashes and for validating passwords against existing hashes.
return '*'; return '*';
} }
......
...@@ -46,6 +46,14 @@ class Payload { ...@@ -46,6 +46,14 @@ class Payload {
* *
* A default value may also be specified, and will be returned in the item doesn't exist. * A default value may also be specified, and will be returned in the item doesn't exist.
* *
* <code>
* // Get an item from the session
* $name = Session::get('name');
*
* // Return a default value if the item doesn't exist
* $name = Session::get('name', 'Taylor');
* </code>
*
* @param string $key * @param string $key
* @param mixed $default * @param mixed $default
* @return mixed * @return mixed
...@@ -66,6 +74,11 @@ class Payload { ...@@ -66,6 +74,11 @@ class Payload {
/** /**
* Write an item to the session. * Write an item to the session.
* *
* <code>
* // Write an item to the session
* Session::put('name', 'Taylor');
* </code>
*
* @param string $key * @param string $key
* @param mixed $value * @param mixed $value
* @return Driver * @return Driver
...@@ -83,6 +96,11 @@ class Payload { ...@@ -83,6 +96,11 @@ class Payload {
* Flash data only exists for the next request. After that, it will be removed from * Flash data only exists for the next request. After that, it will be removed from
* the session. Flash data is useful for temporary status or welcome messages. * the session. Flash data is useful for temporary status or welcome messages.
* *
* <code>
* // Flash an item to the session
* Session::flash('name', 'Taylor');
* </code>
*
* @param string $key * @param string $key
* @param mixed $value * @param mixed $value
* @return Driver * @return Driver
...@@ -110,6 +128,11 @@ class Payload { ...@@ -110,6 +128,11 @@ class Payload {
* If a string is passed to the method, only that item will be kept. An array may also * If a string is passed to the method, only that item will be kept. An array may also
* be passed to the method, in which case all items in the array will be kept. * be passed to the method, in which case all items in the array will be kept.
* *
* <code>
* // Keep a session flash item from expiring
* Session::keep('name');
* </code>
*
* @param string|array $key * @param string|array $key
* @return void * @return void
*/ */
......
...@@ -9,6 +9,13 @@ class Cookie implements Transporter { ...@@ -9,6 +9,13 @@ class Cookie implements Transporter {
*/ */
protected $cookies; protected $cookies;
/**
* The name of the cookie used to store the session ID.
*
* @var string
*/
const key = 'laravel_session';
/** /**
* Create a new cookie session transporter instance. * Create a new cookie session transporter instance.
* *
...@@ -28,7 +35,7 @@ class Cookie implements Transporter { ...@@ -28,7 +35,7 @@ class Cookie implements Transporter {
*/ */
public function get($config) public function get($config)
{ {
return $this->cookies->get('laravel_session'); return $this->cookies->get(Cookie::key);
} }
/** /**
...@@ -40,9 +47,12 @@ class Cookie implements Transporter { ...@@ -40,9 +47,12 @@ class Cookie implements Transporter {
*/ */
public function put($id, $config) public function put($id, $config)
{ {
$minutes = ($config['expire_on_close']) ? 0 : $config['lifetime']; // 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;
$this->cookies->put('laravel_session', $id, $minutes, $config['path'], $config['domain']); $this->cookies->put(Cookie::key, $id, $minutes, $config['path'], $config['domain']);
} }
} }
\ No newline at end of file
<?php namespace Laravel\Session\Transporters; <?php namespace Laravel\Session\Transporters;
/**
* Session transporters are responsible for getting the session identifier
* to the client. This can be done via cookies or some other means.
*/
interface Transporter { interface Transporter {
/** /**
......
...@@ -20,9 +20,16 @@ class URL { ...@@ -20,9 +20,16 @@ class URL {
{ {
if (filter_var($url, FILTER_VALIDATE_URL) !== false) return $url; if (filter_var($url, FILTER_VALIDATE_URL) !== false) return $url;
// First, we need to build the base URL for the application, as well as handle
// the generation of links using SSL. It is possible for the developer to disable
// the generation of SSL links throughout the application, making it more
// convenient to create applications without SSL on the development box.
$base = Config::get('application.url').'/'.Config::get('application.index'); $base = Config::get('application.url').'/'.Config::get('application.index');
if ($https) $base = preg_replace('~http://~', 'https://', $base, 1); if ($https and Config::get('application.ssl'))
{
$base = preg_replace('~http://~', 'https://', $base, 1);
}
return rtrim($base, '/').'/'.ltrim($url, '/'); return rtrim($base, '/').'/'.ltrim($url, '/');
} }
......
...@@ -4,10 +4,17 @@ use Closure; ...@@ -4,10 +4,17 @@ use Closure;
use Laravel\IoC; use Laravel\IoC;
use Laravel\Str; use Laravel\Str;
use Laravel\Lang; use Laravel\Lang;
use Laravel\Database\Manager as Database; use Laravel\Database\Manager as DB;
class Validator { class Validator {
/**
* The registered custom validators.
*
* @var array
*/
protected static $validators = array();
/** /**
* The validation rules. * The validation rules.
* *
...@@ -80,16 +87,16 @@ class Validator { ...@@ -80,16 +87,16 @@ class Validator {
} }
$this->rules = $rules; $this->rules = $rules;
$this->messages = $messages;
$this->attributes = $attributes; $this->attributes = $attributes;
$this->messages = array_merge($this->messages, $messages);
} }
/** /**
* Create a new validator instance. * Create a new validator instance.
* *
* @param array $attributes * @param array $attributes
* @param array $rules * @param array $rules
* @param array $messages * @param array $messages
* @return Validator * @return Validator
*/ */
public static function make($attributes, $rules, $messages = array()) public static function make($attributes, $rules, $messages = array())
...@@ -97,6 +104,18 @@ class Validator { ...@@ -97,6 +104,18 @@ class Validator {
return new static($attributes, $rules, $messages); return new static($attributes, $rules, $messages);
} }
/**
* Register a custom validator.
*
* @param string $name
* @param Closure $validator
* @return void
*/
public static function register($name, $validator)
{
static::$validators[$name] = $validator;
}
/** /**
* Validate the target array using the specified validation rules. * Validate the target array using the specified validation rules.
* *
...@@ -138,7 +157,7 @@ class Validator { ...@@ -138,7 +157,7 @@ class Validator {
{ {
list($rule, $parameters) = $this->parse($rule); list($rule, $parameters) = $this->parse($rule);
if ( ! method_exists($this, $validator = 'validate_'.$rule)) if ( ! method_exists($this, $validator = 'validate_'.$rule) and ! isset(static::$validators[$rule]))
{ {
throw new \Exception("Validation rule [$rule] doesn't exist."); throw new \Exception("Validation rule [$rule] doesn't exist.");
} }
...@@ -327,7 +346,7 @@ class Validator { ...@@ -327,7 +346,7 @@ class Validator {
{ {
if ( ! isset($parameters[1])) $parameters[1] = $attribute; if ( ! isset($parameters[1])) $parameters[1] = $attribute;
if (is_null($this->connection)) $this->connection = Database::connection(); if (is_null($this->connection)) $this->connection = DB::connection();
return $this->connection->table($parameters[0])->where($parameters[1], '=', $this->attributes[$attribute])->count() == 0; return $this->connection->table($parameters[0])->where($parameters[1], '=', $this->attributes[$attribute])->count() == 0;
} }
...@@ -434,8 +453,8 @@ class Validator { ...@@ -434,8 +453,8 @@ class Validator {
* Developer specified attribute specific rules take first priority. * Developer specified attribute specific rules take first priority.
* Developer specified error rules take second priority. * Developer specified error rules take second priority.
* *
* If the message has not been specified by the developer, the default will be used * If the message has not been specified by the developer, the default
* from the validation language file. * will be used from the validation language file.
* *
* @param string $attribute * @param string $attribute
* @param string $rule * @param string $rule
...@@ -479,16 +498,23 @@ class Validator { ...@@ -479,16 +498,23 @@ class Validator {
*/ */
protected function format_message($message, $attribute, $rule, $parameters) protected function format_message($message, $attribute, $rule, $parameters)
{ {
$display = Lang::line('attributes.'.$attribute)->get($this->language, str_replace('_', ' ', $attribute)); // First we will get the language line for the attribute being validated.
// Storing attribute names in a validation file allows the easily replacement
// of attribute names (email) with more reader friendly versions (E-Mail).
$display = Lang::line('validation.attributes.'.$attribute)->get($this->language, str_replace('_', ' ', $attribute));
$message = str_replace(':attribute', $display, $message); $message = str_replace(':attribute', $display, $message);
// The "size" family of rules all have place-holders for the values applicable
// to their function. For example, the "max" rule has a ":max" place-holder.
if (in_array($rule, $this->size_rules)) if (in_array($rule, $this->size_rules))
{ {
$max = ($rule == 'between') ? $parameters[1] : $parameters[0]; $max = ($rule == 'between') ? $parameters[1] : $parameters[0];
$message = str_replace(array(':size', ':min', ':max'), array($parameters[0], $parameters[0], $max), $message); $message = str_replace(array(':size', ':min', ':max'), array($parameters[0], $parameters[0], $max), $message);
} }
// The "inclusion" rules, which are rules that check if a value is within
// a list of values, all have a place-holder to display the allowed values.
elseif (in_array($rule, array('in', 'not_in', 'mimes'))) elseif (in_array($rule, array('in', 'not_in', 'mimes')))
{ {
$message = str_replace(':values', implode(', ', $parameters), $message); $message = str_replace(':values', implode(', ', $parameters), $message);
...@@ -524,6 +550,9 @@ class Validator { ...@@ -524,6 +550,9 @@ class Validator {
*/ */
protected function parse($rule) protected function parse($rule)
{ {
// The format for specifying validation rules and parameters follows a {rule}:{parameters}
// convention. For instance, "max:3" specifies that the value may only be 3 characters in
// length. And "unique:users" specifies that a value must be unique on the "users" table.
$parameters = (($colon = strpos($rule, ':')) !== false) ? explode(',', substr($rule, $colon + 1)) : array(); $parameters = (($colon = strpos($rule, ':')) !== false) ? explode(',', substr($rule, $colon + 1)) : array();
return array(is_numeric($colon) ? substr($rule, 0, $colon) : $rule, $parameters); return array(is_numeric($colon) ? substr($rule, 0, $colon) : $rule, $parameters);
...@@ -553,4 +582,20 @@ class Validator { ...@@ -553,4 +582,20 @@ class Validator {
return $this; return $this;
} }
/**
* Dynamically handle calls to custom registered validators.
*/
public function __call($method, $parameters)
{
// First we will slice the "validate_" prefix off of the validator since custom
// validators are not registered with such a prefix. We will then call the validator
// and pass it the parameters we received.
if (isset(static::$validators[$method = substr($method, 9)]))
{
return call_user_func_array(static::$validators[$method], $parameters);
}
throw new \Exception("Call to undefined method [$method] on Validator instance.");
}
} }
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment