Storage.php 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. /**
  3. * Default session storage
  4. *
  5. * Copyright © Magento, Inc. All rights reserved.
  6. * See COPYING.txt for license details.
  7. */
  8. namespace Magento\Framework\Session;
  9. class Storage extends \Magento\Framework\DataObject implements StorageInterface
  10. {
  11. /**
  12. * Namespace of storage
  13. *
  14. * @var string
  15. */
  16. protected $namespace;
  17. /**
  18. * Constructor
  19. *
  20. * @param string $namespace
  21. * @param array $data
  22. */
  23. public function __construct($namespace = 'default', array $data = [])
  24. {
  25. $this->namespace = $namespace;
  26. parent::__construct($data);
  27. }
  28. /**
  29. * {@inheritdoc}
  30. */
  31. public function init(array $data)
  32. {
  33. $namespace = $this->getNamespace();
  34. if (isset($data[$namespace])) {
  35. $this->setData($data[$namespace]);
  36. }
  37. $_SESSION[$namespace] = & $this->_data;
  38. }
  39. /**
  40. * {@inheritdoc}
  41. */
  42. public function getNamespace()
  43. {
  44. return $this->namespace;
  45. }
  46. /**
  47. * Additional get data with clear mode
  48. *
  49. * @param string $key
  50. * @param bool $clear
  51. * @return mixed
  52. */
  53. public function getData($key = '', $clear = false)
  54. {
  55. $data = parent::getData($key);
  56. if ($clear && isset($this->_data[$key])) {
  57. unset($this->_data[$key]);
  58. }
  59. return $data;
  60. }
  61. }