AbstractProxy.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. /**
  17. * Flag if handler wraps an internal PHP session handler (using \SessionHandler).
  18. *
  19. * @var bool
  20. */
  21. protected $wrapper = false;
  22. /**
  23. * @var string
  24. */
  25. protected $saveHandlerName;
  26. /**
  27. * Gets the session.save_handler name.
  28. */
  29. public function getSaveHandlerName(): ?string
  30. {
  31. return $this->saveHandlerName;
  32. }
  33. /**
  34. * Is this proxy handler and instance of \SessionHandlerInterface.
  35. */
  36. public function isSessionHandlerInterface(): bool
  37. {
  38. return $this instanceof \SessionHandlerInterface;
  39. }
  40. /**
  41. * Returns true if this handler wraps an internal PHP session save handler using \SessionHandler.
  42. */
  43. public function isWrapper(): bool
  44. {
  45. return $this->wrapper;
  46. }
  47. /**
  48. * Has a session started?
  49. */
  50. public function isActive(): bool
  51. {
  52. return \PHP_SESSION_ACTIVE === session_status();
  53. }
  54. /**
  55. * Gets the session ID.
  56. */
  57. public function getId(): string
  58. {
  59. return session_id();
  60. }
  61. /**
  62. * Sets the session ID.
  63. *
  64. * @throws \LogicException
  65. */
  66. public function setId(string $id)
  67. {
  68. if ($this->isActive()) {
  69. throw new \LogicException('Cannot change the ID of an active session.');
  70. }
  71. session_id($id);
  72. }
  73. /**
  74. * Gets the session name.
  75. */
  76. public function getName(): string
  77. {
  78. return session_name();
  79. }
  80. /**
  81. * Sets the session name.
  82. *
  83. * @throws \LogicException
  84. */
  85. public function setName(string $name)
  86. {
  87. if ($this->isActive()) {
  88. throw new \LogicException('Cannot change the name of an active session.');
  89. }
  90. session_name($name);
  91. }
  92. }