Handler.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. if (PHP_VERSION_ID >= 50400) {
  45. session_set_save_handler($this, true);
  46. } else {
  47. session_set_save_handler(
  48. array($this, 'open'),
  49. array($this, 'close'),
  50. array($this, 'read'),
  51. array($this, 'write'),
  52. array($this, 'destroy'),
  53. array($this, 'gc')
  54. );
  55. }
  56. }
  57. /**
  58. * {@inheritdoc}
  59. */
  60. public function open($save_path, $session_id)
  61. {
  62. // NOOP
  63. return true;
  64. }
  65. /**
  66. * {@inheritdoc}
  67. */
  68. public function close()
  69. {
  70. // NOOP
  71. return true;
  72. }
  73. /**
  74. * {@inheritdoc}
  75. */
  76. public function gc($maxlifetime)
  77. {
  78. // NOOP
  79. return true;
  80. }
  81. /**
  82. * {@inheritdoc}
  83. */
  84. public function read($session_id)
  85. {
  86. if ($data = $this->client->get($session_id)) {
  87. return $data;
  88. }
  89. return '';
  90. }
  91. /**
  92. * {@inheritdoc}
  93. */
  94. public function write($session_id, $session_data)
  95. {
  96. $this->client->setex($session_id, $this->ttl, $session_data);
  97. return true;
  98. }
  99. /**
  100. * {@inheritdoc}
  101. */
  102. public function destroy($session_id)
  103. {
  104. $this->client->del($session_id);
  105. return true;
  106. }
  107. /**
  108. * Returns the underlying client instance.
  109. *
  110. * @return ClientInterface
  111. */
  112. public function getClient()
  113. {
  114. return $this->client;
  115. }
  116. /**
  117. * Returns the session max lifetime value.
  118. *
  119. * @return int
  120. */
  121. public function getMaxLifeTime()
  122. {
  123. return $this->ttl;
  124. }
  125. }