Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

62 řádky
1.3KB

  1. <?php
  2. namespace Framework\Session\Driver;
  3. class NativeDriver implements Driver
  4. {
  5. private array $config = [];
  6. public function __construct(array $config)
  7. {
  8. $this->config = $config;
  9. if (session_status() !== PHP_SESSION_ACTIVE) {
  10. session_start();
  11. }
  12. }
  13. public function has(string $key): bool
  14. {
  15. $prefix = $this->config['prefix'];
  16. return isset($_SESSION["{$prefix}{$key}"]);
  17. }
  18. public function get(string $key, mixed $default = null): mixed
  19. {
  20. $prefix = $this->config['prefix'];
  21. if (isset($_SESSION["{$prefix}{$key}"])) {
  22. return $_SESSION["{$prefix}{$key}"];
  23. }
  24. return $default;
  25. }
  26. public function put(string $key, mixed $value): static
  27. {
  28. $prefix = $this->config['prefix'];
  29. $_SESSION["{$prefix}{$key}"] = $value;
  30. return $this;
  31. }
  32. public function forget(string $key): static
  33. {
  34. $prefix = $this->config['prefix'];
  35. unset($_SESSION["{$prefix}{$key}"]);
  36. return $this;
  37. }
  38. public function flush(): static
  39. {
  40. $prefix = $this->config['prefix'];
  41. foreach (array_keys($_SESSION) as $key) {
  42. if (str_starts_with($key, $prefix)) {
  43. unset($_SESSION[$key]);
  44. }
  45. }
  46. return $this;
  47. }
  48. }

Powered by TurnKey Linux.