ParserIni.php

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

Parser for INI format.

Namespace

Drush\Make\Parser

Classes

Namesort descending Description
ParserIni

File

lib/Drush/Make/Parser/ParserIni.php
View source
  1. <?php
  2. /**
  3. * @file
  4. * Parser for INI format.
  5. */
  6. namespace Drush\Make\Parser;
  7. class ParserIni implements ParserInterface {
  8. /**
  9. * Regex for parsing INI format.
  10. */
  11. private static $iniRegex = '
  12. @^\s* # Start at the beginning of a line, ignoring leading whitespace
  13. ((?:
  14. [^=;\[\]]| # Key names cannot contain equal signs, semi-colons or square brackets,
  15. \[[^\[\]]*\] # unless they are balanced and not nested
  16. )+?)
  17. \s*=\s* # Key/value pairs are separated by equal signs (ignoring white-space)
  18. (?:
  19. ("(?:[^"]|(?<=\\\\)")*")| # Double-quoted string, which may contain slash-escaped quotes/slashes
  20. (\'(?:[^\']|(?<=\\\\)\')*\')| # Single-quoted string, which may contain slash-escaped quotes/slashes
  21. ([^\r\n]*?) # Non-quoted string
  22. )\s*$ # Stop at the next end of a line, ignoring trailing whitespace
  23. @msx';
  24. /**
  25. * {@inheritdoc}
  26. */
  27. public static function supportedFile($filename) {
  28. $info = pathinfo($filename);
  29. return isset($info['extension']) && $info['extension'] === 'make';
  30. }
  31. /**
  32. * {@inheritdoc}
  33. */
  34. public static function parse($data) {
  35. if (preg_match_all(self::$iniRegex, $data, $matches, PREG_SET_ORDER)) {
  36. $info = array();
  37. foreach ($matches as $match) {
  38. // Fetch the key and value string.
  39. $i = 0;
  40. foreach (array('key', 'value1', 'value2', 'value3') as $var) {
  41. $$var = isset($match[++$i]) ? $match[$i] : '';
  42. }
  43. $value = stripslashes(substr($value1, 1, -1)) . stripslashes(substr($value2, 1, -1)) . $value3;
  44. // Parse array syntax.
  45. $keys = preg_split('/\]?\[/', rtrim($key, ']'));
  46. $last = array_pop($keys);
  47. $parent = &$info;
  48. // Create nested arrays.
  49. foreach ($keys as $key) {
  50. if ($key == '') {
  51. $key = count($parent);
  52. }
  53. if (isset($merge_item) && isset($parent[$key]) && !is_array($parent[$key])) {
  54. $parent[$key] = array($merge_item => $parent[$key]);
  55. }
  56. if (!isset($parent[$key]) || !is_array($parent[$key])) {
  57. $parent[$key] = array();
  58. }
  59. $parent = &$parent[$key];
  60. }
  61. // Handle PHP constants.
  62. if (defined($value)) {
  63. $value = constant($value);
  64. }
  65. // Insert actual value.
  66. if ($last == '') {
  67. $last = count($parent);
  68. }
  69. if (isset($merge_item) && isset($parent[$last]) && is_array($parent[$last])) {
  70. $parent[$last][$merge_item] = $value;
  71. }
  72. else {
  73. $parent[$last] = $value;
  74. }
  75. }
  76. return $info;
  77. }
  78. }
  79. }