AbstractProxy.php 2.3 KB

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