Commit 6281c8c3 authored by Taylor Otwell's avatar Taylor Otwell

more refactoring for dependency injection.

parent 893bb838
......@@ -8,6 +8,12 @@ return array(
|--------------------------------------------------------------------------
*/
'laravel.auth' => array('resolver' => function($container)
{
return new Security\Authenticator($container->resolve('laravel.session'), $container->resolve('laravel.hasher'));
}),
'laravel.config' => array('singleton' => true, 'resolver' => function($container)
{
$paths = array(SYS_CONFIG_PATH, CONFIG_PATH);
......@@ -21,6 +27,14 @@ return array(
}),
'laravel.crypter' => array('resolver' => function($container)
{
$key = $container->resolve('laravel.config')->get('application.key');
return new Security\Crypter(MCRYPT_RIJNDAEL_256, 'cbc', $key);
}),
'laravel.cookie' => array('singleton' => true, 'resolver' => function()
{
return new Cookie($_COOKIE);
......@@ -59,6 +73,12 @@ return array(
}),
'laravel.hasher' => array('singleton' => true, 'resolver' => function($container)
{
return new Security\Hashing\BCrypt(10, false);
}),
'laravel.html' => array('resolver' => function($container)
{
return new HTML($container->resolve('laravel.url'), $container->resolve('laravel.config')->get('application.encoding'));
......@@ -158,6 +178,12 @@ return array(
}),
'laravel.validator' => array('resolver' => function($container)
{
return new Validation\Validator($container->resolve('laravel.lang'));
}),
'laravel.view' => array('singleton' => true, 'resolver' => function($container)
{
require_once SYS_PATH.'view'.EXT;
......@@ -171,25 +197,6 @@ return array(
return new View_Composer($container->resolve('laravel.application'), require APP_PATH.'composers'.EXT);
}),
/*
|--------------------------------------------------------------------------
| Laravel Security Components
|--------------------------------------------------------------------------
*/
'laravel.security.auth' => array('resolver' => function($container)
{
$hasher = $container->resolve('laravel.security.hashing.engine');
return new Security\Auth(Session\Manager::driver(), $hasher);
}),
'laravel.security.hashing.engine' => array('resolver' => function()
{
return new Security\Hashing\BCrypt(10, false);
}),
/*
|--------------------------------------------------------------------------
| Laravel Cookie Session Components
......
......@@ -8,7 +8,7 @@ class Factory {
* @param array $config
* @return Connector
*/
public static function make($config)
public function make($config)
{
if (isset($config['connector'])) return new Callback;
......
......@@ -9,34 +9,45 @@ class Manager {
*/
public $connections = array();
/**
* The connector factory instance.
*
* @var Connector\Factory
*/
protected $factory;
/**
* The database connection configurations.
*
* @var array
*/
private $config;
protected $config;
/**
* The default database connection name.
*
* @var string
*/
private $default;
protected $default;
/**
* Create a new database manager instance.
*
* @param string $default
* @param Connector\Factory $factory
* @param array $config
* @param string $default
* @return void
*/
public function __construct($config, $default)
public function __construct(Connector\Factory $factory, $config, $default)
{
$this->config = $config;
$this->factory = $factory;
$this->default = $default;
}
/**
* Get a database connection. If no database name is specified, the default
* connection will be returned as defined in the db configuration file.
* connection will be returned as defined in the database configuration file.
*
* Note: Database connections are managed as singletons.
*
......@@ -54,7 +65,7 @@ class Manager {
throw new \Exception("Database connection [$connection] is not defined.");
}
$connector = Connector\Factory::make($this->config[$connection]);
$connector = $this->factory->make($this->config[$connection]);
static::$connections[$connection] = new Connection($connection, $this->config[$connection], $connector);
}
......@@ -67,8 +78,8 @@ class Manager {
*
* This method primarily serves as a short-cut to the $connection->table() method.
*
* @param string $table
* @param string $connection
* @param string $table
* @param string $connection
* @return Database\Query
*/
public function table($table, $connection = null)
......
......@@ -30,6 +30,16 @@ class Request {
*/
private $url;
/**
* The request URI.
*
* After determining the URI once, this property will be set and returned
* on subsequent requests for the URI.
*
* @var string
*/
private $uri;
/**
* Create a new request instance.
*
......@@ -59,6 +69,8 @@ class Request {
*/
public function uri()
{
if ( ! is_null($this->uri)) return $this->uri;
if (isset($this->server['PATH_INFO']))
{
$uri = $this->server['PATH_INFO'];
......@@ -79,7 +91,19 @@ class Request {
$uri = (strpos($uri, $value) === 0) ? substr($uri, strlen($value)) : $uri;
}
return (($uri = trim($uri, '/')) == '') ? '/' : $uri;
return $this->uri = (($uri = trim($uri, '/')) == '') ? '/' : $uri;
}
/**
* Get the request format.
*
* The format is determined by essentially taking the "extension" of the URI.
*
* @return string
*/
public function format()
{
return (($extension = pathinfo($this->uri(), PATHINFO_EXTENSION)) !== '') ? $extension : 'html';
}
/**
......
......@@ -101,6 +101,8 @@ class Router {
{
foreach (explode(', ', $keys) as $key)
{
if ( ! is_null($formats = $this->provides($callback))) $key .= '(\.('.implode('|', $formats).'))?';
if (preg_match('#^'.$this->translate_wildcards($key).'$#', $destination))
{
return $this->request->route = new Route($keys, $callback, $this->parameters($destination, $key), $this->controller_path);
......@@ -172,6 +174,17 @@ class Router {
}
}
/**
* Get the request formats for which the route provides responses.
*
* @param mixed $callback
* @return array
*/
protected function provides($callback)
{
return (is_array($callback) and isset($callback['provides'])) ? explode(', ', $callback['provides']) : null;
}
/**
* Translate route URI wildcards into actual regular expressions.
*
......
<?php namespace Laravel\Security;
use Laravel\IoC;
use Laravel\Config;
use Laravel\Session\Driver;
class Authenticator {
......@@ -53,15 +52,13 @@ class Authenticator {
}
/**
* Create a new Auth class instance.
*
* If no session driver or hasher is provided, the default implementations will be used.
* Get an authenticator instance from the IoC container.
*
* @return Auth
* @return Authenticator
*/
public static function make()
{
return IoC::container()->resolve('laravel.security.auth');
return IoC::container()->resolve('laravel.auth');
}
/**
......
<?php namespace Laravel\Security;
use Laravel\Config;
use Laravel\IoC;
class Crypter {
......@@ -33,7 +33,7 @@ class Crypter {
* @param string $key
* @return void
*/
public function __construct($cipher = MCRYPT_RIJNDAEL_256, $mode = 'cbc', $key = null)
public function __construct($cipher, $mode, $key)
{
$this->cipher = $cipher;
$this->mode = $mode;
......@@ -45,24 +45,6 @@ class Crypter {
}
}
/**
* Create a new Crypter instance.
*
* Any cipher and mode supported by Mcrypt may be specified. For more information regarding
* the supported ciphers and modes, check out: http://php.net/manual/en/mcrypt.ciphers.php
*
* By default, the AES-256 cipher will be used in CBC mode.
*
* @param string $cipher
* @param string $mode
* @param string $key
* @return Crypt
*/
public static function make($cipher = MCRYPT_RIJNDAEL_256, $mode = 'cbc', $key = null)
{
return new static($cipher, $mode, (is_null($key)) ? Config::get('application.key') : $key);
}
/**
* Encrypt a string using Mcrypt.
*
......
<?php namespace Laravel\Validation;
use Laravel\IoC;
use Laravel\Str;
use Laravel\Lang;
use Laravel\Database\Manager as DB;
class Validator {
......@@ -64,21 +65,12 @@ class Validator {
/**
* Create a new validator instance.
*
* @param array $attributes
* @param array $rules
* @param array $messages
* @param Lang $lang
* @return void
*/
public function __construct($attributes, $rules, $messages = array())
public function __construct(Lang $lang)
{
foreach ($rules as $key => &$rule)
{
$rule = (is_string($rule)) ? explode('|', $rule) : $rule;
}
$this->attributes = $attributes;
$this->messages = $messages;
$this->rules = $rules;
$this->lang = $lang;
}
/**
......@@ -91,7 +83,27 @@ class Validator {
*/
public static function make($attributes, $rules, $messages = array())
{
return new static($attributes, $rules, $messages);
return IoC::resolve('laravel.validator')->of($attributes, $rules, $messages);
}
/**
* Set the attributes, rules, and messages for the validator.
*
* @param array $attributes
* @param array $rules
* @param array $messages
* @return Validator
*/
public function of($attributes, $rules, $messages = array())
{
foreach ($rules as $key => &$rule)
{
$rule = (is_string($rule)) ? explode('|', $rule) : $rule;
}
$this->attributes = $attributes;
$this->messages = $messages;
$this->rules = $rules;
}
/**
......@@ -146,9 +158,9 @@ class Validator {
if ( ! $this->$validator($attribute, $parameters))
{
$message = $this->format_message($this->get_message($attribute, $rule);
$message = $this->format_message($this->get_message($attribute, $rule));
$this->errors->add($attribute, $message, $attribute, $rule, $parameters));
$this->errors->add($attribute, $message, $attribute, $rule, $parameters);
}
}
......@@ -322,7 +334,7 @@ class Validator {
{
if ( ! isset($parameters[1])) $parameters[1] = $attribute;
if (is_null($this->connection)) $this->connection = DB::connection();
if (is_null($this->connection)) $this->connection = IoC::resolve('laravel.database')->connection();
return $this->connection->table($parameters[0])->where($parameters[1], '=', $this->attributes[$attribute])->count() == 0;
}
......@@ -450,15 +462,15 @@ class Validator {
}
else
{
$message = Lang::line('validation.'.$rule)->get($this->language);
$message = $this->lang->line('validation.'.$rule)->get($this->language);
// For "size" rules that are validating strings or files, we need to adjust
// the default error message for the appropriate type.
if (in_array($rule, $this->size_rules) and ! $this->has_rule($attribute, $this->numeric_rules))
{
return (array_key_exists($attribute, $_FILES))
? rtrim($message, '.').' '.Lang::line('validation.kilobytes')->get($this->language).'.'
: rtrim($message, '.').' '.Lang::line('validation.characters')->get($this->language).'.';
? rtrim($message, '.').' '.$this->lang->line('validation.kilobytes')->get($this->language).'.'
: rtrim($message, '.').' '.$this->lang->line('validation.characters')->get($this->language).'.';
}
return $message;
......@@ -476,7 +488,7 @@ class Validator {
*/
protected function format_message($message, $attribute, $rule, $parameters)
{
$display = Lang::line('attributes.'.$attribute)->get($this->language, str_replace('_', ' ', $attribute));
$display = $this->lang->line('attributes.'.$attribute)->get($this->language, str_replace('_', ' ', $attribute));
$message = str_replace(':attribute', $display, $message);
......
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