BrowscapPHP\IniParser\IniParser::createIniParts PHP Метод

createIniParts() публичный Метод

Creates new ini part cache files
public createIniParts ( string $content ) : Generator
$content string
Результат 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 => []]);
        }
    }

Usage Example

Пример #1
0
 /**
  * @param string $iniString
  */
 public function convertString($iniString)
 {
     $iniParser = new IniParser();
     $this->logger->info('start creating patterns from the ini data');
     foreach ($iniParser->createPatterns($iniString) as $patternsHashList) {
         foreach ($patternsHashList as $subkey => $content) {
             if (!$this->cache->setItem('browscap.patterns.' . $subkey, $content, true)) {
                 $this->logger->error('could not write pattern data "' . $subkey . '" to the cache');
             }
         }
     }
     $this->logger->info('finished creating patterns from the ini data');
     $this->logger->info('start creating data from the ini data');
     foreach ($iniParser->createIniParts($iniString) as $patternsContentList) {
         foreach ($patternsContentList as $subkey => $content) {
             if (!$this->cache->setItem('browscap.iniparts.' . $subkey, $content, true)) {
                 $this->logger->error('could not write property data "' . $subkey . '" to the cache');
             }
         }
     }
     $this->logger->info('finished creating data from the ini data');
 }