Skip to content

Commit

Permalink
refactor: move logic to create request object to Services
Browse files Browse the repository at this point in the history
  • Loading branch information
kenjis committed Jun 23, 2022
1 parent 1aaf9f3 commit 388c4b5
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 26 deletions.
15 changes: 5 additions & 10 deletions system/CodeIgniter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand All @@ -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();
}

/**
Expand Down
16 changes: 3 additions & 13 deletions system/Config/BaseService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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.
*
Expand Down
47 changes: 45 additions & 2 deletions system/Config/Services.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -470,18 +472,59 @@ 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)
{
if ($getShared) {
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(
Expand Down
1 change: 0 additions & 1 deletion tests/system/CommonSingleServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ public static function serviceNamesProvider(): iterable
'serviceExists',
'reset',
'resetSingle',
'inject',
'injectMock',
'encrypter', // Encrypter needs a starter key
'session', // Headers already sent
Expand Down

0 comments on commit 388c4b5

Please sign in to comment.