refactor: move logic to create request object to Services

This commit is contained in:
kenjis 2022-06-23 11:17:15 +09:00
parent 5792fdff60
commit 49889505c0
No known key found for this signature in database
GPG Key ID: BD254878922AF198
4 changed files with 53 additions and 26 deletions

View File

@ -583,9 +583,7 @@ class CodeIgniter
}
/**
* 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 @@ class CodeIgniter
}
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();
}
/**

View File

@ -96,6 +96,7 @@ use Config\View as ConfigView;
* @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 @@ use Config\View as ConfigView;
* @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 @@ use Config\View as ConfigView;
* @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 @@ class BaseService
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.
*

View File

@ -117,6 +117,8 @@ class Services extends BaseService
* a command line request.
*
* @return CLIRequest
*
* @internal
*/
public static function clirequest(?App $config = null, bool $getShared = true)
{
@ -470,11 +472,13 @@ class Services extends BaseService
}
/**
* 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 @@ class Services extends BaseService
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(

View File

@ -98,7 +98,6 @@ final class CommonSingleServiceTest extends CIUnitTestCase
'serviceExists',
'reset',
'resetSingle',
'inject',
'injectMock',
'encrypter', // Encrypter needs a starter key
'session', // Headers already sent