refactoring. adding back pagination.

This commit is contained in:
Taylor Otwell
2011-10-04 21:43:39 -05:00
parent 34452f5f08
commit 52b68c060b
21 changed files with 551 additions and 379 deletions

View File

@@ -39,18 +39,6 @@ return array(
'language' => 'en', 'language' => 'en',
/*
|--------------------------------------------------------------------------
| Application Character Encoding
|--------------------------------------------------------------------------
|
| The default character encoding used by your application. This is the
| character encoding that will be used by the Str, Text, and Form classes.
|
*/
'encoding' => 'UTF-8',
/* /*
|-------------------------------------------------------------------------- |--------------------------------------------------------------------------
| Application Timezone | Application Timezone
@@ -65,22 +53,15 @@ return array(
/* /*
|-------------------------------------------------------------------------- |--------------------------------------------------------------------------
| Auto-Loaded Packages | Application Character Encoding
|-------------------------------------------------------------------------- |--------------------------------------------------------------------------
| |
| The packages that should be auto-loaded each time Laravel handles | The default character encoding used by your application. This is the
| a request. These should generally be packages that you use on almost | character encoding that will be used by the Str, Text, and Form classes.
| every 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.
| |
*/ */
'packages' => array(), 'encoding' => 'UTF-8',
/* /*
|-------------------------------------------------------------------------- |--------------------------------------------------------------------------
@@ -95,4 +76,40 @@ return array(
'key' => '', 'key' => '',
/*
|--------------------------------------------------------------------------
| SSL Link Generation
|--------------------------------------------------------------------------
|
| Many sites use SSL to protect their users data. However, you may not
| always be able to use SSL on your development machine, meaning all HTTPS
| will be broken during development.
|
| For this reason, you may wish to disable the generation of HTTPS links
| throughout your application. This option does just that. All attempts to
| generate HTTPS links will generate regular HTTP links instead.
|
*/
'ssl' => true,
/*
|--------------------------------------------------------------------------
| Auto-Loaded Packages
|--------------------------------------------------------------------------
|
| The packages that should be auto-loaded each time Laravel handles a
| request. These should generally be packages that you use on almost every
| 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.
|
*/
'packages' => array(),
); );

View File

@@ -1,6 +1,6 @@
<?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 {
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.
* *

View File

@@ -1,4 +1,4 @@
<?php namespace Laravel\Database; <?php namespace Laravel\Database; use Laravel\Paginator;
class Query { 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 {
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.
* *

View File

@@ -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 {
*/ */
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();

View File

@@ -0,0 +1,9 @@
<?php return array(
'previous' => '&larr; Previous',
'first' => 'First',
'next' => 'Next &rarr;',
'last' => 'Last',
'status' => 'Page :current of :last',
);

View File

@@ -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(
"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",

View File

@@ -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;

View File

@@ -9,72 +9,85 @@ class Paginator {
*/ */
public $results; public $results;
/**
* The total number of results.
*
* @var int
*/
public $total;
/** /**
* The current page. * The current page.
* *
* @var int * @var int
*/ */
public $page; protected $page;
/**
* The number of items per page.
*
* @var int
*/
public $per_page;
/** /**
* The last page available for the result set. * The last page available for the result set.
* *
* @var int * @var int
*/ */
public $last_page; protected $last;
/** /**
* The language that should be used when generating page links. * The total number of results.
* *
* @var string * @var int
*/ */
public $language; protected $total;
/**
* The number of items per page.
*
* @var int
*/
protected $per_page;
/** /**
* The values that should be appended to the end of the link query strings. * The values that should be appended to the end of the link query strings.
* *
* @var array * @var array
*/ */
public $append = array(); protected $appends;
/** /**
* The format that will be used to wrap the entire pagination string. * The compiled appendage that will be appended to the links.
*
* This consists of a sprintf format with a page place-holder and query string.
* *
* @var string * @var string
*/ */
protected $wrapper = '<div class="pagination">%s</div>'; protected $appendage;
/**
* The current request instance.
*
* @var Request
*/
protected $request;
/**
* The paginatino elements that will be generated.
*
* @var array
*/
protected $elements = array('first', 'previous', 'status', 'next', 'last');
/** /**
* Create a new Paginator instance. * Create a new Paginator instance.
* *
* @param array $results * @param array $results
* @param int $last
* @param int $page * @param int $page
* @param int $total * @param int $total
* @param int $per_page * @param int $per_page
* @param int $last_page
* @return void * @return void
*/ */
protected function __construct($results, $page, $total, $per_page, $last_page) protected function __construct($results, $page, $total, $per_page, $last)
{ {
$this->page = $page; $this->page = $page;
$this->last = $last;
$this->total = $total; $this->total = $total;
$this->results = $results; $this->results = $results;
$this->per_page = $per_page; $this->per_page = $per_page;
$this->last_page = $last_page;
// Grab the active request instance. This is used to determine the current URI
// and to determine if HTTPS links should be generated.
$this->request = IoC::container()->core('request');
} }
/** /**
@@ -93,10 +106,6 @@ class Paginator {
/** /**
* Get the current page from the request query string. * Get the current page from the request query string.
* *
* The page will be validated and adjusted if it is less than one or greater than the last page.
* For example, if the current page is not an integer or less than one, one will be returned.
* If the current page is greater than the last page, the last page will be returned.
*
* @param int $total * @param int $total
* @param int $per_page * @param int $per_page
* @return int * @return int
@@ -105,9 +114,13 @@ class Paginator {
{ {
$page = IoC::container()->core('input')->get('page', 1); $page = IoC::container()->core('input')->get('page', 1);
if (is_numeric($page) and $page > $last_page = ceil($total / $per_page)) // The page will be validated and adjusted if it is less than one or greater
// than the last page. For example, if the current page is not an integer or
// less than one, one will be returned. If the current page is greater than
// the last page, the last page will be returned.
if (is_numeric($page) and $page > $last = ceil($total / $per_page))
{ {
return ($last_page > 0) ? $last_page : 1; return ($last > 0) ? $last : 1;
} }
return ($page < 1 or filter_var($page, FILTER_VALIDATE_INT) === false) ? 1 : $page; return ($page < 1 or filter_var($page, FILTER_VALIDATE_INT) === false) ? 1 : $page;
@@ -116,163 +129,183 @@ class Paginator {
/** /**
* Create the HTML pagination links. * Create the HTML pagination links.
* *
* @param int $adjacent
* @return string * @return string
*/ */
public function links($adjacent = 4) public function links()
{ {
if ($this->last_page <= 1) return ''; if ($this->last <= 1) return '';
// If there are two few pages to make showing a slider possible, we will just display a // Each pagination element is created by an element method. This allows
// range of plain number links. The hard-coded "7" is to account for all of the constant // us to keep this class clean and simple, because pagination code can
// elements in a sliding range, namely the current page, the two ellipses, the two // become a mess. We would rather keep it simple and beautiful.
// beginning and ending pages. foreach ($this->elements as $element)
$numbers = ($this->last_page < 7 + ($adjacent * 2)) ? $this->range(1, $this->last_page) : $this->slider($adjacent); {
$elements[] = $this->$element(Lang::line("pagination.{$element}")->get());
}
return sprintf($this->wrapper, $this->previous().$numbers.$this->next()); return '<div class="pagination">'.implode(' ', $elements).'</div>'.PHP_EOL;
} }
/** /**
* Build sliding list of HTML numeric page links. * Get the "status" pagination element.
* *
* @param int $adjacent * @param string $text
* @return string * @return string
*/ */
private function slider($adjacent) protected function status($text)
{ {
if ($this->page <= $adjacent * 2) return str_replace(array(':current', ':last'), array($this->page, $this->last), $text);
}
/**
* Create the "first" pagination element.
*
* @param string $text
* @return string
*/
protected function first($text)
{
return $this->backwards(__FUNCTION__, $text, 1);
}
/**
* Create the "previous" pagination element.
*
* @param string $text
* @return string
*/
protected function previous($text)
{
return $this->backwards(__FUNCTION__, $text, $this->page - 1);
}
/**
* Create the "next" pagination element.
*
* @param string $text
* @return string
*/
protected function next($text)
{
return $this->forwards(__FUNCTION__, $text, $this->page + 1);
}
/**
* Create the "last" pagination element.
*
* @param string $text
* @return string
*/
protected function last($text)
{
return $this->forwards(__FUNCTION__, $text, $this->last);
}
/**
* Create a "backwards" paginatino element.
*
* This function handles the creation of the first and previous elements.
*
* @param string $element
* @param string $text
* @param int $last
* @return string
*/
protected function backwards($element, $text, $last)
{
return $this->element($element, $text, $last, function($page) { return $page <= 1; });
}
/**
* Create a "forwards" paginatino element.
*
* This function handles the creation of the next and last elements.
*
* @param string $element
* @param string $text
* @param int $last
* @return string
*/
protected function forwards($element, $text, $last)
{
return $this->element($element, $text, $last, function($page, $last) { return $page >= $last; });
}
/**
* Create a chronological pagination element.
*
* @param string $element
* @param string $text
* @param int $page
* @param Closure $disabler
* @return string
*/
protected function element($element, $text, $page, $disabler)
{
$class = "{$element}_page";
if ($disabler($this->page, $this->last))
{ {
return $this->range(1, 2 + ($adjacent * 2)).$this->ending(); return HTML::span($text, array('class' => "disabled {$class}"));
}
elseif ($this->page >= $this->last_page - ($adjacent * 2))
{
return $this->beginning().$this->range($this->last_page - 2 - ($adjacent * 2), $this->last_page);
} }
else else
{ {
return $this->beginning().$this->range($this->page - $adjacent, $this->page + $adjacent).$this->ending(); // We will assume the page links should use HTTPS if the current request
// is also using HTTPS. Since pagination links automatically point to
// the current URI, this makes pretty good sense.
list($uri, $secure) = array($this->request->uri(), $this->request->secure());
return HTML::link($uri.$this->appendage($element, $page), $text, array('class' => $class), $secure);
} }
} }
/** /**
* Generate the "previous" HTML link. * Create the pagination link "appendage" for an element.
*
* @return string
*/
public function previous()
{
$text = Lang::line('pagination.previous')->get($this->language);
if ($this->page > 1)
{
return $this->link($this->page - 1, $text, 'prev_page').' ';
}
return HTML::span($text, array('class' => 'disabled prev_page')).' ';
}
/**
* Generate the "next" HTML link.
*
* @return string
*/
public function next()
{
$text = Lang::line('pagination.next')->get($this->language);
if ($this->page < $this->last_page)
{
return $this->link($this->page + 1, $text, 'next_page');
}
return HTML::span($text, array('class' => 'disabled next_page'));
}
/**
* Build the first two page links for a sliding page range.
*
* @return string
*/
private function beginning()
{
return $this->range(1, 2).'<span class="dots">...</span>';
}
/**
* Build the last two page links for a sliding page range.
*
* @return string
*/
private function ending()
{
return '<span class="dots">...</span>'.$this->range($this->last_page - 1, $this->last_page);
}
/**
* Build a range of page links.
*
* For the current page, an HTML span element will be generated instead of a link.
*
* @param int $start
* @param int $end
* @return string
*/
private function range($start, $end)
{
$pages = '';
for ($i = $start; $i <= $end; $i++)
{
$pages .= ($this->page == $i) ? HTML::span($i, array('class' => 'current')).' ' : $this->link($i, $i, null).' ';
}
return $pages;
}
/**
* Create a HTML page link.
* *
* @param string $element
* @param int $page * @param int $page
* @param string $text
* @param string $attributes
* @return string * @return string
*/ */
private function link($page, $text, $class) protected function appendage($element, $page)
{ {
$append = ''; // The appendage string contains the query string, but it also contains a
// place-holder for the page number. This will be used to insert the
foreach ($this->append as $key => $value) // correct page number based on the element being created.
if (is_null($this->appendage))
{ {
$append .= '&'.$key.'='.$value; $this->appendage = '?page=%s'.http_build_query((array) $this->appends);
} }
$request = IoC::container()->core('request'); return sprintf($this->appendage, $page);
return HTML::link($request->uri().'?page='.$page.$append, $text, compact('class'), $request->secure());
}
/**
* Set the language that should be used when generating page links.
*
* @param string $language
* @return Paginator
*/
public function lang($language)
{
$this->language = $language;
return $this;
} }
/** /**
* Set the items that should be appended to the link query strings. * Set the items that should be appended to the link query strings.
* *
* This provides a convenient method of maintaining sort or passing other information
* to the route handling pagination.
*
* @param array $values * @param array $values
* @return Paginator * @return Paginator
*/ */
public function append($values) public function appends($values)
{ {
$this->append = $values; $this->appends = $values;
return $this;
}
/**
* Set the elements that should be included when creating the pagination links.
*
* The available elements are "first", "previous", "status", "next", and "last".
*
* @param array $elements
* @return string
*/
public function elements($elements)
{
$this->elements = $elements;
return $this; return $this;
} }

View File

@@ -1,4 +1,4 @@
<?php namespace Laravel; <?php namespace Laravel; use Closure;
class Request { 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;
} }
/** /**

View File

@@ -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 {
* *
* <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()));
} }
} }

View File

@@ -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 {
/** /**
* 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 {
// 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 {
} }
/** /**
* 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 {
* *
* 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 {
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.
* *

View File

@@ -1,13 +0,0 @@
<?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);
}

View File

@@ -1,9 +1,6 @@
<?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 {
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.
* *

View File

@@ -1,18 +1,4 @@
<?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 {

View File

@@ -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 {
/** /**
* 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 {
/** /**
* 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()

View File

@@ -1,30 +1,30 @@
<?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 {
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 {
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 {
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 {
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 '*';
} }

View File

@@ -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 {
/** /**
* 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 {
* 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 {
* 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
*/ */

View File

@@ -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 {
*/ */
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 {
*/ */
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']);
} }
} }

View File

@@ -1,5 +1,9 @@
<?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 {
/** /**

View File

@@ -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, '/');
} }

View File

@@ -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 {
} }
$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 {
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 {
{ {
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 {
{ {
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 {
* 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 {
*/ */
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 {
*/ */
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 {
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.");
}
} }