AbstractProxy.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. * AbstractProxy.
  13. *
  14. * @author Drak <drak@zikula.org>
  15. */
  16. abstract class AbstractProxy
  17. {
  18. /**
  19. * Flag if handler wraps an internal PHP session handler (using \SessionHandler).
  20. *
  21. * @var bool
  22. */
  23. protected $wrapper = false;
  24. /**
  25. * @var bool
  26. */
  27. protected $active = false;
  28. /**
  29. * @var string
  30. */
  31. protected $saveHandlerName;
  32. /**
  33. * Gets the session.save_handler name.
  34. *
  35. * @return string
  36. */
  37. public function getSaveHandlerName()
  38. {
  39. return $this->saveHandlerName;
  40. }
  41. /**
  42. * Is this proxy handler and instance of \SessionHandlerInterface.
  43. *
  44. * @return bool
  45. */
  46. public function isSessionHandlerInterface()
  47. {
  48. return $this instanceof \SessionHandlerInterface;
  49. }
  50. /**
  51. * Returns true if this handler wraps an internal PHP session save handler using \SessionHandler.
  52. *
  53. * @return bool
  54. */
  55. public function isWrapper()
  56. {
  57. return $this->wrapper;
  58. }
  59. /**
  60. * Has a session started?
  61. *
  62. * @return bool
  63. */
  64. public function isActive()
  65. {
  66. if (PHP_VERSION_ID >= 50400) {
  67. return $this->active = \PHP_SESSION_ACTIVE === session_status();
  68. }
  69. return $this->active;
  70. }
  71. /**
  72. * Sets the active flag.
  73. *
  74. * Has no effect under PHP 5.4+ as status is detected
  75. * automatically in isActive()
  76. *
  77. * @internal
  78. *
  79. * @param bool $flag
  80. *
  81. * @throws \LogicException
  82. */
  83. public function setActive($flag)
  84. {
  85. if (PHP_VERSION_ID >= 50400) {
  86. throw new \LogicException('This method is disabled in PHP 5.4.0+');
  87. }
  88. $this->active = (bool) $flag;
  89. }
  90. /**
  91. * Gets the session ID.
  92. *
  93. * @return string
  94. */
  95. public function getId()
  96. {
  97. return session_id();
  98. }
  99. /**
  100. * Sets the session ID.
  101. *
  102. * @param string $id
  103. *
  104. * @throws \LogicException
  105. */
  106. public function setId($id)
  107. {
  108. if ($this->isActive()) {
  109. throw new \LogicException('Cannot change the ID of an active session');
  110. }
  111. session_id($id);
  112. }
  113. /**
  114. * Gets the session name.
  115. *
  116. * @return string
  117. */
  118. public function getName()
  119. {
  120. return session_name();
  121. }
  122. /**
  123. * Sets the session name.
  124. *
  125. * @param string $name
  126. *
  127. * @throws \LogicException
  128. */
  129. public function setName($name)
  130. {
  131. if ($this->isActive()) {
  132. throw new \LogicException('Cannot change the name of an active session');
  133. }
  134. session_name($name);
  135. }
  136. }