Handler.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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
  18. * provided that a polyfill for `SessionHandlerInterface` is defined by either
  19. * you or an external package such as `symfony/http-foundation`.
  20. *
  21. * @author Daniele Alessandri <suppakilla@gmail.com>
  22. */
  23. class Handler 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. if (isset($options['gc_maxlifetime'])) {
  35. $this->ttl = (int) $options['gc_maxlifetime'];
  36. } else {
  37. $this->ttl = ini_get('session.gc_maxlifetime');
  38. }
  39. }
  40. /**
  41. * Registers this instance as the current session handler.
  42. */
  43. public function register()
  44. {
  45. if (version_compare(PHP_VERSION, '5.4.0') >= 0) {
  46. session_set_save_handler($this, true);
  47. } else {
  48. session_set_save_handler(
  49. array($this, 'open'),
  50. array($this, 'close'),
  51. array($this, 'read'),
  52. array($this, 'write'),
  53. array($this, 'destroy'),
  54. array($this, 'gc')
  55. );
  56. }
  57. }
  58. /**
  59. * {@inheritdoc}
  60. */
  61. public function open($save_path, $session_id)
  62. {
  63. // NOOP
  64. return true;
  65. }
  66. /**
  67. * {@inheritdoc}
  68. */
  69. public function close()
  70. {
  71. // NOOP
  72. return true;
  73. }
  74. /**
  75. * {@inheritdoc}
  76. */
  77. public function gc($maxlifetime)
  78. {
  79. // NOOP
  80. return true;
  81. }
  82. /**
  83. * {@inheritdoc}
  84. */
  85. public function read($session_id)
  86. {
  87. if ($data = $this->client->get($session_id)) {
  88. return $data;
  89. }
  90. return '';
  91. }
  92. /**
  93. * {@inheritdoc}
  94. */
  95. public function write($session_id, $session_data)
  96. {
  97. $this->client->setex($session_id, $this->ttl, $session_data);
  98. return true;
  99. }
  100. /**
  101. * {@inheritdoc}
  102. */
  103. public function destroy($session_id)
  104. {
  105. $this->client->del($session_id);
  106. return true;
  107. }
  108. /**
  109. * Returns the underlying client instance.
  110. *
  111. * @return ClientInterface
  112. */
  113. public function getClient()
  114. {
  115. return $this->client;
  116. }
  117. /**
  118. * Returns the session max lifetime value.
  119. *
  120. * @return int
  121. */
  122. public function getMaxLifeTime()
  123. {
  124. return $this->ttl;
  125. }
  126. }