4 * This file is part of the Mink package.
5 * (c) Konstantin Kudryashov <ever.zet@gmail.com>
7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code.
14 * Mink sessions manager.
16 * @author Konstantin Kudryashov <ever.zet@gmail.com>
20 private $defaultSessionName;
27 private $sessions = array();
30 * Initializes manager.
32 * @param Session[] $sessions
34 public function __construct(array $sessions = array())
36 foreach ($sessions as $name => $session) {
37 $this->registerSession($name, $session);
42 * Stops all started sessions.
44 public function __destruct()
46 $this->stopSessions();
50 * Registers new session.
53 * @param Session $session
55 public function registerSession($name, Session $session)
57 $name = strtolower($name);
59 $this->sessions[$name] = $session;
63 * Checks whether session with specified name is registered.
69 public function hasSession($name)
71 return isset($this->sessions[strtolower($name)]);
75 * Sets default session name to use.
77 * @param string $name name of the registered session
79 * @throws \InvalidArgumentException
81 public function setDefaultSessionName($name)
83 $name = strtolower($name);
85 if (!isset($this->sessions[$name])) {
86 throw new \InvalidArgumentException(sprintf('Session "%s" is not registered.', $name));
89 $this->defaultSessionName = $name;
93 * Returns default session name or null if none.
97 public function getDefaultSessionName()
99 return $this->defaultSessionName;
103 * Returns registered session by it's name or active one and automatically starts it if required.
105 * @param string $name session name
109 * @throws \InvalidArgumentException If the named session is not registered
111 public function getSession($name = null)
113 $session = $this->locateSession($name);
115 // start session if needed
116 if (!$session->isStarted()) {
124 * Checks whether a named session (or the default session) has already been started.
126 * @param string $name session name - if null then the default session will be checked
128 * @return bool whether the session has been started
130 * @throws \InvalidArgumentException If the named session is not registered
132 public function isSessionStarted($name = null)
134 $session = $this->locateSession($name);
136 return $session->isStarted();
140 * Returns session asserter.
142 * @param Session|string $session session object or name
146 public function assertSession($session = null)
148 if (!($session instanceof Session)) {
149 $session = $this->getSession($session);
152 return new WebAssert($session);
156 * Resets all started sessions.
158 public function resetSessions()
160 foreach ($this->sessions as $session) {
161 if ($session->isStarted()) {
168 * Restarts all started sessions.
170 public function restartSessions()
172 foreach ($this->sessions as $session) {
173 if ($session->isStarted()) {
180 * Stops all started sessions.
182 public function stopSessions()
184 foreach ($this->sessions as $session) {
185 if ($session->isStarted()) {
192 * Returns the named or default session without starting it.
194 * @param string $name session name
198 * @throws \InvalidArgumentException If the named session is not registered
200 protected function locateSession($name = null)
202 $name = strtolower($name) ?: $this->defaultSessionName;
204 if (null === $name) {
205 throw new \InvalidArgumentException('Specify session name to get');
208 if (!isset($this->sessions[$name])) {
209 throw new \InvalidArgumentException(sprintf('Session "%s" is not registered.', $name));
212 $session = $this->sessions[$name];