more refactoring on DI and IoC.

This commit is contained in:
Taylor Otwell 2011-09-20 23:36:13 -05:00
parent 4525eae25a
commit 600e411ad4
10 changed files with 36 additions and 144 deletions

View File

@ -22,7 +22,7 @@
'Asset' => 'Laravel\\Asset',
'Auth' => 'Laravel\\Facades\\Auth',
'Benchmark' => 'Laravel\\Benchmark',
'Cache' => 'Laravel\\Facades\\Cache',
'Cache' => 'Laravel\\Cache',
'Config' => 'Laravel\\Config',
'Controller' => 'Laravel\\Controller',
'Cookie' => 'Laravel\\Cookie',

View File

@ -1,41 +1,15 @@
<?php namespace Laravel\Cache;
use Laravel\Container;
use Laravel\IoC;
class Manager {
/**
* All of the active cache drivers.
*
* @var Cache\Driver
* @var array
*/
public $drivers = array();
/**
* The application IoC container.
*
* @var Container
*/
private $container;
/**
* The default cache driver.
*
* @var string
*/
private $default;
/**
* Create a new cache manager instance.
*
* @param Container $container
* @return void
*/
public function __construct(Container $container, $default)
{
$this->default = $default;
$this->container = $container;
}
protected static $drivers = array();
/**
* Get a cache driver instance.
@ -46,21 +20,21 @@ public function __construct(Container $container, $default)
* @param string $driver
* @return Cache\Driver
*/
public function driver($driver = null)
public static function driver($driver = null)
{
if (is_null($driver)) $driver = $this->default;
if (is_null($driver)) $driver = Config::get('cache.default');
if ( ! array_key_exists($driver, $this->drivers))
if ( ! array_key_exists($driver, static::$drivers))
{
if ( ! $this->container->registered('laravel.cache.'.$driver))
if ( ! IoC::container()->registered('laravel.cache.'.$driver))
{
throw new \Exception("Cache driver [$driver] is not supported.");
}
return $this->drivers[$driver] = $this->container->resolve('laravel.cache.'.$driver);
return static::$drivers[$driver] = IoC::container()->resolve('laravel.cache.'.$driver);
}
return $this->drivers[$driver];
return static::$drivers[$driver];
}
/**
@ -69,9 +43,9 @@ public function driver($driver = null)
* Passing method calls to the driver instance provides a convenient API for the developer
* when always using the default cache driver.
*/
public function __call($method, $parameters)
public static function __callStatic($method, $parameters)
{
return call_user_func_array(array($this->driver(), $method), $parameters);
return call_user_func_array(array(static::driver(), $method), $parameters);
}
}

View File

@ -54,12 +54,6 @@
|--------------------------------------------------------------------------
*/
'laravel.cache' => array('singleton' => true, 'resolver' => function($c)
{
return new Cache\Manager($c, Config::get('cache.driver'));
}),
'laravel.cache.apc' => array('resolver' => function($c)
{
return new Cache\Drivers\APC(Config::get('cache.key'));
@ -129,9 +123,7 @@
'laravel.session.cookie' => array('resolver' => function($c)
{
$cookies = $c->resolve('laravel.cookie');
return new Session\Drivers\Cookie($c->resolve('laravel.crypter'), $cookies);
return new Session\Drivers\Cookie($c->resolve('laravel.crypter'));
}),

View File

@ -42,15 +42,9 @@ public function __call($method, $parameters)
*/
public function __get($key)
{
$container = IoC::container();
if ($container->registered('laravel.'.$key))
if (IoC::container()->registered($key))
{
return $container->resolve('laravel.'.$key);
}
elseif ($container->registered($key))
{
return $container->resolve($key);
return IoC::container()->resolve($key);
}
throw new \Exception("Attempting to access undefined property [$key] on controller.");

View File

@ -2,13 +2,6 @@
class Cookie {
/**
* The cookies that will be sent to the browser at the end of the request.
*
* @var array
*/
protected static $queue = array();
/**
* Determine if a cookie exists.
*
@ -84,26 +77,13 @@ public static function forever($name, $value, $path = '/', $domain = null, $secu
*/
public static function put($name, $value, $minutes = 0, $path = '/', $domain = null, $secure = false, $http_only = false)
{
if (headers_sent()) return false;
if ($minutes < 0) unset($_COOKIE[$name]);
$time = ($minutes != 0) ? time() + ($minutes * 60) : 0;
static::$queue[] = compact('name', 'value', 'time', 'path', 'domain', 'secure', 'http_only');
}
/**
* Send all of the cookies in the queue to the browser.
*
* This method is called automatically at the end of every request.
*
* @return void
*/
public static function send()
{
foreach (static::$queue as $cookie)
{
call_user_func_array('setcookie', $cookie);
}
return setcookie($name, $value, $time, $path, $domain, $secure, $http_only);
}
/**

View File

@ -33,8 +33,6 @@ public static function __callStatic($method, $parameters)
}
class Auth extends Facade { public static $resolve = 'laravel.auth'; }
class Cache extends Facade { public static $resolve = 'laravel.cache'; }
class Crypter extends Facade { public static $resolve = 'laravel.crypter'; }
class Hasher extends Facade { public static $resolve = 'laravel.hasher'; }
class Package extends Facade { public static $resolve = 'laravel.package'; }
class Session extends Facade { public static $resolve = 'laravel.session'; }

View File

@ -79,11 +79,6 @@
$session->close($container->resolve('laravel.session'), Config::get('session'));
}
// --------------------------------------------------------------
// Send the queued cookies to the browser.
// --------------------------------------------------------------
Cookie::send();
// --------------------------------------------------------------
// Send the response to the browser.
// --------------------------------------------------------------

View File

@ -1,31 +1,5 @@
<?php namespace Laravel;
class Paginator_Factory {
protected $request;
protected $html;
protected $lang;
public function __construct(Request $request, HTML $html, Lang_Factory $lang)
{
$this->html = $html;
$this->lang = $lang;
$this->request = $request;
}
public function make($results, $total, $per_page)
{
$page = Paginator::page($total, $per_page);
$last_page = ceil($total / $per_page);
return new Paginator($this->request, $this->html, $this->lang, $results, $page, $total, $per_page, $last_page);
}
}
class Paginator {
/**
@ -87,13 +61,10 @@ class Paginator {
* @param int $last_page
* @return void
*/
protected function __construct(Request $request, HTML $html, Lang_Factory $lang, $results, $page, $total, $per_page, $last_page)
protected function __construct($results, $page, $total, $per_page, $last_page)
{
$this->html = $html;
$this->lang = $lang;
$this->page = $page;
$this->total = $total;
$this->request = $request;
$this->results = $results;
$this->per_page = $per_page;
$this->last_page = $last_page;
@ -125,7 +96,7 @@ public static function make($results, $total, $per_page)
*/
public static function page($total, $per_page)
{
$page = IoC::container()->resolve('laravel.input')->get('page', 1);
$page = Input::get('page', 1);
if (is_numeric($page) and $page > $last_page = ceil($total / $per_page))
{
@ -273,7 +244,7 @@ private function link($page, $text, $class)
$append .= '&'.$key.'='.$value;
}
return HTML::link(Request::uri().'?page='.$page.$append, $text, compact('class'), Request::is_secure());
return HTML::link(Request::uri().'?page='.$page.$append, $text, compact('class'), Request::secure());
}
/**

View File

@ -1,16 +1,10 @@
<?php namespace Laravel\Session\Drivers;
use Laravel\Cookie as C;
use Laravel\Security\Crypter;
class Cookie implements Driver {
/**
* The cookie manager instance.
*
* @var Cookie
*/
private $cookie;
/**
* The crypter instance.
*
@ -26,12 +20,10 @@ class Cookie implements Driver {
* Create a new Cookie session driver instance.
*
* @param Crypter $crypter
* @param Laravel\Cookie $cookie
* @return void
*/
public function __construct(Crypter $crypter, \Laravel\Cookie $cookie)
public function __construct(Crypter $crypter)
{
$this->cookie = $cookie;
$this->crypter = $crypter;
}
@ -45,9 +37,9 @@ public function __construct(Crypter $crypter, \Laravel\Cookie $cookie)
*/
public function load($id)
{
if ($this->cookie->has('session_payload'))
if (C::has('session_payload'))
{
return unserialize($this->crypter->decrypt($this->cookie->get('session_payload')));
return unserialize($this->crypter->decrypt(C::get('session_payload')));
}
}
@ -59,15 +51,12 @@ public function load($id)
* @return void
*/
public function save($session, $config)
{
if ( ! headers_sent())
{
extract($config);
$payload = $this->crypter->encrypt(serialize($session));
$this->cookie->put('session_payload', $payload, $lifetime, $path, $domain);
}
C::put('session_payload', $payload, $lifetime, $path, $domain);
}
/**
@ -78,7 +67,7 @@ public function save($session, $config)
*/
public function delete($id)
{
$this->cookie->forget('session_payload');
C::forget('session_payload');
}
}

View File

@ -4,6 +4,7 @@
use Laravel\IoC;
use Laravel\Str;
use Laravel\Lang;
use Laravel\Database\Manager as Database;
class Validator {
@ -45,7 +46,7 @@ class Validator {
/**
* The database connection that should be used by the validator.
*
* @var DB\Connection
* @var Database\Connection
*/
public $connection;
@ -324,7 +325,7 @@ protected function validate_unique($attribute, $parameters)
{
if ( ! isset($parameters[1])) $parameters[1] = $attribute;
if (is_null($this->connection)) $this->connection = IoC::resolve('laravel.database')->connection();
if (is_null($this->connection)) $this->connection = Database::connection();
return $this->connection->table($parameters[0])->where($parameters[1], '=', $this->attributes[$attribute])->count() == 0;
}
@ -417,11 +418,9 @@ protected function validate_alpha_dash($attribute)
*/
protected function validate_mimes($attribute, $parameters)
{
$file = IoC::container()->resolve('laravel.file');
foreach ($parameters as $extension)
{
if ($file->is($extension, $this->attributes[$attribute]['tmp_name'])) return true;
if (File::is($extension, $this->attributes[$attribute]['tmp_name'])) return true;
}
return false;
@ -458,7 +457,7 @@ protected function get_message($attribute, $rule)
// the default error message for the appropriate units.
if (in_array($rule, $this->size_rules) and ! $this->has_rule($attribute, $this->numeric_rules))
{
return (array_key_exists($attribute, IoC::container()->resolve('laravel.input')->files()))
return (array_key_exists($attribute, Input::files()))
? rtrim($message, '.').' '.Lang::line('validation.kilobytes')->get($this->language).'.'
: rtrim($message, '.').' '.Lang::line('validation.characters')->get($this->language).'.';
}
@ -546,7 +545,7 @@ public function lang($language)
* @param Database\Connection $connection
* @return Validator
*/
public function connection(Database\Connection $connection)
public function connection(\Laravel\Database\Connection $connection)
{
$this->connection = $connection;
return $this;