refactoring.
This commit is contained in:
parent
ed3e3e73cc
commit
c6f9734603
|
@ -21,11 +21,7 @@ public static function parse($path)
|
||||||
*/
|
*/
|
||||||
public static function parse_string($value)
|
public static function parse_string($value)
|
||||||
{
|
{
|
||||||
$value = static::rewrite_echos($value);
|
return static::closings(static::openings(static::echos($value)));
|
||||||
$value = static::rewrite_openings($value);
|
|
||||||
$value = static::rewrite_closings($value);
|
|
||||||
|
|
||||||
return $value;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -34,7 +30,7 @@ public static function parse_string($value)
|
||||||
* @param string $value
|
* @param string $value
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
protected static function rewrite_echos($value)
|
protected static function echos($value)
|
||||||
{
|
{
|
||||||
return preg_replace('/\{\{(.+)\}\}/', '<?php echo $1; ?>', $value);
|
return preg_replace('/\{\{(.+)\}\}/', '<?php echo $1; ?>', $value);
|
||||||
}
|
}
|
||||||
|
@ -45,7 +41,7 @@ protected static function rewrite_echos($value)
|
||||||
* @param string $value
|
* @param string $value
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
protected static function rewrite_openings($value)
|
protected static function openings($value)
|
||||||
{
|
{
|
||||||
return preg_replace('/@(if|elseif|foreach|for|while)(\s*\(.*?\))\:/', '<?php $1$2: ?>', $value);
|
return preg_replace('/@(if|elseif|foreach|for|while)(\s*\(.*?\))\:/', '<?php $1$2: ?>', $value);
|
||||||
}
|
}
|
||||||
|
@ -56,7 +52,7 @@ protected static function rewrite_openings($value)
|
||||||
* @param string $value
|
* @param string $value
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
protected static function rewrite_closings($value)
|
protected static function closings($value)
|
||||||
{
|
{
|
||||||
$value = preg_replace('/(\s*)@(else)(.*?)\:/', '$1<?php $2$3: ?>', $value);
|
$value = preg_replace('/(\s*)@(else)(.*?)\:/', '$1<?php $2$3: ?>', $value);
|
||||||
$value = preg_replace('/(\s*)@(endif|endforeach|endfor|endwhile)(\s*)/', '$1<?php $2; ?> $3', $value);
|
$value = preg_replace('/(\s*)@(endif|endforeach|endfor|endwhile)(\s*)/', '$1<?php $2; ?> $3', $value);
|
||||||
|
|
|
@ -17,13 +17,15 @@
|
||||||
define('CONTROLLER_PATH', APP_PATH.'controllers/');
|
define('CONTROLLER_PATH', APP_PATH.'controllers/');
|
||||||
define('DATABASE_PATH', STORAGE_PATH.'database/');
|
define('DATABASE_PATH', STORAGE_PATH.'database/');
|
||||||
define('LANG_PATH', APP_PATH.'language/');
|
define('LANG_PATH', APP_PATH.'language/');
|
||||||
|
define('LIBRARY_PATH', APP_PATH.'libraries/');
|
||||||
|
define('MODEL_PATH', APP_PATH.'models/');
|
||||||
define('ROUTE_PATH', APP_PATH.'routes/');
|
define('ROUTE_PATH', APP_PATH.'routes/');
|
||||||
define('SESSION_PATH', STORAGE_PATH.'sessions/');
|
define('SESSION_PATH', STORAGE_PATH.'sessions/');
|
||||||
define('SYS_CONFIG_PATH', SYS_PATH.'config/');
|
define('SYS_CONFIG_PATH', SYS_PATH.'config/');
|
||||||
define('SYS_LANG_PATH', SYS_PATH.'language/');
|
define('SYS_LANG_PATH', SYS_PATH.'language/');
|
||||||
define('VIEW_PATH', APP_PATH.'views/');
|
define('VIEW_PATH', APP_PATH.'views/');
|
||||||
|
|
||||||
define('EXT', '.php');
|
define('EXT', '.php');
|
||||||
define('BLADE_EXT', '.blade.php');
|
define('BLADE_EXT', '.blade.php');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -35,24 +37,6 @@
|
||||||
require SYS_PATH.'loader'.EXT;
|
require SYS_PATH.'loader'.EXT;
|
||||||
require SYS_PATH.'arr'.EXT;
|
require SYS_PATH.'arr'.EXT;
|
||||||
|
|
||||||
/**
|
|
||||||
* Determine the application environment. The environment is typically set by an environment
|
|
||||||
* variable on the server, as this provides the most accident-proof method of handling
|
|
||||||
* application environments. However, the environment could be manually set by the developer
|
|
||||||
* in the front controller if access to the environment variables is not available.
|
|
||||||
* set by an environment variable on the server.
|
|
||||||
*/
|
|
||||||
$environment = (isset($_SERVER['LARAVEL_ENV'])) ? $_SERVER['LARAVEL_ENV'] : null;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Register the path to the configuration files.
|
|
||||||
*/
|
|
||||||
$configs = array(SYS_CONFIG_PATH, CONFIG_PATH);
|
|
||||||
|
|
||||||
if ( ! is_null($environment)) $configs[] = CONFIG_PATH.$environment.'/';
|
|
||||||
|
|
||||||
Config::$paths = $configs;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Bootstrap the application inversion of control (IoC) container. The container provides the
|
* Bootstrap the application inversion of control (IoC) container. The container provides the
|
||||||
* convenient resolution of objects and their dependencies, allowing for flexibility and
|
* convenient resolution of objects and their dependencies, allowing for flexibility and
|
||||||
|
@ -68,11 +52,7 @@
|
||||||
* Register the application auto-loader. The auto-loader is responsible for the lazy-loading
|
* Register the application auto-loader. The auto-loader is responsible for the lazy-loading
|
||||||
* of all of the Laravel core classes, as well as the developer created libraries and models.
|
* of all of the Laravel core classes, as well as the developer created libraries and models.
|
||||||
*/
|
*/
|
||||||
spl_autoload_register(array('Laravel\\Loader', 'load'));
|
spl_autoload_register(array($container->resolve('laravel.loader'), 'load'));
|
||||||
|
|
||||||
Loader::$paths = array(BASE_PATH, APP_PATH.'models/', APP_PATH.'libraries/', APP_PATH);
|
|
||||||
|
|
||||||
Loader::$aliases = Config::get('aliases');
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Define a few convenient global functions.
|
* Define a few convenient global functions.
|
||||||
|
|
|
@ -2,6 +2,13 @@
|
||||||
|
|
||||||
class Config {
|
class Config {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The paths to the configuration files.
|
||||||
|
*
|
||||||
|
* @var array
|
||||||
|
*/
|
||||||
|
public static $paths = array(SYS_CONFIG_PATH, CONFIG_PATH);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* All of the loaded configuration items.
|
* All of the loaded configuration items.
|
||||||
*
|
*
|
||||||
|
@ -11,13 +18,6 @@ class Config {
|
||||||
*/
|
*/
|
||||||
protected static $items = array();
|
protected static $items = array();
|
||||||
|
|
||||||
/**
|
|
||||||
* The paths to the configuration files.
|
|
||||||
*
|
|
||||||
* @var array
|
|
||||||
*/
|
|
||||||
public static $paths = array();
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Determine if a configuration item or file exists.
|
* Determine if a configuration item or file exists.
|
||||||
*
|
*
|
||||||
|
@ -122,8 +122,8 @@ protected static function load($file)
|
||||||
$config = array();
|
$config = array();
|
||||||
|
|
||||||
// Configuration files cascade. Typically, the system configuration array is loaded
|
// Configuration files cascade. Typically, the system configuration array is loaded
|
||||||
// first, followed by the application array, followed by the environment array.
|
// first, followed by the application array, providing the convenient cascading
|
||||||
// This allows the convenient overriding of configuration options.
|
// of configuration options from system to application.
|
||||||
foreach (static::$paths as $directory)
|
foreach (static::$paths as $directory)
|
||||||
{
|
{
|
||||||
if (file_exists($path = $directory.$file.EXT))
|
if (file_exists($path = $directory.$file.EXT))
|
||||||
|
|
|
@ -64,6 +64,12 @@
|
||||||
}),
|
}),
|
||||||
|
|
||||||
|
|
||||||
|
'laravel.loader' => array('singleton' => true, 'resolver' => function($c)
|
||||||
|
{
|
||||||
|
return new Loader(array(BASE_PATH, MODEL_PATH, LIBRARY_PATH, BASE_PATH), Config::get('aliases'));
|
||||||
|
}),
|
||||||
|
|
||||||
|
|
||||||
'laravel.request' => array('singleton' => true, 'resolver' => function($c)
|
'laravel.request' => array('singleton' => true, 'resolver' => function($c)
|
||||||
{
|
{
|
||||||
return new Request($c->resolve('laravel.uri'), $_SERVER, $_POST);
|
return new Request($c->resolve('laravel.uri'), $_SERVER, $_POST);
|
||||||
|
|
|
@ -130,6 +130,25 @@ public function instance($name, $instance)
|
||||||
$this->singletons[$name] = $instance;
|
$this->singletons[$name] = $instance;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Resolve a core Laravel class from the container.
|
||||||
|
*
|
||||||
|
* <code>
|
||||||
|
* // Resolve the "laravel.input" class from the container
|
||||||
|
* $input = IoC::container()->core('input');
|
||||||
|
*
|
||||||
|
* // Equivalent resolution using the "resolve" method
|
||||||
|
* $input = IoC::container()->resolve('laravel.input');
|
||||||
|
* </code>
|
||||||
|
*
|
||||||
|
* @param string $name
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
public function core($name)
|
||||||
|
{
|
||||||
|
return $this->resolve("laravel.{$name}");
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Resolve an object instance from the container.
|
* Resolve an object instance from the container.
|
||||||
*
|
*
|
||||||
|
|
|
@ -35,7 +35,7 @@ public function __get($key)
|
||||||
{
|
{
|
||||||
if (IoC::container()->registered("laravel.{$key}"))
|
if (IoC::container()->registered("laravel.{$key}"))
|
||||||
{
|
{
|
||||||
return IoC::container()->resolve("laravel.{$key}");
|
return IoC::container()->core($key);
|
||||||
}
|
}
|
||||||
elseif (IoC::container()->registered($key))
|
elseif (IoC::container()->registered($key))
|
||||||
{
|
{
|
||||||
|
|
|
@ -1,7 +1,4 @@
|
||||||
<?php namespace Laravel\Database;
|
<?php namespace Laravel\Database; use PDO, PDOStatement;
|
||||||
|
|
||||||
use PDO;
|
|
||||||
use PDOStatement;
|
|
||||||
|
|
||||||
class Connection {
|
class Connection {
|
||||||
|
|
||||||
|
@ -49,27 +46,43 @@ public function __construct(PDO $pdo, $config)
|
||||||
/**
|
/**
|
||||||
* Execute a SQL query against the connection and return a scalar result.
|
* Execute a SQL query against the connection and return a scalar result.
|
||||||
*
|
*
|
||||||
* @param string $sql
|
* <code>
|
||||||
* @param array $bindings
|
* // Get the total number of rows on a table
|
||||||
* @return int|float
|
* $count = DB::connection()->scalar('select count(*) from users');
|
||||||
|
*
|
||||||
|
* // Get the sum of payment amounts from a table
|
||||||
|
* $sum = DB::connection()->scalar('select sum(amount) from payments')
|
||||||
|
* </code>
|
||||||
|
*
|
||||||
|
* @param string $sql
|
||||||
|
* @param array $bindings
|
||||||
|
* @return float
|
||||||
*/
|
*/
|
||||||
public function scalar($sql, $bindings = array())
|
public function scalar($sql, $bindings = array())
|
||||||
{
|
{
|
||||||
$result = (array) $this->first($sql, $bindings);
|
$result = (array) $this->first($sql, $bindings);
|
||||||
|
|
||||||
return (strpos(strtolower(trim($sql)), 'select count') === 0) ? (int) reset($result) : (float) reset($result);
|
return (float) reset($result);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Execute a SQL query against the connection and return the first result.
|
* Execute a SQL query against the connection and return the first result.
|
||||||
*
|
*
|
||||||
|
* <code>
|
||||||
|
* // Execute a query against the database connection
|
||||||
|
* $user = DB::connection()->first('select * from users');
|
||||||
|
*
|
||||||
|
* // Execute a query with bound parameters
|
||||||
|
* $user = DB::connection()->first('select * from users where id = ?', array($id));
|
||||||
|
* </code>
|
||||||
|
*
|
||||||
* @param string $sql
|
* @param string $sql
|
||||||
* @param array $bindings
|
* @param array $bindings
|
||||||
* @return object
|
* @return object
|
||||||
*/
|
*/
|
||||||
public function first($sql, $bindings = array())
|
public function first($sql, $bindings = array())
|
||||||
{
|
{
|
||||||
return (count($results = $this->query($sql, $bindings)) > 0) ? $results[0] : null;
|
if (count($results = $this->query($sql, $bindings)) > 0) return $results[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -82,6 +95,14 @@ public function first($sql, $bindings = array())
|
||||||
* DELETE -> Number of Rows affected.
|
* DELETE -> Number of Rows affected.
|
||||||
* ELSE -> Boolean true / false depending on success.
|
* ELSE -> Boolean true / false depending on success.
|
||||||
*
|
*
|
||||||
|
* <code>
|
||||||
|
* // Execute a query against the database connection
|
||||||
|
* $users = DB::connection()->query('select * from users');
|
||||||
|
*
|
||||||
|
* // Execute a query with bound parameters
|
||||||
|
* $user = DB::connection()->query('select * from users where id = ?', array($id));
|
||||||
|
* </code>
|
||||||
|
*
|
||||||
* @param string $sql
|
* @param string $sql
|
||||||
* @param array $bindings
|
* @param array $bindings
|
||||||
* @return mixed
|
* @return mixed
|
||||||
|
|
|
@ -35,7 +35,7 @@ protected function options($config)
|
||||||
{
|
{
|
||||||
$options = (isset($config['options'])) ? $config['options'] : array();
|
$options = (isset($config['options'])) ? $config['options'] : array();
|
||||||
|
|
||||||
return array_merge($this->options, $options);
|
return $this->options + $options;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
|
@ -147,7 +147,7 @@ public static function query($class)
|
||||||
|
|
||||||
// Since this method is only used for instantiating models for querying
|
// Since this method is only used for instantiating models for querying
|
||||||
// purposes, we will go ahead and set the Query instance on the model.
|
// purposes, we will go ahead and set the Query instance on the model.
|
||||||
$model->query = IoC::resolve('laravel.database')->connection(static::$connection)->table(static::table($class));
|
$model->query = IoC::core('database')->connection(static::$connection)->table(static::table($class));
|
||||||
|
|
||||||
return $model;
|
return $model;
|
||||||
}
|
}
|
||||||
|
@ -361,7 +361,7 @@ public function save()
|
||||||
|
|
||||||
// Since the model was instantiated using "new", a query instance has not been set.
|
// Since the model was instantiated using "new", a query instance has not been set.
|
||||||
// Only models being used for querying have their query instances set by default.
|
// Only models being used for querying have their query instances set by default.
|
||||||
$this->query = IoC::resolve('laravel.database')->connection(static::$connection)->table(static::table($model));
|
$this->query = IoC::core('database')->connection(static::$connection)->table(static::table($model));
|
||||||
|
|
||||||
if (property_exists($model, 'timestamps') and $model::$timestamps)
|
if (property_exists($model, 'timestamps') and $model::$timestamps)
|
||||||
{
|
{
|
||||||
|
@ -410,7 +410,7 @@ public function delete($id = null)
|
||||||
// delete statement to the query instance.
|
// delete statement to the query instance.
|
||||||
if ( ! $this->exists) return $this->query->delete();
|
if ( ! $this->exists) return $this->query->delete();
|
||||||
|
|
||||||
return IoC::resolve('laravel.database')->connection(static::$connection)->table(static::table(get_class($this)))->delete($this->id);
|
return IoC::core('database')->connection(static::$connection)->table(static::table(get_class($this)))->delete($this->id);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -1,81 +1,94 @@
|
||||||
<?php namespace Laravel\Database\Grammars;
|
<?php namespace Laravel\Database\Grammars; use Laravel\Arr, Laravel\Database\Query;
|
||||||
|
|
||||||
use Laravel\Database\Query;
|
|
||||||
|
|
||||||
class Grammar {
|
class Grammar {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Compile a SQL SELECT statment from a Query instance.
|
* All of the query componenets in the order they should be built.
|
||||||
|
*
|
||||||
|
* @var array
|
||||||
|
*/
|
||||||
|
protected $components = array('selects', 'from', 'joins', 'wheres', 'orderings', 'limit', 'offset');
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The keyword identifier for the database system.
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $wrapper = '"';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Compile a SQL SELECT statement from a Query instance.
|
||||||
|
*
|
||||||
|
* The query will be compiled according to the order of the elements specified
|
||||||
|
* in the "components" property. The entire query is pased into each component
|
||||||
|
* compiler for convenience.
|
||||||
*
|
*
|
||||||
* @param Query $query
|
* @param Query $query
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function select(Query $query)
|
public function select(Query $query)
|
||||||
{
|
{
|
||||||
if ( ! is_null($query->aggregate))
|
$sql = array();
|
||||||
|
|
||||||
|
// Iterate through each query component, calling the compiler for that
|
||||||
|
// component, and passing the query instance into the compiler.
|
||||||
|
foreach ($this->components as $component)
|
||||||
{
|
{
|
||||||
$sql[] = $this->compile_aggregate($query->aggregate['aggregator'], $query->aggregate['column']);
|
if ( ! is_null($query->$component)) $sql[] = call_user_func(array($this, $component), $query);
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
$sql[] = $this->compile_select($query);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$sql[] = $this->compile_from($query->table);
|
return implode(' ', Arr::without($sql, array(null, '')));
|
||||||
|
|
||||||
foreach (array('joins', 'wheres', 'orderings', 'limit', 'offset') as $clause)
|
|
||||||
{
|
|
||||||
if ( ! is_null($query->$clause)) $sql[] = call_user_func(array($this, 'compile_'.$clause), $query->$clause);
|
|
||||||
}
|
|
||||||
|
|
||||||
return implode(' ', array_filter($sql, function($value) { return ! is_null($value) and (string) $value !== ''; }));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Compile the query SELECT clause.
|
* Compile the SELECT clause for a query.
|
||||||
*
|
*
|
||||||
* @param Query $query
|
* @param Query $query
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
protected function compile_select(Query $query)
|
protected function selects(Query $query)
|
||||||
{
|
{
|
||||||
return (($query->distinct) ? 'SELECT DISTINCT ' : 'SELECT ').implode(', ', array_map(array($this, 'wrap'), $query->select));
|
if ( ! is_null($query->aggregate)) return $this->aggregate($query);
|
||||||
|
|
||||||
|
return (($query->distinct) ? 'SELECT DISTINCT ' : 'SELECT ').$this->columnize($query->selects);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Compile the query SELECT clause with an aggregate function.
|
* Compile an aggregating SELECT clause for a query.
|
||||||
*
|
*
|
||||||
* @param string $aggregator
|
* This method compiled the SELECT clauses for queries built using the
|
||||||
* @param string $column
|
* count, max, min, abs, and sum methods on the fluent query builder.
|
||||||
|
*
|
||||||
|
* @param Query $query
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
protected function compile_aggregate($aggregator, $column)
|
protected function aggregate(Query $query)
|
||||||
{
|
{
|
||||||
return 'SELECT '.$aggregator.'('.$this->wrap($column).') AS '.$this->wrap('aggregate');
|
list($aggregator, $column) = array($query->aggregate['aggregator'], $query->aggregate['column']);
|
||||||
|
|
||||||
|
return 'SELECT '.$aggregator.'('.$this->wrap($column).')';
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Compile the query FROM clause.
|
* Compile the FROM clause for a query.
|
||||||
*
|
*
|
||||||
* Note: This method does not compile any JOIN clauses. Joins are compiled by the compile_joins method.
|
* @param Query $query
|
||||||
*
|
|
||||||
* @param string $table
|
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
protected function compile_from($table)
|
protected function from(Query $query)
|
||||||
{
|
{
|
||||||
return 'FROM '.$this->wrap($table);
|
return 'FROM '.$this->wrap($query->from);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Compile the query JOIN clauses.
|
* Compile the JOIN clauses for a query.
|
||||||
*
|
*
|
||||||
* @param array $joins
|
* @param Query $query
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
protected function compile_joins($joins)
|
protected function joins(Query $query)
|
||||||
{
|
{
|
||||||
foreach ($joins as $join)
|
foreach ($query->joins as $join)
|
||||||
{
|
{
|
||||||
extract($join);
|
extract($join);
|
||||||
|
|
||||||
|
@ -86,30 +99,36 @@ protected function compile_joins($joins)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Compile the query WHERE clauses.
|
* Compile the WHERE clause for a query.
|
||||||
*
|
*
|
||||||
* @param array $wheres
|
* @param Query $query
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
protected function compile_wheres($wheres)
|
protected function wheres(Query $query)
|
||||||
{
|
{
|
||||||
$sql = array('WHERE 1 = 1');
|
// Each WHERE clause array has a "type" that is assigned by the query builder, and
|
||||||
|
// each type has its own compiler function. For example, "where in" queries are
|
||||||
|
// compiled by the "where_in" function.
|
||||||
|
//
|
||||||
|
// The only exception to this rule are "raw" where clauses, which are simply
|
||||||
|
// appended to the query as-is, without any further compiling.
|
||||||
foreach ($wheres as $where)
|
foreach ($wheres as $where)
|
||||||
{
|
{
|
||||||
$sql[] = (is_string($where)) ? $where : $where['connector'].' '.$this->{'compile_'.$where['type']}($where);
|
$sql[] = ($where['type'] == 'raw') ? $where['sql'] : $where['connector'].' '.$this->{$where['type']}($where);
|
||||||
}
|
}
|
||||||
|
|
||||||
return implode(' ', $sql);
|
return implode(' ', array_merge(array('WHERE 1 = 1'), $sql));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Compile a simple WHERE clause.
|
* Compile a simple WHERE clause.
|
||||||
*
|
*
|
||||||
|
* This method compiles the SQL for the "where" and "or_where" query functions.
|
||||||
|
*
|
||||||
* @param array $where
|
* @param array $where
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
protected function compile_where($where)
|
protected function where($where)
|
||||||
{
|
{
|
||||||
return $this->wrap($where['column']).' '.$where['operator'].' ?';
|
return $this->wrap($where['column']).' '.$where['operator'].' ?';
|
||||||
}
|
}
|
||||||
|
@ -117,10 +136,12 @@ protected function compile_where($where)
|
||||||
/**
|
/**
|
||||||
* Compile a WHERE IN clause.
|
* Compile a WHERE IN clause.
|
||||||
*
|
*
|
||||||
|
* This method compiled the SQL for all of the "where_in" style query functions.
|
||||||
|
*
|
||||||
* @param array $where
|
* @param array $where
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
protected function compile_where_in($where)
|
protected function where_in($where)
|
||||||
{
|
{
|
||||||
$operator = ($where['not']) ? 'NOT IN' : 'IN';
|
$operator = ($where['not']) ? 'NOT IN' : 'IN';
|
||||||
|
|
||||||
|
@ -130,10 +151,12 @@ protected function compile_where_in($where)
|
||||||
/**
|
/**
|
||||||
* Compile a WHERE NULL clause.
|
* Compile a WHERE NULL clause.
|
||||||
*
|
*
|
||||||
|
* This method compiles the SQL for all of the "where_null" style query functions.
|
||||||
|
*
|
||||||
* @param array $where
|
* @param array $where
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
protected function compile_where_null($where)
|
protected function where_null($where)
|
||||||
{
|
{
|
||||||
$operator = ($where['not']) ? 'NOT NULL' : 'NULL';
|
$operator = ($where['not']) ? 'NOT NULL' : 'NULL';
|
||||||
|
|
||||||
|
@ -141,14 +164,14 @@ protected function compile_where_null($where)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Compile the query ORDER BY clause.
|
* Compile ORDER BY clause for a query.
|
||||||
*
|
*
|
||||||
* @param array $orderings
|
* @param Query $query
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
protected function compile_orderings($orderings)
|
protected function orderings(Query $query)
|
||||||
{
|
{
|
||||||
foreach ($orderings as $ordering)
|
foreach ($query->orderings as $ordering)
|
||||||
{
|
{
|
||||||
$sql[] = $this->wrap($ordering['column']).' '.strtoupper($ordering['direction']);
|
$sql[] = $this->wrap($ordering['column']).' '.strtoupper($ordering['direction']);
|
||||||
}
|
}
|
||||||
|
@ -157,25 +180,25 @@ protected function compile_orderings($orderings)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Compile the query LIMIT.
|
* Compile the LIMIT clause for a query.
|
||||||
*
|
*
|
||||||
* @param int $limit
|
* @param Query $query
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
protected function compile_limit($limit)
|
protected function limit(Query $query)
|
||||||
{
|
{
|
||||||
return 'LIMIT '.$limit;
|
return 'LIMIT '.$query->limit;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Compile the query OFFSET.
|
* Compile the OFFSET clause for a query.
|
||||||
*
|
*
|
||||||
* @param int $offset
|
* @param Query $query
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
protected function compile_offset($offset)
|
protected function offset(Query $query)
|
||||||
{
|
{
|
||||||
return 'OFFSET '.$offset;
|
return 'OFFSET '.$query->offset;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -187,21 +210,9 @@ protected function compile_offset($offset)
|
||||||
*/
|
*/
|
||||||
public function insert(Query $query, $values)
|
public function insert(Query $query, $values)
|
||||||
{
|
{
|
||||||
$columns = array_map(array($this, 'wrap'), array_keys($values));
|
$columns = implode(', ', $this->columnize(array_keys($values)));
|
||||||
|
|
||||||
return 'INSERT INTO '.$this->wrap($query->table).' ('.implode(', ', $columns).') VALUES ('.$this->parameterize($values).')';
|
return 'INSERT INTO '.$this->wrap($query->from).' ('.$columns.') VALUES ('.$this->parameterize($values).')';
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Compile a SQL INSERT statment that returns an auto-incrementing ID from a Query instance.
|
|
||||||
*
|
|
||||||
* @param Query $query
|
|
||||||
* @param array $values
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
public function insert_get_id(Query $query, $values)
|
|
||||||
{
|
|
||||||
return $this->insert($query, $values);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -213,11 +224,14 @@ public function insert_get_id(Query $query, $values)
|
||||||
*/
|
*/
|
||||||
public function update(Query $query, $values)
|
public function update(Query $query, $values)
|
||||||
{
|
{
|
||||||
foreach (array_keys($values) as $column) { $sets[] = $this->wrap($column).' = ?'; }
|
foreach (array_keys($values) as $column)
|
||||||
|
{
|
||||||
|
$sets[] = $this->wrap($column).' = ?';
|
||||||
|
}
|
||||||
|
|
||||||
$sql = 'UPDATE '.$this->wrap($query->table).' SET '.implode(', ', $sets);
|
$sql = 'UPDATE '.$this->wrap($query->from).' SET '.implode(', ', $sets);
|
||||||
|
|
||||||
return (count($query->wheres) > 0) ? $sql.' '.$this->compile_wheres($query->wheres) : $sql;
|
return (count($query->wheres) > 0) ? $sql.' '.$this->wheres($query->wheres) : $sql;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -228,15 +242,26 @@ public function update(Query $query, $values)
|
||||||
*/
|
*/
|
||||||
public function delete(Query $query)
|
public function delete(Query $query)
|
||||||
{
|
{
|
||||||
$sql = 'DELETE FROM '.$this->wrap($query->table);
|
$sql = 'DELETE FROM '.$this->wrap($query->from);
|
||||||
|
|
||||||
return (count($query->wheres) > 0) ? $sql.' '.$this->compile_wheres($query->wheres) : $sql;
|
return (count($query->wheres) > 0) ? $sql.' '.$this->wheres($query->wheres) : $sql;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a comma-delimited list of wrapped column names.
|
||||||
|
*
|
||||||
|
* @param array $columns
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
protected function columnize($columns)
|
||||||
|
{
|
||||||
|
return implode(', ', array_map(array($this, 'wrap'), $columns));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Wrap a value in keyword identifiers.
|
* Wrap a value in keyword identifiers.
|
||||||
*
|
*
|
||||||
* @param string $value
|
* @param string $value
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function wrap($value)
|
public function wrap($value)
|
||||||
|
@ -245,7 +270,7 @@ public function wrap($value)
|
||||||
|
|
||||||
foreach (explode('.', $value) as $segment)
|
foreach (explode('.', $value) as $segment)
|
||||||
{
|
{
|
||||||
$wrapped[] = ($segment != '*') ? $this->wrapper().$segment.$this->wrapper() : $segment;
|
$wrapped[] = ($segment != '*') ? $this->wrapper.$segment.$this->wrapper : $segment;
|
||||||
}
|
}
|
||||||
|
|
||||||
return implode('.', $wrapped);
|
return implode('.', $wrapped);
|
||||||
|
@ -254,7 +279,7 @@ public function wrap($value)
|
||||||
/**
|
/**
|
||||||
* Wrap an alias in keyword identifiers.
|
* Wrap an alias in keyword identifiers.
|
||||||
*
|
*
|
||||||
* @param string $value
|
* @param string $value
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function wrap_alias($value)
|
public function wrap_alias($value)
|
||||||
|
@ -264,19 +289,15 @@ public function wrap_alias($value)
|
||||||
return $this->wrap($segments[0]).' AS '.$this->wrap($segments[2]);
|
return $this->wrap($segments[0]).' AS '.$this->wrap($segments[2]);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the keyword identifier wrapper for the connection.
|
|
||||||
*
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
public function wrapper() { return '"'; }
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create query parameters from an array of values.
|
* Create query parameters from an array of values.
|
||||||
*
|
*
|
||||||
* @param array $values
|
* @param array $values
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function parameterize($values) { return implode(', ', array_fill(0, count($values), '?')); }
|
public function parameterize($values)
|
||||||
|
{
|
||||||
|
return implode(', ', array_fill(0, count($values), '?'));
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
|
@ -3,10 +3,10 @@
|
||||||
class MySQL extends Grammar {
|
class MySQL extends Grammar {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the keyword identifier wrapper for the connection.
|
* The keyword identifier for the database system.
|
||||||
*
|
*
|
||||||
* @return string
|
* @var string
|
||||||
*/
|
*/
|
||||||
public function wrapper() { return '`'; }
|
protected $wrapper = '`';
|
||||||
|
|
||||||
}
|
}
|
|
@ -18,6 +18,14 @@ class Manager {
|
||||||
*
|
*
|
||||||
* Note: Database connections are managed as singletons.
|
* Note: Database connections are managed as singletons.
|
||||||
*
|
*
|
||||||
|
* <code>
|
||||||
|
* // Get the default database connection for the application
|
||||||
|
* $connection = DB::connection();
|
||||||
|
*
|
||||||
|
* // Get a specific connection by passing the connection name
|
||||||
|
* $connection = DB::connection('mysql');
|
||||||
|
* </code>
|
||||||
|
*
|
||||||
* @param string $connection
|
* @param string $connection
|
||||||
* @return Connection
|
* @return Connection
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -21,7 +21,7 @@ class Query {
|
||||||
*
|
*
|
||||||
* @var array
|
* @var array
|
||||||
*/
|
*/
|
||||||
public $select;
|
public $selects;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* If the query is performing an aggregate function, this will contain the column
|
* If the query is performing an aggregate function, this will contain the column
|
||||||
|
@ -43,7 +43,7 @@ class Query {
|
||||||
*
|
*
|
||||||
* @var string
|
* @var string
|
||||||
*/
|
*/
|
||||||
public $table;
|
public $from;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The table joins.
|
* The table joins.
|
||||||
|
@ -90,14 +90,14 @@ class Query {
|
||||||
/**
|
/**
|
||||||
* Create a new query instance.
|
* Create a new query instance.
|
||||||
*
|
*
|
||||||
* @param Connection $connection
|
* @param Connection $connection
|
||||||
* @param Grammars\Grammar $grammar
|
* @param Grammars\Grammar $grammar
|
||||||
* @param string $table
|
* @param string $table
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
public function __construct(Connection $connection, Grammars\Grammar $grammar, $table)
|
public function __construct(Connection $connection, Grammars\Grammar $grammar, $table)
|
||||||
{
|
{
|
||||||
$this->table = $table;
|
$this->from = $table;
|
||||||
$this->grammar = $grammar;
|
$this->grammar = $grammar;
|
||||||
$this->connection = $connection;
|
$this->connection = $connection;
|
||||||
}
|
}
|
||||||
|
@ -122,7 +122,7 @@ public function distinct()
|
||||||
*/
|
*/
|
||||||
public function select($columns = array('*'))
|
public function select($columns = array('*'))
|
||||||
{
|
{
|
||||||
$this->select = (array) $columns;
|
$this->selects = (array) $columns;
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
@ -139,7 +139,7 @@ public function select($columns = array('*'))
|
||||||
*/
|
*/
|
||||||
public function join($table, $column1, $operator, $column2, $type = 'INNER')
|
public function join($table, $column1, $operator, $column2, $type = 'INNER')
|
||||||
{
|
{
|
||||||
$this->joins[] = compact('table', 'column1', 'operator', 'column2', 'type');
|
$this->joins[] = compact('type', 'table', 'column1', 'operator', 'column2');
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
@ -178,7 +178,7 @@ public function reset_where()
|
||||||
*/
|
*/
|
||||||
public function raw_where($where, $bindings = array(), $connector = 'AND')
|
public function raw_where($where, $bindings = array(), $connector = 'AND')
|
||||||
{
|
{
|
||||||
$this->wheres[] = ' '.$connector.' '.$where;
|
$this->wheres[] = array('type' => 'raw', 'connector' => $connector, 'sql' => $where);
|
||||||
|
|
||||||
$this->bindings = array_merge($this->bindings, $bindings);
|
$this->bindings = array_merge($this->bindings, $bindings);
|
||||||
|
|
||||||
|
@ -470,7 +470,7 @@ private function aggregate($aggregator, $column)
|
||||||
|
|
||||||
// Reset the SELECT clause so more queries can be performed using the same instance.
|
// Reset the SELECT clause so more queries can be performed using the same instance.
|
||||||
// This is helpful for getting aggregates and then getting actual results.
|
// This is helpful for getting aggregates and then getting actual results.
|
||||||
$this->select = null;
|
$this->selects = null;
|
||||||
|
|
||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
|
@ -507,13 +507,13 @@ public function first($columns = array('*'))
|
||||||
*/
|
*/
|
||||||
public function get($columns = array('*'))
|
public function get($columns = array('*'))
|
||||||
{
|
{
|
||||||
if (is_null($this->select)) $this->select($columns);
|
if (is_null($this->selects)) $this->select($columns);
|
||||||
|
|
||||||
$results = $this->connection->query($this->grammar->select($this), $this->bindings);
|
$results = $this->connection->query($this->grammar->select($this), $this->bindings);
|
||||||
|
|
||||||
// Reset the SELECT clause so more queries can be performed using the same instance.
|
// Reset the SELECT clause so more queries can be performed using the same instance.
|
||||||
// This is helpful for getting aggregates and then getting actual results.
|
// This is helpful for getting aggregates and then getting actual results.
|
||||||
$this->select = null;
|
$this->selects = null;
|
||||||
|
|
||||||
return $results;
|
return $results;
|
||||||
}
|
}
|
||||||
|
@ -582,7 +582,7 @@ public function __call($method, $parameters)
|
||||||
return $this->dynamic_where($method, $parameters, $this);
|
return $this->dynamic_where($method, $parameters, $this);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (in_array($method, array('count', 'min', 'max', 'avg', 'sum')))
|
if (in_array($method, array('abs', 'count', 'min', 'max', 'avg', 'sum')))
|
||||||
{
|
{
|
||||||
return ($method == 'count') ? $this->aggregate(strtoupper($method), '*') : $this->aggregate(strtoupper($method), $parameters[0]);
|
return ($method == 'count') ? $this->aggregate(strtoupper($method), '*') : $this->aggregate(strtoupper($method), $parameters[0]);
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,32 +7,56 @@ class Loader {
|
||||||
*
|
*
|
||||||
* @var array
|
* @var array
|
||||||
*/
|
*/
|
||||||
public static $paths = array();
|
protected $paths = array();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The class aliases defined for the application.
|
* The class aliases defined for the application.
|
||||||
*
|
*
|
||||||
* @var array
|
* @var array
|
||||||
*/
|
*/
|
||||||
public static $aliases = array();
|
protected $aliases = array();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a new class loader instance.
|
||||||
|
*
|
||||||
|
* @param array $paths
|
||||||
|
* @param array $aliases
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function __construct($paths, $aliases = array())
|
||||||
|
{
|
||||||
|
$this->paths = $paths;
|
||||||
|
$this->aliases = $aliases;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Load the file for a given class.
|
* Load the file for a given class.
|
||||||
*
|
*
|
||||||
|
* <code>
|
||||||
|
* // Load the file for the "User" class
|
||||||
|
* Loader::load('User');
|
||||||
|
*
|
||||||
|
* // Load the file for the "Repositories\User" class
|
||||||
|
* Loader::load('Repositories\\User');
|
||||||
|
* </code>
|
||||||
|
*
|
||||||
* @param string $class
|
* @param string $class
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
public static function load($class)
|
public function load($class)
|
||||||
{
|
{
|
||||||
// All Laravel core classes follow a namespace to directory convention. So, we will
|
// All Laravel core classes follow a namespace to directory convention.
|
||||||
// replace all of the namespace slashes with directory slashes.
|
// We will replace all of the namespace slashes with directory slashes.
|
||||||
$file = strtolower(str_replace('\\', '/', $class));
|
$file = strtolower(str_replace('\\', '/', $class));
|
||||||
|
|
||||||
// First, we'll check to determine if an alias exists. If it does, we will define the
|
// Check to determine if an alias exists. If it does, we will define the
|
||||||
// alias and bail out. Aliases are defined for most developer used core classes.
|
// alias and bail out. Aliases are defined for most used core classes.
|
||||||
if (array_key_exists($class, static::$aliases)) return class_alias(static::$aliases[$class], $class);
|
if (array_key_exists($class, $this->aliases))
|
||||||
|
{
|
||||||
|
return class_alias($this->aliases[$class], $class);
|
||||||
|
}
|
||||||
|
|
||||||
foreach (static::$paths as $path)
|
foreach ($this->paths as $path)
|
||||||
{
|
{
|
||||||
if (file_exists($path = $path.$file.EXT))
|
if (file_exists($path = $path.$file.EXT))
|
||||||
{
|
{
|
||||||
|
@ -46,15 +70,13 @@ public static function load($class)
|
||||||
/**
|
/**
|
||||||
* Register a class alias with the auto-loader.
|
* Register a class alias with the auto-loader.
|
||||||
*
|
*
|
||||||
* Note: Aliases are lazy-loaded, so the aliased class will not be included until it is needed.
|
|
||||||
*
|
|
||||||
* @param string $alias
|
* @param string $alias
|
||||||
* @param string $class
|
* @param string $class
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
public static function alias($alias, $class)
|
public function alias($alias, $class)
|
||||||
{
|
{
|
||||||
static::$aliases[$alias] = $class;
|
$this->aliases[$alias] = $class;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -63,9 +85,9 @@ public static function alias($alias, $class)
|
||||||
* @param string $path
|
* @param string $path
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
public static function path($path)
|
public function path($path)
|
||||||
{
|
{
|
||||||
static::$paths[] = rtrim($path, '/').'/';
|
$this->paths[] = rtrim($path, '/').'/';
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -74,9 +96,9 @@ public static function path($path)
|
||||||
* @param string $alias
|
* @param string $alias
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
public static function forget_alias($alias)
|
public function forget_alias($alias)
|
||||||
{
|
{
|
||||||
unset(static::$aliases[$alias]);
|
unset($this->aliases[$alias]);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
|
@ -285,7 +285,9 @@ protected function get_size($attribute)
|
||||||
|
|
||||||
$value = $this->attributes[$attribute];
|
$value = $this->attributes[$attribute];
|
||||||
|
|
||||||
return (array_key_exists($attribute, $_FILES)) ? $value['size'] / 1024 : Str::length(trim($value));
|
$files = IoC::container()->resolve('laravel.input')->files();
|
||||||
|
|
||||||
|
return (array_key_exists($attribute, $files) ? $value['size'] / 1024 : Str::length(trim($value));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in New Issue