AbstractProxy.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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
  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. * @param string $id
  75. *
  76. * @throws \LogicException
  77. */
  78. public function setId($id)
  79. {
  80. if ($this->isActive()) {
  81. throw new \LogicException('Cannot change the ID of an active session');
  82. }
  83. session_id($id);
  84. }
  85. /**
  86. * Gets the session name.
  87. *
  88. * @return string
  89. */
  90. public function getName()
  91. {
  92. return session_name();
  93. }
  94. /**
  95. * Sets the session name.
  96. *
  97. * @param string $name
  98. *
  99. * @throws \LogicException
  100. */
  101. public function setName($name)
  102. {
  103. if ($this->isActive()) {
  104. throw new \LogicException('Cannot change the name of an active session');
  105. }
  106. session_name($name);
  107. }
  108. }