From 54c69d8c9fa5f9654b6944316dfc9a38e827bae4 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Mon, 26 Sep 2011 21:48:06 -0500 Subject: [PATCH] refactoring. --- laravel/blade.php | 12 +++---- laravel/bootstrap/core.php | 8 ++--- laravel/config/container.php | 6 ++++ laravel/database/connection.php | 31 +++++++++++++++--- laravel/database/manager.php | 8 +++++ laravel/loader.php | 56 ++++++++++++++++++++++---------- laravel/validation/validator.php | 4 ++- 7 files changed, 89 insertions(+), 36 deletions(-) diff --git a/laravel/blade.php b/laravel/blade.php index 98052992..f692669d 100644 --- a/laravel/blade.php +++ b/laravel/blade.php @@ -21,11 +21,7 @@ public static function parse($path) */ public static function parse_string($value) { - $value = static::rewrite_echos($value); - $value = static::rewrite_openings($value); - $value = static::rewrite_closings($value); - - return $value; + return static::closings(static::openings(static::echos($value))); } /** @@ -34,7 +30,7 @@ public static function parse_string($value) * @param string $value * @return string */ - protected static function rewrite_echos($value) + protected static function echos($value) { return preg_replace('/\{\{(.+)\}\}/', '', $value); } @@ -45,7 +41,7 @@ protected static function rewrite_echos($value) * @param string $value * @return string */ - protected static function rewrite_openings($value) + protected static function openings($value) { return preg_replace('/@(if|elseif|foreach|for|while)(\s*\(.*?\))\:/', '', $value); } @@ -56,7 +52,7 @@ protected static function rewrite_openings($value) * @param string $value * @return string */ - protected static function rewrite_closings($value) + protected static function closings($value) { $value = preg_replace('/(\s*)@(else)(.*?)\:/', '$1', $value); $value = preg_replace('/(\s*)@(endif|endforeach|endfor|endwhile)(\s*)/', '$1 $3', $value); diff --git a/laravel/bootstrap/core.php b/laravel/bootstrap/core.php index f157c4cd..217d9f02 100644 --- a/laravel/bootstrap/core.php +++ b/laravel/bootstrap/core.php @@ -17,6 +17,8 @@ define('CONTROLLER_PATH', APP_PATH.'controllers/'); define('DATABASE_PATH', STORAGE_PATH.'database/'); define('LANG_PATH', APP_PATH.'language/'); +define('LIBRARY_PATH', APP_PATH.'libraries/'); +define('MODEL_PATH', APP_PATH.'models/'); define('ROUTE_PATH', APP_PATH.'routes/'); define('SESSION_PATH', STORAGE_PATH.'sessions/'); define('SYS_CONFIG_PATH', SYS_PATH.'config/'); @@ -68,11 +70,7 @@ * Register the application auto-loader. The auto-loader is responsible for the lazy-loading * of all of the Laravel core classes, as well as the developer created libraries and models. */ -spl_autoload_register(array('Laravel\\Loader', 'load')); - -Loader::$paths = array(BASE_PATH, APP_PATH.'models/', APP_PATH.'libraries/', APP_PATH); - -Loader::$aliases = Config::get('aliases'); +spl_autoload_register(array($container->resolve('laravel.loader'), 'load')); /** * Define a few convenient global functions. diff --git a/laravel/config/container.php b/laravel/config/container.php index 009b461d..6396daa3 100644 --- a/laravel/config/container.php +++ b/laravel/config/container.php @@ -64,6 +64,12 @@ }), + 'laravel.loader' => array('singleton' => true, 'resolver' => function($c) + { + return new Loader(array(BASE_PATH, MODEL_PATH, LIBRARY_PATH, BASE_PATH), Config::get('aliases')); + }), + + 'laravel.request' => array('singleton' => true, 'resolver' => function($c) { return new Request($c->resolve('laravel.uri'), $_SERVER, $_POST); diff --git a/laravel/database/connection.php b/laravel/database/connection.php index 74311054..e605116e 100644 --- a/laravel/database/connection.php +++ b/laravel/database/connection.php @@ -1,7 +1,4 @@ - + * // Get the total number of rows on a table + * $count = DB::connection()->scalar('select count(*) from users'); + * + * // Get the sum of payment amounts from a table + * $sum = DB::connection()->scalar('select sum(amount) from payments') + * + * * @param string $sql * @param array $bindings * @return int|float @@ -63,13 +68,21 @@ public function scalar($sql, $bindings = array()) /** * Execute a SQL query against the connection and return the first result. * + * + * // Execute a query against the database connection + * $user = DB::connection()->first('select * from users'); + * + * // Execute a query with bound parameters + * $user = DB::connection()->first('select * from users where id = ?', array($id)); + * + * * @param string $sql * @param array $bindings * @return object */ public function first($sql, $bindings = array()) { - return (count($results = $this->query($sql, $bindings)) > 0) ? $results[0] : null; + if (count($results = $this->query($sql, $bindings)) > 0) return $results[0]; } /** @@ -82,6 +95,14 @@ public function first($sql, $bindings = array()) * DELETE -> Number of Rows affected. * ELSE -> Boolean true / false depending on success. * + * + * // Execute a query against the database connection + * $users = DB::connection()->query('select * from users'); + * + * // Execute a query with bound parameters + * $user = DB::connection()->query('select * from users where id = ?', array($id)); + * + * * @param string $sql * @param array $bindings * @return mixed diff --git a/laravel/database/manager.php b/laravel/database/manager.php index 0234d018..92d49233 100644 --- a/laravel/database/manager.php +++ b/laravel/database/manager.php @@ -18,6 +18,14 @@ class Manager { * * Note: Database connections are managed as singletons. * + * + * // Get the default database connection for the application + * $connection = DB::connection(); + * + * // Get a specific connection by passing the connection name + * $connection = DB::connection('mysql'); + * + * * @param string $connection * @return Connection */ diff --git a/laravel/loader.php b/laravel/loader.php index f76d8ed6..693742af 100644 --- a/laravel/loader.php +++ b/laravel/loader.php @@ -7,32 +7,56 @@ class Loader { * * @var array */ - public static $paths = array(); + protected $paths = array(); /** * The class aliases defined for the application. * * @var array */ - public static $aliases = array(); + protected $aliases = array(); + + /** + * Create a new class loader instance. + * + * @param array $paths + * @param array $aliases + * @return void + */ + public function __construct($paths, $aliases = array()) + { + $this->paths = $paths; + $this->aliases = $aliases; + } /** * Load the file for a given class. * + * + * // Load the file for the "User" class + * Loader::load('User'); + * + * // Load the file for the "Repositories\User" class + * Loader::load('Repositories\\User'); + * + * * @param string $class * @return void */ - public static function load($class) + public function load($class) { - // All Laravel core classes follow a namespace to directory convention. So, we will - // replace all of the namespace slashes with directory slashes. + // All Laravel core classes follow a namespace to directory convention. + // We will replace all of the namespace slashes with directory slashes. $file = strtolower(str_replace('\\', '/', $class)); - // First, we'll check to determine if an alias exists. If it does, we will define the - // alias and bail out. Aliases are defined for most developer used core classes. - if (array_key_exists($class, static::$aliases)) return class_alias(static::$aliases[$class], $class); + // Check to determine if an alias exists. If it does, we will define the + // alias and bail out. Aliases are defined for most used core classes. + if (array_key_exists($class, $this->aliases)) + { + return class_alias($this->aliases[$class], $class); + } - foreach (static::$paths as $path) + foreach ($this->paths as $path) { if (file_exists($path = $path.$file.EXT)) { @@ -46,15 +70,13 @@ public static function load($class) /** * Register a class alias with the auto-loader. * - * Note: Aliases are lazy-loaded, so the aliased class will not be included until it is needed. - * * @param string $alias * @param string $class * @return void */ - public static function alias($alias, $class) + public function alias($alias, $class) { - static::$aliases[$alias] = $class; + $this->aliases[$alias] = $class; } /** @@ -63,9 +85,9 @@ public static function alias($alias, $class) * @param string $path * @return void */ - public static function path($path) + public function path($path) { - static::$paths[] = rtrim($path, '/').'/'; + $this->paths[] = rtrim($path, '/').'/'; } /** @@ -74,9 +96,9 @@ public static function path($path) * @param string $alias * @return void */ - public static function forget_alias($alias) + public function forget_alias($alias) { - unset(static::$aliases[$alias]); + unset($this->aliases[$alias]); } } \ No newline at end of file diff --git a/laravel/validation/validator.php b/laravel/validation/validator.php index b17cd814..b7dc43a6 100644 --- a/laravel/validation/validator.php +++ b/laravel/validation/validator.php @@ -285,7 +285,9 @@ protected function get_size($attribute) $value = $this->attributes[$attribute]; - return (array_key_exists($attribute, $_FILES)) ? $value['size'] / 1024 : Str::length(trim($value)); + $files = IoC::container()->resolve('laravel.input')->files(); + + return (array_key_exists($attribute, $files) ? $value['size'] / 1024 : Str::length(trim($value)); } /**