|
| 1 | +<?php declare(strict_types=1); |
| 2 | +/** |
| 3 | + * This file is part of toolkit/cli-utils. |
| 4 | + * |
| 5 | + * @link https://github.com/inhere |
| 6 | + * @author https://github.com/inhere |
| 7 | + * @license MIT |
| 8 | + */ |
| 9 | + |
| 10 | +namespace Toolkit\Cli\Util; |
| 11 | + |
| 12 | +use function count; |
| 13 | +use function explode; |
| 14 | +use function ltrim; |
| 15 | +use function strlen; |
| 16 | +use function substr; |
| 17 | + |
| 18 | +/** |
| 19 | + * Class LineParser |
| 20 | + * |
| 21 | + * @package Inhere\Console\Util |
| 22 | + */ |
| 23 | +class LineParser |
| 24 | +{ |
| 25 | + /** |
| 26 | + * full command line string. |
| 27 | + * eg: 'kite git acp -m "feat: support start an interactive shell for run application"' |
| 28 | + * |
| 29 | + * @var string |
| 30 | + */ |
| 31 | + private $line; |
| 32 | + |
| 33 | + /** |
| 34 | + * the exploded nodes by space. |
| 35 | + * |
| 36 | + * @var array |
| 37 | + */ |
| 38 | + private $nodes = []; |
| 39 | + |
| 40 | + /** |
| 41 | + * the parsed args |
| 42 | + * |
| 43 | + * @var array |
| 44 | + */ |
| 45 | + private $args = []; |
| 46 | + |
| 47 | + /** |
| 48 | + * @param string $line full command line string. |
| 49 | + * |
| 50 | + * @return array |
| 51 | + */ |
| 52 | + public static function parseIt(string $line): array |
| 53 | + { |
| 54 | + return (new self($line))->parse(); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Class constructor. |
| 59 | + * |
| 60 | + * @param string $line full command line string. |
| 61 | + */ |
| 62 | + public function __construct(string $line) |
| 63 | + { |
| 64 | + $this->setLine($line); |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * @return array |
| 69 | + */ |
| 70 | + public function parse(): array |
| 71 | + { |
| 72 | + if ('' === $this->line) { |
| 73 | + return []; |
| 74 | + } |
| 75 | + |
| 76 | + $this->nodes = explode(' ', $this->line); |
| 77 | + if (count($this->nodes) === 1) { |
| 78 | + $this->args = $this->nodes; |
| 79 | + return $this->args; |
| 80 | + } |
| 81 | + |
| 82 | + $quoteChar = ''; |
| 83 | + $fullItem = ''; |
| 84 | + foreach ($this->nodes as $item) { |
| 85 | + if ('' === $item) { |
| 86 | + continue; |
| 87 | + } |
| 88 | + |
| 89 | + $goon = true; |
| 90 | + $start = $item[0]; |
| 91 | + |
| 92 | + $len = strlen($item); |
| 93 | + $end = $item[$len - 1]; |
| 94 | + |
| 95 | + // $start is start char |
| 96 | + if ($start === "'" || $start === '"') { |
| 97 | + $item = substr($item, 1); |
| 98 | + if ($quoteChar === $start) { |
| 99 | + $this->args[] = $fullItem . ' ' . $item; |
| 100 | + // must clear |
| 101 | + $quoteChar = $fullItem = ''; |
| 102 | + } else { // start |
| 103 | + if ($fullItem) { |
| 104 | + $this->args[] = $fullItem; |
| 105 | + } |
| 106 | + |
| 107 | + $quoteChar = $start; |
| 108 | + $fullItem = $item; |
| 109 | + } |
| 110 | + |
| 111 | + $goon = false; |
| 112 | + } |
| 113 | + |
| 114 | + // $end is end char |
| 115 | + if ($end === "'" || $end === '"') { |
| 116 | + $item = substr($item, 0, -1); |
| 117 | + if ($quoteChar === $end) { |
| 118 | + $this->args[] = $fullItem . ' ' . $item; |
| 119 | + // must clear |
| 120 | + $quoteChar = $fullItem = ''; |
| 121 | + } else { |
| 122 | + if ($fullItem) { |
| 123 | + $this->args[] = $fullItem; |
| 124 | + } |
| 125 | + |
| 126 | + $fullItem = $item; |
| 127 | + } |
| 128 | + |
| 129 | + $goon = false; |
| 130 | + } |
| 131 | + |
| 132 | + if ($goon === true) { |
| 133 | + if ($quoteChar) { |
| 134 | + $fullItem .= ' ' . $item; |
| 135 | + } else { |
| 136 | + $this->args[] = $item; |
| 137 | + } |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + if ($fullItem) { |
| 142 | + $this->args[] = $fullItem; |
| 143 | + } |
| 144 | + |
| 145 | + return $this->args; |
| 146 | + } |
| 147 | + |
| 148 | + /** |
| 149 | + * @return string |
| 150 | + */ |
| 151 | + public function getLine(): string |
| 152 | + { |
| 153 | + return $this->line; |
| 154 | + } |
| 155 | + |
| 156 | + /** |
| 157 | + * @param string $line |
| 158 | + */ |
| 159 | + public function setLine(string $line): void |
| 160 | + { |
| 161 | + $this->line = ltrim($line); |
| 162 | + } |
| 163 | + |
| 164 | + /** |
| 165 | + * @return array |
| 166 | + */ |
| 167 | + public function getNodes(): array |
| 168 | + { |
| 169 | + return $this->nodes; |
| 170 | + } |
| 171 | +} |
0 commit comments