session.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 'memory':
  56. return new Session\Drivers\Memory;
  57. case 'redis':
  58. return new Session\Drivers\Redis(Cache::driver('redis'));
  59. default:
  60. throw new \Exception("Session driver [$driver] is not supported.");
  61. }
  62. }
  63. /**
  64. * Retrieve the active session payload instance for the request.
  65. *
  66. * <code>
  67. * // Retrieve the session instance and get an item
  68. * Session::instance()->get('name');
  69. *
  70. * // Retrieve the session instance and place an item in the session
  71. * Session::instance()->put('name', 'Taylor');
  72. * </code>
  73. *
  74. * @return Session\Payload
  75. */
  76. public static function instance()
  77. {
  78. if (static::started()) return static::$instance;
  79. throw new \Exception("A driver must be set before using the session.");
  80. }
  81. /**
  82. * Determine if session handling has been started for the request.
  83. *
  84. * @return bool
  85. */
  86. public static function started()
  87. {
  88. return ! is_null(static::$instance);
  89. }
  90. /**
  91. * Magic Method for calling the methods on the session singleton instance.
  92. *
  93. * <code>
  94. * // Retrieve a value from the session
  95. * $value = Session::get('name');
  96. *
  97. * // Write a value to the session storage
  98. * $value = Session::put('name', 'Taylor');
  99. *
  100. * // Equivalent statement using the "instance" method
  101. * $value = Session::instance()->put('name', 'Taylor');
  102. * </code>
  103. */
  104. public static function __callStatic($method, $parameters)
  105. {
  106. return call_user_func_array(array(static::instance(), $method), $parameters);
  107. }
  108. }