session.php 2.7 KB

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