Handler.php 2.8 KB

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