AbstractProxy.php 2.2 KB

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