mirror of
https://github.com/codeigniter4/CodeIgniter4.git
synced 2025-02-20 11:44:28 +08:00
refactor: CodeIgniter has context: web, php-cli, spark
Make the constant SPARKED deprecated. Remove is_cli() in CodeIgniter.
This commit is contained in:
parent
d76e2a29cb
commit
8815745381
@ -25,7 +25,10 @@ $paths = new Config\Paths();
|
||||
|
||||
// Location of the framework bootstrap file.
|
||||
$bootstrap = rtrim($paths->systemDirectory, '\\/ ') . DIRECTORY_SEPARATOR . 'bootstrap.php';
|
||||
$app = require realpath($bootstrap) ?: $bootstrap;
|
||||
/** @var CodeIgniter\CodeIgniter $app */
|
||||
$app = require realpath($bootstrap) ?: $bootstrap;
|
||||
$context = is_cli() ? 'php-cli' : 'web';
|
||||
$app->setContext($context);
|
||||
|
||||
/*
|
||||
*---------------------------------------------------------------
|
||||
|
9
spark
9
spark
@ -21,6 +21,11 @@
|
||||
* this class mainly acts as a passthru to the framework itself.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @var bool
|
||||
*
|
||||
* @deprecated No longer in use. `CodeIgniter` has `$context` property.
|
||||
*/
|
||||
define('SPARKED', true);
|
||||
|
||||
/*
|
||||
@ -51,7 +56,9 @@ $paths = new Config\Paths();
|
||||
chdir(FCPATH);
|
||||
|
||||
$bootstrap = rtrim($paths->systemDirectory, '\\/ ') . DIRECTORY_SEPARATOR . 'bootstrap.php';
|
||||
$app = require realpath($bootstrap) ?: $bootstrap;
|
||||
/** @var CodeIgniter\CodeIgniter $app */
|
||||
$app = require realpath($bootstrap) ?: $bootstrap;
|
||||
$app->setContext('spark');
|
||||
|
||||
// Grab our Console
|
||||
$console = new CodeIgniter\CLI\Console($app);
|
||||
|
@ -141,6 +141,11 @@ class CodeIgniter
|
||||
*/
|
||||
protected $useSafeOutput = false;
|
||||
|
||||
/**
|
||||
* Context: 'web', 'php-cli', 'spark'
|
||||
*/
|
||||
protected string $context;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*/
|
||||
@ -294,6 +299,8 @@ class CodeIgniter
|
||||
*/
|
||||
public function run(?RouteCollectionInterface $routes = null, bool $returnResponse = false)
|
||||
{
|
||||
assert($this->context !== null, 'Context must be set before run() is called');
|
||||
|
||||
$this->startBenchmark();
|
||||
|
||||
$this->getRequestObject();
|
||||
@ -321,7 +328,7 @@ class CodeIgniter
|
||||
}
|
||||
|
||||
// spark command has nothing to do with HTTP redirect and 404
|
||||
if (defined('SPARKED')) {
|
||||
if ($this->isSparked()) {
|
||||
return $this->handleRequest($routes, $cacheConfig, $returnResponse);
|
||||
}
|
||||
|
||||
@ -358,6 +365,30 @@ class CodeIgniter
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Invoked via spark command?
|
||||
*/
|
||||
private function isSparked(): bool
|
||||
{
|
||||
return $this->context === 'spark';
|
||||
}
|
||||
|
||||
/**
|
||||
* Invoked via php-cli command?
|
||||
*/
|
||||
private function isPhpCli(): bool
|
||||
{
|
||||
return $this->context === 'php-cli';
|
||||
}
|
||||
|
||||
/**
|
||||
* Web access?
|
||||
*/
|
||||
private function isWeb(): bool
|
||||
{
|
||||
return $this->context === 'web';
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles the main request logic and fires the controller.
|
||||
*
|
||||
@ -389,7 +420,7 @@ class CodeIgniter
|
||||
}
|
||||
|
||||
// Never run filters when running through Spark cli
|
||||
if (! defined('SPARKED')) {
|
||||
if (! $this->isSparked()) {
|
||||
// Run "before" filters
|
||||
$this->benchmark->start('before_filters');
|
||||
$possibleResponse = $filters->run($uri, 'before');
|
||||
@ -430,7 +461,7 @@ class CodeIgniter
|
||||
$this->gatherOutput($cacheConfig, $returned);
|
||||
|
||||
// Never run filters when running through Spark cli
|
||||
if (! defined('SPARKED')) {
|
||||
if (! $this->isSparked()) {
|
||||
$filters->setResponse($this->response);
|
||||
|
||||
// Run "after" filters
|
||||
@ -548,10 +579,8 @@ class CodeIgniter
|
||||
return;
|
||||
}
|
||||
|
||||
if (is_cli() && ENVIRONMENT !== 'testing') {
|
||||
// @codeCoverageIgnoreStart
|
||||
if ($this->isSparked() || $this->isPhpCli()) {
|
||||
$this->request = Services::clirequest($this->config);
|
||||
// @codeCoverageIgnoreEnd
|
||||
} else {
|
||||
$this->request = Services::request($this->config);
|
||||
// guess at protocol if needed
|
||||
@ -567,7 +596,7 @@ class CodeIgniter
|
||||
{
|
||||
$this->response = Services::response($this->config);
|
||||
|
||||
if (! is_cli() || ENVIRONMENT === 'testing') {
|
||||
if ($this->isWeb()) {
|
||||
$this->response->setProtocolVersion($this->request->getProtocolVersion());
|
||||
}
|
||||
|
||||
@ -826,7 +855,7 @@ class CodeIgniter
|
||||
protected function runController($class)
|
||||
{
|
||||
// If this is a console request then use the input segments as parameters
|
||||
$params = defined('SPARKED') ? $this->request->getSegments() : $this->router->params();
|
||||
$params = $this->isSparked() ? $this->request->getSegments() : $this->router->params();
|
||||
|
||||
if (method_exists($class, '_remap')) {
|
||||
$output = $class->_remap($this->method, ...$params);
|
||||
@ -884,7 +913,9 @@ class CodeIgniter
|
||||
ob_end_flush(); // @codeCoverageIgnore
|
||||
}
|
||||
|
||||
throw PageNotFoundException::forPageNotFound(ENVIRONMENT !== 'production' || is_cli() ? $e->getMessage() : '');
|
||||
throw PageNotFoundException::forPageNotFound(
|
||||
ENVIRONMENT !== 'production' || ! $this->isWeb() ? $e->getMessage() : ''
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -950,8 +981,8 @@ class CodeIgniter
|
||||
public function storePreviousURL($uri)
|
||||
{
|
||||
// Ignore CLI requests
|
||||
if (is_cli() && ENVIRONMENT !== 'testing') {
|
||||
return; // @codeCoverageIgnore
|
||||
if (! $this->isWeb()) {
|
||||
return;
|
||||
}
|
||||
// Ignore AJAX requests
|
||||
if (method_exists($this->request, 'isAJAX') && $this->request->isAJAX()) {
|
||||
@ -1015,4 +1046,16 @@ class CodeIgniter
|
||||
{
|
||||
exit($code); // @codeCoverageIgnore
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the app context.
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setContext(string $context)
|
||||
{
|
||||
$this->context = $context;
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
|
@ -194,6 +194,7 @@ class FeatureTestCase extends CIUnitTestCase
|
||||
Services::injectMock('filters', Services::filters(null, false));
|
||||
|
||||
$response = $this->app
|
||||
->setContext('web')
|
||||
->setRequest($request)
|
||||
->run($routes, true);
|
||||
|
||||
|
@ -184,6 +184,7 @@ trait FeatureTestTrait
|
||||
Services::injectMock('filters', Services::filters(null, false));
|
||||
|
||||
$response = $this->app
|
||||
->setContext('web')
|
||||
->setRequest($request)
|
||||
->run($routes, true);
|
||||
|
||||
|
@ -49,6 +49,7 @@ final class ConsoleTest extends CIUnitTestCase
|
||||
CLI::init();
|
||||
|
||||
$this->app = new MockCodeIgniter(new MockCLIConfig());
|
||||
$this->app->setContext('spark');
|
||||
}
|
||||
|
||||
protected function tearDown(): void
|
||||
|
@ -41,6 +41,7 @@ final class CodeIgniterTest extends CIUnitTestCase
|
||||
$_SERVER['SERVER_PROTOCOL'] = 'HTTP/1.1';
|
||||
|
||||
$this->codeigniter = new MockCodeIgniter(new App());
|
||||
$this->codeigniter->setContext('web');
|
||||
}
|
||||
|
||||
protected function tearDown(): void
|
||||
@ -257,6 +258,7 @@ final class CodeIgniterTest extends CIUnitTestCase
|
||||
$config->forceGlobalSecureRequests = true;
|
||||
|
||||
$codeigniter = new MockCodeIgniter($config);
|
||||
$codeigniter->setContext('web');
|
||||
|
||||
$this->getPrivateMethodInvoker($codeigniter, 'getRequestObject')();
|
||||
$this->getPrivateMethodInvoker($codeigniter, 'getResponseObject')();
|
||||
|
@ -71,6 +71,7 @@ final class ControllerTest extends CIUnitTestCase
|
||||
$this->response = new Response($this->config);
|
||||
$this->logger = \Config\Services::logger();
|
||||
$this->codeigniter = new MockCodeIgniter($this->config);
|
||||
$this->codeigniter->setContext('web');
|
||||
}
|
||||
|
||||
public function testConstructor()
|
||||
|
@ -65,6 +65,7 @@ final class ResourceControllerTest extends CIUnitTestCase
|
||||
|
||||
$config = new App();
|
||||
$this->codeigniter = new MockCodeIgniter($config);
|
||||
$this->codeigniter->setContext('web');
|
||||
}
|
||||
|
||||
protected function tearDown(): void
|
||||
|
@ -59,6 +59,7 @@ final class ResourcePresenterTest extends CIUnitTestCase
|
||||
|
||||
$config = new App();
|
||||
$this->codeigniter = new MockCodeIgniter($config);
|
||||
$this->codeigniter->setContext('web');
|
||||
}
|
||||
|
||||
protected function tearDown(): void
|
||||
|
Loading…
x
Reference in New Issue
Block a user