From 600e411ad47d427478b01d2c2ff50f0be5588854 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Tue, 20 Sep 2011 23:36:13 -0500 Subject: [PATCH] more refactoring on DI and IoC. --- application/config/aliases.php | 2 +- laravel/cache/manager.php | 48 +++++++----------------------- laravel/config/container.php | 10 +------ laravel/controller.php | 10 ++----- laravel/cookie.php | 26 ++-------------- laravel/facades.php | 2 -- laravel/laravel.php | 5 ---- laravel/paginator.php | 35 ++-------------------- laravel/session/drivers/cookie.php | 29 ++++++------------ laravel/validation/validator.php | 13 ++++---- 10 files changed, 36 insertions(+), 144 deletions(-) diff --git a/application/config/aliases.php b/application/config/aliases.php index 5badd7de..8c184453 100644 --- a/application/config/aliases.php +++ b/application/config/aliases.php @@ -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', diff --git a/laravel/cache/manager.php b/laravel/cache/manager.php index a70cc3ed..d23e3c73 100644 --- a/laravel/cache/manager.php +++ b/laravel/cache/manager.php @@ -1,41 +1,15 @@ 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); } } \ No newline at end of file diff --git a/laravel/config/container.php b/laravel/config/container.php index 57a63403..3e6e3604 100644 --- a/laravel/config/container.php +++ b/laravel/config/container.php @@ -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')); }), diff --git a/laravel/controller.php b/laravel/controller.php index 03b0a786..7a20b1fc 100644 --- a/laravel/controller.php +++ b/laravel/controller.php @@ -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."); diff --git a/laravel/cookie.php b/laravel/cookie.php index 65fb1c14..254f1d01 100644 --- a/laravel/cookie.php +++ b/laravel/cookie.php @@ -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); } /** diff --git a/laravel/facades.php b/laravel/facades.php index 46fbe0a0..622e9515 100644 --- a/laravel/facades.php +++ b/laravel/facades.php @@ -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'; } \ No newline at end of file diff --git a/laravel/laravel.php b/laravel/laravel.php index 7c12ef18..efaa47c6 100644 --- a/laravel/laravel.php +++ b/laravel/laravel.php @@ -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. // -------------------------------------------------------------- diff --git a/laravel/paginator.php b/laravel/paginator.php index dd8fede5..2e63c495 100644 --- a/laravel/paginator.php +++ b/laravel/paginator.php @@ -1,31 +1,5 @@ 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()); } /** diff --git a/laravel/session/drivers/cookie.php b/laravel/session/drivers/cookie.php index 36e7e6f5..9261773c 100644 --- a/laravel/session/drivers/cookie.php +++ b/laravel/session/drivers/cookie.php @@ -1,16 +1,10 @@ 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'))); } } @@ -60,14 +52,11 @@ public function load($id) */ public function save($session, $config) { - if ( ! headers_sent()) - { - extract($config); + extract($config); - $payload = $this->crypter->encrypt(serialize($session)); + $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'); } } \ No newline at end of file diff --git a/laravel/validation/validator.php b/laravel/validation/validator.php index 1b84dca3..74f23de1 100644 --- a/laravel/validation/validator.php +++ b/laravel/validation/validator.php @@ -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;