session.php 3.22 KB
Newer Older
1
<?php namespace Laravel; use Closure;
2 3 4 5 6 7

class Session {

	/**
	 * The session singleton instance for the request.
	 *
Phill Sparks committed
8
	 * @var Session\Payload
9 10 11 12
	 */
	public static $instance;

	/**
13 14 15 16 17 18 19
	 * The third-party driver registrar.
	 *
	 * @var array
	 */
	public static $registrar = array();

	/**
20 21 22 23 24 25 26
	 * The string name of the CSRF token stored in the session.
	 *
	 * @var string
	 */
	const csrf_token = 'csrf_token';

	/**
Chris Berthe committed
27
	 * Create the session payload and load the session.
28 29 30 31 32 33 34 35 36 37 38 39
	 *
	 * @return void
	 */
	public static function load()
	{
		static::start(Config::get('session.driver'));

		static::$instance->load(Cookie::get(Config::get('session.cookie')));
	}

	/**
	 * Create the session payload instance for the request.
40 41 42 43 44 45 46 47 48 49 50 51 52
	 *
	 * @param  string  $driver
	 * @return void
	 */
	public static function start($driver)
	{
		static::$instance = new Session\Payload(static::factory($driver));
	}

	/**
	 * Create a new session driver instance.
	 *
	 * @param  string  $driver
Phill Sparks committed
53
	 * @return Session\Drivers\Driver
54
	 */
55
	public static function factory($driver)
56
	{
57 58
		if (isset(static::$registrar[$driver]))
		{
59 60 61
			$resolver = static::$registrar[$driver];

			return $resolver();
62 63
		}

64 65 66 67 68 69 70 71 72 73 74 75
		switch ($driver)
		{
			case 'apc':
				return new Session\Drivers\APC(Cache::driver('apc'));

			case 'cookie':
				return new Session\Drivers\Cookie;

			case 'database':
				return new Session\Drivers\Database(Database::connection());

			case 'file':
Taylor Otwell committed
76
				return new Session\Drivers\File(path('storage').'sessions'.DS);
77 78 79 80

			case 'memcached':
				return new Session\Drivers\Memcached(Cache::driver('memcached'));

81 82 83
			case 'memory':
				return new Session\Drivers\Memory;

84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
			case 'redis':
				return new Session\Drivers\Redis(Cache::driver('redis'));

			default:
				throw new \Exception("Session driver [$driver] is not supported.");
		}
	}

	/**
	 * Retrieve the active session payload instance for the request.
	 *
	 * <code>
	 *		// Retrieve the session instance and get an item
	 *		Session::instance()->get('name');
	 *
	 *		// Retrieve the session instance and place an item in the session
	 *		Session::instance()->put('name', 'Taylor');
	 * </code>
	 *
Phill Sparks committed
103
	 * @return Session\Payload
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
	 */
	public static function instance()
	{
		if (static::started()) return static::$instance;

		throw new \Exception("A driver must be set before using the session.");
	}

	/**
	 * Determine if session handling has been started for the request.
	 *
	 * @return bool
	 */
	public static function started()
	{
		return ! is_null(static::$instance);
	}

	/**
123 124 125 126 127 128
	 * Register a third-party cache driver.
	 *
	 * @param  string   $driver
	 * @param  Closure  $resolver
	 * @return void
	 */
129
	public static function extend($driver, Closure $resolver)
130 131 132 133 134
	{
		static::$registrar[$driver] = $resolver;
	}

	/**
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
	 * Magic Method for calling the methods on the session singleton instance.
	 *
	 * <code>
	 *		// Retrieve a value from the session
	 *		$value = Session::get('name');
	 *
	 *		// Write a value to the session storage
	 *		$value = Session::put('name', 'Taylor');
	 *
	 *		// Equivalent statement using the "instance" method
	 *		$value = Session::instance()->put('name', 'Taylor');
	 * </code>
	 */
	public static function __callStatic($method, $parameters)
	{
150
		return call_user_func_array(array(static::instance(), $method), $parameters);
151 152 153
	}

}