Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
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\Console\Input;
13
 
14
use Symfony\Component\Console\Exception\InvalidArgumentException;
15
 
16
/**
17
 * StringInput represents an input provided as a string.
18
 *
19
 * Usage:
20
 *
21
 *     $input = new StringInput('foo --bar="foobar"');
22
 *
23
 * @author Fabien Potencier <fabien@symfony.com>
24
 */
25
class StringInput extends ArgvInput
26
{
27
    /**
28
     * @deprecated since Symfony 6.1
29
     */
30
    public const REGEX_STRING = '([^\s]+?)(?:\s|(?<!\\\\)"|(?<!\\\\)\'|$)';
31
    public const REGEX_UNQUOTED_STRING = '([^\s\\\\]+?)';
32
    public const REGEX_QUOTED_STRING = '(?:"([^"\\\\]*(?:\\\\.[^"\\\\]*)*)"|\'([^\'\\\\]*(?:\\\\.[^\'\\\\]*)*)\')';
33
 
34
    /**
35
     * @param string $input A string representing the parameters from the CLI
36
     */
37
    public function __construct(string $input)
38
    {
39
        parent::__construct([]);
40
 
41
        $this->setTokens($this->tokenize($input));
42
    }
43
 
44
    /**
45
     * Tokenizes a string.
46
     *
47
     * @throws InvalidArgumentException When unable to parse input (should never happen)
48
     */
49
    private function tokenize(string $input): array
50
    {
51
        $tokens = [];
52
        $length = \strlen($input);
53
        $cursor = 0;
54
        $token = null;
55
        while ($cursor < $length) {
56
            if ('\\' === $input[$cursor]) {
57
                $token .= $input[++$cursor] ?? '';
58
                ++$cursor;
59
                continue;
60
            }
61
 
62
            if (preg_match('/\s+/A', $input, $match, 0, $cursor)) {
63
                if (null !== $token) {
64
                    $tokens[] = $token;
65
                    $token = null;
66
                }
67
            } elseif (preg_match('/([^="\'\s]+?)(=?)('.self::REGEX_QUOTED_STRING.'+)/A', $input, $match, 0, $cursor)) {
68
                $token .= $match[1].$match[2].stripcslashes(str_replace(['"\'', '\'"', '\'\'', '""'], '', substr($match[3], 1, -1)));
69
            } elseif (preg_match('/'.self::REGEX_QUOTED_STRING.'/A', $input, $match, 0, $cursor)) {
70
                $token .= stripcslashes(substr($match[0], 1, -1));
71
            } elseif (preg_match('/'.self::REGEX_UNQUOTED_STRING.'/A', $input, $match, 0, $cursor)) {
72
                $token .= $match[1];
73
            } else {
74
                // should never happen
75
                throw new InvalidArgumentException(sprintf('Unable to parse input near "... %s ...".', substr($input, $cursor, 10)));
76
            }
77
 
78
            $cursor += \strlen($match[0]);
79
        }
80
 
81
        if (null !== $token) {
82
            $tokens[] = $token;
83
        }
84
 
85
        return $tokens;
86
    }
87
}