session.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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 third-party driver registrar.
  11. *
  12. * @var array
  13. */
  14. public static $registrar = array();
  15. /**
  16. * The string name of the CSRF token stored in the session.
  17. *
  18. * @var string
  19. */
  20. const csrf_token = 'csrf_token';
  21. /**
  22. * Create the session payload and the load the session.
  23. *
  24. * @return void
  25. */
  26. public static function load()
  27. {
  28. static::start(Config::get('session.driver'));
  29. static::$instance->load(Cookie::get(Config::get('session.cookie')));
  30. }
  31. /**
  32. * Create the session payload instance for the request.
  33. *
  34. * @param string $driver
  35. * @return void
  36. */
  37. public static function start($driver)
  38. {
  39. static::$instance = new Session\Payload(static::factory($driver));
  40. }
  41. /**
  42. * Create a new session driver instance.
  43. *
  44. * @param string $driver
  45. * @return Session\Drivers\Driver
  46. */
  47. public static function factory($driver)
  48. {
  49. if (isset(static::$registrar[$driver]))
  50. {
  51. $resolver = static::$registrar[$driver];
  52. return $resolver();
  53. }
  54. switch ($driver)
  55. {
  56. case 'apc':
  57. return new Session\Drivers\APC(Cache::driver('apc'));
  58. case 'cookie':
  59. return new Session\Drivers\Cookie;
  60. case 'database':
  61. return new Session\Drivers\Database(Database::connection());
  62. case 'file':
  63. return new Session\Drivers\File(path('storage').'sessions'.DS);
  64. case 'memcached':
  65. return new Session\Drivers\Memcached(Cache::driver('memcached'));
  66. case 'memory':
  67. return new Session\Drivers\Memory;
  68. case 'redis':
  69. return new Session\Drivers\Redis(Cache::driver('redis'));
  70. default:
  71. throw new \Exception("Session driver [$driver] is not supported.");
  72. }
  73. }
  74. /**
  75. * Retrieve the active session payload instance for the request.
  76. *
  77. * <code>
  78. * // Retrieve the session instance and get an item
  79. * Session::instance()->get('name');
  80. *
  81. * // Retrieve the session instance and place an item in the session
  82. * Session::instance()->put('name', 'Taylor');
  83. * </code>
  84. *
  85. * @return Session\Payload
  86. */
  87. public static function instance()
  88. {
  89. if (static::started()) return static::$instance;
  90. throw new \Exception("A driver must be set before using the session.");
  91. }
  92. /**
  93. * Determine if session handling has been started for the request.
  94. *
  95. * @return bool
  96. */
  97. public static function started()
  98. {
  99. return ! is_null(static::$instance);
  100. }
  101. /**
  102. * Register a third-party cache driver.
  103. *
  104. * @param string $driver
  105. * @param Closure $resolver
  106. * @return void
  107. */
  108. public static function extend($driver, Closure $resolver)
  109. {
  110. static::$registrar[$driver] = $resolver;
  111. }
  112. /**
  113. * Magic Method for calling the methods on the session singleton instance.
  114. *
  115. * <code>
  116. * // Retrieve a value from the session
  117. * $value = Session::get('name');
  118. *
  119. * // Write a value to the session storage
  120. * $value = Session::put('name', 'Taylor');
  121. *
  122. * // Equivalent statement using the "instance" method
  123. * $value = Session::instance()->put('name', 'Taylor');
  124. * </code>
  125. */
  126. public static function __callStatic($method, $parameters)
  127. {
  128. return call_user_func_array(array(static::instance(), $method), $parameters);
  129. }
  130. }