class ParserIni

  1. 8.0.x lib/Drush/Make/Parser/ParserIni.php ParserIni
  2. 7.x lib/Drush/Make/Parser/ParserIni.php ParserIni
  3. master lib/Drush/Make/Parser/ParserIni.php ParserIni

Namespace

Drush\Make\Parser

Hierarchy

Expanded class hierarchy of ParserIni

Members

Contains filters are case sensitive
Namesort descending Modifiers Type Description
ParserIni::$iniRegex private static property Regex for parsing INI format.
ParserIni::parse public static function Parse an input string into an array. Overrides ParserInterface::parse
ParserIni::supportedFile public static function Determine if a given file is supported. Overrides ParserInterface::supportedFile

File

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

View source
class ParserIni implements ParserInterface {

  /**
   * Regex for parsing INI format.
   */
  private static $iniRegex = '
    @^\s*                           # Start at the beginning of a line, ignoring leading whitespace
    ((?:
      [^=;\[\]]|                    # Key names cannot contain equal signs, semi-colons or square brackets,
      \[[^\[\]]*\]                  # unless they are balanced and not nested
    )+?)
    \s*=\s*                         # Key/value pairs are separated by equal signs (ignoring white-space)
    (?:
      ("(?:[^"]|(?<=\\\\)")*")|     # Double-quoted string, which may contain slash-escaped quotes/slashes
      (\'(?:[^\']|(?<=\\\\)\')*\')| # Single-quoted string, which may contain slash-escaped quotes/slashes
      ([^\r\n]*?)                   # Non-quoted string
    )\s*$                           # Stop at the next end of a line, ignoring trailing whitespace
    @msx';

  /**
   * {@inheritdoc}
   */
  public static function supportedFile($filename) {
    $info = pathinfo($filename);
    return isset($info['extension']) && $info['extension'] === 'make';
  }

  /**
   * {@inheritdoc}
   */
  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;
    }
  }

}
2 files declare their use of ParserIni
make.utilities.inc in commands/make/make.utilities.inc
General utility functions for Drush Make.
ParserIniTest.php in tests/Drush/Tests/Make/Parser/ParserIniTest.php