Commit f7bb0c55 authored by Taylor Otwell's avatar Taylor Otwell

trimmed comment bloat. returning boolean on eloquent save.

parent b66be283
...@@ -92,6 +92,7 @@ return array( ...@@ -92,6 +92,7 @@ return array(
'Response' => 'System\\Response', 'Response' => 'System\\Response',
'Session' => 'System\\Session', 'Session' => 'System\\Session',
'Str' => 'System\\Str', 'Str' => 'System\\Str',
'Test' => 'System\\Test',
'Text' => 'System\\Text', 'Text' => 'System\\Text',
'View' => 'System\View', 'View' => 'System\View',
), ),
......
...@@ -41,9 +41,6 @@ class Auth { ...@@ -41,9 +41,6 @@ class Auth {
throw new \Exception("You must specify a session driver before using the Auth class."); throw new \Exception("You must specify a session driver before using the Auth class.");
} }
// -----------------------------------------------------
// Get the authentication model.
// -----------------------------------------------------
$model = static::model(); $model = static::model();
// ----------------------------------------------------- // -----------------------------------------------------
...@@ -65,9 +62,6 @@ class Auth { ...@@ -65,9 +62,6 @@ class Auth {
*/ */
public static function login($username, $password) public static function login($username, $password)
{ {
// -----------------------------------------------------
// Get the authentication model.
// -----------------------------------------------------
$model = static::model(); $model = static::model();
// ----------------------------------------------------- // -----------------------------------------------------
...@@ -82,19 +76,10 @@ class Auth { ...@@ -82,19 +76,10 @@ class Auth {
// ----------------------------------------------------- // -----------------------------------------------------
$password = (isset($user->salt)) ? Hash::make($password, $user->salt)->value : sha1($password); $password = (isset($user->salt)) ? Hash::make($password, $user->salt)->value : sha1($password);
// -----------------------------------------------------
// Verify that the passwords match.
// -----------------------------------------------------
if ($user->password == $password) if ($user->password == $password)
{ {
// -----------------------------------------------------
// Set the user property.
// -----------------------------------------------------
static::$user = $user; static::$user = $user;
// -----------------------------------------------------
// Store the user ID in the session.
// -----------------------------------------------------
Session::put(static::$key, $user->id); Session::put(static::$key, $user->id);
return true; return true;
...@@ -111,14 +96,7 @@ class Auth { ...@@ -111,14 +96,7 @@ class Auth {
*/ */
public static function logout() public static function logout()
{ {
// -----------------------------------------------------
// Remove the user ID from the session.
// -----------------------------------------------------
Session::forget(static::$key); Session::forget(static::$key);
// -----------------------------------------------------
// Clear the current user variable.
// -----------------------------------------------------
static::$user = null; static::$user = null;
} }
......
...@@ -38,16 +38,13 @@ class File implements \System\Cache\Driver { ...@@ -38,16 +38,13 @@ class File implements \System\Cache\Driver {
} }
// -------------------------------------------------- // --------------------------------------------------
// Verify that the cache file exists. // Does the cache item even exist?
// -------------------------------------------------- // --------------------------------------------------
if ( ! file_exists(APP_PATH.'cache/'.$key)) if ( ! file_exists(APP_PATH.'cache/'.$key))
{ {
return $default; return $default;
} }
// --------------------------------------------------
// Read the contents of the cache file.
// --------------------------------------------------
$cache = file_get_contents(APP_PATH.'cache/'.$key); $cache = file_get_contents(APP_PATH.'cache/'.$key);
// -------------------------------------------------- // --------------------------------------------------
...@@ -74,10 +71,6 @@ class File implements \System\Cache\Driver { ...@@ -74,10 +71,6 @@ class File implements \System\Cache\Driver {
*/ */
public function put($key, $value, $minutes) public function put($key, $value, $minutes)
{ {
// --------------------------------------------------
// The expiration time is stored as a UNIX timestamp
// at the beginning of the cache file.
// --------------------------------------------------
file_put_contents(APP_PATH.'cache/'.$key, (time() + ($minutes * 60)).serialize($value), LOCK_EX); file_put_contents(APP_PATH.'cache/'.$key, (time() + ($minutes * 60)).serialize($value), LOCK_EX);
} }
......
...@@ -17,19 +17,10 @@ class Config { ...@@ -17,19 +17,10 @@ class Config {
*/ */
public static function get($key) public static function get($key)
{ {
// ---------------------------------------------
// Parse the configuration key.
// ---------------------------------------------
list($file, $key) = static::parse($key); list($file, $key) = static::parse($key);
// ---------------------------------------------
// Load the configuration file.
// ---------------------------------------------
static::load($file); static::load($file);
// ---------------------------------------------
// Return the requested item.
// ---------------------------------------------
return (array_key_exists($key, static::$items[$file])) ? static::$items[$file][$key] : null; return (array_key_exists($key, static::$items[$file])) ? static::$items[$file][$key] : null;
} }
...@@ -42,19 +33,10 @@ class Config { ...@@ -42,19 +33,10 @@ class Config {
*/ */
public static function set($file, $value) public static function set($file, $value)
{ {
// ---------------------------------------------
// Parse the configuration key.
// ---------------------------------------------
list($file, $key) = static::parse($key); list($file, $key) = static::parse($key);
// ---------------------------------------------
// Load the configuration file.
// ---------------------------------------------
static::load($file); static::load($file);
// ---------------------------------------------
// Set the item's value.
// ---------------------------------------------
static::$items[$file][$key] = $value; static::$items[$file][$key] = $value;
} }
...@@ -66,22 +48,13 @@ class Config { ...@@ -66,22 +48,13 @@ class Config {
*/ */
private static function parse($key) private static function parse($key)
{ {
// ---------------------------------------------
// Get the key segments.
// ---------------------------------------------
$segments = explode('.', $key); $segments = explode('.', $key);
// ---------------------------------------------
// Validate the key format.
// ---------------------------------------------
if (count($segments) < 2) if (count($segments) < 2)
{ {
throw new \Exception("Invalid configuration key [$key]."); throw new \Exception("Invalid configuration key [$key].");
} }
// ---------------------------------------------
// Return the file and item name.
// ---------------------------------------------
return array($segments[0], implode('.', array_slice($segments, 1))); return array($segments[0], implode('.', array_slice($segments, 1)));
} }
...@@ -93,25 +66,16 @@ class Config { ...@@ -93,25 +66,16 @@ class Config {
*/ */
public static function load($file) public static function load($file)
{ {
// ---------------------------------------------
// If the file has already been loaded, bail.
// ---------------------------------------------
if (array_key_exists($file, static::$items)) if (array_key_exists($file, static::$items))
{ {
return; return;
} }
// ---------------------------------------------
// Verify that the configuration file exists.
// ---------------------------------------------
if ( ! file_exists($path = APP_PATH.'config/'.$file.EXT)) if ( ! file_exists($path = APP_PATH.'config/'.$file.EXT))
{ {
throw new \Exception("Configuration file [$file] does not exist."); throw new \Exception("Configuration file [$file] does not exist.");
} }
// ---------------------------------------------
// Load the configuration file.
// ---------------------------------------------
static::$items[$file] = require $path; static::$items[$file] = require $path;
} }
......
...@@ -53,9 +53,6 @@ class Cookie { ...@@ -53,9 +53,6 @@ class Cookie {
*/ */
public static function put($key, $value, $minutes = 0, $path = '/', $domain = null, $secure = false) public static function put($key, $value, $minutes = 0, $path = '/', $domain = null, $secure = false)
{ {
// ----------------------------------------------------------
// If the lifetime is less than zero, delete the cookie.
// ----------------------------------------------------------
if ($minutes < 0) if ($minutes < 0)
{ {
unset($_COOKIE[$key]); unset($_COOKIE[$key]);
......
...@@ -48,14 +48,7 @@ class Crypt { ...@@ -48,14 +48,7 @@ class Crypt {
mt_srand(); mt_srand();
} }
// -----------------------------------------------------
// Create the input vector.
// -----------------------------------------------------
$iv = mcrypt_create_iv(static::iv_size(), $random); $iv = mcrypt_create_iv(static::iv_size(), $random);
// -----------------------------------------------------
// Encrypt the value using MCrypt.
// -----------------------------------------------------
$value = mcrypt_encrypt(static::$cipher, static::key(), $value, static::$mode, $iv); $value = mcrypt_encrypt(static::$cipher, static::key(), $value, static::$mode, $iv);
// ----------------------------------------------------- // -----------------------------------------------------
...@@ -72,14 +65,8 @@ class Crypt { ...@@ -72,14 +65,8 @@ class Crypt {
*/ */
public static function decrypt($value) public static function decrypt($value)
{ {
// -----------------------------------------------------
// Decode the base64 value.
// -----------------------------------------------------
$value = base64_decode($value, true); $value = base64_decode($value, true);
// -----------------------------------------------------
// Validate the base64 conversion.
// -----------------------------------------------------
if ( ! $value) if ( ! $value)
{ {
throw new \Exception('Decryption error. Input value is not valid base64 data.'); throw new \Exception('Decryption error. Input value is not valid base64 data.');
...@@ -95,9 +82,6 @@ class Crypt { ...@@ -95,9 +82,6 @@ class Crypt {
// ----------------------------------------------------- // -----------------------------------------------------
$value = substr($value, static::iv_size()); $value = substr($value, static::iv_size());
// -----------------------------------------------------
// Decrypt the value using MCrypt.
// -----------------------------------------------------
return rtrim(mcrypt_decrypt(static::$cipher, static::key(), $value, static::$mode, $iv), "\0"); return rtrim(mcrypt_decrypt(static::$cipher, static::key(), $value, static::$mode, $iv), "\0");
} }
...@@ -108,9 +92,6 @@ class Crypt { ...@@ -108,9 +92,6 @@ class Crypt {
*/ */
private static function key() private static function key()
{ {
// -----------------------------------------------------
// Validate the application key.
// -----------------------------------------------------
if (is_null($key = Config::get('application.key')) or $key == '') if (is_null($key = Config::get('application.key')) or $key == '')
{ {
throw new \Exception("The encryption class can not be used without an encryption key."); throw new \Exception("The encryption class can not be used without an encryption key.");
......
...@@ -17,9 +17,6 @@ class DB { ...@@ -17,9 +17,6 @@ class DB {
*/ */
public static function connection($connection = null) public static function connection($connection = null)
{ {
// ---------------------------------------------------
// If no connection was given, use the default.
// ---------------------------------------------------
if (is_null($connection)) if (is_null($connection))
{ {
$connection = Config::get('db.default'); $connection = Config::get('db.default');
...@@ -31,14 +28,8 @@ class DB { ...@@ -31,14 +28,8 @@ class DB {
// --------------------------------------------------- // ---------------------------------------------------
if ( ! array_key_exists($connection, static::$connections)) if ( ! array_key_exists($connection, static::$connections))
{ {
// ---------------------------------------------------
// Get the database configurations.
// ---------------------------------------------------
$config = Config::get('db.connections'); $config = Config::get('db.connections');
// ---------------------------------------------------
// Verify the connection has been defined.
// ---------------------------------------------------
if ( ! array_key_exists($connection, $config)) if ( ! array_key_exists($connection, $config))
{ {
throw new \Exception("Database connection [$connection] is not defined."); throw new \Exception("Database connection [$connection] is not defined.");
...@@ -63,14 +54,8 @@ class DB { ...@@ -63,14 +54,8 @@ class DB {
*/ */
public static function query($sql, $bindings = array(), $connection = null) public static function query($sql, $bindings = array(), $connection = null)
{ {
// ---------------------------------------------------
// Create a new PDO statement from the SQL.
// ---------------------------------------------------
$query = static::connection($connection)->prepare($sql); $query = static::connection($connection)->prepare($sql);
// ---------------------------------------------------
// Execute the query with the bindings.
// ---------------------------------------------------
$result = $query->execute($bindings); $result = $query->execute($bindings);
// --------------------------------------------------- // ---------------------------------------------------
......
...@@ -36,9 +36,6 @@ class Connector { ...@@ -36,9 +36,6 @@ class Connector {
{ {
$connection = new \PDO($config->driver.':host='.$config->host.';dbname='.$config->database, $config->username, $config->password, static::$options); $connection = new \PDO($config->driver.':host='.$config->host.';dbname='.$config->database, $config->username, $config->password, static::$options);
// ---------------------------------------------------
// Set the correct character set.
// ---------------------------------------------------
if (isset($config->charset)) if (isset($config->charset))
{ {
$connection->prepare("SET NAMES '".$config->charset."'")->execute(); $connection->prepare("SET NAMES '".$config->charset."'")->execute();
...@@ -46,9 +43,6 @@ class Connector { ...@@ -46,9 +43,6 @@ class Connector {
return $connection; return $connection;
} }
// ---------------------------------------------------
// If the driver isn't supported, bail out.
// ---------------------------------------------------
else else
{ {
throw new \Exception('Database driver '.$config->driver.' is not supported.'); throw new \Exception('Database driver '.$config->driver.' is not supported.');
......
...@@ -76,14 +76,7 @@ abstract class Eloquent { ...@@ -76,14 +76,7 @@ abstract class Eloquent {
*/ */
public static function with() public static function with()
{ {
// -----------------------------------------------------
// Create a new model instance.
// -----------------------------------------------------
$model = Eloquent\Factory::make(get_called_class()); $model = Eloquent\Factory::make(get_called_class());
// -----------------------------------------------------
// Set the eager relationships.
// -----------------------------------------------------
$model->includes = func_get_args(); $model->includes = func_get_args();
return $model; return $model;
...@@ -117,14 +110,8 @@ abstract class Eloquent { ...@@ -117,14 +110,8 @@ abstract class Eloquent {
*/ */
private function _first() private function _first()
{ {
// -----------------------------------------------------
// Load the hydrated models.
// -----------------------------------------------------
$results = Eloquent\Hydrate::from($this->take(1)); $results = Eloquent\Hydrate::from($this->take(1));
// -----------------------------------------------------
// Return the first result.
// -----------------------------------------------------
if (count($results) > 0) if (count($results) > 0)
{ {
reset($results); reset($results);
...@@ -185,11 +172,16 @@ abstract class Eloquent { ...@@ -185,11 +172,16 @@ abstract class Eloquent {
/** /**
* Save the model to the database. * Save the model to the database.
* *
* @return void * @return bool
*/ */
public function save() public function save()
{ {
Eloquent\Warehouse::store($this); if ($this->exists and count($this->dirty) == 0)
{
return true;
}
return Eloquent\Warehouse::store($this);
} }
/** /**
...@@ -215,17 +207,11 @@ abstract class Eloquent { ...@@ -215,17 +207,11 @@ abstract class Eloquent {
// ----------------------------------------------------- // -----------------------------------------------------
$model = $this->$key(); $model = $this->$key();
// -----------------------------------------------------
// Return the relationship results.
// -----------------------------------------------------
return ($this->relating == 'has_one' or $this->relating == 'belongs_to') return ($this->relating == 'has_one' or $this->relating == 'belongs_to')
? $this->ignore[$key] = $model->first() ? $this->ignore[$key] = $model->first()
: $this->ignore[$key] = $model->get(); : $this->ignore[$key] = $model->get();
} }
// -----------------------------------------------------
// Check the "regular" attributes.
// -----------------------------------------------------
return (array_key_exists($key, $this->attributes)) ? $this->attributes[$key] : null; return (array_key_exists($key, $this->attributes)) ? $this->attributes[$key] : null;
} }
...@@ -243,9 +229,6 @@ abstract class Eloquent { ...@@ -243,9 +229,6 @@ abstract class Eloquent {
} }
else else
{ {
// -----------------------------------------------------
// Add the value to the attributes.
// -----------------------------------------------------
$this->attributes[$key] = $value; $this->attributes[$key] = $value;
$this->dirty[$key] = $value; $this->dirty[$key] = $value;
} }
...@@ -274,17 +257,11 @@ abstract class Eloquent { ...@@ -274,17 +257,11 @@ abstract class Eloquent {
*/ */
public function __call($method, $parameters) public function __call($method, $parameters)
{ {
// -----------------------------------------------------
// Is the "get" method being called?
// -----------------------------------------------------
if ($method == 'get') if ($method == 'get')
{ {
return $this->_get(); return $this->_get();
} }
// -----------------------------------------------------
// Is the "first" method being called?
// -----------------------------------------------------
if ($method == 'first') if ($method == 'first')
{ {
return $this->_first(); return $this->_first();
...@@ -312,22 +289,13 @@ abstract class Eloquent { ...@@ -312,22 +289,13 @@ abstract class Eloquent {
*/ */
public static function __callStatic($method, $parameters) public static function __callStatic($method, $parameters)
{ {
// -----------------------------------------------------
// Create a new model instance.
// -----------------------------------------------------
$model = Eloquent\Factory::make(get_called_class()); $model = Eloquent\Factory::make(get_called_class());
// -----------------------------------------------------
// Do we need to return the entire table?
// -----------------------------------------------------
if ($method == 'get') if ($method == 'get')
{ {
return $model->_get(); return $model->_get();
} }
// -----------------------------------------------------
// Do we need to return the first model from the table?
// -----------------------------------------------------
if ($method == 'first') if ($method == 'first')
{ {
return $model->_first(); return $model->_first();
......
...@@ -10,13 +10,10 @@ class Factory { ...@@ -10,13 +10,10 @@ class Factory {
*/ */
public static function make($class) public static function make($class)
{ {
// -----------------------------------------------------
// Create a new model instance.
// -----------------------------------------------------
$model = new $class; $model = new $class;
// ----------------------------------------------------- // -----------------------------------------------------
// Set the active query instance on the model. // Set the fluent query builder on the model.
// ----------------------------------------------------- // -----------------------------------------------------
$model->query = \System\DB\Query::table(Meta::table($class)); $model->query = \System\DB\Query::table(Meta::table($class));
......
...@@ -22,17 +22,11 @@ class Hydrate { ...@@ -22,17 +22,11 @@ class Hydrate {
{ {
foreach ($eloquent->includes as $include) foreach ($eloquent->includes as $include)
{ {
// -----------------------------------------------------
// Verify the relationship is defined.
// -----------------------------------------------------
if ( ! method_exists($eloquent, $include)) if ( ! method_exists($eloquent, $include))
{ {
throw new \Exception("Attempting to eager load [$include], but the relationship is not defined."); throw new \Exception("Attempting to eager load [$include], but the relationship is not defined.");
} }
// -----------------------------------------------------
// Eagerly load the relationship.
// -----------------------------------------------------
static::eagerly($eloquent, $include, $results); static::eagerly($eloquent, $include, $results);
} }
} }
...@@ -49,34 +43,17 @@ class Hydrate { ...@@ -49,34 +43,17 @@ class Hydrate {
*/ */
private static function base($class, $models) private static function base($class, $models)
{ {
// -----------------------------------------------------
// Initialize the hydrated model array.
// -----------------------------------------------------
$results = array(); $results = array();
// -----------------------------------------------------
// Hydrate the models from the results.
// -----------------------------------------------------
foreach ($models as $model) foreach ($models as $model)
{ {
// -----------------------------------------------------
// Instantiate a new model instance.
// -----------------------------------------------------
$result = new $class; $result = new $class;
// -----------------------------------------------------
// Set the model's attributes.
// -----------------------------------------------------
$result->attributes = (array) $model; $result->attributes = (array) $model;
// -----------------------------------------------------
// Indicate that the model already exists.
// -----------------------------------------------------
$result->exists = true; $result->exists = true;
// ----------------------------------------------------- // -----------------------------------------------------
// Add the hydrated model to the array of models. // The results are keyed by the ID on the record.
// The array is keyed by the primary keys of the models.
// ----------------------------------------------------- // -----------------------------------------------------
$results[$result->id] = $result; $results[$result->id] = $result;
} }
...@@ -107,13 +84,9 @@ class Hydrate { ...@@ -107,13 +84,9 @@ class Hydrate {
unset($eloquent->attributes[$spoof]); unset($eloquent->attributes[$spoof]);
// ----------------------------------------------------- // -----------------------------------------------------
// Reset the WHERE clause on the query. // Reset the WHERE clause and bindings on the query.
// ----------------------------------------------------- // -----------------------------------------------------
$model->query->where = 'WHERE 1 = 1'; $model->query->where = 'WHERE 1 = 1';
// -----------------------------------------------------
// Reset the bindings on the query.
// -----------------------------------------------------
$model->query->bindings = array(); $model->query->bindings = array();
// ----------------------------------------------------- // -----------------------------------------------------
...@@ -124,23 +97,14 @@ class Hydrate { ...@@ -124,23 +97,14 @@ class Hydrate {
$result->ignore[$include] = (strpos($eloquent->relating, 'has_many') === 0) ? array() : null; $result->ignore[$include] = (strpos($eloquent->relating, 'has_many') === 0) ? array() : null;
} }
// -----------------------------------------------------
// Eagerly load a 1:1 or 1:* relationship.
// -----------------------------------------------------
if ($eloquent->relating == 'has_one' or $eloquent->relating == 'has_many') if ($eloquent->relating == 'has_one' or $eloquent->relating == 'has_many')
{ {
static::eagerly_load_one_or_many($eloquent->relating_key, $eloquent->relating, $include, $model, $results); static::eagerly_load_one_or_many($eloquent->relating_key, $eloquent->relating, $include, $model, $results);
} }
// -----------------------------------------------------
// Eagerly load a 1:1 (belonging) relationship.
// -----------------------------------------------------
elseif ($eloquent->relating == 'belongs_to') elseif ($eloquent->relating == 'belongs_to')
{ {
static::eagerly_load_belonging($eloquent->relating_key, $include, $model, $results); static::eagerly_load_belonging($eloquent->relating_key, $include, $model, $results);
} }
// -----------------------------------------------------
// Eagerly load a *:* relationship.
// -----------------------------------------------------
else else
{ {
static::eagerly_load_many_to_many($eloquent->relating_key, $eloquent->relating_table, strtolower(get_class($eloquent)).'_id', $include, $model, $results); static::eagerly_load_many_to_many($eloquent->relating_key, $eloquent->relating_table, strtolower(get_class($eloquent)).'_id', $include, $model, $results);
......
...@@ -11,14 +11,7 @@ class Relate { ...@@ -11,14 +11,7 @@ class Relate {
*/ */
public static function has_one($model, $eloquent) public static function has_one($model, $eloquent)
{ {
// -----------------------------------------------------
// Set the relating type.
// -----------------------------------------------------
$eloquent->relating = __FUNCTION__; $eloquent->relating = __FUNCTION__;
// -----------------------------------------------------
// Return the Eloquent model.
// -----------------------------------------------------
return static::has_one_or_many($model, $eloquent); return static::has_one_or_many($model, $eloquent);
} }
...@@ -31,14 +24,7 @@ class Relate { ...@@ -31,14 +24,7 @@ class Relate {
*/ */
public static function has_many($model, $eloquent) public static function has_many($model, $eloquent)
{ {
// -----------------------------------------------------
// Set the relating type.
// -----------------------------------------------------
$eloquent->relating = __FUNCTION__; $eloquent->relating = __FUNCTION__;
// -----------------------------------------------------
// Return the Eloquent model.
// -----------------------------------------------------
return static::has_one_or_many($model, $eloquent); return static::has_one_or_many($model, $eloquent);
} }
...@@ -51,11 +37,7 @@ class Relate { ...@@ -51,11 +37,7 @@ class Relate {
*/ */
private static function has_one_or_many($model, $eloquent) private static function has_one_or_many($model, $eloquent)
{ {
// -----------------------------------------------------
// Set the relating key.
// -----------------------------------------------------
$eloquent->relating_key = \System\Str::lower(get_class($eloquent)).'_id'; $eloquent->relating_key = \System\Str::lower(get_class($eloquent)).'_id';
return Factory::make($model)->where($eloquent->relating_key, '=', $eloquent->id); return Factory::make($model)->where($eloquent->relating_key, '=', $eloquent->id);
} }
...@@ -69,19 +51,9 @@ class Relate { ...@@ -69,19 +51,9 @@ class Relate {
*/ */
public static function belongs_to($caller, $model, $eloquent) public static function belongs_to($caller, $model, $eloquent)
{ {
// -----------------------------------------------------
// Set the relating type.
// -----------------------------------------------------
$eloquent->relating = __FUNCTION__; $eloquent->relating = __FUNCTION__;
// -----------------------------------------------------
// Set the relating key.
// -----------------------------------------------------
$eloquent->relating_key = $caller['function'].'_id'; $eloquent->relating_key = $caller['function'].'_id';
// -----------------------------------------------------
// Return the Eloquent model.
// -----------------------------------------------------
return Factory::make($model)->where('id', '=', $eloquent->attributes[$eloquent->relating_key]); return Factory::make($model)->where('id', '=', $eloquent->attributes[$eloquent->relating_key]);
} }
...@@ -98,31 +70,12 @@ class Relate { ...@@ -98,31 +70,12 @@ class Relate {
// Get the models involved in the relationship. // Get the models involved in the relationship.
// ----------------------------------------------------- // -----------------------------------------------------
$models = array(\System\Str::lower($model), \System\Str::lower(get_class($eloquent))); $models = array(\System\Str::lower($model), \System\Str::lower(get_class($eloquent)));
// -----------------------------------------------------
// Sort the model names involved in the relationship.
// -----------------------------------------------------
sort($models); sort($models);
// -----------------------------------------------------
// Get the intermediate table name, which is the names
// of the two related models alphabetized.
// -----------------------------------------------------
$eloquent->relating_table = implode('_', $models); $eloquent->relating_table = implode('_', $models);
// -----------------------------------------------------
// Set the relating type.
// -----------------------------------------------------
$eloquent->relating = __FUNCTION__; $eloquent->relating = __FUNCTION__;
// -----------------------------------------------------
// Set the relating key.
// -----------------------------------------------------
$eloquent->relating_key = $eloquent->relating_table.'.'.\System\Str::lower(get_class($eloquent)).'_id'; $eloquent->relating_key = $eloquent->relating_table.'.'.\System\Str::lower(get_class($eloquent)).'_id';
// -----------------------------------------------------
// Return the Eloquent model.
// -----------------------------------------------------
return Factory::make($model) return Factory::make($model)
->select(Meta::table($model).'.*') ->select(Meta::table($model).'.*')
->join($eloquent->relating_table, Meta::table($model).'.id', '=', $eloquent->relating_table.'.'.\System\Str::lower($model).'_id') ->join($eloquent->relating_table, Meta::table($model).'.id', '=', $eloquent->relating_table.'.'.\System\Str::lower($model).'_id')
......
...@@ -6,13 +6,10 @@ class Warehouse { ...@@ -6,13 +6,10 @@ class Warehouse {
* Save an Eloquent model to the database. * Save an Eloquent model to the database.
* *
* @param object $eloquent * @param object $eloquent
* @return void * @return bool
*/ */
public static function store($eloquent) public static function store($eloquent)
{ {
// -----------------------------------------------------
// Get the model name.
// -----------------------------------------------------
$model = get_class($eloquent); $model = get_class($eloquent);
// ----------------------------------------------------- // -----------------------------------------------------
...@@ -21,30 +18,25 @@ class Warehouse { ...@@ -21,30 +18,25 @@ class Warehouse {
$eloquent->query = \System\DB\Query::table(Meta::table($model)); $eloquent->query = \System\DB\Query::table(Meta::table($model));
// ----------------------------------------------------- // -----------------------------------------------------
// Set the activity timestamps. // Set the creation and update timestamps.
// ----------------------------------------------------- // -----------------------------------------------------
if (property_exists($model, 'timestamps') and $model::$timestamps) if (property_exists($model, 'timestamps') and $model::$timestamps)
{ {
static::timestamp($eloquent); static::timestamp($eloquent);
} }
// -----------------------------------------------------
// If the model exists in the database, update it.
// Otherwise, insert the model and set the ID.
// -----------------------------------------------------
if ($eloquent->exists) if ($eloquent->exists)
{ {
return $eloquent->query->where('id', '=', $eloquent->attributes['id'])->update($eloquent->dirty); return ($eloquent->query->where('id', '=', $eloquent->attributes['id'])->update($eloquent->dirty) == 1) ? true : false;
} }
else else
{ {
$eloquent->attributes['id'] = $eloquent->query->insert_get_id($eloquent->attributes); $eloquent->attributes['id'] = $eloquent->query->insert_get_id($eloquent->attributes);
} }
// -----------------------------------------------------
// Set the existence flag to true.
// -----------------------------------------------------
$eloquent->exists = true; $eloquent->exists = true;
return true;
} }
/** /**
......
...@@ -81,14 +81,7 @@ class Query { ...@@ -81,14 +81,7 @@ class Query {
*/ */
public function __construct($table, $connection = null) public function __construct($table, $connection = null)
{ {
// ---------------------------------------------------
// Set the database connection name.
// ---------------------------------------------------
$this->connection = (is_null($connection)) ? \System\Config::get('db.default') : $connection; $this->connection = (is_null($connection)) ? \System\Config::get('db.default') : $connection;
// ---------------------------------------------------
// Build the FROM clause.
// ---------------------------------------------------
$this->from = 'FROM '.$this->wrap($this->table = $table); $this->from = 'FROM '.$this->wrap($this->table = $table);
} }
...@@ -122,9 +115,6 @@ class Query { ...@@ -122,9 +115,6 @@ class Query {
*/ */
public function select() public function select()
{ {
// ---------------------------------------------------
// Handle DISTINCT selections.
// ---------------------------------------------------
$this->select = ($this->distinct) ? 'SELECT DISTINCT ' : 'SELECT '; $this->select = ($this->distinct) ? 'SELECT DISTINCT ' : 'SELECT ';
// --------------------------------------------------- // ---------------------------------------------------
...@@ -372,15 +362,7 @@ class Query { ...@@ -372,15 +362,7 @@ class Query {
*/ */
public function find($id) public function find($id)
{ {
// --------------------------------------------------- return $this->where('id', '=', $id)->first();
// Set the primary key.
// ---------------------------------------------------
$this->where('id', '=', $id);
// ---------------------------------------------------
// Get the first result.
// ---------------------------------------------------
return $this->first();
} }
/** /**
...@@ -400,9 +382,6 @@ class Query { ...@@ -400,9 +382,6 @@ class Query {
*/ */
public function get() public function get()
{ {
// ---------------------------------------------------
// Initialize the SELECT clause if it's null.
// ---------------------------------------------------
if (is_null($this->select)) if (is_null($this->select))
{ {
call_user_func_array(array($this, 'select'), (count(func_get_args()) > 0) ? func_get_args() : array('*')); call_user_func_array(array($this, 'select'), (count(func_get_args()) > 0) ? func_get_args() : array('*'));
...@@ -420,14 +399,8 @@ class Query { ...@@ -420,14 +399,8 @@ class Query {
*/ */
private function aggregate($aggregator, $column) private function aggregate($aggregator, $column)
{ {
// ---------------------------------------------------
// Build the SELECT clause.
// ---------------------------------------------------
$this->select = 'SELECT '.$aggregator.'('.$this->wrap($column).') AS '.$this->wrap('aggregate'); $this->select = 'SELECT '.$aggregator.'('.$this->wrap($column).') AS '.$this->wrap('aggregate');
// ---------------------------------------------------
// Execute the statement.
// ---------------------------------------------------
$results = \System\DB::query(Query\Compiler::select($this), $this->bindings); $results = \System\DB::query(Query\Compiler::select($this), $this->bindings);
return $results[0]->aggregate; return $results[0]->aggregate;
...@@ -452,54 +425,28 @@ class Query { ...@@ -452,54 +425,28 @@ class Query {
*/ */
public function insert_get_id($values) public function insert_get_id($values)
{ {
// ---------------------------------------------------
// Compile the SQL statement.
// ---------------------------------------------------
$sql = Query\Compiler::insert($this, $values); $sql = Query\Compiler::insert($this, $values);
// --------------------------------------------------- // ---------------------------------------------------
// The Postgres PDO implementation does not cleanly // Postgres.
// implement the last insert ID function. So, we'll
// use the RETURNING clause available in Postgres.
// --------------------------------------------------- // ---------------------------------------------------
if (\System\DB::connection($this->connection)->getAttribute(\PDO::ATTR_DRIVER_NAME) == 'pgsql') if (\System\DB::connection($this->connection)->getAttribute(\PDO::ATTR_DRIVER_NAME) == 'pgsql')
{ {
// ---------------------------------------------------
// Add the RETURNING clause to the SQL.
// ---------------------------------------------------
$sql .= ' RETURNING '.$this->wrap('id'); $sql .= ' RETURNING '.$this->wrap('id');
// ---------------------------------------------------
// Prepare the PDO statement.
// ---------------------------------------------------
$query = \System\DB::connection($this->connection)->prepare($sql); $query = \System\DB::connection($this->connection)->prepare($sql);
// ---------------------------------------------------
// Execute the PDO statement.
// ---------------------------------------------------
$query->execute(array_values($values)); $query->execute(array_values($values));
// ---------------------------------------------------
// Fetch the insert ID from the results.
// ---------------------------------------------------
$result = $query->fetch(\PDO::FETCH_ASSOC); $result = $query->fetch(\PDO::FETCH_ASSOC);
return $result['id']; return $result['id'];
} }
// --------------------------------------------------- // ---------------------------------------------------
// When using MySQL or SQLite, we can just use the PDO // MySQL and SQLite.
// last insert ID function.
// --------------------------------------------------- // ---------------------------------------------------
else else
{ {
// ---------------------------------------------------
// Execute the statement.
// ---------------------------------------------------
\System\DB::query($sql, array_values($values), $this->connection); \System\DB::query($sql, array_values($values), $this->connection);
// ---------------------------------------------------
// Get the last insert ID.
// ---------------------------------------------------
return \System\DB::connection($this->connection)->lastInsertId(); return \System\DB::connection($this->connection)->lastInsertId();
} }
} }
...@@ -523,17 +470,11 @@ class Query { ...@@ -523,17 +470,11 @@ class Query {
*/ */
public function delete($id = null) public function delete($id = null)
{ {
// ---------------------------------------------------
// Set the primary key.
// ---------------------------------------------------
if ( ! is_null($id)) if ( ! is_null($id))
{ {
$this->where('id', '=', $id); $this->where('id', '=', $id);
} }
// ---------------------------------------------------
// Execute the statement.
// ---------------------------------------------------
return \System\DB::query(Query\Compiler::delete($this), $this->bindings, $this->connection); return \System\DB::query(Query\Compiler::delete($this), $this->bindings, $this->connection);
} }
...@@ -555,9 +496,6 @@ class Query { ...@@ -555,9 +496,6 @@ class Query {
$wrap = '`'; $wrap = '`';
} }
// ---------------------------------------------------
// Wrap the element in keyword identifiers.
// ---------------------------------------------------
return implode('.', array_map(function($segment) use ($wrap) {return ($segment != '*') ? $wrap.$segment.$wrap : $segment;}, explode('.', $value))); return implode('.', array_map(function($segment) use ($wrap) {return ($segment != '*') ? $wrap.$segment.$wrap : $segment;}, explode('.', $value)));
} }
......
...@@ -10,30 +10,18 @@ class Compiler { ...@@ -10,30 +10,18 @@ class Compiler {
*/ */
public static function select($query) public static function select($query)
{ {
// ---------------------------------------------------
// Add the SELECT, FROM, and WHERE clauses.
// ---------------------------------------------------
$sql = $query->select.' '.$query->from.' '.$query->where; $sql = $query->select.' '.$query->from.' '.$query->where;
// ---------------------------------------------------
// Add the ORDER BY clause.
// ---------------------------------------------------
if (count($query->orderings) > 0) if (count($query->orderings) > 0)
{ {
$sql .= ' ORDER BY '.implode(', ', $query->orderings); $sql .= ' ORDER BY '.implode(', ', $query->orderings);
} }
// ---------------------------------------------------
// Add the LIMIT.
// ---------------------------------------------------
if ( ! is_null($query->limit)) if ( ! is_null($query->limit))
{ {
$sql .= ' LIMIT '.$query->limit; $sql .= ' LIMIT '.$query->limit;
} }
// ---------------------------------------------------
// Add the OFFSET.
// ---------------------------------------------------
if ( ! is_null($query->offset)) if ( ! is_null($query->offset))
{ {
$sql .= ' OFFSET '.$query->offset; $sql .= ' OFFSET '.$query->offset;
...@@ -51,9 +39,6 @@ class Compiler { ...@@ -51,9 +39,6 @@ class Compiler {
*/ */
public static function insert($query, $values) public static function insert($query, $values)
{ {
// ---------------------------------------------------
// Start the query. Add the table name.
// ---------------------------------------------------
$sql = 'INSERT INTO '.$query->table.' ('; $sql = 'INSERT INTO '.$query->table.' (';
// --------------------------------------------------- // ---------------------------------------------------
...@@ -66,9 +51,6 @@ class Compiler { ...@@ -66,9 +51,6 @@ class Compiler {
$columns[] = $query->wrap($column); $columns[] = $query->wrap($column);
} }
// ---------------------------------------------------
// Concatenate the column names and values.
// ---------------------------------------------------
return $sql .= implode(', ', $columns).') VALUES ('.$query->parameterize($values).')'; return $sql .= implode(', ', $columns).') VALUES ('.$query->parameterize($values).')';
} }
...@@ -81,13 +63,10 @@ class Compiler { ...@@ -81,13 +63,10 @@ class Compiler {
*/ */
public static function update($query, $values) public static function update($query, $values)
{ {
// ---------------------------------------------------
// Start the query. Add the table name.
// ---------------------------------------------------
$sql = 'UPDATE '.$query->table.' SET '; $sql = 'UPDATE '.$query->table.' SET ';
// --------------------------------------------------- // ---------------------------------------------------
// Wrap each column name in keyword identifiers. // Add each column set the query.
// --------------------------------------------------- // ---------------------------------------------------
$columns = array(); $columns = array();
...@@ -96,9 +75,6 @@ class Compiler { ...@@ -96,9 +75,6 @@ class Compiler {
$columns[] = $query->wrap($column).' = ?'; $columns[] = $query->wrap($column).' = ?';
} }
// ---------------------------------------------------
// Concatenate the column names and the WHERE clause.
// ---------------------------------------------------
return $sql .= implode(', ', $columns).' '.$query->where; return $sql .= implode(', ', $columns).' '.$query->where;
} }
......
...@@ -109,17 +109,11 @@ class Download { ...@@ -109,17 +109,11 @@ class Download {
*/ */
public static function file($path, $name = null) public static function file($path, $name = null)
{ {
// -------------------------------------------------
// If no name was specified, just use the basename.
// -------------------------------------------------
if (is_null($name)) if (is_null($name))
{ {
$name = basename($path); $name = basename($path);
} }
// -------------------------------------------------
// Set the headers to force the download to occur.
// -------------------------------------------------
return Response::make(file_get_contents($path))->header('Content-Description', 'File Transfer') return Response::make(file_get_contents($path))->header('Content-Description', 'File Transfer')
->header('Content-Type', static::mime(pathinfo($path, PATHINFO_EXTENSION))) ->header('Content-Type', static::mime(pathinfo($path, PATHINFO_EXTENSION)))
->header('Content-Disposition', 'attachment; filename="'.$name.'"') ->header('Content-Disposition', 'attachment; filename="'.$name.'"')
......
...@@ -72,9 +72,6 @@ class Error { ...@@ -72,9 +72,6 @@ class Error {
if (Config::get('error.detail')) if (Config::get('error.detail'))
{ {
// -----------------------------------------------------
// Build the error view.
// -----------------------------------------------------
$view = View::make('error/exception') $view = View::make('error/exception')
->bind('severity', $severity) ->bind('severity', $severity)
->bind('message', $message) ->bind('message', $message)
...@@ -83,16 +80,10 @@ class Error { ...@@ -83,16 +80,10 @@ class Error {
->bind('trace', $e->getTraceAsString()) ->bind('trace', $e->getTraceAsString())
->bind('contexts', static::context($file, $e->getLine())); ->bind('contexts', static::context($file, $e->getLine()));
// -----------------------------------------------------
// Send the detailed error response.
// -----------------------------------------------------
Response::make($view, 500)->send(); Response::make($view, 500)->send();
} }
else else
{ {
// -----------------------------------------------------
// Send the generic error response.
// -----------------------------------------------------
Response::make(View::make('error/500'), 500)->send(); Response::make(View::make('error/500'), 500)->send();
} }
...@@ -109,19 +100,10 @@ class Error { ...@@ -109,19 +100,10 @@ class Error {
*/ */
private static function context($path, $line, $padding = 5) private static function context($path, $line, $padding = 5)
{ {
// -----------------------------------------------------
// Verify that the file exists.
// -----------------------------------------------------
if (file_exists($path)) if (file_exists($path))
{ {
// -----------------------------------------------------
// Get the contents of the file.
// -----------------------------------------------------
$file = file($path, FILE_IGNORE_NEW_LINES); $file = file($path, FILE_IGNORE_NEW_LINES);
// -----------------------------------------------------
// Unshift the array.
// -----------------------------------------------------
array_unshift($file, ''); array_unshift($file, '');
// ----------------------------------------------------- // -----------------------------------------------------
...@@ -144,9 +126,6 @@ class Error { ...@@ -144,9 +126,6 @@ class Error {
$length = null; $length = null;
} }
// -----------------------------------------------------
// Return the context.
// -----------------------------------------------------
return array_slice($file, $start, $length, true); return array_slice($file, $start, $length, true);
} }
......
...@@ -18,9 +18,6 @@ class Filter { ...@@ -18,9 +18,6 @@ class Filter {
*/ */
public static function call($filters, $parameters = array()) public static function call($filters, $parameters = array())
{ {
// --------------------------------------------------------------
// Load the route filters.
// --------------------------------------------------------------
if (is_null(static::$filters)) if (is_null(static::$filters))
{ {
static::$filters = require APP_PATH.'filters'.EXT; static::$filters = require APP_PATH.'filters'.EXT;
...@@ -28,9 +25,6 @@ class Filter { ...@@ -28,9 +25,6 @@ class Filter {
foreach (explode(', ', $filters) as $filter) foreach (explode(', ', $filters) as $filter)
{ {
// --------------------------------------------------------------
// Verify that the filter is defined.
// --------------------------------------------------------------
if ( ! isset(static::$filters[$filter])) if ( ! isset(static::$filters[$filter]))
{ {
throw new \Exception("Route filter [$filter] is not defined."); throw new \Exception("Route filter [$filter] is not defined.");
...@@ -39,7 +33,8 @@ class Filter { ...@@ -39,7 +33,8 @@ class Filter {
$response = call_user_func_array(static::$filters[$filter], $parameters); $response = call_user_func_array(static::$filters[$filter], $parameters);
// -------------------------------------------------------------- // --------------------------------------------------------------
// If the filter returned a response, return it. // If the filter returned a response, return it since route
// filters can override route methods.
// -------------------------------------------------------------- // --------------------------------------------------------------
if ( ! is_null($response)) if ( ! is_null($response))
{ {
......
...@@ -20,32 +20,16 @@ class Form { ...@@ -20,32 +20,16 @@ class Form {
$action = Request::uri(); $action = Request::uri();
} }
// -------------------------------------------------------
// Prepare the action URL.
// -------------------------------------------------------
$action = URL::to($action); $action = URL::to($action);
// -------------------------------------------------------
// Set the action attribute.
// -------------------------------------------------------
$attributes['action'] = $action; $attributes['action'] = $action;
// -------------------------------------------------------
// Set the method attribute.
// -------------------------------------------------------
$attributes['method'] = ($method == 'GET' or $method == 'POST') ? $method : 'POST'; $attributes['method'] = ($method == 'GET' or $method == 'POST') ? $method : 'POST';
// -------------------------------------------------------
// Set the default character set.
// -------------------------------------------------------
if ( ! array_key_exists('accept-charset', $attributes)) if ( ! array_key_exists('accept-charset', $attributes))
{ {
$attributes['accept-charset'] = 'UTF-8'; $attributes['accept-charset'] = 'UTF-8';
} }
// -------------------------------------------------------
// Build the form tag.
// -------------------------------------------------------
$html = '<form'.HTML::attributes($attributes).'>'; $html = '<form'.HTML::attributes($attributes).'>';
// ------------------------------------------------------- // -------------------------------------------------------
...@@ -79,9 +63,6 @@ class Form { ...@@ -79,9 +63,6 @@ class Form {
*/ */
public static function raw_token() public static function raw_token()
{ {
// -------------------------------------------------------
// Verify that sessions are enabled.
// -------------------------------------------------------
if (Config::get('session.driver') == '') if (Config::get('session.driver') == '')
{ {
throw new \Exception('Sessions must be enabled to retrieve a CSRF token.'); throw new \Exception('Sessions must be enabled to retrieve a CSRF token.');
...@@ -204,9 +185,6 @@ class Form { ...@@ -204,9 +185,6 @@ class Form {
*/ */
private static function checkable($type, $name, $value, $checked, $attributes) private static function checkable($type, $name, $value, $checked, $attributes)
{ {
// -------------------------------------------------------
// Set the checked attribute.
// -------------------------------------------------------
if ($checked === true) if ($checked === true)
{ {
$attributes['checked'] = 'checked'; $attributes['checked'] = 'checked';
...@@ -225,9 +203,6 @@ class Form { ...@@ -225,9 +203,6 @@ class Form {
*/ */
public static function textarea($name, $value = '', $attributes = array()) public static function textarea($name, $value = '', $attributes = array())
{ {
// -------------------------------------------------------
// Add the name to the attributes.
// -------------------------------------------------------
$attributes['name'] = $name; $attributes['name'] = $name;
// ------------------------------------------------------- // -------------------------------------------------------
...@@ -260,36 +235,17 @@ class Form { ...@@ -260,36 +235,17 @@ class Form {
*/ */
public static function select($name, $options = array(), $selected = null, $attributes = array()) public static function select($name, $options = array(), $selected = null, $attributes = array())
{ {
// -------------------------------------------------------
// Set the name attribute.
// -------------------------------------------------------
$attributes['name'] = $name; $attributes['name'] = $name;
// -------------------------------------------------------
// Initialize the options array.
// -------------------------------------------------------
$html_options = array(); $html_options = array();
// -------------------------------------------------------
// Build the options in HTML.
// -------------------------------------------------------
foreach ($options as $value => $display) foreach ($options as $value => $display)
{ {
$option_attributes = array(); $option_attributes = array();
// -------------------------------------------------------
// Set the value attribute.
// -------------------------------------------------------
$option_attributes['value'] = $value; $option_attributes['value'] = $value;
// -------------------------------------------------------
// Set the selected attribute.
// -------------------------------------------------------
$option_attributes['selected'] = ($value == $selected) ? 'selected' : null; $option_attributes['selected'] = ($value == $selected) ? 'selected' : null;
// -------------------------------------------------------
// Add the option HTML to the array of options.
// -------------------------------------------------------
$html_options[] = '<option'.HTML::attributes($option_attributes).'>'.$display.'</option>'; $html_options[] = '<option'.HTML::attributes($option_attributes).'>'.$display.'</option>';
} }
...@@ -306,19 +262,8 @@ class Form { ...@@ -306,19 +262,8 @@ class Form {
*/ */
private static function input($type, $name, $value = null, $attributes = array()) private static function input($type, $name, $value = null, $attributes = array())
{ {
// -------------------------------------------------------
// Set the type attribute.
// -------------------------------------------------------
$attributes['type'] = $type; $attributes['type'] = $type;
// -------------------------------------------------------
// Set the name attribute.
// -------------------------------------------------------
$attributes['name'] = $name; $attributes['name'] = $name;
// -------------------------------------------------------
// Set the value attribute.
// -------------------------------------------------------
$attributes['value'] = $value; $attributes['value'] = $value;
return '<input'.HTML::attributes($attributes).' />'.PHP_EOL; return '<input'.HTML::attributes($attributes).' />'.PHP_EOL;
......
...@@ -25,14 +25,7 @@ class Hash { ...@@ -25,14 +25,7 @@ class Hash {
*/ */
public function __construct($value, $salt = null) public function __construct($value, $salt = null)
{ {
// --------------------------------------------------------------
// Get a random salt to hash the value with.
// --------------------------------------------------------------
$this->salt = (is_null($salt)) ? Str::random(16) : $salt; $this->salt = (is_null($salt)) ? Str::random(16) : $salt;
// --------------------------------------------------------------
// Perform a salted, SHA-1 hash on the value.
// --------------------------------------------------------------
$this->value = sha1($value.$this->salt); $this->value = sha1($value.$this->salt);
} }
......
...@@ -66,9 +66,6 @@ class HTML { ...@@ -66,9 +66,6 @@ class HTML {
// ------------------------------------------------------- // -------------------------------------------------------
$email = static::email($email); $email = static::email($email);
// -------------------------------------------------------
// If no title is specified, just use the e-mail address.
// -------------------------------------------------------
if (is_null($title)) if (is_null($title))
{ {
$title = $email; $title = $email;
...@@ -98,11 +95,7 @@ class HTML { ...@@ -98,11 +95,7 @@ class HTML {
*/ */
public static function image($url, $alt = '', $attributes = array()) public static function image($url, $alt = '', $attributes = array())
{ {
// -------------------------------------------------------
// Add the "alt" tag to the attributes.
// -------------------------------------------------------
$attributes['alt'] = Str::entities($alt); $attributes['alt'] = Str::entities($alt);
return '<img src="'.URL::to($url).'"'.static::attributes($attributes).' />'; return '<img src="'.URL::to($url).'"'.static::attributes($attributes).' />';
} }
...@@ -162,33 +155,19 @@ class HTML { ...@@ -162,33 +155,19 @@ class HTML {
*/ */
private static function list_elements($type, $list, $attributes) private static function list_elements($type, $list, $attributes)
{ {
// -------------------------------------------------------
// Verify the list is an array.
// -------------------------------------------------------
if ( ! is_array($list)) if ( ! is_array($list))
{ {
return ''; return '';
} }
// -------------------------------------------------------
// Initialize the output value.
// -------------------------------------------------------
$html = ''; $html = '';
// -------------------------------------------------------
// Add the list items.
// -------------------------------------------------------
foreach ($list as $key => $value) foreach ($list as $key => $value)
{ {
$html .= '<li>'.Str::entities($value).'</li>'; $html .= '<li>'.Str::entities($value).'</li>';
} }
// ------------------------------------------------------- return '<'.$type.static::attributes($attributes).'>'.$html.'</'.$type.'>';
// Build the list opening tag.
// -------------------------------------------------------
$start = '<'.$type.static::attributes($attributes).'>';
return $start.$html.'</'.$type.'>';
} }
/** /**
...@@ -203,23 +182,12 @@ class HTML { ...@@ -203,23 +182,12 @@ class HTML {
foreach ($attributes as $key => $value) foreach ($attributes as $key => $value)
{ {
// ------------------------------------------------------- if ( ! is_null($value))
// If the value is null, skip it.
// -------------------------------------------------------
if (is_null($value))
{ {
continue;
}
// -------------------------------------------------------
// Add the HTML attribute to the array of attributes.
// -------------------------------------------------------
$html[] = $key.'="'.Str::entities($value).'"'; $html[] = $key.'="'.Str::entities($value).'"';
} }
}
// -------------------------------------------------------
// Concatenate all of the attributes together.
// -------------------------------------------------------
if (count($html) > 0) if (count($html) > 0)
{ {
return ' '.implode(' ', $html); return ' '.implode(' ', $html);
......
...@@ -121,9 +121,6 @@ class Inflector { ...@@ -121,9 +121,6 @@ class Inflector {
*/ */
public static function plural($value) public static function plural($value)
{ {
// -----------------------------------------------------
// Have we already converted this word?
// -----------------------------------------------------
if (array_key_exists($value, static::$plural_cache)) if (array_key_exists($value, static::$plural_cache))
{ {
return static::$plural_cache[$value]; return static::$plural_cache[$value];
...@@ -172,9 +169,6 @@ class Inflector { ...@@ -172,9 +169,6 @@ class Inflector {
*/ */
public static function singular($value) public static function singular($value)
{ {
// -----------------------------------------------------
// Have we already converted this word?
// -----------------------------------------------------
if (array_key_exists($value, static::$singular_cache)) if (array_key_exists($value, static::$singular_cache))
{ {
return static::$singular_cache[$value]; return static::$singular_cache[$value];
......
...@@ -40,11 +40,7 @@ class Input { ...@@ -40,11 +40,7 @@ class Input {
*/ */
public static function get($key = null, $default = null) public static function get($key = null, $default = null)
{ {
// -------------------------------------------------
// Hydrate the input data for the request.
// -------------------------------------------------
static::hydrate(); static::hydrate();
return static::from_array(static::$input, $key, $default); return static::from_array(static::$input, $key, $default);
} }
...@@ -57,9 +53,6 @@ class Input { ...@@ -57,9 +53,6 @@ class Input {
*/ */
public static function old($key = null, $default = null) public static function old($key = null, $default = null)
{ {
// -------------------------------------------------
// Verify that sessions are enabled.
// -------------------------------------------------
if (Config::get('session.driver') == '') if (Config::get('session.driver') == '')
{ {
throw new \Exception("Sessions must be enabled to retrieve old input data."); throw new \Exception("Sessions must be enabled to retrieve old input data.");
...@@ -69,7 +62,7 @@ class Input { ...@@ -69,7 +62,7 @@ class Input {
} }
/** /**
* Get an item from an array. * Get an item from an array. If no key is specified, the entire array will be returned.
* *
* @param array $array * @param array $array
* @param string $key * @param string $key
...@@ -78,9 +71,6 @@ class Input { ...@@ -78,9 +71,6 @@ class Input {
*/ */
private static function from_array($array, $key, $default) private static function from_array($array, $key, $default)
{ {
// -------------------------------------------------
// If no key is given, return the entire array.
// -------------------------------------------------
if (is_null($key)) if (is_null($key))
{ {
return $array; return $array;
......
...@@ -60,22 +60,13 @@ class Lang { ...@@ -60,22 +60,13 @@ class Lang {
*/ */
public function get($language = null) public function get($language = null)
{ {
// --------------------------------------------------------------
// If no language was specified, use the default language.
// --------------------------------------------------------------
if (is_null($language)) if (is_null($language))
{ {
$language = Config::get('application.language'); $language = Config::get('application.language');
} }
// --------------------------------------------------------------
// Extract the file and item from the key.
// --------------------------------------------------------------
list($file, $line) = $this->parse($this->key); list($file, $line) = $this->parse($this->key);
// --------------------------------------------------------------
// Load the language file.
// --------------------------------------------------------------
$this->load($file, $language); $this->load($file, $language);
// -------------------------------------------------------------- // --------------------------------------------------------------
...@@ -109,22 +100,13 @@ class Lang { ...@@ -109,22 +100,13 @@ class Lang {
*/ */
private function parse($key) private function parse($key)
{ {
// ---------------------------------------------
// Get the key segments.
// ---------------------------------------------
$segments = explode('.', $key); $segments = explode('.', $key);
// ---------------------------------------------
// Validate the key format.
// ---------------------------------------------
if (count($segments) < 2) if (count($segments) < 2)
{ {
throw new \Exception("Invalid language key [$key]."); throw new \Exception("Invalid language key [$key].");
} }
// ---------------------------------------------
// Return the file and item name.
// ---------------------------------------------
return array($segments[0], implode('.', array_slice($segments, 1))); return array($segments[0], implode('.', array_slice($segments, 1)));
} }
...@@ -137,16 +119,13 @@ class Lang { ...@@ -137,16 +119,13 @@ class Lang {
*/ */
private function load($file, $language) private function load($file, $language)
{ {
// --------------------------------------------------------------
// Do not load the file if it has already been loaded.
// --------------------------------------------------------------
if (in_array($language.$file, static::$loaded)) if (in_array($language.$file, static::$loaded))
{ {
return; return;
} }
// -------------------------------------------------------------- // --------------------------------------------------------------
// Does the language file exist? // Load the language file into the array of lines.
// -------------------------------------------------------------- // --------------------------------------------------------------
if (file_exists($path = APP_PATH.'lang/'.$language.'/'.$file.EXT)) if (file_exists($path = APP_PATH.'lang/'.$language.'/'.$file.EXT))
{ {
...@@ -157,9 +136,6 @@ class Lang { ...@@ -157,9 +136,6 @@ class Lang {
throw new \Exception("Language file [$file] does not exist for language [$language]."); throw new \Exception("Language file [$file] does not exist for language [$language].");
} }
// --------------------------------------------------------------
// Add the file to the array of loaded files.
// --------------------------------------------------------------
static::$loaded[] = $language.$file; static::$loaded[] = $language.$file;
} }
......
...@@ -18,30 +18,18 @@ return function($class) { ...@@ -18,30 +18,18 @@ return function($class) {
return class_alias($aliases[$class], $class); return class_alias($aliases[$class], $class);
} }
// ----------------------------------------------------------
// Check for the class in the system directory.
// ----------------------------------------------------------
if (file_exists($path = BASE_PATH.$file.EXT)) if (file_exists($path = BASE_PATH.$file.EXT))
{ {
require $path; require $path;
} }
// ----------------------------------------------------------
// Check for the class in the models directory.
// ----------------------------------------------------------
elseif (file_exists($path = APP_PATH.'models/'.$file.EXT)) elseif (file_exists($path = APP_PATH.'models/'.$file.EXT))
{ {
require $path; require $path;
} }
// ----------------------------------------------------------
// Check for the class in the packages directory.
// ----------------------------------------------------------
elseif (file_exists($path = APP_PATH.'packages/'.$file.EXT)) elseif (file_exists($path = APP_PATH.'packages/'.$file.EXT))
{ {
require $path; require $path;
} }
// ----------------------------------------------------------
// Check for the class in the application directory.
// ----------------------------------------------------------
elseif (file_exists($path = APP_PATH.$file.EXT)) elseif (file_exists($path = APP_PATH.$file.EXT))
{ {
require $path; require $path;
......
...@@ -69,14 +69,7 @@ class Log { ...@@ -69,14 +69,7 @@ class Log {
// ----------------------------------------------------- // -----------------------------------------------------
$file = $directory.'/'.date('d').EXT; $file = $directory.'/'.date('d').EXT;
// -----------------------------------------------------
// Write the message to the log.
// -----------------------------------------------------
file_put_contents($file, date('Y-m-d H:i:s').' '.$type.' - '.$message.PHP_EOL, LOCK_EX | FILE_APPEND); file_put_contents($file, date('Y-m-d H:i:s').' '.$type.' - '.$message.PHP_EOL, LOCK_EX | FILE_APPEND);
// -----------------------------------------------------
// Set the log file permissions.
// -----------------------------------------------------
chmod($file, 0666); chmod($file, 0666);
} }
...@@ -88,14 +81,7 @@ class Log { ...@@ -88,14 +81,7 @@ class Log {
*/ */
private static function make_directory($directory) private static function make_directory($directory)
{ {
// -----------------------------------------------------
// Create the directory.
// -----------------------------------------------------
mkdir($directory, 02777); mkdir($directory, 02777);
// -----------------------------------------------------
// Set the directory permissions.
// -----------------------------------------------------
chmod($directory, 02777); chmod($directory, 02777);
} }
......
...@@ -18,17 +18,11 @@ class Memcached { ...@@ -18,17 +18,11 @@ class Memcached {
{ {
if (is_null(static::$instance)) if (is_null(static::$instance))
{ {
// -----------------------------------------------------
// Verify that the Memcache extension is installed.
// -----------------------------------------------------
if ( ! class_exists('Memcache')) if ( ! class_exists('Memcache'))
{ {
throw new \Exception('Attempting to use Memcached, but the Memcached PHP extension is not installed on this server.'); throw new \Exception('Attempting to use Memcached, but the Memcached PHP extension is not installed on this server.');
} }
// -----------------------------------------------------
// Instantiate the Memcache class.
// -----------------------------------------------------
$memcache = new \Memcache; $memcache = new \Memcache;
// ----------------------------------------------------- // -----------------------------------------------------
......
...@@ -13,14 +13,8 @@ class Redirect { ...@@ -13,14 +13,8 @@ class Redirect {
*/ */
public static function to($url, $method = 'location', $status = 302, $https = false) public static function to($url, $method = 'location', $status = 302, $https = false)
{ {
// -------------------------------------------------
// Prepare the URL.
// -------------------------------------------------
$url = URL::to($url, $https); $url = URL::to($url, $https);
// -------------------------------------------------
// Return the redirect response.
// -------------------------------------------------
return ($method == 'refresh') return ($method == 'refresh')
? Response::make('', $status)->header('Refresh', '0;url='.$url) ? Response::make('', $status)->header('Refresh', '0;url='.$url)
: Response::make('', $status)->header('Location', $url); : Response::make('', $status)->header('Location', $url);
......
...@@ -16,33 +16,21 @@ class Request { ...@@ -16,33 +16,21 @@ class Request {
*/ */
public static function uri() public static function uri()
{ {
// --------------------------------------------------------------
// Have we already determined the URI?
// --------------------------------------------------------------
if ( ! is_null(static::$uri)) if ( ! is_null(static::$uri))
{ {
return static::$uri; return static::$uri;
} }
// --------------------------------------------------------------
// Use the PATH_INFO variable if it is available.
// --------------------------------------------------------------
if (isset($_SERVER['PATH_INFO'])) if (isset($_SERVER['PATH_INFO']))
{ {
return static::$uri = static::tidy($_SERVER['PATH_INFO']); return static::$uri = static::tidy($_SERVER['PATH_INFO']);
} }
// --------------------------------------------------------------
// If the server REQUEST_URI variable is not available, bail out.
// --------------------------------------------------------------
if ( ! isset($_SERVER['REQUEST_URI'])) if ( ! isset($_SERVER['REQUEST_URI']))
{ {
throw new \Exception('Unable to determine the request URI.'); throw new \Exception('Unable to determine the request URI.');
} }
// --------------------------------------------------------------
// Get the PHP_URL_PATH of the request URI.
// --------------------------------------------------------------
$uri = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH); $uri = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
// -------------------------------------------------------------- // --------------------------------------------------------------
......
...@@ -120,9 +120,6 @@ class Response { ...@@ -120,9 +120,6 @@ class Response {
*/ */
public function send() public function send()
{ {
// -------------------------------------------------
// Set the content type if it has not been set.
// -------------------------------------------------
if ( ! array_key_exists('Content-Type', $this->headers)) if ( ! array_key_exists('Content-Type', $this->headers))
{ {
$this->header('Content-Type', 'text/html; charset=utf-8'); $this->header('Content-Type', 'text/html; charset=utf-8');
...@@ -133,16 +130,10 @@ class Response { ...@@ -133,16 +130,10 @@ class Response {
// ------------------------------------------------- // -------------------------------------------------
if ( ! headers_sent()) if ( ! headers_sent())
{ {
// -------------------------------------------------
// Send the HTTP protocol and status code.
// -------------------------------------------------
$protocol = (isset($_SERVER['SERVER_PROTOCOL'])) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.1'; $protocol = (isset($_SERVER['SERVER_PROTOCOL'])) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.1';
header($protocol.' '.$this->status.' '.$this->statuses[$this->status]); header($protocol.' '.$this->status.' '.$this->statuses[$this->status]);
// -------------------------------------------------
// Send the rest of the headers.
// -------------------------------------------------
foreach ($this->headers as $name => $value) foreach ($this->headers as $name => $value)
{ {
header($name.': '.$value, true); header($name.': '.$value, true);
......
...@@ -66,9 +66,6 @@ class Route { ...@@ -66,9 +66,6 @@ class Route {
} }
} }
// --------------------------------------------------------------
// Make sure the response is a Response instance.
// --------------------------------------------------------------
$response = ( ! $response instanceof Response) ? new Response($response) : $response; $response = ( ! $response instanceof Response) ? new Response($response) : $response;
// -------------------------------------------------------------- // --------------------------------------------------------------
......
...@@ -26,13 +26,10 @@ class Router { ...@@ -26,13 +26,10 @@ class Router {
public static function route($method, $uri) public static function route($method, $uri)
{ {
// -------------------------------------------------------------- // --------------------------------------------------------------
// Add a forward slash to the URI if necessary. // Force the URI to have a forward slash.
// -------------------------------------------------------------- // --------------------------------------------------------------
$uri = ($uri != '/') ? '/'.$uri : $uri; $uri = ($uri != '/') ? '/'.$uri : $uri;
// --------------------------------------------------------------
// Load all of the application routes.
// --------------------------------------------------------------
static::$routes = require APP_PATH.'routes'.EXT; static::$routes = require APP_PATH.'routes'.EXT;
// -------------------------------------------------------------- // --------------------------------------------------------------
...@@ -54,19 +51,10 @@ class Router { ...@@ -54,19 +51,10 @@ class Router {
// -------------------------------------------------------------- // --------------------------------------------------------------
if (strpos($keys, '(') !== false or strpos($keys, ',') !== false ) if (strpos($keys, '(') !== false or strpos($keys, ',') !== false )
{ {
// --------------------------------------------------------------
// Multiple routes can be assigned to a callback using commas.
// --------------------------------------------------------------
foreach (explode(', ', $keys) as $route) foreach (explode(', ', $keys) as $route)
{ {
// --------------------------------------------------------------
// Change wildcards into regular expressions.
// --------------------------------------------------------------
$route = str_replace(':num', '[0-9]+', str_replace(':any', '.+', $route)); $route = str_replace(':num', '[0-9]+', str_replace(':any', '.+', $route));
// --------------------------------------------------------------
// Test the route for a match.
// --------------------------------------------------------------
if (preg_match('#^'.$route.'$#', $method.' '.$uri)) if (preg_match('#^'.$route.'$#', $method.' '.$uri))
{ {
return new Route($callback, static::parameters(explode('/', $uri), explode('/', $route))); return new Route($callback, static::parameters(explode('/', $uri), explode('/', $route)));
...@@ -84,28 +72,14 @@ class Router { ...@@ -84,28 +72,14 @@ class Router {
*/ */
public static function find($name) public static function find($name)
{ {
// ----------------------------------------------------
// Have we already looked up this named route?
// ----------------------------------------------------
if (array_key_exists($name, static::$names)) if (array_key_exists($name, static::$names))
{ {
return static::$names[$name]; return static::$names[$name];
} }
// ----------------------------------------------------
// Instantiate the recursive array iterator.
// ----------------------------------------------------
$arrayIterator = new \RecursiveArrayIterator(static::$routes); $arrayIterator = new \RecursiveArrayIterator(static::$routes);
// ----------------------------------------------------
// Instantiate the recursive iterator iterator.
// ----------------------------------------------------
$recursiveIterator = new \RecursiveIteratorIterator($arrayIterator); $recursiveIterator = new \RecursiveIteratorIterator($arrayIterator);
// ----------------------------------------------------
// Iterate through the routes searching for a route
// name that matches the given name.
// ----------------------------------------------------
foreach ($recursiveIterator as $iterator) foreach ($recursiveIterator as $iterator)
{ {
$route = $recursiveIterator->getSubIterator(); $route = $recursiveIterator->getSubIterator();
...@@ -128,9 +102,6 @@ class Router { ...@@ -128,9 +102,6 @@ class Router {
{ {
$parameters = array(); $parameters = array();
// --------------------------------------------------------------
// Spin through the route segments looking for parameters.
// --------------------------------------------------------------
for ($i = 0; $i < count($route_segments); $i++) for ($i = 0; $i < count($route_segments); $i++)
{ {
// -------------------------------------------------------------- // --------------------------------------------------------------
......
...@@ -63,9 +63,6 @@ class Session { ...@@ -63,9 +63,6 @@ class Session {
static::put('csrf_token', Str::random(16)); static::put('csrf_token', Str::random(16));
} }
// -----------------------------------------------------
// Set the last activity timestamp for the user.
// -----------------------------------------------------
static::$session['last_activity'] = time(); static::$session['last_activity'] = time();
} }
...@@ -120,14 +117,8 @@ class Session { ...@@ -120,14 +117,8 @@ class Session {
*/ */
public static function once($key, $default = null) public static function once($key, $default = null)
{ {
// -----------------------------------------------------
// Get the item from the session.
// -----------------------------------------------------
$value = static::get($key, $default); $value = static::get($key, $default);
// -----------------------------------------------------
// Delete the item from the session.
// -----------------------------------------------------
static::forget($key); static::forget($key);
return $value; return $value;
...@@ -185,14 +176,7 @@ class Session { ...@@ -185,14 +176,7 @@ class Session {
*/ */
public static function regenerate() public static function regenerate()
{ {
// -----------------------------------------------------
// Delete the old session from storage.
// -----------------------------------------------------
static::driver()->delete(static::$session['id']); static::driver()->delete(static::$session['id']);
// -----------------------------------------------------
// Create a new session ID.
// -----------------------------------------------------
static::$session['id'] = Str::random(40); static::$session['id'] = Str::random(40);
} }
...@@ -203,31 +187,17 @@ class Session { ...@@ -203,31 +187,17 @@ class Session {
*/ */
public static function close() public static function close()
{ {
// -----------------------------------------------------
// Flash the old input data to the session.
// -----------------------------------------------------
static::flash('laravel_old_input', Input::get()); static::flash('laravel_old_input', Input::get());
// -----------------------------------------------------
// Age the session flash data.
// -----------------------------------------------------
static::age_flash(); static::age_flash();
// -----------------------------------------------------
// Save the session to storage.
// -----------------------------------------------------
static::driver()->save(static::$session); static::driver()->save(static::$session);
if ( ! headers_sent())
{
// ----------------------------------------------------- // -----------------------------------------------------
// Calculate the cookie lifetime. // Set the session cookie.
// ----------------------------------------------------- // -----------------------------------------------------
if ( ! headers_sent())
{
$lifetime = (Config::get('session.expire_on_close')) ? 0 : Config::get('session.lifetime'); $lifetime = (Config::get('session.expire_on_close')) ? 0 : Config::get('session.lifetime');
// -----------------------------------------------------
// Write the session cookie.
// -----------------------------------------------------
Cookie::put('laravel_session', static::$session['id'], $lifetime, Config::get('session.path'), Config::get('session.domain'), Config::get('session.https')); Cookie::put('laravel_session', static::$session['id'], $lifetime, Config::get('session.path'), Config::get('session.domain'), Config::get('session.https'));
} }
......
...@@ -10,14 +10,8 @@ class DB implements \System\Session\Driver { ...@@ -10,14 +10,8 @@ class DB implements \System\Session\Driver {
*/ */
public function load($id) public function load($id)
{ {
// -----------------------------------------------------
// Find the session in the database.
// -----------------------------------------------------
$session = $this->query()->find($id); $session = $this->query()->find($id);
// -----------------------------------------------------
// If the session was found, return it.
// -----------------------------------------------------
if ( ! is_null($session)) if ( ! is_null($session))
{ {
return array('id' => $session->id, 'last_activity' => $session->last_activity, 'data' => unserialize($session->data)); return array('id' => $session->id, 'last_activity' => $session->last_activity, 'data' => unserialize($session->data));
...@@ -32,14 +26,7 @@ class DB implements \System\Session\Driver { ...@@ -32,14 +26,7 @@ class DB implements \System\Session\Driver {
*/ */
public function save($session) public function save($session)
{ {
// -----------------------------------------------------
// Delete the existing session row.
// -----------------------------------------------------
$this->delete($session['id']); $this->delete($session['id']);
// -----------------------------------------------------
// Insert a new session row.
// -----------------------------------------------------
$this->query()->insert(array('id' => $session['id'], 'last_activity' => $session['last_activity'], 'data' => serialize($session['data']))); $this->query()->insert(array('id' => $session['id'], 'last_activity' => $session['last_activity'], 'data' => serialize($session['data'])));
} }
......
...@@ -10,9 +10,6 @@ class File implements \System\Session\Driver { ...@@ -10,9 +10,6 @@ class File implements \System\Session\Driver {
*/ */
public function load($id) public function load($id)
{ {
// -----------------------------------------------------
// Look for the session on the file system.
// -----------------------------------------------------
if (file_exists($path = APP_PATH.'sessions/'.$id)) if (file_exists($path = APP_PATH.'sessions/'.$id))
{ {
return unserialize(file_get_contents($path)); return unserialize(file_get_contents($path));
...@@ -51,9 +48,6 @@ class File implements \System\Session\Driver { ...@@ -51,9 +48,6 @@ class File implements \System\Session\Driver {
{ {
foreach (glob(APP_PATH.'sessions/*') as $file) foreach (glob(APP_PATH.'sessions/*') as $file)
{ {
// -----------------------------------------------------
// If the session file has expired, delete it.
// -----------------------------------------------------
if (filetype($file) == 'file' and filemtime($file) < $expiration) if (filetype($file) == 'file' and filemtime($file) < $expiration)
{ {
@unlink($file); @unlink($file);
......
...@@ -61,19 +61,10 @@ class Str { ...@@ -61,19 +61,10 @@ class Str {
*/ */
public static function random($length = 16) public static function random($length = 16)
{ {
// -----------------------------------------------------
// Split the character pool into an array.
// -----------------------------------------------------
$pool = str_split('0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', 1); $pool = str_split('0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', 1);
// -----------------------------------------------------
// Initialize the return value.
// -----------------------------------------------------
$value = ''; $value = '';
// -----------------------------------------------------
// Build the random string.
// -----------------------------------------------------
for ($i = 0; $i < $length; $i++) for ($i = 0; $i < $length; $i++)
{ {
$value .= $pool[mt_rand(0, 61)]; $value .= $pool[mt_rand(0, 61)];
......
<?php namespace System;
class Test {
/**
* All of the test results.
*
* @var array
*/
public static $results = array();
/**
* Total number of tests being run.
*
* @var int
*/
public static $total = 0;
/**
* Total number of passed tests.
*
* @var int
*/
public static $passed = 0;
/**
* Run a test suite.
*
* @param string $suite
* @param array $tests
* @return void
*/
public static function run($suite, $tests)
{
static::$total = static::$total + count($tests);
// -----------------------------------------------------
// Run each test in the suite.
// -----------------------------------------------------
foreach ($tests as $name => $test)
{
if ( ! is_callable($test))
{
throw new \Exception("Test [$name] in suite [$suite] is not callable.");
}
static::$passed = ($result = call_user_func($test)) ? static::$passed + 1 : static::$passed;
static::$results[$suite][] = array('name' => $name, 'result' => $result);
}
}
/**
* Get the test report view.
*
* @return View
*/
public static function report()
{
return View::make('test/report')
->bind('results', static::$results)
->bind('passed', static::$passed)
->bind('total', static::$total);
}
}
\ No newline at end of file
...@@ -12,9 +12,6 @@ class Text { ...@@ -12,9 +12,6 @@ class Text {
*/ */
public static function words($value, $limit, $end = '&#8230;') public static function words($value, $limit, $end = '&#8230;')
{ {
// -----------------------------------------------------
// If the value is an empty string, bail out.
// -----------------------------------------------------
if (trim($value) == '') if (trim($value) == '')
{ {
return $value; return $value;
...@@ -34,9 +31,6 @@ class Text { ...@@ -34,9 +31,6 @@ class Text {
$end = ''; $end = '';
} }
// -----------------------------------------------------
// Add the ending character to the string.
// -----------------------------------------------------
return rtrim($matches[0]).$end; return rtrim($matches[0]).$end;
} }
...@@ -50,9 +44,6 @@ class Text { ...@@ -50,9 +44,6 @@ class Text {
*/ */
public static function characters($value, $limit, $end = '&#8230;') public static function characters($value, $limit, $end = '&#8230;')
{ {
// -----------------------------------------------------
// If the value does not exceed the limit, bail out.
// -----------------------------------------------------
if (strlen($value) < $limit) if (strlen($value) < $limit)
{ {
return $value; return $value;
...@@ -63,17 +54,11 @@ class Text { ...@@ -63,17 +54,11 @@ class Text {
// ----------------------------------------------------- // -----------------------------------------------------
$value = preg_replace("/\s+/", ' ', str_replace(array("\r\n", "\r", "\n"), ' ', $value)); $value = preg_replace("/\s+/", ' ', str_replace(array("\r\n", "\r", "\n"), ' ', $value));
// -----------------------------------------------------
// If the value does not exceed the limit, bail out.
// -----------------------------------------------------
if (strlen($value) <= $limit) if (strlen($value) <= $limit)
{ {
return $value; return $value;
} }
// -----------------------------------------------------
// Initialize the output string.
// -----------------------------------------------------
$out = ''; $out = '';
// ----------------------------------------------------- // -----------------------------------------------------
...@@ -82,24 +67,12 @@ class Text { ...@@ -82,24 +67,12 @@ class Text {
// ----------------------------------------------------- // -----------------------------------------------------
foreach (explode(' ', trim($value)) as $val) foreach (explode(' ', trim($value)) as $val)
{ {
// -----------------------------------------------------
// Add the word to the output.
// -----------------------------------------------------
$out .= $val.' '; $out .= $val.' ';
// -----------------------------------------------------
// Check the output length.
// -----------------------------------------------------
if (strlen($out) >= $limit) if (strlen($out) >= $limit)
{ {
// -----------------------------------------------------
// Trim the output.
// -----------------------------------------------------
$out = trim($out); $out = trim($out);
// -----------------------------------------------------
// Add the ending character to the string.
// -----------------------------------------------------
return (strlen($out) == strlen($value)) ? $out : $out.$end; return (strlen($out) == strlen($value)) ? $out : $out.$end;
} }
} }
...@@ -115,9 +88,6 @@ class Text { ...@@ -115,9 +88,6 @@ class Text {
*/ */
public static function censor($value, $censored, $replacement = '####') public static function censor($value, $censored, $replacement = '####')
{ {
// -----------------------------------------------------
// Pad the value with spaces.
// -----------------------------------------------------
$value = ' '.$value.' '; $value = ' '.$value.' ';
// ----------------------------------------------------- // -----------------------------------------------------
......
...@@ -18,9 +18,6 @@ class URL { ...@@ -18,9 +18,6 @@ class URL {
return $url; return $url;
} }
// ----------------------------------------------------
// Get the base URL of the application.
// ----------------------------------------------------
$base = Config::get('application.url'); $base = Config::get('application.url');
// ---------------------------------------------------- // ----------------------------------------------------
...@@ -55,9 +52,6 @@ class URL { ...@@ -55,9 +52,6 @@ class URL {
*/ */
public static function to_route($name, $parameters = array(), $https = false) public static function to_route($name, $parameters = array(), $https = false)
{ {
// ----------------------------------------------------
// Does the named route exist?
// ----------------------------------------------------
if ( ! is_null($route = Router::find($name))) if ( ! is_null($route = Router::find($name)))
{ {
$uris = explode(', ', key($route)); $uris = explode(', ', key($route));
......
...@@ -41,10 +41,6 @@ class View { ...@@ -41,10 +41,6 @@ class View {
{ {
$this->view = $view; $this->view = $view;
$this->data = $data; $this->data = $data;
// -----------------------------------------------------
// Get the contents of the view from the file system.
// -----------------------------------------------------
$this->content = $this->load($view); $this->content = $this->load($view);
} }
...@@ -68,23 +64,14 @@ class View { ...@@ -68,23 +64,14 @@ class View {
*/ */
private function load($view) private function load($view)
{ {
// -----------------------------------------------------
// Is the view in the application directory?
// -----------------------------------------------------
if (file_exists($path = APP_PATH.'views/'.$view.EXT)) if (file_exists($path = APP_PATH.'views/'.$view.EXT))
{ {
return file_get_contents($path); return file_get_contents($path);
} }
// -----------------------------------------------------
// Is the view in the system directory?
// -----------------------------------------------------
elseif (file_exists($path = SYS_PATH.'views/'.$view.EXT)) elseif (file_exists($path = SYS_PATH.'views/'.$view.EXT))
{ {
return file_get_contents($path); return file_get_contents($path);
} }
// -----------------------------------------------------
// Could not locate the view... bail out.
// -----------------------------------------------------
else else
{ {
throw new \Exception("View [$view] doesn't exist."); throw new \Exception("View [$view] doesn't exist.");
...@@ -111,9 +98,6 @@ class View { ...@@ -111,9 +98,6 @@ class View {
*/ */
public function get() public function get()
{ {
// -----------------------------------------------------
// Set the name of the last rendered view.
// -----------------------------------------------------
static::$last = $this->view; static::$last = $this->view;
// ----------------------------------------------------- // -----------------------------------------------------
...@@ -127,24 +111,12 @@ class View { ...@@ -127,24 +111,12 @@ class View {
} }
} }
// -----------------------------------------------------
// Extract the view data into the local scope.
// -----------------------------------------------------
extract($this->data, EXTR_SKIP); extract($this->data, EXTR_SKIP);
// -----------------------------------------------------
// Start an output buffer to catch the content.
// -----------------------------------------------------
ob_start(); ob_start();
// -----------------------------------------------------
// Echo the content of the view.
// -----------------------------------------------------
echo eval('?>'.$this->content); echo eval('?>'.$this->content);
// -----------------------------------------------------
// Get the contents of the output buffer.
// -----------------------------------------------------
return ob_get_clean(); return ob_get_clean();
} }
......
...@@ -20,6 +20,7 @@ ...@@ -20,6 +20,7 @@
font-size: 45px; font-size: 45px;
color: #6d6d6d; color: #6d6d6d;
margin: 0 0 10px 0; margin: 0 0 10px 0;
text-shadow: 1px 1px #000;
} }
h3 { h3 {
......
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Laravel - Test Report</title>
<link href='http://fonts.googleapis.com/css?family=Ubuntu&amp;subset=latin' rel='stylesheet' type='text/css'>
<style type="text/css">
body {
background-color: #fff;
font-family: 'Ubuntu', sans-serif;
font-size: 18px;
color: #3f3f3f;
padding: 10px;
}
h1 {
font-size: 35px;
color: #6d6d6d;
margin: 0 0 10px 0;
text-shadow: 1px 1px #000;
}
h2 {
font-size: 25px;
color: #6d6d6d;
text-shadow: 1px 1px #000;
}
h3 {
font-size: 20px;
color: #6d6d6d;
text-shadow: 1px 1px #000;
}
#wrapper {
width: 100%;
}
div.content {
padding: 10px 10px 10px 10px;
background-color: #eee;
border-radius: 10px;
margin-bottom: 10px;
}
div.basic {
background-color: #eee;
}
div.passed {
background-color: #d8f5cf;
}
div.failed {
background-color: #ffebe8;
}
</style>
</head>
<body>
<div id="wrapper">
<h1>Test Report</h1>
<h2>Passed <?php echo $passed; ?> / <?php echo $total; ?> Tests</h2>
<?php foreach ($results as $suite => $results): ?>
<h3><?php echo $suite; ?></h3>
<?php foreach ($results as $result): ?>
<div class="content <?php echo ($result['result']) ? 'passed' : 'failed'; ?>">
<strong><?php echo ($result['result']) ? 'Passed' : 'Failed'; ?>:</strong> <?php echo $result['name']; ?>
</div>
<?php endforeach; ?>
<?php endforeach; ?>
</div>
</body>
</html>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment