Skip to content

Commit 782e340

Browse files
committed
refactor: move logic to create request object to Services
1 parent 1aaf9f3 commit 782e340

File tree

4 files changed

+55
-26
lines changed

4 files changed

+55
-26
lines changed

system/CodeIgniter.php

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -583,9 +583,7 @@ public function setRequest(Request $request)
583583
}
584584

585585
/**
586-
* Get our Request object, (either IncomingRequest or CLIRequest)
587-
* and set the server protocol based on the information provided
588-
* by the server.
586+
* Get our Request object, (either IncomingRequest or CLIRequest).
589587
*/
590588
protected function getRequestObject()
591589
{
@@ -594,15 +592,12 @@ protected function getRequestObject()
594592
}
595593

596594
if ($this->isSparked() || $this->isPhpCli()) {
597-
$this->request = Services::clirequest($this->config);
598-
599-
// Inject the request object into Services::request().
600-
Services::inject('request', $this->request);
595+
Services::createRequest($this->config, true);
601596
} else {
602-
$this->request = Services::request($this->config);
603-
// guess at protocol if needed
604-
$this->request->setProtocolVersion($_SERVER['SERVER_PROTOCOL'] ?? 'HTTP/1.1');
597+
Services::createRequest($this->config);
605598
}
599+
600+
$this->request = Services::request();
606601
}
607602

608603
/**

system/Config/BaseService.php

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@
9696
* @method static CLIRequest clirequest(App $config = null, $getShared = true)
9797
* @method static CodeIgniter codeigniter(App $config = null, $getShared = true)
9898
* @method static Commands commands($getShared = true)
99+
* @method static void createRequest(App $config, bool $isCli = false)
99100
* @method static ContentSecurityPolicy csp(CSPConfig $config = null, $getShared = true)
100101
* @method static CURLRequest curlrequest($options = [], ResponseInterface $response = null, App $config = null, $getShared = true)
101102
* @method static Email email($config = null, $getShared = true)
@@ -105,6 +106,7 @@
105106
* @method static Format format(ConfigFormat $config = null, $getShared = true)
106107
* @method static Honeypot honeypot(ConfigHoneyPot $config = null, $getShared = true)
107108
* @method static BaseHandler image($handler = null, Images $config = null, $getShared = true)
109+
* @method static IncomingRequest incommingrequest(?App $config = null, bool $getShared = true)
108110
* @method static Iterator iterator($getShared = true)
109111
* @method static Language language($locale = null, $getShared = true)
110112
* @method static Logger logger($getShared = true)
@@ -114,7 +116,7 @@
114116
* @method static Parser parser($viewPath = null, ConfigView $config = null, $getShared = true)
115117
* @method static RedirectResponse redirectresponse(App $config = null, $getShared = true)
116118
* @method static View renderer($viewPath = null, ConfigView $config = null, $getShared = true)
117-
* @method static IncomingRequest request(App $config = null, $getShared = true)
119+
* @method static IncomingRequest|CLIRequest request(App $config = null, $getShared = true)
118120
* @method static Response response(App $config = null, $getShared = true)
119121
* @method static Router router(RouteCollectionInterface $routes = null, Request $request = null, $getShared = true)
120122
* @method static RouteCollection routes($getShared = true)
@@ -292,18 +294,6 @@ public static function resetSingle(string $name)
292294
unset(static::$mocks[$name], static::$instances[$name]);
293295
}
294296

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-
307297
/**
308298
* Inject mock object for testing.
309299
*

system/Config/Services.php

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,8 @@ public static function cache(?Cache $config = null, bool $getShared = true)
117117
* a command line request.
118118
*
119119
* @return CLIRequest
120+
*
121+
* @internal
120122
*/
121123
public static function clirequest(?App $config = null, bool $getShared = true)
122124
{
@@ -470,18 +472,61 @@ public static function renderer(?string $viewPath = null, ?ViewConfig $config =
470472
}
471473

472474
/**
473-
* The Request class models an HTTP request.
475+
* Returns the current Request object.
474476
*
475-
* CodeIgniter::getRequestObject() injects CLIRequest if $this->request is CLIRequest.
477+
* createRequest() injects IncomingRequest or CLIRequest.
476478
*
477479
* @return CLIRequest|IncomingRequest
480+
*
481+
* @deprecated The parameter $config and $getShared are deprecated.
478482
*/
479483
public static function request(?App $config = null, bool $getShared = true)
480484
{
481485
if ($getShared) {
482486
return static::getSharedInstance('request', $config);
483487
}
484488

489+
// @TODO remove the following code for backward compatibility
490+
return static::incommingrequest($config, $getShared);
491+
}
492+
493+
/**
494+
* Create the current Request object, either IncomingRequest or CLIRequest.
495+
*
496+
* This method is called from CodeIgniter::getRequestObject().
497+
*
498+
* @internal
499+
*/
500+
public static function createRequest(App $config, bool $isCli = false): void
501+
{
502+
if ($isCli) {
503+
$request = AppServices::clirequest($config);
504+
} else {
505+
$request = AppServices::incommingrequest($config);
506+
507+
// guess at protocol if needed
508+
$request->setProtocolVersion($_SERVER['SERVER_PROTOCOL'] ?? 'HTTP/1.1');
509+
}
510+
511+
// Inject the request object into Services::request().
512+
static::$instances['request'] = $request;
513+
}
514+
515+
/**
516+
* The IncomingRequest class models an HTTP request.
517+
*
518+
* CodeIgniter::getRequestObject() injects CLIRequest if $this->request is CLIRequest.
519+
*
520+
* @return IncomingRequest
521+
*
522+
* @internal
523+
*/
524+
public static function incommingrequest(?App $config = null, bool $getShared = true)
525+
{
526+
if ($getShared) {
527+
return static::getSharedInstance('request', $config);
528+
}
529+
485530
$config ??= config('App');
486531

487532
return new IncomingRequest(

tests/system/CommonSingleServiceTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,6 @@ public static function serviceNamesProvider(): iterable
9898
'serviceExists',
9999
'reset',
100100
'resetSingle',
101-
'inject',
102101
'injectMock',
103102
'encrypter', // Encrypter needs a starter key
104103
'session', // Headers already sent

0 commit comments

Comments
 (0)