/** * Creates new ini part cache files * * @param string $content * * @return \Generator */ public function createIniParts($content) { // get all patterns from the ini file in the correct order, // so that we can calculate with index number of the resulting array, // which part to use when the ini file is splitted into its sections. preg_match_all('/(?<=\\[)(?:[^\\r\\n]+)(?=\\])/m', $content, $patternpositions); $patternpositions = $patternpositions[0]; // split the ini file into sections and save the data in one line with a hash of the beloging // pattern (filtered in the previous step) $iniParts = preg_split('/\\[[^\\r\\n]+\\]/', $content); $contents = []; $propertyFormatter = new PropertyFormatter(new PropertyHolder()); foreach ($patternpositions as $position => $pattern) { $pattern = strtolower($pattern); $patternhash = Pattern::getHashForParts($pattern); $subkey = SubKey::getIniPartCacheSubKey($patternhash); if (!isset($contents[$subkey])) { $contents[$subkey] = []; } $browserProperties = parse_ini_string($iniParts[$position + 1], INI_SCANNER_RAW); foreach (array_keys($browserProperties) as $property) { $browserProperties[$property] = $propertyFormatter->formatPropertyValue($browserProperties[$property], $property); } // the position has to be moved by one, because the header of the ini file // is also returned as a part $contents[$subkey][] = $patternhash . "\t" . json_encode($browserProperties, JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP); } unset($patternpositions); unset($iniParts); $subkeys = array_flip(SubKey::getAllIniPartCacheSubKeys()); foreach ($contents as $subkey => $content) { $subkey = (string) $subkey; (yield [$subkey => $content]); unset($subkeys[$subkey]); } foreach (array_keys($subkeys) as $subkey) { $subkey = (string) $subkey; (yield [$subkey => []]); } }
/** * 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; }
/** * @group pattern */ public function testGetHashForParts() { self::assertSame('529f1ddb64ea27d5cc6fc8ce8048d9e7', Pattern::getHashForParts('mozilla/5.0 (*linux i686*rv:0.9*) gecko*')); }