Commit df9130da by Taylor Otwell

refactoring and working on redis.

parent ba29a0bd
<?php
/**
* Define the extensions used by the framework.
*/
define('EXT', '.php');
define('BLADE_EXT', '.blade.php');
define('CRLF', chr(13).chr(10));
define('EXT', '.php');
/**
* Define a function that registers an array of constants
* if they haven't already been registered. This allows the
* constants to be changed from their default values when
* unit testing the framework.
* Define a function that registers an array of constants if they
* haven't already been registered. This allows the constants to
* be changed from their default values when unit testing.
*/
function constants($constants)
{
......@@ -21,9 +18,9 @@ function constants($constants)
}
/**
* Register the core framework paths. All other paths are
* built on top of these core paths. All of these paths are
* changable by the developer in the front controller.
* Register the core framework paths. All other paths are built on
* top of these core paths. All of these paths are changable by
* the developer in the front controller.
*/
$constants = array(
'APP_PATH' => realpath($application).'/',
......@@ -37,8 +34,9 @@ $constants = array(
constants($constants);
/**
* Register all of the other framework paths. All of these
* paths are built on top of the core paths above.
* Register all of the other framework paths. All of these paths
* are built on top of the core paths above. We still allow the
* developer to override these for easy unit testing.
*/
$constants = array(
'CACHE_PATH' => STORAGE_PATH.'cache/',
......@@ -63,7 +61,7 @@ unset($constants);
/**
* 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.
*/
$environment = (isset($_SERVER['LARAVEL_ENV'])) ? $_SERVER['LARAVEL_ENV'] : '';
......
......@@ -59,20 +59,31 @@ $handler = function($e) use ($message, $severity)
};
/**
* Register the PHP exception, error, and shutdown error handlers.
* These handlers will catch all PHP exceptions and errors and pass
* the exceptions into the common Laravel error handler.
* Register the PHP exception handler. The framework throws exceptions
* on every error that cannot be handled. All of those exceptions will
* fall into this closure for processing.
*/
set_exception_handler(function($e) use ($handler)
{
$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)
{
$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)
{
if ( ! is_null($error = error_get_last()))
......
<?php namespace Laravel\Cache\Drivers; use Memcache, Laravel\Config;
<?php namespace Laravel\Cache\Drivers; use Memcache;
class Memcached extends Driver {
......
......@@ -22,7 +22,16 @@ class Grammar {
*
* @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.
......@@ -272,9 +281,12 @@ class Grammar {
// every insert to the table.
$columns = $this->columnize(array_keys(reset($values)));
// We need to create a string of comma-delimited insert segments. Each segment
// contains PDO place-holders for each value being inserted into the table.
$parameters = implode(', ', array_fill(0, count($values), '('.$this->parameterize(reset($values)).')'));
// Build the list of parameter place-holders for the array of values bound
// to the query. Each insert statement should have the same number of bound
// 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;
}
......
......@@ -40,7 +40,7 @@ class Manager {
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);
......
<?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);
}
}
\ No newline at end of file
......@@ -54,52 +54,43 @@ class Crypter {
$iv = mcrypt_create_iv(static::iv_size(), $randomizer);
$key = Config::$items['application']['key'];
return base64_encode($iv.mcrypt_encrypt(static::$cipher, $key, $value, static::$mode, $iv));
return base64_encode($iv.mcrypt_encrypt(static::$cipher, static::key(), $value, static::$mode, $iv));
}
/**
* Decrypt a string using Mcrypt.
*
* <code>
* // Decrypt a string using the Mcrypt PHP extension
* $decrypted = Crypter::decrypt($secret);
* </code>
*
* @param string $value
* @return string
*/
public static function decrypt($value)
{
if ( ! is_string($value = base64_decode($value, true)))
{
throw new \Exception('Decryption error. Input value is not valid base64 data.');
}
list($iv, $value) = static::parse($value);
$key = Config::$items['application']['key'];
list($iv, $value) = static::parse(base64_decode($value, true));
return rtrim(mcrypt_decrypt(static::$cipher, $key, $value, static::$mode, $iv), "\0");
return rtrim(mcrypt_decrypt(static::$cipher, static::key(), $value, static::$mode, $iv), "\0");
}
/**
* 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
* @return array
*/
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()));
}
/**
* Get the input vector size for the cipher and mode.
*
* Different ciphers and modes use varying lengths of input vectors.
*
* @return int
*/
protected static function iv_size()
......@@ -107,4 +98,14 @@ class Crypter {
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'];
}
}
\ No newline at end of file
......@@ -67,15 +67,13 @@ class Manager {
$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;
// If a CSRF token is not present in the session, we will generate one.
// 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));
}
if ( ! static::has('csrf_token')) static::put('csrf_token', Str::random(16));
list(static::$driver, static::$transporter) = array($driver, $transporter);
}
......
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