refactoring and working on redis.
This commit is contained in:
parent
ba29a0bd38
commit
df9130dafa
|
@ -1,16 +1,13 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
/**
|
|
||||||
* Define the extensions used by the framework.
|
|
||||||
*/
|
|
||||||
define('EXT', '.php');
|
|
||||||
define('BLADE_EXT', '.blade.php');
|
define('BLADE_EXT', '.blade.php');
|
||||||
|
define('CRLF', chr(13).chr(10));
|
||||||
|
define('EXT', '.php');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Define a function that registers an array of constants
|
* Define a function that registers an array of constants if they
|
||||||
* if they haven't already been registered. This allows the
|
* haven't already been registered. This allows the constants to
|
||||||
* constants to be changed from their default values when
|
* be changed from their default values when unit testing.
|
||||||
* unit testing the framework.
|
|
||||||
*/
|
*/
|
||||||
function constants($constants)
|
function constants($constants)
|
||||||
{
|
{
|
||||||
|
@ -21,9 +18,9 @@ function constants($constants)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Register the core framework paths. All other paths are
|
* Register the core framework paths. All other paths are built on
|
||||||
* built on top of these core paths. All of these paths are
|
* top of these core paths. All of these paths are changable by
|
||||||
* changable by the developer in the front controller.
|
* the developer in the front controller.
|
||||||
*/
|
*/
|
||||||
$constants = array(
|
$constants = array(
|
||||||
'APP_PATH' => realpath($application).'/',
|
'APP_PATH' => realpath($application).'/',
|
||||||
|
@ -37,8 +34,9 @@ function constants($constants)
|
||||||
constants($constants);
|
constants($constants);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Register all of the other framework paths. All of these
|
* Register all of the other framework paths. All of these paths
|
||||||
* paths are built on top of the core paths above.
|
* are built on top of the core paths above. We still allow the
|
||||||
|
* developer to override these for easy unit testing.
|
||||||
*/
|
*/
|
||||||
$constants = array(
|
$constants = array(
|
||||||
'CACHE_PATH' => STORAGE_PATH.'cache/',
|
'CACHE_PATH' => STORAGE_PATH.'cache/',
|
||||||
|
@ -63,7 +61,7 @@ function constants($constants)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set the Laravel environment configuration path constant.
|
* Set the Laravel environment configuration path constant.
|
||||||
* The environment is controller by setting an environment
|
* The environment is controlled by setting an environment
|
||||||
* variable on the server running Laravel.
|
* variable on the server running Laravel.
|
||||||
*/
|
*/
|
||||||
$environment = (isset($_SERVER['LARAVEL_ENV'])) ? $_SERVER['LARAVEL_ENV'] : '';
|
$environment = (isset($_SERVER['LARAVEL_ENV'])) ? $_SERVER['LARAVEL_ENV'] : '';
|
||||||
|
|
|
@ -59,20 +59,31 @@
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Register the PHP exception, error, and shutdown error handlers.
|
* Register the PHP exception handler. The framework throws exceptions
|
||||||
* These handlers will catch all PHP exceptions and errors and pass
|
* on every error that cannot be handled. All of those exceptions will
|
||||||
* the exceptions into the common Laravel error handler.
|
* fall into this closure for processing.
|
||||||
*/
|
*/
|
||||||
set_exception_handler(function($e) use ($handler)
|
set_exception_handler(function($e) use ($handler)
|
||||||
{
|
{
|
||||||
$handler($e);
|
$handler($e);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Register the PHP error handler. All PHP errors will fall into this
|
||||||
|
* handler, which will convert the error into an ErrorException object
|
||||||
|
* and pass the exception into the common exception handler.
|
||||||
|
*/
|
||||||
set_error_handler(function($number, $error, $file, $line) use ($handler)
|
set_error_handler(function($number, $error, $file, $line) use ($handler)
|
||||||
{
|
{
|
||||||
$handler(new \ErrorException($error, $number, 0, $file, $line));
|
$handler(new \ErrorException($error, $number, 0, $file, $line));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Register the PHP shutdown handler. This function will be called at
|
||||||
|
* the end of the PHP script or on a fatal PHP error. If an error has
|
||||||
|
* occurred, we will convert it to an ErrorException and pass it to
|
||||||
|
* the common exception handler.
|
||||||
|
*/
|
||||||
register_shutdown_function(function() use ($handler)
|
register_shutdown_function(function() use ($handler)
|
||||||
{
|
{
|
||||||
if ( ! is_null($error = error_get_last()))
|
if ( ! is_null($error = error_get_last()))
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
<?php namespace Laravel\Cache\Drivers; use Memcache, Laravel\Config;
|
<?php namespace Laravel\Cache\Drivers; use Memcache;
|
||||||
|
|
||||||
class Memcached extends Driver {
|
class Memcached extends Driver {
|
||||||
|
|
||||||
|
|
|
@ -22,7 +22,16 @@ class Grammar {
|
||||||
*
|
*
|
||||||
* @var array
|
* @var array
|
||||||
*/
|
*/
|
||||||
protected $components = array('aggregate', 'selects', 'from', 'joins', 'wheres', 'orderings', 'limit', 'offset');
|
protected $components = array(
|
||||||
|
'aggregate',
|
||||||
|
'selects',
|
||||||
|
'from',
|
||||||
|
'joins',
|
||||||
|
'wheres',
|
||||||
|
'orderings',
|
||||||
|
'limit',
|
||||||
|
'offset'
|
||||||
|
);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Compile a SQL SELECT statement from a Query instance.
|
* Compile a SQL SELECT statement from a Query instance.
|
||||||
|
@ -272,9 +281,12 @@ public function insert(Query $query, $values)
|
||||||
// every insert to the table.
|
// every insert to the table.
|
||||||
$columns = $this->columnize(array_keys(reset($values)));
|
$columns = $this->columnize(array_keys(reset($values)));
|
||||||
|
|
||||||
// We need to create a string of comma-delimited insert segments. Each segment
|
// Build the list of parameter place-holders for the array of values bound
|
||||||
// contains PDO place-holders for each value being inserted into the table.
|
// to the query. Each insert statement should have the same number of bound
|
||||||
$parameters = implode(', ', array_fill(0, count($values), '('.$this->parameterize(reset($values)).')'));
|
// parameters, so we can just use the first array of values.
|
||||||
|
$parameters = $this->parameterize(reset($values));
|
||||||
|
|
||||||
|
$parameters = implode(', ', array_fill(0, count($values), '('.$parameters.')'));
|
||||||
|
|
||||||
return 'INSERT INTO '.$this->wrap($query->from).' ('.$columns.') VALUES '.$parameters;
|
return 'INSERT INTO '.$this->wrap($query->from).' ('.$columns.') VALUES '.$parameters;
|
||||||
}
|
}
|
||||||
|
|
|
@ -40,7 +40,7 @@ public static function connection($connection = null)
|
||||||
|
|
||||||
if (is_null($config))
|
if (is_null($config))
|
||||||
{
|
{
|
||||||
throw new \Exception("Database connection configuration is not defined for connection [$connection].");
|
throw new \Exception("Database connection is not defined for connection [$connection].");
|
||||||
}
|
}
|
||||||
|
|
||||||
static::$connections[$connection] = new Connection(static::connect($config), $config);
|
static::$connections[$connection] = new Connection(static::connect($config), $config);
|
||||||
|
|
|
@ -0,0 +1,155 @@
|
||||||
|
<?php namespace Laravel;
|
||||||
|
|
||||||
|
class Redis {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The active Redis connections.
|
||||||
|
*
|
||||||
|
* @var array
|
||||||
|
*/
|
||||||
|
protected static $connections = array();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The name of the Redis connection.
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
public $name;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The configuration array for the Redis connection.
|
||||||
|
*
|
||||||
|
* @var array
|
||||||
|
*/
|
||||||
|
public $config = array();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The connection to the Redis database.
|
||||||
|
*
|
||||||
|
* @var resource
|
||||||
|
*/
|
||||||
|
protected $connection;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a new Redis connection instance.
|
||||||
|
*
|
||||||
|
* @param string $name
|
||||||
|
* @param array $config
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function __construct($name, $config)
|
||||||
|
{
|
||||||
|
$this->name = $name;
|
||||||
|
$this->config = $config;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a new Redis connection instance.
|
||||||
|
*
|
||||||
|
* @param string $connection
|
||||||
|
* @param array $config
|
||||||
|
* @return Redis
|
||||||
|
*/
|
||||||
|
public static function make($name, $config)
|
||||||
|
{
|
||||||
|
return new static($name, $config);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a new Redis connection instance.
|
||||||
|
*
|
||||||
|
* Redis connections are managed as singletons, so if the connection has
|
||||||
|
* already been established, that same connection instance will be returned
|
||||||
|
* on subsequent requests for the connection.
|
||||||
|
*
|
||||||
|
* @param string $connection
|
||||||
|
* @return Redis
|
||||||
|
*/
|
||||||
|
public static function connection($name)
|
||||||
|
{
|
||||||
|
if ( ! array_key_exists($name, static::$connections))
|
||||||
|
{
|
||||||
|
$config = Config::get("database.redis.{$name}");
|
||||||
|
|
||||||
|
if (is_null($config))
|
||||||
|
{
|
||||||
|
throw new \Exception("Redis connection [$name] has not been configured.");
|
||||||
|
}
|
||||||
|
|
||||||
|
static::$connections[$name] = static::make($name, $config)->connect();
|
||||||
|
}
|
||||||
|
|
||||||
|
return static::$connections[$name];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Connect to the Redis database.
|
||||||
|
*
|
||||||
|
* The Redis instance itself will be returned by the method.
|
||||||
|
*
|
||||||
|
* @return Redis
|
||||||
|
*/
|
||||||
|
public function connect()
|
||||||
|
{
|
||||||
|
$this->connection = @fsockopen($this->config['host'], $this->config['port'], $error, $message);
|
||||||
|
|
||||||
|
if ($this->connection === false)
|
||||||
|
{
|
||||||
|
throw new \Exception("Error establishing Redis connection [{$this->name}]: {$error} - {$message}");
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Execute a command agaisnt the Redis database.
|
||||||
|
*
|
||||||
|
* @param string $method
|
||||||
|
* @param array $parameters
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
public function do($method, $parameters)
|
||||||
|
{
|
||||||
|
fwrite($this->connection, $this->command($method, $parameters));
|
||||||
|
|
||||||
|
$reply = trim(fgets($this->connection, 512));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Build the Redis command based from a given method and parameters.
|
||||||
|
*
|
||||||
|
* @param string $method
|
||||||
|
* @param array $parameters
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
protected function command($method, $parameters)
|
||||||
|
{
|
||||||
|
$command = '*'.(count($parameters) + 1).CRLF.'$'.strlen($method).CRLF.strtoupper($method).CRLF;
|
||||||
|
|
||||||
|
foreach ($parameters as $parameter)
|
||||||
|
{
|
||||||
|
$command .= '$'.strlen($parameter).CRLF.$parameter.CRLF;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $command;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Dynamically make calls to the Redis database.
|
||||||
|
*/
|
||||||
|
public function __call($method, $parameters)
|
||||||
|
{
|
||||||
|
return $this->do($method, $parameters);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Close the connection to the Redis database.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function __destruct()
|
||||||
|
{
|
||||||
|
fclose($this->connection);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -54,52 +54,43 @@ public static function encrypt($value)
|
||||||
|
|
||||||
$iv = mcrypt_create_iv(static::iv_size(), $randomizer);
|
$iv = mcrypt_create_iv(static::iv_size(), $randomizer);
|
||||||
|
|
||||||
$key = Config::$items['application']['key'];
|
return base64_encode($iv.mcrypt_encrypt(static::$cipher, static::key(), $value, static::$mode, $iv));
|
||||||
|
|
||||||
return base64_encode($iv.mcrypt_encrypt(static::$cipher, $key, $value, static::$mode, $iv));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Decrypt a string using Mcrypt.
|
* Decrypt a string using Mcrypt.
|
||||||
*
|
*
|
||||||
* <code>
|
|
||||||
* // Decrypt a string using the Mcrypt PHP extension
|
|
||||||
* $decrypted = Crypter::decrypt($secret);
|
|
||||||
* </code>
|
|
||||||
*
|
|
||||||
* @param string $value
|
* @param string $value
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public static function decrypt($value)
|
public static function decrypt($value)
|
||||||
{
|
{
|
||||||
if ( ! is_string($value = base64_decode($value, true)))
|
list($iv, $value) = static::parse(base64_decode($value, true));
|
||||||
{
|
|
||||||
throw new \Exception('Decryption error. Input value is not valid base64 data.');
|
|
||||||
}
|
|
||||||
|
|
||||||
list($iv, $value) = static::parse($value);
|
return rtrim(mcrypt_decrypt(static::$cipher, static::key(), $value, static::$mode, $iv), "\0");
|
||||||
|
|
||||||
$key = Config::$items['application']['key'];
|
|
||||||
|
|
||||||
return rtrim(mcrypt_decrypt(static::$cipher, $key, $value, static::$mode, $iv), "\0");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Parse an encrypted value into the input vector and the actual value.
|
* Parse an encrypted value into the input vector and the actual value.
|
||||||
*
|
*
|
||||||
|
* If the given value is not valid base64 data, an exception will be thrown.
|
||||||
|
*
|
||||||
* @param string $value
|
* @param string $value
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
protected static function parse($value)
|
protected static function parse($value)
|
||||||
{
|
{
|
||||||
|
if ( ! is_string($value))
|
||||||
|
{
|
||||||
|
throw new \Exception('Decryption error. Input value is not valid base64 data.');
|
||||||
|
}
|
||||||
|
|
||||||
return array(substr($value, 0, static::iv_size()), substr($value, static::iv_size()));
|
return array(substr($value, 0, static::iv_size()), substr($value, static::iv_size()));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the input vector size for the cipher and mode.
|
* Get the input vector size for the cipher and mode.
|
||||||
*
|
*
|
||||||
* Different ciphers and modes use varying lengths of input vectors.
|
|
||||||
*
|
|
||||||
* @return int
|
* @return int
|
||||||
*/
|
*/
|
||||||
protected static function iv_size()
|
protected static function iv_size()
|
||||||
|
@ -107,4 +98,14 @@ protected static function iv_size()
|
||||||
return mcrypt_get_iv_size(static::$cipher, static::$mode);
|
return mcrypt_get_iv_size(static::$cipher, static::$mode);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the encryption key from the application configuration.
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
protected static function key()
|
||||||
|
{
|
||||||
|
return Config::$items['application']['key'];
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
|
@ -67,15 +67,13 @@ public static function start(Driver $driver, Transporter $transporter)
|
||||||
$session = array('id' => Str::random(40), 'data' => array());
|
$session = array('id' => Str::random(40), 'data' => array());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Now that we should have a valid session, we can set the static session
|
||||||
|
// property and check for session data such as the CSRF token. We will
|
||||||
|
// also set the static driver and transporter properties, since they
|
||||||
|
// will be used to close the session at the end of the request.
|
||||||
static::$session = $session;
|
static::$session = $session;
|
||||||
|
|
||||||
// If a CSRF token is not present in the session, we will generate one.
|
if ( ! static::has('csrf_token')) static::put('csrf_token', Str::random(16));
|
||||||
// These tokens are generated per session to protect against Cross-Site
|
|
||||||
// Request Forgery attacks on the application.
|
|
||||||
if ( ! static::has('csrf_token'))
|
|
||||||
{
|
|
||||||
static::put('csrf_token', Str::random(16));
|
|
||||||
}
|
|
||||||
|
|
||||||
list(static::$driver, static::$transporter) = array($driver, $transporter);
|
list(static::$driver, static::$transporter) = array($driver, $transporter);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue