AbstractProxy.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\HttpFoundation\Session\Storage\Proxy;
  11. /**
  12. * @author Drak <drak@zikula.org>
  13. */
  14. abstract class AbstractProxy
  15. {
  16. protected bool $wrapper = false;
  17. protected ?string $saveHandlerName = null;
  18. /**
  19. * Gets the session.save_handler name.
  20. */
  21. public function getSaveHandlerName(): ?string
  22. {
  23. return $this->saveHandlerName;
  24. }
  25. /**
  26. * Is this proxy handler and instance of \SessionHandlerInterface.
  27. */
  28. public function isSessionHandlerInterface(): bool
  29. {
  30. return $this instanceof \SessionHandlerInterface;
  31. }
  32. /**
  33. * Returns true if this handler wraps an internal PHP session save handler using \SessionHandler.
  34. */
  35. public function isWrapper(): bool
  36. {
  37. return $this->wrapper;
  38. }
  39. /**
  40. * Has a session started?
  41. */
  42. public function isActive(): bool
  43. {
  44. return \PHP_SESSION_ACTIVE === session_status();
  45. }
  46. /**
  47. * Gets the session ID.
  48. */
  49. public function getId(): string
  50. {
  51. return session_id();
  52. }
  53. /**
  54. * Sets the session ID.
  55. *
  56. * @throws \LogicException
  57. */
  58. public function setId(string $id): void
  59. {
  60. if ($this->isActive()) {
  61. throw new \LogicException('Cannot change the ID of an active session.');
  62. }
  63. session_id($id);
  64. }
  65. /**
  66. * Gets the session name.
  67. */
  68. public function getName(): string
  69. {
  70. return session_name();
  71. }
  72. /**
  73. * Sets the session name.
  74. *
  75. * @throws \LogicException
  76. */
  77. public function setName(string $name): void
  78. {
  79. if ($this->isActive()) {
  80. throw new \LogicException('Cannot change the name of an active session.');
  81. }
  82. session_name($name);
  83. }
  84. }