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