SessionComponent.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. namespace Tools\Controller\Component;
  3. use Cake\Controller\Component;
  4. /**
  5. * The CakePHP SessionComponent provides a way to persist client data between
  6. * page requests. It acts as a wrapper for the `$_SESSION` as well as providing
  7. * convenience methods for several `$_SESSION` related functions.
  8. *
  9. * This class is here for backwards compatibility with CakePHP 2.x
  10. */
  11. class SessionComponent extends Component {
  12. /**
  13. * The Session object instance
  14. *
  15. * @var \Cake\Network\Session
  16. */
  17. protected $_session;
  18. /**
  19. * Initialize properties.
  20. *
  21. * @param array $config The config data.
  22. * @return void
  23. */
  24. public function initialize(array $config) {
  25. $this->_session = $this->_registry->getController()->request->session();
  26. }
  27. /**
  28. * Used to write a value to a session key.
  29. *
  30. * In your controller: $this->Session->write('Controller.sessKey', 'session value');
  31. *
  32. * @param string $name The name of the key your are setting in the session.
  33. * This should be in a Controller.key format for better organizing
  34. * @param string $value The value you want to store in a session.
  35. * @return void
  36. * @link http://book.cakephp.org/2.0/en/core-libraries/components/sessions.html#SessionComponent::write
  37. */
  38. public function write($name, $value = null) {
  39. $this->_session->write($name, $value);
  40. }
  41. /**
  42. * Used to read a session values for a key or return values for all keys.
  43. *
  44. * In your controller: $this->Session->read('Controller.sessKey');
  45. * Calling the method without a param will return all session vars
  46. *
  47. * @param string $name the name of the session key you want to read
  48. * @return mixed value from the session vars
  49. * @link http://book.cakephp.org/2.0/en/core-libraries/components/sessions.html#SessionComponent::read
  50. */
  51. public function read($name = null) {
  52. return $this->_session->read($name);
  53. }
  54. /**
  55. * Wrapper for SessionComponent::del();
  56. *
  57. * In your controller: $this->Session->delete('Controller.sessKey');
  58. *
  59. * @param string $name the name of the session key you want to delete
  60. * @return void
  61. * @link http://book.cakephp.org/2.0/en/core-libraries/components/sessions.html#SessionComponent::delete
  62. */
  63. public function delete($name) {
  64. $this->_session->delete($name);
  65. }
  66. /**
  67. * Used to check if a session variable is set
  68. *
  69. * In your controller: $this->Session->check('Controller.sessKey');
  70. *
  71. * @param string $name the name of the session key you want to check
  72. * @return bool true is session variable is set, false if not
  73. * @link http://book.cakephp.org/2.0/en/core-libraries/components/sessions.html#SessionComponent::check
  74. */
  75. public function check($name) {
  76. return $this->_session->check($name);
  77. }
  78. /**
  79. * Used to renew a session id
  80. *
  81. * In your controller: $this->Session->renew();
  82. *
  83. * @return void
  84. */
  85. public function renew() {
  86. $this->_session->renew();
  87. }
  88. /**
  89. * Used to destroy sessions
  90. *
  91. * In your controller: $this->Session->destroy();
  92. *
  93. * @return void
  94. * @link http://book.cakephp.org/2.0/en/core-libraries/components/sessions.html#SessionComponent::destroy
  95. */
  96. public function destroy() {
  97. $this->_session->destroy();
  98. }
  99. /**
  100. * Get/Set the session id.
  101. *
  102. * When fetching the session id, the session will be started
  103. * if it has not already been started. When setting the session id,
  104. * the session will not be started.
  105. *
  106. * @param string $id Id to use (optional)
  107. * @return string The current session id.
  108. */
  109. public function id($id = null) {
  110. if ($id === null) {
  111. $session = $this->_session;
  112. $session->start();
  113. return $session->id();
  114. }
  115. $this->_session->id($id);
  116. }
  117. /**
  118. * Returns a bool, whether or not the session has been started.
  119. *
  120. * @return bool
  121. */
  122. public function started() {
  123. return $this->_session->started();
  124. }
  125. /**
  126. * Events supported by this component.
  127. *
  128. * @return array
  129. */
  130. public function implementedEvents() {
  131. return [];
  132. }
  133. }