manager.php 3.0 KB

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