diff --git a/system/CodeIgniter.php b/system/CodeIgniter.php index 02f595e1ab6d..d9777133ba12 100644 --- a/system/CodeIgniter.php +++ b/system/CodeIgniter.php @@ -583,9 +583,7 @@ public function setRequest(Request $request) } /** - * Get our Request object, (either IncomingRequest or CLIRequest) - * and set the server protocol based on the information provided - * by the server. + * Get our Request object, (either IncomingRequest or CLIRequest). */ protected function getRequestObject() { @@ -594,15 +592,12 @@ protected function getRequestObject() } if ($this->isSparked() || $this->isPhpCli()) { - $this->request = Services::clirequest($this->config); - - // Inject the request object into Services::request(). - Services::inject('request', $this->request); + Services::createRequest($this->config, true); } else { - $this->request = Services::request($this->config); - // guess at protocol if needed - $this->request->setProtocolVersion($_SERVER['SERVER_PROTOCOL'] ?? 'HTTP/1.1'); + Services::createRequest($this->config); } + + $this->request = Services::request(); } /** diff --git a/system/Config/BaseService.php b/system/Config/BaseService.php index 259abd27130d..14b9aa718413 100644 --- a/system/Config/BaseService.php +++ b/system/Config/BaseService.php @@ -96,6 +96,7 @@ * @method static CLIRequest clirequest(App $config = null, $getShared = true) * @method static CodeIgniter codeigniter(App $config = null, $getShared = true) * @method static Commands commands($getShared = true) + * @method static void createRequest(App $config, bool $isCli = false) * @method static ContentSecurityPolicy csp(CSPConfig $config = null, $getShared = true) * @method static CURLRequest curlrequest($options = [], ResponseInterface $response = null, App $config = null, $getShared = true) * @method static Email email($config = null, $getShared = true) @@ -105,6 +106,7 @@ * @method static Format format(ConfigFormat $config = null, $getShared = true) * @method static Honeypot honeypot(ConfigHoneyPot $config = null, $getShared = true) * @method static BaseHandler image($handler = null, Images $config = null, $getShared = true) + * @method static IncomingRequest incommingrequest(?App $config = null, bool $getShared = true) * @method static Iterator iterator($getShared = true) * @method static Language language($locale = null, $getShared = true) * @method static Logger logger($getShared = true) @@ -114,7 +116,7 @@ * @method static Parser parser($viewPath = null, ConfigView $config = null, $getShared = true) * @method static RedirectResponse redirectresponse(App $config = null, $getShared = true) * @method static View renderer($viewPath = null, ConfigView $config = null, $getShared = true) - * @method static IncomingRequest request(App $config = null, $getShared = true) + * @method static IncomingRequest|CLIRequest request(App $config = null, $getShared = true) * @method static Response response(App $config = null, $getShared = true) * @method static Router router(RouteCollectionInterface $routes = null, Request $request = null, $getShared = true) * @method static RouteCollection routes($getShared = true) @@ -292,18 +294,6 @@ public static function resetSingle(string $name) unset(static::$mocks[$name], static::$instances[$name]); } - /** - * Inject object. - * - * @param object $obj - * - * @internal - */ - public static function inject(string $name, $obj) - { - static::$instances[strtolower($name)] = $obj; - } - /** * Inject mock object for testing. * diff --git a/system/Config/Services.php b/system/Config/Services.php index 82e089741892..4d1a94547ad4 100644 --- a/system/Config/Services.php +++ b/system/Config/Services.php @@ -117,6 +117,8 @@ public static function cache(?Cache $config = null, bool $getShared = true) * a command line request. * * @return CLIRequest + * + * @internal */ public static function clirequest(?App $config = null, bool $getShared = true) { @@ -470,11 +472,13 @@ public static function renderer(?string $viewPath = null, ?ViewConfig $config = } /** - * The Request class models an HTTP request. + * Returns the current Request object. * - * CodeIgniter::getRequestObject() injects CLIRequest if $this->request is CLIRequest. + * createRequest() injects IncomingRequest or CLIRequest. * * @return CLIRequest|IncomingRequest + * + * @deprecated The parameter $config and $getShared are deprecated. */ public static function request(?App $config = null, bool $getShared = true) { @@ -482,6 +486,45 @@ public static function request(?App $config = null, bool $getShared = true) return static::getSharedInstance('request', $config); } + // @TODO remove the following code for backward compatibility + return static::incommingrequest($config, $getShared); + } + + /** + * Create the current Request object, either IncomingRequest or CLIRequest. + * + * This method is called from CodeIgniter::getRequestObject(). + * + * @internal + */ + public static function createRequest(App $config, bool $isCli = false): void + { + if ($isCli) { + $request = AppServices::clirequest($config); + } else { + $request = AppServices::incommingrequest($config); + + // guess at protocol if needed + $request->setProtocolVersion($_SERVER['SERVER_PROTOCOL'] ?? 'HTTP/1.1'); + } + + // Inject the request object into Services::request(). + static::$instances['request'] = $request; + } + + /** + * The IncomingRequest class models an HTTP request. + * + * @return IncomingRequest + * + * @internal + */ + public static function incommingrequest(?App $config = null, bool $getShared = true) + { + if ($getShared) { + return static::getSharedInstance('request', $config); + } + $config ??= config('App'); return new IncomingRequest( diff --git a/tests/system/CommonSingleServiceTest.php b/tests/system/CommonSingleServiceTest.php index 6fefba152b56..7d078a6f9a2e 100644 --- a/tests/system/CommonSingleServiceTest.php +++ b/tests/system/CommonSingleServiceTest.php @@ -98,7 +98,6 @@ public static function serviceNamesProvider(): iterable 'serviceExists', 'reset', 'resetSingle', - 'inject', 'injectMock', 'encrypter', // Encrypter needs a starter key 'session', // Headers already sent