Browse Source

Allow the registration of third party cache and session drivers.

Taylor Otwell 12 years ago
parent
commit
ba9a7263b0
3 changed files with 50 additions and 2 deletions
  1. 1 1
      application/start.php
  2. 25 1
      laravel/cache.php
  3. 24 0
      laravel/session.php

+ 1 - 1
application/start.php

@@ -170,4 +170,4 @@ date_default_timezone_set(Config::get('application.timezone'));
 if ( ! Request::cli() and Config::get('session.driver') !== '')
 if ( ! Request::cli() and Config::get('session.driver') !== '')
 {
 {
 	Session::load();
 	Session::load();
-}
+}

+ 25 - 1
laravel/cache.php

@@ -1,4 +1,4 @@
-<?php namespace Laravel; defined('DS') or die('No direct script access.');
+<?php namespace Laravel; use Closure;
 
 
 class Cache {
 class Cache {
 
 
@@ -9,6 +9,13 @@ class Cache {
 	 */
 	 */
 	public static $drivers = array();
 	public static $drivers = array();
 
 
+	/**
+	 * The third-party driver registrar.
+	 *
+	 * @var array
+	 */
+	public static $registrar = array();
+
 	/**
 	/**
 	 * Get a cache driver instance.
 	 * Get a cache driver instance.
 	 *
 	 *
@@ -45,6 +52,11 @@ class Cache {
 	 */
 	 */
 	protected static function factory($driver)
 	protected static function factory($driver)
 	{
 	{
+		if (isset(static::$registrar[$driver]))
+		{
+			return static::$registrar[$driver]();
+		}
+
 		switch ($driver)
 		switch ($driver)
 		{
 		{
 			case 'apc':
 			case 'apc':
@@ -70,6 +82,18 @@ class Cache {
 		}
 		}
 	}
 	}
 
 
+	/**
+	 * Register a third-party cache driver.
+	 *
+	 * @param  string   $driver
+	 * @param  Closure  $resolver
+	 * @return void
+	 */
+	public static function register($driver, Closure $resolver)
+	{
+		static::$registrar[$driver] = $resolver;
+	}
+
 	/**
 	/**
 	 * Magic Method for calling the methods on the default cache driver.
 	 * Magic Method for calling the methods on the default cache driver.
 	 *
 	 *

+ 24 - 0
laravel/session.php

@@ -9,6 +9,13 @@ class Session {
 	 */
 	 */
 	public static $instance;
 	public static $instance;
 
 
+	/**
+	 * The third-party driver registrar.
+	 *
+	 * @var array
+	 */
+	public static $registrar = array();
+
 	/**
 	/**
 	 * The string name of the CSRF token stored in the session.
 	 * The string name of the CSRF token stored in the session.
 	 *
 	 *
@@ -47,6 +54,11 @@ class Session {
 	 */
 	 */
 	public static function factory($driver)
 	public static function factory($driver)
 	{
 	{
+		if (isset(static::$registrar[$driver]))
+		{
+			return static::$registrar[$driver]();
+		}
+
 		switch ($driver)
 		switch ($driver)
 		{
 		{
 			case 'apc':
 			case 'apc':
@@ -105,6 +117,18 @@ class Session {
 		return ! is_null(static::$instance);
 		return ! is_null(static::$instance);
 	}
 	}
 
 
+	/**
+	 * Register a third-party cache driver.
+	 *
+	 * @param  string   $driver
+	 * @param  Closure  $resolver
+	 * @return void
+	 */
+	public static function register($driver, Closure $resolver)
+	{
+		static::$registrar[$driver] = $resolver;
+	}
+
 	/**
 	/**
 	 * Magic Method for calling the methods on the session singleton instance.
 	 * Magic Method for calling the methods on the session singleton instance.
 	 *
 	 *