AbstractProxy.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. * @return void
  65. *
  66. * @throws \LogicException
  67. */
  68. public function setId(string $id)
  69. {
  70. if ($this->isActive()) {
  71. throw new \LogicException('Cannot change the ID of an active session.');
  72. }
  73. session_id($id);
  74. }
  75. /**
  76. * Gets the session name.
  77. */
  78. public function getName(): string
  79. {
  80. return session_name();
  81. }
  82. /**
  83. * Sets the session name.
  84. *
  85. * @return void
  86. *
  87. * @throws \LogicException
  88. */
  89. public function setName(string $name)
  90. {
  91. if ($this->isActive()) {
  92. throw new \LogicException('Cannot change the name of an active session.');
  93. }
  94. session_name($name);
  95. }
  96. }