Skip to content

Commit ed5a3b9

Browse files
committed
call Console::run() within command()
1 parent 163a20b commit ed5a3b9

File tree

2 files changed

+20
-35
lines changed

2 files changed

+20
-35
lines changed

system/Common.php

Lines changed: 18 additions & 33 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,41 +156,25 @@ 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-
}
159+
if (! in_array('--no-header', $tokens, true)) {
160+
// Don't show the header as it is not needed when running commands from code.
161+
$tokens[] = '--no-header';
162+
}
174163

175-
continue;
176-
}
164+
// Prepend an application name, as Console expects one.
165+
array_unshift($tokens, 'spark');
177166

178-
$arg = ltrim($arg, '-');
179-
$value = null;
167+
ob_start();
180168

181-
if (isset($args[$i + 1]) && mb_strpos($args[$i + 1], '-') !== 0) {
182-
$value = $args[$i + 1];
183-
$optionValue = true;
184-
}
169+
try {
170+
(new Console())->run($tokens);
185171

186-
$params[$arg] = $value;
172+
return ob_get_clean();
173+
} finally {
174+
// `Console::run()` creates a `CLIRequest` instance stored globally, which
175+
// can cause issues if the next code expects a different type of request.
176+
Services::createRequest(config(App::class), is_cli());
187177
}
188-
189-
ob_start();
190-
service('commands')->run($command, $params);
191-
192-
return ob_get_clean();
193178
}
194179
}
195180

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)