manager.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php namespace Laravel\Session;
  2. use Laravel\Str;
  3. use Laravel\Config;
  4. use Laravel\Session\Drivers\Driver;
  5. use Laravel\Session\Transporters\Transporter;
  6. class Manager {
  7. /**
  8. * The session driver instance.
  9. *
  10. * @var Driver
  11. */
  12. private $driver;
  13. /**
  14. * The session identifier transporter instance.
  15. *
  16. * @var Transporter
  17. */
  18. private $transporter;
  19. /**
  20. * The configuration manager instance.
  21. *
  22. * @var Config
  23. */
  24. private $config;
  25. /**
  26. * The session payload instance.
  27. *
  28. * @var Payload
  29. */
  30. private $payload;
  31. /**
  32. * Create a new session manager instance.
  33. *
  34. * @param Driver $driver
  35. * @param Transporter $transporter
  36. * @param Config $config
  37. * @return void
  38. */
  39. public function __construct(Driver $driver, Transporter $transporter, Config $config)
  40. {
  41. $this->driver = $driver;
  42. $this->config = $config;
  43. $this->transporter = $transporter;
  44. }
  45. /**
  46. * Get the session payload for the request.
  47. *
  48. * @return Payload
  49. */
  50. public function payload()
  51. {
  52. $session = $this->driver->load($this->transporter->get());
  53. // If the session is expired, a new session will be generated and all of the data from
  54. // the previous session will be lost. The new session will be assigned a random, long
  55. // string ID to uniquely identify it among the application's current users.
  56. if (is_null($session) or $this->expired($session))
  57. {
  58. $session = array('id' => Str::random(40), 'data' => array());
  59. }
  60. $payload = new Payload($session);
  61. // If a CSRF token is not present in the session, we will generate one. These tokens
  62. // are generated per session to protect against Cross-Site Request Forgery attacks on
  63. // the application. It is up to the developer to take advantage of them using the token
  64. // methods on the Form class and the "csrf" route filter.
  65. if ( ! $payload->has('csrf_token')) $payload->put('csrf_token', Str::random(16));
  66. return $payload;
  67. }
  68. /**
  69. * Deteremine if the session is expired based on the last activity timestamp
  70. * and the session lifetime set in the configuration file.
  71. *
  72. * @param array $payload
  73. * @return bool
  74. */
  75. private function expired($payload)
  76. {
  77. return (time() - $payload['last_activity']) > ($this->config->get('session.lifetime') * 60);
  78. }
  79. /**
  80. * Close the session handling for the request.
  81. *
  82. * @param Payload $payload
  83. * @return void
  84. */
  85. public function close(Payload $payload)
  86. {
  87. $config = $this->config->get('session');
  88. $this->driver->save($payload->age(), $config);
  89. $this->transporter->put($payload->session['id'], $config);
  90. // Some session drivers implement the Sweeper interface, which specified that the driver
  91. // must do its garbage collection manually. Alternatively, some drivers such as APC and
  92. // Memcached are not required to manually clean up their sessions.
  93. if (mt_rand(1, $config['sweepage'][1]) <= $config['sweepage'][0] and $this->driver instanceof Sweeper)
  94. {
  95. $this->driver->sweep(time() - ($config['lifetime'] * 60));
  96. }
  97. }
  98. /**
  99. * Magic Method for calling methods on the session payload instance.
  100. */
  101. public function __call($method, $parameters)
  102. {
  103. if (method_exists($this->payload, $method))
  104. {
  105. return call_user_func_array(array($this->payload, $method), $parameters);
  106. }
  107. throw new \Exception("Attempting to call undefined method [$method] on session manager.");
  108. }
  109. }