session.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php namespace Laravel;
  2. class Session {
  3. /**
  4. * The session singleton instance for the request.
  5. *
  6. * @var Payload
  7. */
  8. public static $instance;
  9. /**
  10. * The string name of the CSRF token stored in the session.
  11. *
  12. * @var string
  13. */
  14. const csrf_token = 'csrf_token';
  15. /**
  16. * Create the session payload instance and load the session for the request.
  17. *
  18. * @param string $driver
  19. * @return void
  20. */
  21. public static function start($driver)
  22. {
  23. if (Config::get('application.key') === '')
  24. {
  25. throw new \Exception("An application key is required to use sessions.");
  26. }
  27. static::$instance = new Session\Payload(static::factory($driver));
  28. }
  29. /**
  30. * Create a new session driver instance.
  31. *
  32. * @param string $driver
  33. * @return Driver
  34. */
  35. public static function factory($driver)
  36. {
  37. switch ($driver)
  38. {
  39. case 'apc':
  40. return new Session\Drivers\APC(Cache::driver('apc'));
  41. case 'cookie':
  42. return new Session\Drivers\Cookie;
  43. case 'database':
  44. return new Session\Drivers\Database(Database::connection());
  45. case 'file':
  46. return new Session\Drivers\File($GLOBALS['STORAGE_PATH'].'sessions'.DS);
  47. case 'memcached':
  48. return new Session\Drivers\Memcached(Cache::driver('memcached'));
  49. case 'redis':
  50. return new Session\Drivers\Redis(Cache::driver('redis'));
  51. default:
  52. throw new \Exception("Session driver [$driver] is not supported.");
  53. }
  54. }
  55. /**
  56. * Retrieve the active session payload instance for the request.
  57. *
  58. * <code>
  59. * // Retrieve the session instance and get an item
  60. * Session::instance()->get('name');
  61. *
  62. * // Retrieve the session instance and place an item in the session
  63. * Session::instance()->put('name', 'Taylor');
  64. * </code>
  65. *
  66. * @return Payload
  67. */
  68. public static function instance()
  69. {
  70. if (static::started()) return static::$instance;
  71. throw new \Exception("A driver must be set before using the session.");
  72. }
  73. /**
  74. * Determine if session handling has been started for the request.
  75. *
  76. * @return bool
  77. */
  78. public static function started()
  79. {
  80. return ! is_null(static::$instance);
  81. }
  82. /**
  83. * Magic Method for calling the methods on the session singleton instance.
  84. *
  85. * <code>
  86. * // Retrieve a value from the session
  87. * $value = Session::get('name');
  88. *
  89. * // Write a value to the session storage
  90. * $value = Session::put('name', 'Taylor');
  91. *
  92. * // Equivalent statement using the "instance" method
  93. * $value = Session::instance()->put('name', 'Taylor');
  94. * </code>
  95. */
  96. public static function __callStatic($method, $parameters)
  97. {
  98. return call_user_func_array(array(static::instance(), $method), $parameters);
  99. }
  100. }