Skip to content

Commit abec0d7

Browse files
committed
call Console::run() within command()
1 parent f09bc90 commit abec0d7

File tree

2 files changed

+17
-34
lines changed

2 files changed

+17
-34
lines changed

system/Common.php

Lines changed: 15 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
*/
1313

1414
use CodeIgniter\Cache\CacheInterface;
15+
use CodeIgniter\CLI\Console;
1516
use CodeIgniter\Config\BaseConfig;
1617
use CodeIgniter\Config\Factories;
1718
use CodeIgniter\Context\Context;
@@ -127,7 +128,7 @@ function command(string $command)
127128
$regexString = '([^\s]+?)(?:\s|(?<!\\\\)"|(?<!\\\\)\'|$)';
128129
$regexQuoted = '(?:"([^"\\\\]*(?:\\\\.[^"\\\\]*)*)"|\'([^\'\\\\]*(?:\\\\.[^\'\\\\]*)*)\')';
129130

130-
$args = [];
131+
$tokens = [];
131132
$length = strlen($command);
132133
$cursor = 0;
133134

@@ -140,9 +141,9 @@ function command(string $command)
140141
if (preg_match('/\s+/A', $command, $match, 0, $cursor)) {
141142
// nothing to do
142143
} elseif (preg_match('/' . $regexQuoted . '/A', $command, $match, 0, $cursor)) {
143-
$args[] = stripcslashes(substr($match[0], 1, strlen($match[0]) - 2));
144+
$tokens[] = stripcslashes(substr($match[0], 1, strlen($match[0]) - 2));
144145
} elseif (preg_match('/' . $regexString . '/A', $command, $match, 0, $cursor)) {
145-
$args[] = stripcslashes($match[1]);
146+
$tokens[] = stripcslashes($match[1]);
146147
} else {
147148
// @codeCoverageIgnoreStart
148149
throw new InvalidArgumentException(sprintf(
@@ -155,39 +156,21 @@ function command(string $command)
155156
$cursor += strlen($match[0]);
156157
}
157158

158-
/** @var array<int|string, string|null> */
159-
$params = [];
160-
$command = array_shift($args);
161-
$optionValue = false;
162-
163-
foreach ($args as $i => $arg) {
164-
if (mb_strpos($arg, '-') !== 0) {
165-
if ($optionValue) {
166-
// if this was an option value, it was already
167-
// included in the previous iteration
168-
$optionValue = false;
169-
} else {
170-
// add to segments if not starting with '-'
171-
// and not an option value
172-
$params[] = $arg;
173-
}
174-
175-
continue;
176-
}
177-
178-
$arg = ltrim($arg, '-');
179-
$value = null;
180-
181-
if (isset($args[$i + 1]) && mb_strpos($args[$i + 1], '-') !== 0) {
182-
$value = $args[$i + 1];
183-
$optionValue = true;
159+
// Don't show the header as it is not needed when running commands from code.
160+
if (! in_array('--no-header', $tokens, true)) {
161+
if (! in_array('--', $tokens, true)) {
162+
$tokens[] = '--no-header';
163+
} else {
164+
$index = (int) array_search('--', $tokens, true);
165+
array_splice($tokens, $index, 0, '--no-header');
184166
}
185-
186-
$params[$arg] = $value;
187167
}
188168

169+
// Prepend an application name, as Console expects one.
170+
array_unshift($tokens, 'spark');
171+
189172
ob_start();
190-
service('commands')->run($command, $params);
173+
(new Console())->run($tokens);
191174

192175
return ob_get_clean();
193176
}

tests/system/Commands/CommandTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,11 +165,11 @@ public static function provideCommandParsesArgsCorrectly(): iterable
165165
],
166166
[
167167
'reveal seg1 seg2 -opt1 val1 seg3',
168-
['seg1', 'seg2', 'opt1' => 'val1', 'seg3'],
168+
['seg1', 'seg2', 'seg3', 'opt1' => 'val1'],
169169
],
170170
[
171171
'reveal as df -gh -jk -qw 12 zx cv',
172-
['as', 'df', 'gh' => null, 'jk' => null, 'qw' => '12', 'zx', 'cv'],
172+
['as', 'df', 'zx', 'cv', 'gh' => null, 'jk' => null, 'qw' => '12'],
173173
],
174174
[
175175
'reveal as -df "some stuff" -jk 12 -sd "Some longer stuff" -fg \'using single quotes\'',

0 commit comments

Comments
 (0)