Mink.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. <?php
  2. /*
  3. * This file is part of the Mink package.
  4. * (c) Konstantin Kudryashov <ever.zet@gmail.com>
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. namespace Behat\Mink;
  10. /**
  11. * Mink sessions manager.
  12. *
  13. * @author Konstantin Kudryashov <ever.zet@gmail.com>
  14. */
  15. class Mink
  16. {
  17. private $defaultSessionName;
  18. /**
  19. * Sessions.
  20. *
  21. * @var Session[]
  22. */
  23. private $sessions = array();
  24. /**
  25. * Initializes manager.
  26. *
  27. * @param Session[] $sessions
  28. */
  29. public function __construct(array $sessions = array())
  30. {
  31. foreach ($sessions as $name => $session) {
  32. $this->registerSession($name, $session);
  33. }
  34. }
  35. /**
  36. * Stops all started sessions.
  37. */
  38. public function __destruct()
  39. {
  40. $this->stopSessions();
  41. }
  42. /**
  43. * Registers new session.
  44. *
  45. * @param string $name
  46. * @param Session $session
  47. */
  48. public function registerSession($name, Session $session)
  49. {
  50. $name = strtolower($name);
  51. $this->sessions[$name] = $session;
  52. }
  53. /**
  54. * Checks whether session with specified name is registered.
  55. *
  56. * @param string $name
  57. *
  58. * @return Boolean
  59. */
  60. public function hasSession($name)
  61. {
  62. return isset($this->sessions[strtolower($name)]);
  63. }
  64. /**
  65. * Sets default session name to use.
  66. *
  67. * @param string $name name of the registered session
  68. *
  69. * @throws \InvalidArgumentException
  70. */
  71. public function setDefaultSessionName($name)
  72. {
  73. $name = strtolower($name);
  74. if (!isset($this->sessions[$name])) {
  75. throw new \InvalidArgumentException(sprintf('Session "%s" is not registered.', $name));
  76. }
  77. $this->defaultSessionName = $name;
  78. }
  79. /**
  80. * Returns default session name or null if none.
  81. *
  82. * @return null|string
  83. */
  84. public function getDefaultSessionName()
  85. {
  86. return $this->defaultSessionName;
  87. }
  88. /**
  89. * Returns registered session by it's name or active one and automatically starts it if required.
  90. *
  91. * @param string $name session name
  92. *
  93. * @return Session
  94. *
  95. * @throws \InvalidArgumentException If the named session is not registered
  96. */
  97. public function getSession($name = null)
  98. {
  99. $session = $this->locateSession($name);
  100. // start session if needed
  101. if (!$session->isStarted()) {
  102. $session->start();
  103. }
  104. return $session;
  105. }
  106. /**
  107. * Checks whether a named session (or the default session) has already been started.
  108. *
  109. * @param string $name session name - if null then the default session will be checked
  110. *
  111. * @return bool whether the session has been started
  112. *
  113. * @throws \InvalidArgumentException If the named session is not registered
  114. */
  115. public function isSessionStarted($name = null)
  116. {
  117. $session = $this->locateSession($name);
  118. return $session->isStarted();
  119. }
  120. /**
  121. * Returns session asserter.
  122. *
  123. * @param Session|string $session session object or name
  124. *
  125. * @return WebAssert
  126. */
  127. public function assertSession($session = null)
  128. {
  129. if (!($session instanceof Session)) {
  130. $session = $this->getSession($session);
  131. }
  132. return new WebAssert($session);
  133. }
  134. /**
  135. * Resets all started sessions.
  136. */
  137. public function resetSessions()
  138. {
  139. foreach ($this->sessions as $session) {
  140. if ($session->isStarted()) {
  141. $session->reset();
  142. }
  143. }
  144. }
  145. /**
  146. * Restarts all started sessions.
  147. */
  148. public function restartSessions()
  149. {
  150. foreach ($this->sessions as $session) {
  151. if ($session->isStarted()) {
  152. $session->restart();
  153. }
  154. }
  155. }
  156. /**
  157. * Stops all started sessions.
  158. */
  159. public function stopSessions()
  160. {
  161. foreach ($this->sessions as $session) {
  162. if ($session->isStarted()) {
  163. $session->stop();
  164. }
  165. }
  166. }
  167. /**
  168. * Returns the named or default session without starting it.
  169. *
  170. * @param string $name session name
  171. *
  172. * @return Session
  173. *
  174. * @throws \InvalidArgumentException If the named session is not registered
  175. */
  176. protected function locateSession($name = null)
  177. {
  178. $name = strtolower($name) ?: $this->defaultSessionName;
  179. if (null === $name) {
  180. throw new \InvalidArgumentException('Specify session name to get');
  181. }
  182. if (!isset($this->sessions[$name])) {
  183. throw new \InvalidArgumentException(sprintf('Session "%s" is not registered.', $name));
  184. }
  185. $session = $this->sessions[$name];
  186. return $session;
  187. }
  188. }