Files
spa-api/laravel/session.php

69 lines
1.6 KiB
PHP
Raw Normal View History

2011-08-18 19:56:29 -05:00
<?php namespace Laravel;
2011-06-08 23:45:08 -05:00
class Session {
/**
* The active session driver.
*
* @var Session\Driver
*/
public static $driver;
2011-06-08 23:45:08 -05:00
/**
* Get the session driver.
2011-06-08 23:45:08 -05:00
*
2011-08-19 20:12:39 -05:00
* The session driver returned will be the driver specified in the session configuration
* file. Only one session driver may be active for a given request, so the driver will
* be managed as a singleton.
*
2011-06-08 23:45:08 -05:00
* @return Session\Driver
*/
public static function driver()
{
if (is_null(static::$driver))
{
switch (Config::get('session.driver'))
{
case 'cookie':
2011-08-06 20:49:53 -05:00
return static::$driver = new Session\Cookie;
case 'file':
2011-08-06 20:49:53 -05:00
return static::$driver = new Session\File;
case 'db':
2011-08-06 20:49:53 -05:00
return static::$driver = new Session\DB;
case 'memcached':
2011-08-06 20:49:53 -05:00
return static::$driver = new Session\Memcached;
case 'apc':
2011-08-06 20:49:53 -05:00
return static::$driver = new Session\APC;
default:
throw new \Exception("Session driver [$driver] is not supported.");
}
2011-06-08 23:45:08 -05:00
}
2011-08-08 09:58:44 -05:00
return static::$driver;
2011-06-08 23:45:08 -05:00
}
/**
2011-08-19 20:12:39 -05:00
* Pass all other methods to the default session driver.
2011-06-08 23:45:08 -05:00
*
2011-08-19 20:12:39 -05:00
* By dynamically passing these method calls to the default driver, the developer is
* able to use with a convenient API when working with the session.
2011-06-08 23:45:08 -05:00
*
2011-08-19 20:12:39 -05:00
* <code>
* // Get an item from the default session driver
* $name = Session::get('name');
2011-06-08 23:45:08 -05:00
*
2011-08-19 20:12:39 -05:00
* // Equivalent call using the driver method
* $name = Session::driver()->get('name');
* </code>
2011-06-08 23:45:08 -05:00
*/
2011-08-19 20:12:39 -05:00
public static function __callStatic($method, $parameters)
2011-06-08 23:45:08 -05:00
{
2011-08-19 20:12:39 -05:00
return call_user_func_array(array(static::driver(), $method), $parameters);
2011-08-08 09:58:44 -05:00
}
2011-06-08 23:45:08 -05:00
}