SaveHandler.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Session;
  7. use Magento\Framework\Session\Config\ConfigInterface;
  8. use \Magento\Framework\Exception\SessionException;
  9. /**
  10. * Magento session save handler
  11. */
  12. class SaveHandler implements SaveHandlerInterface
  13. {
  14. /**
  15. * Session handler
  16. *
  17. * @var \SessionHandler
  18. */
  19. protected $saveHandlerAdapter;
  20. /**
  21. * Constructor
  22. *
  23. * @param SaveHandlerFactory $saveHandlerFactory
  24. * @param ConfigInterface $sessionConfig
  25. * @param string $default
  26. */
  27. public function __construct(
  28. SaveHandlerFactory $saveHandlerFactory,
  29. ConfigInterface $sessionConfig,
  30. $default = self::DEFAULT_HANDLER
  31. ) {
  32. /**
  33. * Session handler
  34. *
  35. * Save handler may be set to custom value in deployment config, which will override everything else.
  36. * Otherwise, try to read PHP settings for session.save_handler value. Otherwise, use 'files' as default.
  37. */
  38. $saveMethod = $sessionConfig->getOption('session.save_handler') ?: $default;
  39. try {
  40. $this->saveHandlerAdapter = $saveHandlerFactory->create($saveMethod);
  41. } catch (SessionException $e) {
  42. $this->saveHandlerAdapter = $saveHandlerFactory->create($default);
  43. }
  44. }
  45. /**
  46. * Open Session - retrieve resources
  47. *
  48. * @param string $savePath
  49. * @param string $name
  50. * @return bool
  51. */
  52. public function open($savePath, $name)
  53. {
  54. return $this->saveHandlerAdapter->open($savePath, $name);
  55. }
  56. /**
  57. * Close Session - free resources
  58. *
  59. * @return bool
  60. */
  61. public function close()
  62. {
  63. return $this->saveHandlerAdapter->close();
  64. }
  65. /**
  66. * Read session data
  67. *
  68. * @param string $sessionId
  69. * @return string
  70. */
  71. public function read($sessionId)
  72. {
  73. return $this->saveHandlerAdapter->read($sessionId);
  74. }
  75. /**
  76. * Write Session - commit data to resource
  77. *
  78. * @param string $sessionId
  79. * @param string $data
  80. * @return bool
  81. */
  82. public function write($sessionId, $data)
  83. {
  84. return $this->saveHandlerAdapter->write($sessionId, $data);
  85. }
  86. /**
  87. * Destroy Session - remove data from resource for given session id
  88. *
  89. * @param string $sessionId
  90. * @return bool
  91. */
  92. public function destroy($sessionId)
  93. {
  94. return $this->saveHandlerAdapter->destroy($sessionId);
  95. }
  96. /**
  97. * Garbage Collection - remove old session data older
  98. * than $maxLifetime (in seconds)
  99. *
  100. * @param int $maxLifetime
  101. * @return bool
  102. * @SuppressWarnings(PHPMD.ShortMethodName)
  103. */
  104. public function gc($maxLifetime)
  105. {
  106. return $this->saveHandlerAdapter->gc($maxLifetime);
  107. }
  108. }