SessionHandler.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. /*
  3. * This file is part of the Predis package.
  4. *
  5. * (c) Daniele Alessandri <suppakilla@gmail.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Predis\Session;
  11. use SessionHandlerInterface;
  12. use Predis\ClientInterface;
  13. /**
  14. * Session handler class that relies on Predis\Client to store PHP's sessions
  15. * data into one or multiple Redis servers.
  16. *
  17. * This class is mostly intended for PHP 5.4 but it can be used under PHP 5.3 provided
  18. * that a polyfill for `SessionHandlerInterface` is defined by either you or an external
  19. * package such as `symfony/http-foundation`.
  20. *
  21. * @author Daniele Alessandri <suppakilla@gmail.com>
  22. */
  23. class SessionHandler implements SessionHandlerInterface
  24. {
  25. protected $client;
  26. protected $ttl;
  27. /**
  28. * @param ClientInterface $client Fully initialized client instance.
  29. * @param array $options Session handler options.
  30. */
  31. public function __construct(ClientInterface $client, Array $options = array())
  32. {
  33. $this->client = $client;
  34. $this->ttl = (int) (isset($options['gc_maxlifetime']) ? $options['gc_maxlifetime'] : ini_get('session.gc_maxlifetime'));
  35. }
  36. /**
  37. * Registers the handler instance as the current session handler.
  38. */
  39. public function register()
  40. {
  41. if (version_compare(PHP_VERSION, '5.4.0') >= 0) {
  42. session_set_save_handler($this, true);
  43. } else {
  44. session_set_save_handler(
  45. array($this, 'open'),
  46. array($this, 'close'),
  47. array($this, 'read'),
  48. array($this, 'write'),
  49. array($this, 'destroy'),
  50. array($this, 'gc')
  51. );
  52. }
  53. }
  54. /**
  55. * {@inheritdoc}
  56. */
  57. public function open($save_path, $session_id)
  58. {
  59. // NOOP
  60. return true;
  61. }
  62. /**
  63. * {@inheritdoc}
  64. */
  65. public function close()
  66. {
  67. // NOOP
  68. return true;
  69. }
  70. /**
  71. * {@inheritdoc}
  72. */
  73. public function gc($maxlifetime)
  74. {
  75. // NOOP
  76. return true;
  77. }
  78. /**
  79. * {@inheritdoc}
  80. */
  81. public function read($session_id)
  82. {
  83. if ($data = $this->client->get($session_id)) {
  84. return $data;
  85. }
  86. return '';
  87. }
  88. /**
  89. * {@inheritdoc}
  90. */
  91. public function write($session_id, $session_data)
  92. {
  93. $this->client->setex($session_id, $this->ttl, $session_data);
  94. return true;
  95. }
  96. /**
  97. * {@inheritdoc}
  98. */
  99. public function destroy($session_id)
  100. {
  101. $this->client->del($session_id);
  102. return true;
  103. }
  104. /**
  105. * Returns the underlying client instance.
  106. *
  107. * @return ClientInterface
  108. */
  109. public function getClient()
  110. {
  111. return $this->client;
  112. }
  113. /**
  114. * Returns the session max lifetime value.
  115. *
  116. * @return int
  117. */
  118. public function getMaxLifeTime()
  119. {
  120. return $this->ttl;
  121. }
  122. }