123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164 |
- <?php
- /**
- * Pimf
- *
- * @copyright Copyright (c) Gjero Krsteski (http://krsteski.de)
- * @license http://krsteski.de/new-bsd-license New BSD License
- */
- namespace Pimf;
- use Pimf\Session\Payload;
- use Pimf\Session\Storages as Storage;
- /**
- * Using the session
- *
- * <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', 'Robin');
- *
- * // Retrieve a value from the session
- * $value = Session::get('name');
- *
- * // Write a value to the session storage
- * $value = Session::put('name', 'Robin');
- *
- * // Equivalent statement using the "instance" method
- * $value = Session::instance()->put('name', 'Robin');
- *
- * </code>
- *
- * @package Pimf
- * @author Gjero Krsteski <[email protected]>
- *
- * @method static save()
- */
- class Session
- {
- /**
- * The session singleton instance for the request.
- *
- * @var Payload
- */
- public static $instance;
- /**
- * The string name of the CSRF token stored in the session.
- *
- * @var string
- */
- const CSRF = 'csrf_token';
- /**
- * Create the session payload and load the session.
- *
- * @return void
- */
- public static function load()
- {
- $conf = Registry::get('conf');
- static::start($conf['session']['storage']);
- static::$instance->load(Cookie::get($conf['session']['cookie']));
- }
- /**
- * Create the session payload instance for the request.
- *
- * @param string $storage
- *
- * @return void
- */
- public static function start($storage)
- {
- static::$instance = new Payload(static::factory($storage));
- }
- /**
- * Create a new session storage instance.
- *
- * @param string $storage
- *
- * @return Storage\Storage
- * @throws \RuntimeException
- */
- public static function factory($storage)
- {
- $conf = Registry::get('conf');
- switch ($storage) {
- case 'apc':
- return new Storage\Apc(Cache::storage('apc'));
- case 'cookie':
- return new Storage\Cookie();
- case 'file':
- return new Storage\File($conf['session']['storage_path']);
- case 'pdo':
- return new Storage\Pdo(Pdo\Factory::get($conf['session']['database']));
- case 'memcached':
- return new Storage\Memcached(Cache::storage('memcached'));
- case 'memory':
- return new Storage\Memory();
- case 'redis':
- return new Storage\Redis(Cache::storage('redis'));
- case 'dba':
- return new Storage\Dba(Cache::storage('dba'));
- default:
- throw new \RuntimeException("Session storage [$storage] is not supported.");
- }
- }
- /**
- * Retrieve the active session payload instance for the request.
- *
- * @return Payload
- * @throws \RuntimeException
- */
- public static function instance()
- {
- if (static::started()) {
- return static::$instance;
- }
- throw new \RuntimeException("A storage must be set before using the session.");
- }
- /**
- * Determine if session handling has been started for the request.
- *
- * @return bool
- */
- public static function started()
- {
- return (static::$instance !== null);
- }
- /**
- * Magic Method for calling the methods on the session singleton instance.
- *
- * @param $method
- * @param $parameters
- *
- * @return mixed
- */
- public static function __callStatic($method, $parameters)
- {
- return call_user_func_array(
- array(static::instance(), $method), $parameters
- );
- }
- }
|