Просмотр файла app/Services/Session.php

Размер файла: 3.06Kb
  1. <?php
  2.  
  3. declare(strict_types=1);
  4.  
  5. namespace App\Services;
  6.  
  7. /**
  8. * Session class
  9. */
  10. class Session
  11. {
  12. /**
  13. * Get a session variable.
  14. *
  15. * @param string $key
  16. * @param mixed|null $default
  17. *
  18. * @return mixed
  19. */
  20. public function get(string $key, mixed $default = null): mixed
  21. {
  22. $session = $_SESSION;
  23.  
  24. if (! str_contains($key, '.')) {
  25. return $session[$key] ?? $default;
  26. }
  27.  
  28. foreach (explode('.', $key) as $segment) {
  29. if (is_array($session) && array_key_exists($segment, $session)) {
  30. $session = $session[$segment];
  31. } else {
  32. return $default;
  33. }
  34. }
  35.  
  36. return $session;
  37. }
  38.  
  39. /**
  40. * Set a session variable.
  41. *
  42. * @param string $key
  43. * @param mixed $value
  44. *
  45. * @return $this
  46. */
  47. public function set(string $key, mixed $value): static
  48. {
  49. $_SESSION[$key] = $value;
  50.  
  51. return $this;
  52. }
  53.  
  54. /**
  55. * Merge values recursively.
  56. *
  57. * @param string $key
  58. * @param mixed $value
  59. *
  60. * @return $this
  61. */
  62. public function merge(string $key, mixed $value): static
  63. {
  64. if (is_array($value) && is_array($old = $this->get($key))) {
  65. $value = array_merge_recursive($old, $value);
  66. }
  67.  
  68. return $this->set($key, $value);
  69. }
  70.  
  71. /**
  72. * Delete a session variable.
  73. *
  74. * @param string $key
  75. *
  76. * @return $this
  77. */
  78. public function delete(string $key): static
  79. {
  80. if ($this->has($key)) {
  81. unset($_SESSION[$key]);
  82. }
  83.  
  84. return $this;
  85. }
  86.  
  87. /**
  88. * Clear all session variables.
  89. *
  90. * @return $this
  91. */
  92. public function clear(): static
  93. {
  94. $_SESSION = [];
  95.  
  96. return $this;
  97. }
  98.  
  99. /**
  100. * Check if a session variable is set.
  101. *
  102. * @param string $key
  103. *
  104. * @return bool
  105. */
  106. public function has(string $key): bool
  107. {
  108. return array_key_exists($key, $_SESSION);
  109. }
  110.  
  111. /**
  112. * Get or regenerate current session ID.
  113. *
  114. * @param bool $new
  115. *
  116. * @return string
  117. */
  118. public function id(bool $new = false): string
  119. {
  120. if ($new && session_id()) {
  121. session_regenerate_id(true);
  122. }
  123.  
  124. return session_id() ?: '';
  125. }
  126.  
  127. /**
  128. * Destroy the session.
  129. */
  130. public function destroy(): void
  131. {
  132. if ($this->id()) {
  133. session_unset();
  134. session_destroy();
  135. session_write_close();
  136.  
  137. if (ini_get('session.use_cookies')) {
  138. $options = [
  139. 'expires' => strtotime('-1 hour'),
  140. 'path' => '/',
  141. 'domain' => setting('session.cookie_domain'),
  142. 'secure' => setting('session.cookie_secure'),
  143. 'httponly' => setting('session.cookie_httponly'),
  144. 'samesite' => setting('session.cookie_samesite'),
  145. ];
  146.  
  147. setcookie(session_name(), '', $options);
  148. }
  149. }
  150. }
  151. }