1: <?php namespace Laravel; use Closure;
2:
3: class Session {
4:
5: /**
6: * The session singleton instance for the request.
7: *
8: * @var Session\Payload
9: */
10: public static $instance;
11:
12: /**
13: * The third-party driver registrar.
14: *
15: * @var array
16: */
17: public static $registrar = array();
18:
19: /**
20: * The string name of the CSRF token stored in the session.
21: *
22: * @var string
23: */
24: const csrf_token = 'csrf_token';
25:
26: /**
27: * Create the session payload and load the session.
28: *
29: * @return void
30: */
31: public static function load()
32: {
33: static::start(Config::get('session.driver'));
34:
35: static::$instance->load(Cookie::get(Config::get('session.cookie')));
36: }
37:
38: /**
39: * Create the session payload instance for the request.
40: *
41: * @param string $driver
42: * @return void
43: */
44: public static function start($driver)
45: {
46: static::$instance = new Session\Payload(static::factory($driver));
47: }
48:
49: /**
50: * Create a new session driver instance.
51: *
52: * @param string $driver
53: * @return Session\Drivers\Driver
54: */
55: public static function factory($driver)
56: {
57: if (isset(static::$registrar[$driver]))
58: {
59: $resolver = static::$registrar[$driver];
60:
61: return $resolver();
62: }
63:
64: switch ($driver)
65: {
66: case 'apc':
67: return new Session\Drivers\APC(Cache::driver('apc'));
68:
69: case 'cookie':
70: return new Session\Drivers\Cookie;
71:
72: case 'database':
73: return new Session\Drivers\Database(Database::connection());
74:
75: case 'file':
76: return new Session\Drivers\File(path('storage').'sessions'.DS);
77:
78: case 'memcached':
79: return new Session\Drivers\Memcached(Cache::driver('memcached'));
80:
81: case 'memory':
82: return new Session\Drivers\Memory;
83:
84: case 'redis':
85: return new Session\Drivers\Redis(Cache::driver('redis'));
86:
87: default:
88: throw new \Exception("Session driver [$driver] is not supported.");
89: }
90: }
91:
92: /**
93: * Retrieve the active session payload instance for the request.
94: *
95: * <code>
96: * // Retrieve the session instance and get an item
97: * Session::instance()->get('name');
98: *
99: * // Retrieve the session instance and place an item in the session
100: * Session::instance()->put('name', 'Taylor');
101: * </code>
102: *
103: * @return Session\Payload
104: */
105: public static function instance()
106: {
107: if (static::started()) return static::$instance;
108:
109: throw new \Exception("A driver must be set before using the session.");
110: }
111:
112: /**
113: * Determine if session handling has been started for the request.
114: *
115: * @return bool
116: */
117: public static function started()
118: {
119: return ! is_null(static::$instance);
120: }
121:
122: /**
123: * Register a third-party cache driver.
124: *
125: * @param string $driver
126: * @param Closure $resolver
127: * @return void
128: */
129: public static function extend($driver, Closure $resolver)
130: {
131: static::$registrar[$driver] = $resolver;
132: }
133:
134: /**
135: * Magic Method for calling the methods on the session singleton instance.
136: *
137: * <code>
138: * // Retrieve a value from the session
139: * $value = Session::get('name');
140: *
141: * // Write a value to the session storage
142: * $value = Session::put('name', 'Taylor');
143: *
144: * // Equivalent statement using the "instance" method
145: * $value = Session::instance()->put('name', 'Taylor');
146: * </code>
147: */
148: public static function __callStatic($method, $parameters)
149: {
150: return call_user_func_array(array(static::instance(), $method), $parameters);
151: }
152:
153: }