Session.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. /**
  3. * @package Grav.Common
  4. *
  5. * @copyright Copyright (C) 2015 - 2018 Trilby Media, LLC. All rights reserved.
  6. * @license MIT License; see LICENSE file for details.
  7. */
  8. namespace Grav\Common;
  9. class Session extends \Grav\Framework\Session\Session
  10. {
  11. /** @var bool */
  12. protected $autoStart = false;
  13. /**
  14. * @return \Grav\Framework\Session\Session
  15. * @deprecated 1.5 Use getInstance() method instead
  16. */
  17. public static function instance()
  18. {
  19. user_error(__CLASS__ . '::' . __FUNCTION__ . '() is deprecated since Grav 1.5, use getInstance() method instead', E_USER_DEPRECATED);
  20. return static::getInstance();
  21. }
  22. /**
  23. * Initialize session.
  24. *
  25. * Code in this function has been moved into SessionServiceProvider class.
  26. */
  27. public function init()
  28. {
  29. if ($this->autoStart) {
  30. $this->start();
  31. $this->autoStart = false;
  32. }
  33. }
  34. /**
  35. * @param bool $auto
  36. * @return $this
  37. */
  38. public function setAutoStart($auto)
  39. {
  40. $this->autoStart = (bool)$auto;
  41. return $this;
  42. }
  43. /**
  44. * Returns attributes.
  45. *
  46. * @return array Attributes
  47. * @deprecated 1.5 Use getAll() method instead
  48. */
  49. public function all()
  50. {
  51. user_error(__CLASS__ . '::' . __FUNCTION__ . '() is deprecated since Grav 1.5, use getAll() method instead', E_USER_DEPRECATED);
  52. return $this->getAll();
  53. }
  54. /**
  55. * Checks if the session was started.
  56. *
  57. * @return Boolean
  58. * @deprecated 1.5 Use isStarted() method instead
  59. */
  60. public function started()
  61. {
  62. user_error(__CLASS__ . '::' . __FUNCTION__ . '() is deprecated since Grav 1.5, use isStarted() method instead', E_USER_DEPRECATED);
  63. return $this->isStarted();
  64. }
  65. /**
  66. * Store something in session temporarily.
  67. *
  68. * @param string $name
  69. * @param mixed $object
  70. * @return $this
  71. */
  72. public function setFlashObject($name, $object)
  73. {
  74. $this->{$name} = serialize($object);
  75. return $this;
  76. }
  77. /**
  78. * Return object and remove it from session.
  79. *
  80. * @param string $name
  81. * @return mixed
  82. */
  83. public function getFlashObject($name)
  84. {
  85. $object = unserialize($this->{$name});
  86. $this->{$name} = null;
  87. return $object;
  88. }
  89. /**
  90. * Store something in cookie temporarily.
  91. *
  92. * @param string $name
  93. * @param mixed $object
  94. * @param int $time
  95. * @return $this
  96. */
  97. public function setFlashCookieObject($name, $object, $time = 60)
  98. {
  99. setcookie($name, json_encode($object), time() + $time, '/');
  100. return $this;
  101. }
  102. /**
  103. * Return object and remove it from the cookie.
  104. *
  105. * @param string $name
  106. * @return mixed|null
  107. */
  108. public function getFlashCookieObject($name)
  109. {
  110. if (isset($_COOKIE[$name])) {
  111. $object = json_decode($_COOKIE[$name]);
  112. setcookie($name, '', time() - 3600, '/');
  113. return $object;
  114. }
  115. return null;
  116. }
  117. }