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