improved performance. added support for database ports.

This commit is contained in:
Taylor Otwell
2011-06-28 12:21:21 -05:00
parent fef1809982
commit 1aa86d0798
16 changed files with 288 additions and 144 deletions

View File

@@ -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'] : '';
}
}

View File

@@ -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();

View File

@@ -1,6 +1,7 @@
<?php namespace System\DB;
use System\Str;
use System\Config;
use System\Inflector;
abstract class Eloquent {
@@ -102,7 +103,7 @@ abstract class Eloquent {
return $class::$table;
}
return Str::lower(Inflector::plural($class));
return strtolower(Inflector::plural($class));
}
/**
@@ -213,7 +214,7 @@ abstract class Eloquent {
// For example, the foreign key for a User model would
// be user_id. Photo would be photo_id, etc.
// -----------------------------------------------------
$this->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);
}

View File

@@ -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.");

View File

@@ -55,7 +55,7 @@ class Dynamic {
}
else
{
$connector = trim(Str::upper($segment), '_');
$connector = trim(strtoupper($segment), '_');
}
}

View File

@@ -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;
}

View File

@@ -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);
}

View File

@@ -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');
}
/**

View File

@@ -0,0 +1,123 @@
<?php namespace System\Validation;
class Error_Collector {
/**
* All of the error messages.
*
* @var array
*/
public $messages;
/**
* Create a new Error Collector instance.
*
* @return void
*/
public function __construct($messages = array())
{
$this->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;
}
}

View File

@@ -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;

View File

@@ -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));
}
}
}

View File

@@ -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;
}
/**