more refactoring for dependency injection.

This commit is contained in:
Taylor Otwell 2011-09-02 19:36:19 -05:00
parent 893bb83895
commit 6281c8c360
8 changed files with 122 additions and 76 deletions

View File

@ -8,6 +8,12 @@
|-------------------------------------------------------------------------- |--------------------------------------------------------------------------
*/ */
'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) 'laravel.config' => array('singleton' => true, 'resolver' => function($container)
{ {
$paths = array(SYS_CONFIG_PATH, CONFIG_PATH); $paths = array(SYS_CONFIG_PATH, CONFIG_PATH);
@ -21,6 +27,14 @@
}), }),
'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() 'laravel.cookie' => array('singleton' => true, 'resolver' => function()
{ {
return new Cookie($_COOKIE); return new Cookie($_COOKIE);
@ -59,6 +73,12 @@
}), }),
'laravel.hasher' => array('singleton' => true, 'resolver' => function($container)
{
return new Security\Hashing\BCrypt(10, false);
}),
'laravel.html' => array('resolver' => function($container) 'laravel.html' => array('resolver' => function($container)
{ {
return new HTML($container->resolve('laravel.url'), $container->resolve('laravel.config')->get('application.encoding')); return new HTML($container->resolve('laravel.url'), $container->resolve('laravel.config')->get('application.encoding'));
@ -158,6 +178,12 @@
}), }),
'laravel.validator' => array('resolver' => function($container)
{
return new Validation\Validator($container->resolve('laravel.lang'));
}),
'laravel.view' => array('singleton' => true, 'resolver' => function($container) 'laravel.view' => array('singleton' => true, 'resolver' => function($container)
{ {
require_once SYS_PATH.'view'.EXT; require_once SYS_PATH.'view'.EXT;
@ -171,25 +197,6 @@
return new View_Composer($container->resolve('laravel.application'), require APP_PATH.'composers'.EXT); 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 | Laravel Cookie Session Components

View File

@ -8,7 +8,7 @@ class Factory {
* @param array $config * @param array $config
* @return Connector * @return Connector
*/ */
public static function make($config) public function make($config)
{ {
if (isset($config['connector'])) return new Callback; if (isset($config['connector'])) return new Callback;

View File

@ -9,34 +9,45 @@ class Manager {
*/ */
public $connections = array(); public $connections = array();
/**
* The connector factory instance.
*
* @var Connector\Factory
*/
protected $factory;
/** /**
* The database connection configurations. * The database connection configurations.
* *
* @var array * @var array
*/ */
private $config; protected $config;
/** /**
* The default database connection name. * The default database connection name.
* *
* @var string * @var string
*/ */
private $default; protected $default;
/** /**
* Create a new database manager instance. * 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->config = $config;
$this->factory = $factory;
$this->default = $default; $this->default = $default;
} }
/** /**
* Get a database connection. If no database name is specified, the 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. * Note: Database connections are managed as singletons.
* *
@ -54,7 +65,7 @@ public function connection($connection = null)
throw new \Exception("Database connection [$connection] is not defined."); 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); static::$connections[$connection] = new Connection($connection, $this->config[$connection], $connector);
} }
@ -67,8 +78,8 @@ public function connection($connection = null)
* *
* This method primarily serves as a short-cut to the $connection->table() method. * This method primarily serves as a short-cut to the $connection->table() method.
* *
* @param string $table * @param string $table
* @param string $connection * @param string $connection
* @return Database\Query * @return Database\Query
*/ */
public function table($table, $connection = null) public function table($table, $connection = null)

View File

@ -30,6 +30,16 @@ class Request {
*/ */
private $url; 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. * Create a new request instance.
* *
@ -59,6 +69,8 @@ public function __construct($server, $post, $url)
*/ */
public function uri() public function uri()
{ {
if ( ! is_null($this->uri)) return $this->uri;
if (isset($this->server['PATH_INFO'])) if (isset($this->server['PATH_INFO']))
{ {
$uri = $this->server['PATH_INFO']; $uri = $this->server['PATH_INFO'];
@ -79,7 +91,19 @@ public function uri()
$uri = (strpos($uri, $value) === 0) ? substr($uri, strlen($value)) : $uri; $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';
} }
/** /**

View File

@ -101,6 +101,8 @@ public function route()
{ {
foreach (explode(', ', $keys) as $key) foreach (explode(', ', $keys) as $key)
{ {
if ( ! is_null($formats = $this->provides($callback))) $key .= '(\.('.implode('|', $formats).'))?';
if (preg_match('#^'.$this->translate_wildcards($key).'$#', $destination)) if (preg_match('#^'.$this->translate_wildcards($key).'$#', $destination))
{ {
return $this->request->route = new Route($keys, $callback, $this->parameters($destination, $key), $this->controller_path); return $this->request->route = new Route($keys, $callback, $this->parameters($destination, $key), $this->controller_path);
@ -172,6 +174,17 @@ protected function controller_key($segments)
} }
} }
/**
* 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. * Translate route URI wildcards into actual regular expressions.
* *

View File

@ -1,7 +1,6 @@
<?php namespace Laravel\Security; <?php namespace Laravel\Security;
use Laravel\IoC; use Laravel\IoC;
use Laravel\Config;
use Laravel\Session\Driver; use Laravel\Session\Driver;
class Authenticator { class Authenticator {
@ -53,15 +52,13 @@ public function __construct(Driver $driver, Hashing\Engine $hasher)
} }
/** /**
* Create a new Auth class instance. * Get an authenticator instance from the IoC container.
* *
* If no session driver or hasher is provided, the default implementations will be used. * @return Authenticator
*
* @return Auth
*/ */
public static function make() public static function make()
{ {
return IoC::container()->resolve('laravel.security.auth'); return IoC::container()->resolve('laravel.auth');
} }
/** /**

View File

@ -1,6 +1,6 @@
<?php namespace Laravel\Security; <?php namespace Laravel\Security;
use Laravel\Config; use Laravel\IoC;
class Crypter { class Crypter {
@ -33,7 +33,7 @@ class Crypter {
* @param string $key * @param string $key
* @return void * @return void
*/ */
public function __construct($cipher = MCRYPT_RIJNDAEL_256, $mode = 'cbc', $key = null) public function __construct($cipher, $mode, $key)
{ {
$this->cipher = $cipher; $this->cipher = $cipher;
$this->mode = $mode; $this->mode = $mode;
@ -45,24 +45,6 @@ public function __construct($cipher = MCRYPT_RIJNDAEL_256, $mode = 'cbc', $key =
} }
} }
/**
* 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. * Encrypt a string using Mcrypt.
* *

View File

@ -1,7 +1,8 @@
<?php namespace Laravel\Validation; <?php namespace Laravel\Validation;
use Laravel\IoC;
use Laravel\Str;
use Laravel\Lang; use Laravel\Lang;
use Laravel\Database\Manager as DB;
class Validator { class Validator {
@ -64,21 +65,12 @@ class Validator {
/** /**
* Create a new validator instance. * Create a new validator instance.
* *
* @param array $attributes * @param Lang $lang
* @param array $rules
* @param array $messages
* @return void * @return void
*/ */
public function __construct($attributes, $rules, $messages = array()) public function __construct(Lang $lang)
{ {
foreach ($rules as $key => &$rule) $this->lang = $lang;
{
$rule = (is_string($rule)) ? explode('|', $rule) : $rule;
}
$this->attributes = $attributes;
$this->messages = $messages;
$this->rules = $rules;
} }
/** /**
@ -91,7 +83,27 @@ public function __construct($attributes, $rules, $messages = array())
*/ */
public static function make($attributes, $rules, $messages = array()) 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 @@ protected function check($attribute, $rule)
if ( ! $this->$validator($attribute, $parameters)) 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 @@ protected function validate_unique($attribute, $parameters)
{ {
if ( ! isset($parameters[1])) $parameters[1] = $attribute; 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; return $this->connection->table($parameters[0])->where($parameters[1], '=', $this->attributes[$attribute])->count() == 0;
} }
@ -450,15 +462,15 @@ protected function get_message($attribute, $rule)
} }
else 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 // For "size" rules that are validating strings or files, we need to adjust
// the default error message for the appropriate type. // the default error message for the appropriate type.
if (in_array($rule, $this->size_rules) and ! $this->has_rule($attribute, $this->numeric_rules)) if (in_array($rule, $this->size_rules) and ! $this->has_rule($attribute, $this->numeric_rules))
{ {
return (array_key_exists($attribute, $_FILES)) return (array_key_exists($attribute, $_FILES))
? rtrim($message, '.').' '.Lang::line('validation.kilobytes')->get($this->language).'.' ? rtrim($message, '.').' '.$this->lang->line('validation.kilobytes')->get($this->language).'.'
: rtrim($message, '.').' '.Lang::line('validation.characters')->get($this->language).'.'; : rtrim($message, '.').' '.$this->lang->line('validation.characters')->get($this->language).'.';
} }
return $message; return $message;
@ -476,7 +488,7 @@ protected function get_message($attribute, $rule)
*/ */
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)); $display = $this->lang->line('attributes.'.$attribute)->get($this->language, str_replace('_', ' ', $attribute));
$message = str_replace(':attribute', $display, $message); $message = str_replace(':attribute', $display, $message);