From 14186a00e0478e61b6d5380d32443d4ad489cf91 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Thu, 29 Sep 2011 21:22:48 -0500 Subject: [PATCH] refactoring and bug fixes. --- application/config/auth.php | 2 +- application/config/error.php | 21 +++--------- application/filters.php | 4 +-- laravel/bootstrap/core.php | 32 +++++++------------ laravel/bootstrap/errors.php | 32 +++++++++++++------ laravel/bootstrap/functions.php | 16 ++++++++++ laravel/database/connection.php | 12 +++---- laravel/database/grammars/grammar.php | 2 +- laravel/database/query.php | 46 +++++++++++++++------------ laravel/language/en/validation.php | 22 ++++++------- laravel/laravel.php | 46 ++++++++++++++++----------- laravel/security/auth.php | 12 +++---- laravel/session/drivers/apc.php | 3 +- laravel/session/drivers/cookie.php | 3 +- laravel/session/drivers/database.php | 25 ++++++++++----- laravel/session/drivers/driver.php | 3 +- laravel/session/drivers/file.php | 3 +- laravel/session/drivers/memcached.php | 3 +- laravel/session/manager.php | 22 +++++++++++-- laravel/session/payload.php | 9 ++++++ laravel/validation/validator.php | 2 +- public/index.php | 4 ++- 22 files changed, 192 insertions(+), 132 deletions(-) create mode 100644 laravel/bootstrap/functions.php diff --git a/application/config/auth.php b/application/config/auth.php index 252c6723..8c9b14fa 100644 --- a/application/config/auth.php +++ b/application/config/auth.php @@ -59,6 +59,6 @@ | */ - 'logout' => function($id) {} + 'logout' => function($user) {} ); \ No newline at end of file diff --git a/application/config/error.php b/application/config/error.php index d5886eac..1f8c1cd9 100644 --- a/application/config/error.php +++ b/application/config/error.php @@ -40,23 +40,15 @@ | flexibility in Laravel to manage error logging as you see fit. | | This function will be called when an error occurs in your application. - | You can log the error however you like. + | You are free to handle the exception any way your heart desires. | | The error "severity" passed to the method is a human-readable severity | level such as "Parsing Error" or "Fatal Error". | - | A simple logging system has been setup for you. By default, all errors - | will be logged to the storage/log.txt file. - | */ 'handler' => function($exception, $severity, $message, $config) { - if ($config['log']) - { - call_user_func($config['logger'], $severity, $message); - } - if ($config['detail']) { $data = compact('exception', 'severity', 'message'); @@ -69,8 +61,6 @@ } $response->send(); - - exit(1); }, /* @@ -81,18 +71,15 @@ | Because of the various ways of managing error logging, you get complete | flexibility to manage error logging as you see fit. | - | This function will be called when an error occurs in your application. - | You can log the error however you like. - | - | The error "severity" passed to the method is a human-readable severity - | level such as "Parsing Error" or "Fatal Error". + | This function will be called when an error occurs in your application + | and error loggins is enabled. You can log the error however you like. | | A simple logging system has been setup for you. By default, all errors | will be logged to the storage/log.txt file. | */ - 'logger' => function($severity, $message) + 'logger' => function($exception, $severity, $message, $config) { File::append(STORAGE_PATH.'log.txt', date('Y-m-d H:i:s').' '.$severity.' - '.$message.PHP_EOL); } diff --git a/application/filters.php b/application/filters.php index a2d87a30..01a70efc 100644 --- a/application/filters.php +++ b/application/filters.php @@ -56,13 +56,13 @@ 'auth' => function() { - return ( ! Auth::check()) ? Redirect::to('login') : null; + if ( ! Auth::check()) return Redirect::to('login'); }, 'csrf' => function() { - return (Input::get('csrf_token') !== Form::raw_token()) ? Response::error('500') : null; + if (Input::get('csrf_token') !== Form::raw_token()) return Response::error('500'); }, ); \ No newline at end of file diff --git a/laravel/bootstrap/core.php b/laravel/bootstrap/core.php index c9e18571..6e68e692 100644 --- a/laravel/bootstrap/core.php +++ b/laravel/bootstrap/core.php @@ -26,8 +26,9 @@ define('BLADE_EXT', '.blade.php'); /** - * Load the classes that can't be resolved through the auto-loader. These are typically classes - * that are used by the auto-loader or configuration classes, and therefore cannot be auto-loaded. + * Load the classes that can't be resolved through the auto-loader. + * These are typically classes that are used by the auto-loader or + * configuration classes, and therefore cannot be auto-loaded. */ require SYS_PATH.'facades'.EXT; require SYS_PATH.'config'.EXT; @@ -35,9 +36,10 @@ require SYS_PATH.'arr'.EXT; /** - * Bootstrap the application inversion of control (IoC) container. The container provides the - * convenient resolution of objects and their dependencies, allowing for flexibility and - * testability within the framework and application. + * Bootstrap the application inversion of control (IoC) container. + * The container provides the convenient resolution of objects and + * their dependencies, allowing for flexibility and testability + * within the framework and application. */ require SYS_PATH.'container'.EXT; @@ -46,25 +48,13 @@ IoC::$container = $container; /** - * 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. + * 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($container->resolve('laravel.loader'), 'load')); /** * Define a few convenient global functions. */ -function e($value) -{ - return HTML::entities($value); -} - -function __($key, $replacements = array(), $language = null) -{ - return Lang::line($key, $replacements, $language); -} - -function fe($function) -{ - return function_exists($function); -} \ No newline at end of file +require 'functions'.EXT; \ No newline at end of file diff --git a/laravel/bootstrap/errors.php b/laravel/bootstrap/errors.php index 80806144..452cb1d6 100644 --- a/laravel/bootstrap/errors.php +++ b/laravel/bootstrap/errors.php @@ -1,8 +1,9 @@ * // Get the total number of rows on a table - * $count = DB::connection()->scalar('select count(*) from users'); + * $count = DB::connection()->only('select count(*) from users'); * * // Get the sum of payment amounts from a table - * $sum = DB::connection()->scalar('select sum(amount) from payments') + * $sum = DB::connection()->only('select sum(amount) from payments') * * * @param string $sql * @param array $bindings - * @return float + * @return mixed */ - public function scalar($sql, $bindings = array()) + public function only($sql, $bindings = array()) { $result = (array) $this->first($sql, $bindings); - return (float) reset($result); + return reset($result); } /** diff --git a/laravel/database/grammars/grammar.php b/laravel/database/grammars/grammar.php index 47ad7a9c..6d652b26 100644 --- a/laravel/database/grammars/grammar.php +++ b/laravel/database/grammars/grammar.php @@ -113,7 +113,7 @@ final protected function wheres(Query $query) // // The only exception to this rule are "raw" where clauses, which are simply // appended to the query as-is, without any further compiling. - foreach ($wheres as $where) + foreach ($query->wheres as $where) { $sql[] = ($where['type'] == 'raw') ? $where['sql'] : $where['connector'].' '.$this->{$where['type']}($where); } diff --git a/laravel/database/query.php b/laravel/database/query.php index b74d7f14..b98d9d0f 100644 --- a/laravel/database/query.php +++ b/laravel/database/query.php @@ -456,23 +456,16 @@ public function find($id, $columns = array('*')) } /** - * Get an aggregate value. + * Execute the query as a SELECT statement and return a single column. * - * @param string $aggregate * @param string $column * @return mixed */ - private function aggregate($aggregator, $column) + public function only($column) { - $this->aggregate = compact('aggregator', 'column'); + $this->select(array($column)); - $result = $this->connection->scalar($this->grammar->select($this), $this->bindings); - - // Reset the aggregate so more queries can be performed using the same instance. - // This is helpful for getting aggregates and then getting actual results. - $this->aggregate = null; - - return $result; + return $this->connection->only($this->grammar->select($this), $this->bindings); } /** @@ -487,16 +480,7 @@ public function first($columns = array('*')) { $columns = (array) $columns; - $results = (count($results = $this->take(1)->get($columns)) > 0) ? $results[0] : null; - - // If we have results and only a single column was selected from the database, - // we will simply return the value of that column for convenience. - if ( ! is_null($results) and count($columns) == 1 and $columns[0] !== '*') - { - return $results->{$columns[0]}; - } - - return $results; + return (count($results = $this->take(1)->get($columns)) > 0) ? $results[0] : null; } /** @@ -518,6 +502,26 @@ public function get($columns = array('*')) return $results; } + /** + * Get an aggregate value. + * + * @param string $aggregate + * @param string $column + * @return mixed + */ + private function aggregate($aggregator, $column) + { + $this->aggregate = compact('aggregator', 'column'); + + $result = $this->connection->only($this->grammar->select($this), $this->bindings); + + // Reset the aggregate so more queries can be performed using the same instance. + // This is helpful for getting aggregates and then getting actual results. + $this->aggregate = null; + + return $result; + } + /** * Insert an array of values into the database table. * diff --git a/laravel/language/en/validation.php b/laravel/language/en/validation.php index 44fc1a99..ea7e1047 100644 --- a/laravel/language/en/validation.php +++ b/laravel/language/en/validation.php @@ -2,11 +2,12 @@ return array( - /* - |-------------------------------------------------------------------------- - | Validation Error Messages - |-------------------------------------------------------------------------- - */ + /** + * The validation error messages. + * + * These error messages will be used by the Validator class if no + * other messages are provided by the developer. + */ "accepted" => "The :attribute must be accepted.", "active_url" => "The :attribute does not exist.", @@ -29,12 +30,11 @@ "unique" => "The :attribute has already been taken.", "url" => "The :attribute format is invalid.", - /* - |-------------------------------------------------------------------------- - | The following words are appended to the "size" messages when applicable, - | such as when validating string lengths or the size of file uploads. - |-------------------------------------------------------------------------- - */ + /** + * The following words are appended to the "size" messages when + * applicable, such as when validating string lengths or the + * size of file uploads. + */ "characters" => "characters", "kilobytes" => "kilobytes", diff --git a/laravel/laravel.php b/laravel/laravel.php index 9bb5e624..a7e0bd03 100644 --- a/laravel/laravel.php +++ b/laravel/laravel.php @@ -1,15 +1,16 @@ core('request'); list($method, $uri) = array($request->method(), $request->uri()); @@ -52,16 +60,18 @@ } /** - * Stringify the response. We need to force the response to be stringed before - * closing the session, since the developer may be using the session within their - * views, so we cannot age the session data until the view is rendered. + * Stringify the response. We need to force the response to be + * stringed before closing the session, since the developer may + * be using the session within their views, so we cannot age + * the session data until the view is rendered. */ $response->content = $response->render(); /** - * Close the session and write the active payload to persistent storage. The input - * for the current request is also flashed to the session so it will be available - * for the next request via the Input::old method. + * Close the session and write the active payload to persistent + * storage. The input for the current request is also flashed + * to the session so it will be available for the next request + * via the Input::old method. */ if (isset($session)) { diff --git a/laravel/security/auth.php b/laravel/security/auth.php index 333400ba..4f06ee11 100644 --- a/laravel/security/auth.php +++ b/laravel/security/auth.php @@ -1,7 +1,7 @@ session = $session; } @@ -105,7 +105,7 @@ public function remember($user) */ public function logout() { - call_user_func(Config::get('auth.logout'), $this->user()->id); + call_user_func(Config::get('auth.logout'), $this->user()); $this->user = null; diff --git a/laravel/session/drivers/apc.php b/laravel/session/drivers/apc.php index 975cb228..8c70aaf1 100644 --- a/laravel/session/drivers/apc.php +++ b/laravel/session/drivers/apc.php @@ -42,9 +42,10 @@ public function load($id) * * @param array $session * @param array $config + * @param bool $exists * @return void */ - public function save($session, $config) + public function save($session, $config, $exists) { $this->apc->put($session['id'], $session, $config['lifetime']); } diff --git a/laravel/session/drivers/cookie.php b/laravel/session/drivers/cookie.php index 36782217..92f12f62 100644 --- a/laravel/session/drivers/cookie.php +++ b/laravel/session/drivers/cookie.php @@ -56,9 +56,10 @@ public function load($id) * * @param array $session * @param array $config + * @param bool $exists * @return void */ - public function save($session, $config) + public function save($session, $config, $exists) { extract($config); diff --git a/laravel/session/drivers/database.php b/laravel/session/drivers/database.php index 0968539c..6279b10e 100644 --- a/laravel/session/drivers/database.php +++ b/laravel/session/drivers/database.php @@ -50,17 +50,26 @@ public function load($id) * * @param array $session * @param array $config + * @param bool $exists * @return void */ - public function save($session, $config) + public function save($session, $config, $exists) { - $this->delete($session['id']); - - $this->table()->insert(array( - 'id' => $session['id'], - 'last_activity' => $session['last_activity'], - 'data' => serialize($session['data']) - )); + if ($exists) + { + $this->table()->where('id', '=', $session['id'])->update(array( + 'last_activity' => $session['last_activity'], + 'data' => serialize($session['data']), + )); + } + else + { + $this->table()->insert(array( + 'id' => $session['id'], + 'last_activity' => $session['last_activity'], + 'data' => serialize($session['data']) + )); + } } /** diff --git a/laravel/session/drivers/driver.php b/laravel/session/drivers/driver.php index b767bf94..9ba0884e 100644 --- a/laravel/session/drivers/driver.php +++ b/laravel/session/drivers/driver.php @@ -17,9 +17,10 @@ public function load($id); * * @param array $session * @param array $config + * @param bool $exists * @return void */ - public function save($session, $config); + public function save($session, $config, $exists); /** * Delete a session from storage by a given ID. diff --git a/laravel/session/drivers/file.php b/laravel/session/drivers/file.php index 33669894..237fbbab 100644 --- a/laravel/session/drivers/file.php +++ b/laravel/session/drivers/file.php @@ -40,9 +40,10 @@ public function load($id) * * @param array $session * @param array $config + * @param bool $exists * @return void */ - public function save($session, $config) + public function save($session, $config, $exists) { F::put($this->path.$session['id'], serialize($session), LOCK_EX); } diff --git a/laravel/session/drivers/memcached.php b/laravel/session/drivers/memcached.php index ae500e65..5ea208b9 100644 --- a/laravel/session/drivers/memcached.php +++ b/laravel/session/drivers/memcached.php @@ -38,9 +38,10 @@ public function load($id) * * @param array $session * @param array $config + * @param bool $exists * @return void */ - public function save($session, $config) + public function save($session, $config, $exists) { $this->memcached->put($session['id'], $session, $config['lifetime']); } diff --git a/laravel/session/manager.php b/laravel/session/manager.php index 0516184e..c5d905df 100644 --- a/laravel/session/manager.php +++ b/laravel/session/manager.php @@ -28,6 +28,13 @@ class Manager { */ private $payload; + /** + * Indicates if the session exists in persistent storage. + * + * @var bool + */ + private $exists = true; + /** * Create a new session manager instance. * @@ -56,6 +63,8 @@ public function payload($config) // string ID to uniquely identify it among the application's current users. if (is_null($session) or $this->expired($session, $config)) { + $this->exists = false; + $session = array('id' => Str::random(40), 'data' => array()); } @@ -96,12 +105,19 @@ private function expired($session, $config) */ public function close(Payload $payload, $config, $flash = array()) { - foreach ($flash as $key => $value) + // If the session ID has been regenerated, we will need to inform the session driver + // that the session will need to be persisted to the data store as a new session. + if ($payload->regenerated) { - $this->driver->flash($key, $value); + $this->exists = false; } - $this->driver->save($payload->age(), $config); + foreach ($flash as $key => $value) + { + $payload->flash($key, $value); + } + + $this->driver->save($payload->age(), $config, $this->exists); $this->transporter->put($payload->session['id'], $config); diff --git a/laravel/session/payload.php b/laravel/session/payload.php index ba573836..3685aaf6 100644 --- a/laravel/session/payload.php +++ b/laravel/session/payload.php @@ -12,6 +12,13 @@ class Payload { */ public $session = array(); + /** + * Indicates if the session ID has been regenerated. + * + * @var bool + */ + public $regenerated = false; + /** * Create a new session container instance. * @@ -144,6 +151,8 @@ public function flush() public function regenerate() { $this->session['id'] = Str::random(40); + + $this->regenerated = true; } /** diff --git a/laravel/validation/validator.php b/laravel/validation/validator.php index b7dc43a6..5aac70af 100644 --- a/laravel/validation/validator.php +++ b/laravel/validation/validator.php @@ -535,7 +535,7 @@ protected function parse($rule) * @param string $language * @return Validator */ - public function lang($language) + public function speaks($language) { $this->language = $language; return $this; diff --git a/public/index.php b/public/index.php index 44e94ffe..7c03c0d8 100644 --- a/public/index.php +++ b/public/index.php @@ -43,4 +43,6 @@ | 3... 2... 1... Lift-off! |-------------------------------------------------------------------------- */ -require $laravel.'/laravel.php'; \ No newline at end of file +require $laravel.'/laravel.php'; + +echo number_format((microtime(true) - START_TIME) * 1000, 2); \ No newline at end of file