SaveHandlerFactory.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Session;
  7. /**
  8. * Magento session save handler factory
  9. */
  10. class SaveHandlerFactory
  11. {
  12. /**
  13. * Php native session handler
  14. */
  15. const PHP_NATIVE_HANDLER = \Magento\Framework\Session\SaveHandler\Native::class;
  16. /**
  17. * Object manager
  18. *
  19. * @var \Magento\Framework\ObjectManagerInterface
  20. */
  21. protected $objectManager;
  22. /**
  23. * Handlers
  24. *
  25. * @var array
  26. */
  27. protected $handlers = [];
  28. /**
  29. * Constructor
  30. *
  31. * @param \Magento\Framework\ObjectManagerInterface $objectManger
  32. * @param array $handlers
  33. */
  34. public function __construct(\Magento\Framework\ObjectManagerInterface $objectManger, array $handlers = [])
  35. {
  36. $this->objectManager = $objectManger;
  37. if (!empty($handlers)) {
  38. $this->handlers = array_merge($handlers, $this->handlers);
  39. }
  40. }
  41. /**
  42. * Create session save handler
  43. *
  44. * @param string $saveMethod
  45. * @param array $params
  46. * @return \SessionHandler
  47. * @throws \LogicException
  48. */
  49. public function create($saveMethod, $params = [])
  50. {
  51. $sessionHandler = self::PHP_NATIVE_HANDLER;
  52. if (isset($this->handlers[$saveMethod])) {
  53. $sessionHandler = $this->handlers[$saveMethod];
  54. }
  55. $model = $this->objectManager->create($sessionHandler, $params);
  56. if (!$model instanceof \SessionHandlerInterface) {
  57. throw new \LogicException($sessionHandler . ' doesn\'t implement \SessionHandlerInterface');
  58. }
  59. return $model;
  60. }
  61. }