Skip to content

Commit b79a77d

Browse files
committed
fix: when running on CLI, two Request objects were used in the system
CodeIgniter created CLIRequest, but some classes call Services::request() that returned IncommingRequest.
1 parent 53460ea commit b79a77d

5 files changed

Lines changed: 28 additions & 5 deletions

File tree

‎system/API/ResponseTrait.php‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,9 @@ protected function format($data = null)
321321

322322
// Determine correct response type through content negotiation if not explicitly declared
323323
if (empty($this->format) || ! in_array($this->format, ['json', 'xml'], true)) {
324-
$mime = $this->request->negotiate('media', $format->getConfig()->supportedResponseFormats, false);
324+
if ($this->request instanceof IncomingRequest) {
325+
$mime = $this->request->negotiate('media', $format->getConfig()->supportedResponseFormats, false);
326+
}
325327
}
326328

327329
$this->response->setContentType($mime);

‎system/CodeIgniter.php‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -595,6 +595,9 @@ protected function getRequestObject()
595595

596596
if ($this->isSparked() || $this->isPhpCli()) {
597597
$this->request = Services::clirequest($this->config);
598+
599+
// Inject the request object into Services::request().
600+
Services::inject('request', $this->request);
598601
} else {
599602
$this->request = Services::request($this->config);
600603
// guess at protocol if needed

‎system/Config/BaseService.php‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,18 @@ public static function resetSingle(string $name)
292292
unset(static::$mocks[$name], static::$instances[$name]);
293293
}
294294

295+
/**
296+
* Inject object.
297+
*
298+
* @param object $obj
299+
*
300+
* @internal
301+
*/
302+
public static function inject(string $name, $obj)
303+
{
304+
static::$instances[strtolower($name)] = $obj;
305+
}
306+
295307
/**
296308
* Inject mock object for testing.
297309
*

‎system/Config/Services.php‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,9 @@ public static function renderer(?string $viewPath = null, ?ViewConfig $config =
472472
/**
473473
* The Request class models an HTTP request.
474474
*
475-
* @return IncomingRequest
475+
* CodeIgniter::getRequestObject() injects CLIRequest if $this->request is CLIRequest.
476+
*
477+
* @return CLIRequest|IncomingRequest
476478
*/
477479
public static function request(?App $config = null, bool $getShared = true)
478480
{

‎system/Debug/Exceptions.php‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use CodeIgniter\API\ResponseTrait;
1515
use CodeIgniter\Exceptions\PageNotFoundException;
16+
use CodeIgniter\HTTP\CLIRequest;
1617
use CodeIgniter\HTTP\IncomingRequest;
1718
use CodeIgniter\HTTP\Response;
1819
use Config\Exceptions as ExceptionsConfig;
@@ -50,9 +51,9 @@ class Exceptions
5051
protected $config;
5152

5253
/**
53-
* The incoming request.
54+
* The request.
5455
*
55-
* @var IncomingRequest
56+
* @var CLIRequest|IncomingRequest
5657
*/
5758
protected $request;
5859

@@ -63,7 +64,10 @@ class Exceptions
6364
*/
6465
protected $response;
6566

66-
public function __construct(ExceptionsConfig $config, IncomingRequest $request, Response $response)
67+
/**
68+
* @param CLIRequest|IncomingRequest $request
69+
*/
70+
public function __construct(ExceptionsConfig $config, $request, Response $response)
6771
{
6872
$this->ob_level = ob_get_level();
6973
$this->viewPath = rtrim($config->errorViewPath, '\\/ ') . DIRECTORY_SEPARATOR;

0 commit comments

Comments
 (0)