session.php 2.5 KB

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