/** * Gets the relevant part (array of settings) of the ini file for a given pattern. * * @param string $pattern * @return array */ private function getIniPart($pattern) { $pattern = strtolower($pattern); $patternhash = Pattern::getHashForParts($pattern); $subkey = SubKey::getIniPartCacheSubKey($patternhash); if (!$this->cache->hasItem('browscap.iniparts.' . $subkey, true)) { $this->logger->debug('cache key "browscap.iniparts.' . $subkey . '" not found'); return []; } $success = null; $file = $this->cache->getItem('browscap.iniparts.' . $subkey, true, $success); if (!$success) { $this->logger->debug('cache key "browscap.iniparts.' . $subkey . '" not found'); return []; } if (!is_array($file) || !count($file)) { $this->logger->debug('cache key "browscap.iniparts.' . $subkey . '" was empty'); return []; } $propertyFormatter = new PropertyFormatter(new PropertyHolder()); $return = []; foreach ($file as $buffer) { list($tmpBuffer, $patterns) = explode("\t", $buffer, 2); if ($tmpBuffer === $patternhash) { $return = json_decode($patterns, true); foreach (array_keys($return) as $property) { $return[$property] = $propertyFormatter->formatPropertyValue($return[$property], $property); } break; } } return $return; }
/** * Gets some possible patterns that have to be matched against the user agent. With the given * user agent string, we can optimize the search for potential patterns: * - We check the first characters of the user agent (or better: a hash, generated from it) * - We compare the length of the pattern with the length of the user agent * (the pattern cannot be longer than the user agent!) * * @param string $userAgent * * @return \Generator */ public function getPatterns($userAgent) { $starts = Pattern::getHashForPattern($userAgent, true); $length = strlen($userAgent); // add special key to fall back to the default browser $starts[] = str_repeat('z', 32); // get patterns, first for the given browser and if that is not found, // for the default browser (with a special key) foreach ($starts as $tmpStart) { $tmpSubkey = SubKey::getPatternCacheSubkey($tmpStart); if (!$this->cache->hasItem('browscap.patterns.' . $tmpSubkey, true)) { $this->logger->debug('cache key "browscap.patterns.' . $tmpSubkey . '" not found'); continue; } $success = null; $file = $this->cache->getItem('browscap.patterns.' . $tmpSubkey, true, $success); if (!$success) { $this->logger->debug('cache key "browscap.patterns.' . $tmpSubkey . '" not found'); continue; } if (!is_array($file) || !count($file)) { $this->logger->debug('cache key "browscap.patterns.' . $tmpSubkey . '" was empty'); continue; } $found = false; foreach ($file as $buffer) { list($tmpBuffer, $len, $patterns) = explode("\t", $buffer, 3); if ($tmpBuffer === $tmpStart) { if ($len <= $length) { (yield trim($patterns)); } $found = true; } elseif ($found === true) { break; } } } (yield ''); }
/** * stores the version of the ini file into cache * * @return \BrowscapPHP\Helper\Converter */ public function storeVersion() { $this->cache->setItem('browscap.version', $this->iniVersion, false); return $this; }