From 1aa86d0798fe08af04c283f4f627294783a44606 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Tue, 28 Jun 2011 12:21:21 -0500 Subject: [PATCH] improved performance. added support for database ports. --- application/config/aliases.php | 45 ++++++++++ application/config/application.php | 42 --------- application/views/home/index.php | 114 ++++++++++++------------ public/index.php | 8 +- system/db.php | 22 ++++- system/db/connector.php | 15 +++- system/db/eloquent.php | 11 +-- system/db/query.php | 4 +- system/db/query/dynamic.php | 2 +- system/inflector.php | 4 +- system/loader.php | 6 +- system/request.php | 4 +- system/validation/error_collector.php | 123 ++++++++++++++++++++++++++ system/validation/message.php | 2 +- system/validation/rule.php | 20 +---- system/validator.php | 10 ++- 16 files changed, 288 insertions(+), 144 deletions(-) create mode 100644 application/config/aliases.php create mode 100644 system/validation/error_collector.php diff --git a/application/config/aliases.php b/application/config/aliases.php new file mode 100644 index 00000000..72a0b1bf --- /dev/null +++ b/application/config/aliases.php @@ -0,0 +1,45 @@ + 'System\\Auth', + 'Benchmark' => 'System\\Benchmark', + 'Cache' => 'System\\Cache', + 'Config' => 'System\\Config', + 'Cookie' => 'System\\Cookie', + 'Crypt' => 'System\\Crypt', + 'Date' => 'System\\Date', + 'DB' => 'System\\DB', + 'Download' => 'System\\Download', + 'Eloquent' => 'System\\DB\\Eloquent', + 'File' => 'System\\File', + 'Form' => 'System\\Form', + 'Hash' => 'System\\Hash', + 'HTML' => 'System\\HTML', + 'Inflector' => 'System\\Inflector', + 'Input' => 'System\\Input', + 'Lang' => 'System\\Lang', + 'Log' => 'System\\Log', + 'URL' => 'System\\URL', + 'Redirect' => 'System\\Redirect', + 'Request' => 'System\\Request', + 'Response' => 'System\\Response', + 'Session' => 'System\\Session', + 'Str' => 'System\\Str', + 'Text' => 'System\\Text', + 'Validator' => 'System\\Validator', + 'View' => 'System\\View', + +); \ No newline at end of file diff --git a/application/config/application.php b/application/config/application.php index 00fba80a..2f55b140 100644 --- a/application/config/application.php +++ b/application/config/application.php @@ -80,46 +80,4 @@ return array( 'key' => '', - /* - |-------------------------------------------------------------------------- - | Class Aliases - |-------------------------------------------------------------------------- - | - | Here, you can specify any class aliases that you would like registered - | when Laravel loads. Aliases are lazy-loaded, so add as many as you want. - | - | We have already setup a few to make your life easier. - | - */ - - 'aliases' => array( - 'Auth' => 'System\\Auth', - 'Benchmark' => 'System\\Benchmark', - 'Cache' => 'System\\Cache', - 'Config' => 'System\\Config', - 'Cookie' => 'System\\Cookie', - 'Crypt' => 'System\\Crypt', - 'Date' => 'System\\Date', - 'DB' => 'System\\DB', - 'Download' => 'System\\Download', - 'Eloquent' => 'System\\DB\\Eloquent', - 'File' => 'System\\File', - 'Form' => 'System\\Form', - 'Hash' => 'System\\Hash', - 'HTML' => 'System\\HTML', - 'Inflector' => 'System\\Inflector', - 'Input' => 'System\\Input', - 'Lang' => 'System\\Lang', - 'Log' => 'System\\Log', - 'URL' => 'System\\URL', - 'Redirect' => 'System\\Redirect', - 'Request' => 'System\\Request', - 'Response' => 'System\\Response', - 'Session' => 'System\\Session', - 'Str' => 'System\\Str', - 'Text' => 'System\\Text', - 'Validator' => 'System\\Validator', - 'View' => 'System\\View', - ), - ); \ No newline at end of file diff --git a/application/views/home/index.php b/application/views/home/index.php index 59b997e8..4e17bf50 100644 --- a/application/views/home/index.php +++ b/application/views/home/index.php @@ -4,74 +4,76 @@ Welcome To Laravel! - - + + + - - + +
-

Laravel

- -
- You have successfully installed Laravel. +

Installation Complete!

-

+

Ready to dig in? Start building your application in the application/routes.php file.

- Perhaps you would like to peruse the documentation or contribute on GitHub? -
- - -
+

Need to learn more? Peruse our wonderful documentation.

+ \ No newline at end of file diff --git a/public/index.php b/public/index.php index 09dc52ec..f813717e 100644 --- a/public/index.php +++ b/public/index.php @@ -29,21 +29,15 @@ define('PACKAGE_PATH', APP_PATH.'packages/'); define('EXT', '.php'); // -------------------------------------------------------------- -// Load the configuration, error, and string classes. +// Load the configuration class. // -------------------------------------------------------------- require SYS_PATH.'config'.EXT; -require SYS_PATH.'str'.EXT; // -------------------------------------------------------------- // Register the auto-loader. // -------------------------------------------------------------- spl_autoload_register(require SYS_PATH.'loader'.EXT); -// -------------------------------------------------------------- -// Set the Laravel starting time in the Benchmark class. -// -------------------------------------------------------------- -System\Benchmark::$marks['laravel'] = LARAVEL_START; - // -------------------------------------------------------------- // Set the error reporting level. // -------------------------------------------------------------- diff --git a/system/db.php b/system/db.php index fbdcaf1f..0f064ef8 100644 --- a/system/db.php +++ b/system/db.php @@ -66,11 +66,11 @@ class DB { // // For all other statements, return a boolean. // --------------------------------------------------- - if (strpos(Str::upper($sql), 'SELECT') === 0) + if (strpos(strtoupper($sql), 'SELECT') === 0) { return $query->fetchAll(\PDO::FETCH_CLASS, 'stdClass'); } - elseif (strpos(Str::upper($sql), 'UPDATE') === 0 or strpos(Str::upper($sql), 'DELETE') === 0) + elseif (strpos(strtoupper($sql), 'UPDATE') === 0 or strpos(strtoupper($sql), 'DELETE') === 0) { return $query->rowCount(); } @@ -105,4 +105,22 @@ class DB { return static::connection($connection)->getAttribute(\PDO::ATTR_DRIVER_NAME); } + /** + * Get the table prefix for a database connection. + * + * @param string $connection + * @return string + */ + public static function prefix($connection = null) + { + $connections = Config::get('db.connections'); + + if (is_null($connection)) + { + $connection = Config::get('db.default'); + } + + return (array_key_exists('prefix', $connections[$connection])) ? $connections[$connection]['prefix'] : ''; + } + } \ No newline at end of file diff --git a/system/db/connector.php b/system/db/connector.php index f494b4ed..25a50375 100644 --- a/system/db/connector.php +++ b/system/db/connector.php @@ -51,8 +51,21 @@ class Connector { // ----------------------------------------------------- elseif ($config->driver == 'mysql' or $config->driver == 'pgsql') { - $connection = new \PDO($config->driver.':host='.$config->host.';dbname='.$config->database, $config->username, $config->password, static::$options); + // ----------------------------------------------------- + // Build the PDO connection DSN. + // ----------------------------------------------------- + $dsn = $config->driver.':host='.$config->host.';dbname='.$config->database; + if (isset($config->port)) + { + $dsn .= ';port='.$config->port; + } + + $connection = new \PDO($dsn, $config->username, $config->password, static::$options); + + // ----------------------------------------------------- + // Set the appropriate character set for the datbase. + // ----------------------------------------------------- if (isset($config->charset)) { $connection->prepare("SET NAMES '".$config->charset."'")->execute(); diff --git a/system/db/eloquent.php b/system/db/eloquent.php index a5b383c5..584a03bf 100644 --- a/system/db/eloquent.php +++ b/system/db/eloquent.php @@ -1,6 +1,7 @@ relating_key = (is_null($foreign_key)) ? Str::lower(get_class($this)).'_id' : $foreign_key; + $this->relating_key = (is_null($foreign_key)) ? strtolower(get_class($this)).'_id' : $foreign_key; return static::make($model)->where($this->relating_key, '=', $this->id); } @@ -276,7 +277,7 @@ abstract class Eloquent { sort($models); - $this->relating_table = Str::lower($models[0].'_'.$models[1]); + $this->relating_table = strtolower($models[0].'_'.$models[1]); } // ----------------------------------------------------- @@ -286,11 +287,11 @@ abstract class Eloquent { // // This is the same convention as has_one and has_many. // ----------------------------------------------------- - $this->relating_key = Str::lower(get_class($this)).'_id'; + $this->relating_key = strtolower(get_class($this)).'_id'; return static::make($model) ->select(static::table($model).'.*') - ->join($this->relating_table, static::table($model).'.id', '=', $this->relating_table.'.'.Str::lower($model).'_id') + ->join($this->relating_table, static::table($model).'.id', '=', $this->relating_table.'.'.strtolower($model).'_id') ->where($this->relating_table.'.'.$this->relating_key, '=', $this->id); } diff --git a/system/db/query.php b/system/db/query.php index 0d053554..85e02b2f 100644 --- a/system/db/query.php +++ b/system/db/query.php @@ -326,7 +326,7 @@ class Query { */ public function order_by($column, $direction) { - $this->orderings[] = $this->wrap($column).' '.Str::upper($direction); + $this->orderings[] = $this->wrap($column).' '.strtoupper($direction); return $this; } @@ -516,7 +516,7 @@ class Query { // --------------------------------------------------------- if (in_array($method, array('count', 'min', 'max', 'avg', 'sum'))) { - return ($method == 'count') ? $this->aggregate(Str::upper($method), '*') : $this->aggregate(Str::upper($method), $parameters[0]); + return ($method == 'count') ? $this->aggregate(strtoupper($method), '*') : $this->aggregate(strtoupper($method), $parameters[0]); } throw new \Exception("Method [$method] is not defined on the Query class."); diff --git a/system/db/query/dynamic.php b/system/db/query/dynamic.php index 5b507546..354ac57b 100644 --- a/system/db/query/dynamic.php +++ b/system/db/query/dynamic.php @@ -55,7 +55,7 @@ class Dynamic { } else { - $connector = trim(Str::upper($segment), '_'); + $connector = trim(strtoupper($segment), '_'); } } diff --git a/system/inflector.php b/system/inflector.php index 981c5a48..44e0ca61 100644 --- a/system/inflector.php +++ b/system/inflector.php @@ -126,7 +126,7 @@ class Inflector { return static::$plural_cache[$value]; } - if (in_array(Str::lower($value), static::$uncountable)) + if (in_array(strtolower($value), static::$uncountable)) { return static::$plural_cache[$value] = $value; } @@ -165,7 +165,7 @@ class Inflector { return static::$singular_cache[$value]; } - if (in_array(Str::lower($value), static::$uncountable)) + if (in_array(strtolower($value), static::$uncountable)) { return static::$singular_cache[$value] = $value; } diff --git a/system/loader.php b/system/loader.php index a9491592..86960f89 100644 --- a/system/loader.php +++ b/system/loader.php @@ -4,16 +4,16 @@ * This function is registered on the auto-loader stack by the front controller. */ return function($class) { - + // ---------------------------------------------------------- // Replace namespace slashes with directory slashes. // ---------------------------------------------------------- - $file = System\Str::lower(str_replace('\\', '/', $class)); + $file = strtolower(str_replace('\\', '/', $class)); // ---------------------------------------------------------- // Should the class be aliased? // ---------------------------------------------------------- - if (array_key_exists($class, $aliases = System\Config::get('application.aliases'))) + if (array_key_exists($class, $aliases = System\Config::get('aliases'))) { return class_alias($aliases[$class], $class); } diff --git a/system/request.php b/system/request.php index 064eef3b..e9b4d618 100644 --- a/system/request.php +++ b/system/request.php @@ -71,7 +71,7 @@ class Request { // If the requests is to the root of the application, we // always return a single forward slash. // ------------------------------------------------------- - return ($uri == '') ? '/' : Str::lower($uri); + return ($uri == '') ? '/' : strtolower($uri); } /** @@ -136,7 +136,7 @@ class Request { */ public static function is_ajax() { - return (isset($_SERVER['HTTP_X_REQUESTED_WITH']) and Str::lower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest'); + return (isset($_SERVER['HTTP_X_REQUESTED_WITH']) and strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest'); } /** diff --git a/system/validation/error_collector.php b/system/validation/error_collector.php new file mode 100644 index 00000000..b0bbee9b --- /dev/null +++ b/system/validation/error_collector.php @@ -0,0 +1,123 @@ +messages = $messages; + } + + /** + * Add an error message to the collector. + * + * Duplicate messages will not be added. + * + * @param string $attribute + * @param string $message + * @return void + */ + public function add($attribute, $message) + { + // ------------------------------------------------------------- + // Make sure the error message is not duplicated. + // + // For example, the Nullable rules can add a "required" message. + // If the same message has already been added we don't want to + // add it again. + // ------------------------------------------------------------- + if ( ! array_key_exists($attribute, $this->messages) or ! is_array($this->messages[$attribute]) or ! in_array($message, $this->messages[$attribute])) + { + $this->messages[$attribute][] = $message; + } + } + + /** + * Determine if errors exist for an attribute. + * + * @param string $attribute + * @return bool + */ + public function has($attribute) + { + return $this->first($attribute) !== ''; + } + + /** + * Get the first error message for an attribute. + * + * @param string $attribute + * @return string + */ + public function first($attribute) + { + return (count($messages = $this->get($attribute)) > 0) ? $messages[0] : ''; + } + + /** + * Get all of the error messages for an attribute. + * + * If no attribute is specified, all of the error messages will be returned. + * + * @param string $attribute + * @param string $format + * @return array + */ + public function get($attribute = null, $format = ':message') + { + if (is_null($attribute)) + { + return $this->all($format); + } + + return (array_key_exists($attribute, $this->messages)) ? $this->format($this->messages[$attribute], $format) : array(); + } + + /** + * Get all of the error messages. + * + * @param string $format + * @return array + */ + public function all($format = ':message') + { + $all = array(); + + // --------------------------------------------------------- + // Add each error message to the array of messages. Each + // messages will have the specified format applied to it. + // --------------------------------------------------------- + foreach ($this->messages as $messages) + { + $all = array_merge($all, $this->format($messages, $format)); + } + + return $all; + } + + /** + * Format an array of messages. + * + * @param array $messages + * @param string $format + * @return array + */ + private function format($messages, $format) + { + array_walk($messages, function(&$message, $key) use ($format) { $message = str_replace(':message', $message, $format); }); + + return $messages; + } + +} \ No newline at end of file diff --git a/system/validation/message.php b/system/validation/message.php index 669656ae..50d93746 100644 --- a/system/validation/message.php +++ b/system/validation/message.php @@ -47,7 +47,7 @@ class Message { { $class = explode('\\', get_class($rule)); - $rule->error = Str::lower(end($class)); + $rule->error = strtolower(end($class)); } return (is_null($rule->message)) ? Lang::line('validation.'.$rule->error)->get() : $rule->message; diff --git a/system/validation/rule.php b/system/validation/rule.php index 2087bdf7..fa0c6692 100644 --- a/system/validation/rule.php +++ b/system/validation/rule.php @@ -40,11 +40,11 @@ abstract class Rule { /** * Run the validation rule. * - * @param array $attributes - * @param array $errors + * @param array $attributes + * @param Error_Collector $errors * @return void */ - public function validate($attributes, &$errors) + public function validate($attributes, $errors) { foreach ($this->attributes as $attribute) { @@ -52,19 +52,7 @@ abstract class Rule { if ( ! $this->check($attribute, $attributes)) { - $message = Message::get($this, $attribute); - - // ------------------------------------------------------------- - // Make sure the error message is not duplicated. - // - // For example, the Nullable rules can add a "required" message. - // If the same message has already been added we don't want to - // add it again. - // ------------------------------------------------------------- - if ( ! array_key_exists($attribute, $errors) or ! is_array($errors[$attribute]) or ! in_array($message, $errors[$attribute])) - { - $errors[$attribute][] = $message; - } + $errors->add($attribute, Message::get($this, $attribute)); } } } diff --git a/system/validator.php b/system/validator.php index cfb2a7f4..895a22d4 100644 --- a/system/validator.php +++ b/system/validator.php @@ -10,9 +10,9 @@ class Validator { public $attributes; /** - * The validation errors + * The validation error collector. * - * @var array + * @var Error_Collector */ public $errors; @@ -31,6 +31,8 @@ class Validator { */ public function __construct($target = array()) { + $this->errors = new Validation\Error_Collector; + // --------------------------------------------------------- // If the source is an Eloquent model, use the model's // attributes as the validation attributes. @@ -56,7 +58,7 @@ class Validator { */ public function is_valid() { - $this->errors = array(); + $this->errors->messages = array(); foreach ($this->rules as $rule) { @@ -67,7 +69,7 @@ class Validator { $rule->validate($this->attributes, $this->errors); } - return count($this->errors) == 0; + return count($this->errors->messages) == 0; } /**