Skip to content

Commit bb97748

Browse files
authored
Merge pull request #5206 from kenjis/fix-command-params
Fix options are not passed to Command $params
2 parents 0c7f108 + 21a21cc commit bb97748

File tree

5 files changed

+67
-11
lines changed

5 files changed

+67
-11
lines changed

phpstan-baseline.neon.dist

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -105,11 +105,6 @@ parameters:
105105
count: 1
106106
path: system/CodeIgniter.php
107107

108-
-
109-
message: "#^Call to an undefined method CodeIgniter\\\\HTTP\\\\Request\\:\\:getSegments\\(\\)\\.$#"
110-
count: 1
111-
path: system/CodeIgniter.php
112-
113108
-
114109
message: "#^Call to an undefined method CodeIgniter\\\\HTTP\\\\Request\\:\\:setLocale\\(\\)\\.$#"
115110
count: 1

system/CodeIgniter.php

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -858,21 +858,34 @@ protected function createController()
858858
/**
859859
* Runs the controller, allowing for _remap methods to function.
860860
*
861+
* CI4 supports three types of requests:
862+
* 1. Web: URI segments become parameters, sent to Controllers via Routes,
863+
* output controlled by Headers to browser
864+
* 2. Spark: accessed by CLI via the spark command, arguments are Command arguments,
865+
* sent to Commands by CommandRunner, output controlled by CLI class
866+
* 3. PHP CLI: accessed by CLI via php public/index.php, arguments become URI segments,
867+
* sent to Controllers via Routes, output varies
868+
*
861869
* @param mixed $class
862870
*
863871
* @return false|ResponseInterface|string|void
864872
*/
865873
protected function runController($class)
866874
{
867-
// If this is a console request then use the input segments as parameters
868-
$params = $this->isSparked() ? $this->request->getSegments() : $this->router->params();
869-
870-
if (method_exists($class, '_remap')) {
871-
$output = $class->_remap($this->method, ...$params);
875+
if ($this->isSparked()) {
876+
// This is a Spark request
877+
/** @var CLIRequest $request */
878+
$request = $this->request;
879+
$params = $request->getArgs();
872880
} else {
873-
$output = $class->{$this->method}(...$params);
881+
// This is a Web request or PHP CLI request
882+
$params = $this->router->params();
874883
}
875884

885+
$output = method_exists($class, '_remap')
886+
? $class->_remap($this->method, ...$params)
887+
: $class->{$this->method}(...$params);
888+
876889
$this->benchmark->stop('controller');
877890

878891
return $output;

system/HTTP/CLIRequest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,13 @@ class CLIRequest extends Request
4141
*/
4242
protected $options = [];
4343

44+
/**
45+
* Command line arguments (segments and options).
46+
*
47+
* @var array
48+
*/
49+
protected $args = [];
50+
4451
/**
4552
* Set the expected HTTP verb
4653
*
@@ -94,6 +101,14 @@ public function getOptions(): array
94101
return $this->options;
95102
}
96103

104+
/**
105+
* Returns an array of all CLI arguments (segments and options).
106+
*/
107+
public function getArgs(): array
108+
{
109+
return $this->args;
110+
}
111+
97112
/**
98113
* Returns the path segments.
99114
*/
@@ -173,6 +188,7 @@ protected function parseCommand()
173188
$optionValue = false;
174189
} else {
175190
$this->segments[] = $arg;
191+
$this->args[] = $arg;
176192
}
177193

178194
continue;
@@ -187,6 +203,7 @@ protected function parseCommand()
187203
}
188204

189205
$this->options[$arg] = $value;
206+
$this->args[$arg] = $value;
190207
}
191208
}
192209

tests/system/CLI/CommandRunnerTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,9 @@ public function testBadCommand()
118118
$this->assertStringContainsString('Command "bogus" not found', CITestStreamFilter::$buffer);
119119
}
120120

121+
/**
122+
* @TODO When the first param is empty? Use case?
123+
*/
121124
public function testRemapEmptyFirstParams()
122125
{
123126
self::$runner->_remap('anyvalue', null, 'list');

tests/system/HTTP/CLIRequestTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,34 @@ public function testParsingNoOptions()
162162
$this->assertSame($expected, $this->request->getOptionString());
163163
}
164164

165+
public function testParsingArgs()
166+
{
167+
$_SERVER['argv'] = [
168+
'spark',
169+
'command',
170+
'param1',
171+
'param2',
172+
'--opt1',
173+
'opt1val',
174+
'--opt-2',
175+
'opt 2 val',
176+
'param3',
177+
];
178+
179+
// reinstantiate it to force parsing
180+
$this->request = new CLIRequest(new App());
181+
182+
$options = [
183+
'command',
184+
'param1',
185+
'param2',
186+
'opt1' => 'opt1val',
187+
'opt-2' => 'opt 2 val',
188+
'param3',
189+
];
190+
$this->assertSame($options, $this->request->getArgs());
191+
}
192+
165193
public function testParsingPath()
166194
{
167195
$_SERVER['argv'] = [

0 commit comments

Comments
 (0)