Dashboard sipadu mbip
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. namespace Dotenv;
  3. class Lines
  4. {
  5. /**
  6. * Process the array of lines of environment variables.
  7. *
  8. * This will produce an array of entries, one per variable.
  9. *
  10. * @param string[] $lines
  11. *
  12. * @return string[]
  13. */
  14. public static function process(array $lines)
  15. {
  16. $output = [];
  17. $multiline = false;
  18. $multilineBuffer = [];
  19. foreach ($lines as $line) {
  20. list($multiline, $line, $multilineBuffer) = self::multilineProcess($multiline, $line, $multilineBuffer);
  21. if (!$multiline && !self::isComment($line) && self::isSetter($line)) {
  22. $output[] = $line;
  23. }
  24. }
  25. return $output;
  26. }
  27. /**
  28. * Used to make all multiline variable process.
  29. *
  30. * @param bool $multiline
  31. * @param string $line
  32. * @param string[] $buffer
  33. *
  34. * @return array
  35. */
  36. private static function multilineProcess($multiline, $line, array $buffer)
  37. {
  38. // check if $line can be multiline variable
  39. if ($started = self::looksLikeMultilineStart($line)) {
  40. $multiline = true;
  41. }
  42. if ($multiline) {
  43. array_push($buffer, $line);
  44. if (self::looksLikeMultilineStop($line, $started)) {
  45. $multiline = false;
  46. $line = implode("\n", $buffer);
  47. $buffer = [];
  48. }
  49. }
  50. return [$multiline, $line, $buffer];
  51. }
  52. /**
  53. * Determine if the given line can be the start of a multiline variable.
  54. *
  55. * @param string $line
  56. *
  57. * @return bool
  58. */
  59. private static function looksLikeMultilineStart($line)
  60. {
  61. if (strpos($line, '="') === false) {
  62. return false;
  63. }
  64. return self::looksLikeMultilineStop($line, true) === false;
  65. }
  66. /**
  67. * Determine if the given line can be the start of a multiline variable.
  68. *
  69. * @param string $line
  70. * @param bool $started
  71. *
  72. * @return bool
  73. */
  74. private static function looksLikeMultilineStop($line, $started)
  75. {
  76. if ($line === '"') {
  77. return true;
  78. }
  79. $seen = $started ? 0 : 1;
  80. foreach (self::getCharPairs(str_replace('\\\\', '', $line)) as $pair) {
  81. if ($pair[0] !== '\\' && $pair[1] === '"') {
  82. $seen++;
  83. }
  84. }
  85. return $seen > 1;
  86. }
  87. /**
  88. * Get all pairs of adjacent characters within the line.
  89. *
  90. * @param string $line
  91. *
  92. * @return bool
  93. */
  94. private static function getCharPairs($line)
  95. {
  96. $chars = str_split($line);
  97. return array_map(null, $chars, array_slice($chars, 1));
  98. }
  99. /**
  100. * Determine if the line in the file is a comment, e.g. begins with a #.
  101. *
  102. * @param string $line
  103. *
  104. * @return bool
  105. */
  106. private static function isComment($line)
  107. {
  108. $line = ltrim($line);
  109. return isset($line[0]) && $line[0] === '#';
  110. }
  111. /**
  112. * Determine if the given line looks like it's setting a variable.
  113. *
  114. * @param string $line
  115. *
  116. * @return bool
  117. */
  118. private static function isSetter($line)
  119. {
  120. return strpos($line, '=') !== false;
  121. }
  122. }