public static function ParserIni::parse

8.0.x ParserIni.php public static ParserIni::parse($data)
7.x ParserIni.php public static ParserIni::parse($data)
master ParserIni.php public static ParserIni::parse($data)

Parse an input string into an array.

Parameters

string $data:

Return value

array Makefile data as an array.

Overrides ParserInterface::parse

2 calls to ParserIni::parse()
ParserIniTest::testParse in tests/Drush/Tests/Make/Parser/ParserIniTest.php
@dataProvider providerParse @covers ::parse
_make_determine_format in commands/make/make.utilities.inc
Given data from stdin, determine format.

File

lib/Drush/Make/Parser/ParserIni.php, line 40
Parser for INI format.

Namespace

Drush\Make\Parser

Class

ParserIni

Code

public static function parse($data) {
  if (preg_match_all(self::$iniRegex, $data, $matches, PREG_SET_ORDER)) {
    $info = array();
    foreach ($matches as $match) {
      // Fetch the key and value string.
      $i = 0;
      foreach (array('key', 'value1', 'value2', 'value3') as $var) {
        $$var = isset($match[++$i]) ? $match[$i] : '';
      }
      $value = stripslashes(substr($value1, 1, -1)) . stripslashes(substr($value2, 1, -1)) . $value3;

      // Parse array syntax.
      $keys = preg_split('/\]?\[/', rtrim($key, ']'));
      $last = array_pop($keys);
      $parent = &$info;

      // Create nested arrays.
      foreach ($keys as $key) {
        if ($key == '') {
          $key = count($parent);
        }
        if (isset($merge_item) && isset($parent[$key]) && !is_array($parent[$key])) {
          $parent[$key] = array($merge_item => $parent[$key]);
        }
        if (!isset($parent[$key]) || !is_array($parent[$key])) {
          $parent[$key] = array();
        }
        $parent = &$parent[$key];
      }

      // Handle PHP constants.
      if (defined($value)) {
        $value = constant($value);
      }

      // Insert actual value.
      if ($last == '') {
        $last = count($parent);
      }
      if (isset($merge_item) && isset($parent[$last]) && is_array($parent[$last])) {
        $parent[$last][$merge_item] = $value;
      }
      else {
        $parent[$last] = $value;
      }
    }
    return $info;
  }
}