/** * Fetch the SessionInfo(s) for a request * @param WebRequest $request * @return SessionInfo|null */ private function getSessionInfoForRequest(WebRequest $request) { // Call all providers to fetch "the" session $infos = array(); foreach ($this->getProviders() as $provider) { $info = $provider->provideSessionInfo($request); if (!$info) { continue; } if ($info->getProvider() !== $provider) { throw new \UnexpectedValueException("{$provider} returned session info for a different provider: {$info}"); } $infos[] = $info; } // Sort the SessionInfos. Then find the first one that can be // successfully loaded, and then all the ones after it with the same // priority. usort($infos, 'MediaWiki\\Session\\SessionInfo::compare'); $retInfos = array(); while ($infos) { $info = array_pop($infos); if ($this->loadSessionInfoFromStore($info, $request)) { $retInfos[] = $info; while ($infos) { $info = array_pop($infos); if (SessionInfo::compare($retInfos[0], $info)) { // We hit a lower priority, stop checking. break; } if ($this->loadSessionInfoFromStore($info, $request)) { // This is going to error out below, but we want to // provide a complete list. $retInfos[] = $info; } } } } if (count($retInfos) > 1) { $ex = new \OverflowException('Multiple sessions for this request tied for top priority: ' . join(', ', $retInfos)); $ex->sessionInfos = $retInfos; throw $ex; } return $retInfos ? $retInfos[0] : null; }
public function testCompare() { $id = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'; $info1 = new SessionInfo(SessionInfo::MIN_PRIORITY + 1, array('id' => $id)); $info2 = new SessionInfo(SessionInfo::MIN_PRIORITY + 2, array('id' => $id)); $this->assertTrue(SessionInfo::compare($info1, $info2) < 0, '<'); $this->assertTrue(SessionInfo::compare($info2, $info1) > 0, '>'); $this->assertTrue(SessionInfo::compare($info1, $info1) === 0, '=='); }