PhpStringTokenParser.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Translation\Extractor;
  11. trigger_deprecation('symfony/translation', '6.2', '"%s" is deprecated.', PhpStringTokenParser::class);
  12. /*
  13. * The following is derived from code at http://github.com/nikic/PHP-Parser
  14. *
  15. * Copyright (c) 2011 by Nikita Popov
  16. *
  17. * Some rights reserved.
  18. *
  19. * Redistribution and use in source and binary forms, with or without
  20. * modification, are permitted provided that the following conditions are
  21. * met:
  22. *
  23. * * Redistributions of source code must retain the above copyright
  24. * notice, this list of conditions and the following disclaimer.
  25. *
  26. * * Redistributions in binary form must reproduce the above
  27. * copyright notice, this list of conditions and the following
  28. * disclaimer in the documentation and/or other materials provided
  29. * with the distribution.
  30. *
  31. * * The names of the contributors may not be used to endorse or
  32. * promote products derived from this software without specific
  33. * prior written permission.
  34. *
  35. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  36. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  37. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  38. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  39. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  40. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  41. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  42. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  43. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  44. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  45. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  46. */
  47. /**
  48. * @deprecated since Symfony 6.2
  49. */
  50. class PhpStringTokenParser
  51. {
  52. protected static $replacements = [
  53. '\\' => '\\',
  54. '$' => '$',
  55. 'n' => "\n",
  56. 'r' => "\r",
  57. 't' => "\t",
  58. 'f' => "\f",
  59. 'v' => "\v",
  60. 'e' => "\x1B",
  61. ];
  62. /**
  63. * Parses a string token.
  64. *
  65. * @param string $str String token content
  66. */
  67. public static function parse(string $str): string
  68. {
  69. $bLength = 0;
  70. if ('b' === $str[0]) {
  71. $bLength = 1;
  72. }
  73. if ('\'' === $str[$bLength]) {
  74. return str_replace(
  75. ['\\\\', '\\\''],
  76. ['\\', '\''],
  77. substr($str, $bLength + 1, -1)
  78. );
  79. } else {
  80. return self::parseEscapeSequences(substr($str, $bLength + 1, -1), '"');
  81. }
  82. }
  83. /**
  84. * Parses escape sequences in strings (all string types apart from single quoted).
  85. *
  86. * @param string $str String without quotes
  87. * @param string|null $quote Quote type
  88. */
  89. public static function parseEscapeSequences(string $str, ?string $quote = null): string
  90. {
  91. if (null !== $quote) {
  92. $str = str_replace('\\'.$quote, $quote, $str);
  93. }
  94. return preg_replace_callback(
  95. '~\\\\([\\\\$nrtfve]|[xX][0-9a-fA-F]{1,2}|[0-7]{1,3})~',
  96. [__CLASS__, 'parseCallback'],
  97. $str
  98. );
  99. }
  100. private static function parseCallback(array $matches): string
  101. {
  102. $str = $matches[1];
  103. if (isset(self::$replacements[$str])) {
  104. return self::$replacements[$str];
  105. } elseif ('x' === $str[0] || 'X' === $str[0]) {
  106. return \chr(hexdec($str));
  107. } else {
  108. return \chr(octdec($str));
  109. }
  110. }
  111. /**
  112. * Parses a constant doc string.
  113. *
  114. * @param string $startToken Doc string start token content (<<<SMTHG)
  115. * @param string $str String token content
  116. */
  117. public static function parseDocString(string $startToken, string $str): string
  118. {
  119. // strip last newline (thanks tokenizer for sticking it into the string!)
  120. $str = preg_replace('~(\r\n|\n|\r)$~', '', $str);
  121. // nowdoc string
  122. if (str_contains($startToken, '\'')) {
  123. return $str;
  124. }
  125. return self::parseEscapeSequences($str, null);
  126. }
  127. }