Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: SPARKED #5649

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion spark
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
* this class mainly acts as a passthru to the framework itself.
*/

// @TODO want to make deprecated. Use CLIRequest::isSparked() instead.
define('SPARKED', true);

/*
Expand Down Expand Up @@ -42,7 +43,14 @@ $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;

// Set sparked CLIRequest in CodeIgniter
$clirequest = Config\Services::clirequest();
$clirequest->sparked();
$app->setRequest($clirequest);

// Grab our Console
$console = new CodeIgniter\CLI\Console($app);
Expand Down
13 changes: 9 additions & 4 deletions system/CodeIgniter.php
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ public function run(?RouteCollectionInterface $routes = null, bool $returnRespon
}

// spark command has nothing to do with HTTP redirect and 404
if (defined('SPARKED')) {
if ($this->isSparked()) {
return $this->handleRequest($routes, $cacheConfig, $returnResponse);
}

Expand All @@ -340,6 +340,11 @@ public function run(?RouteCollectionInterface $routes = null, bool $returnRespon
}
}

private function isSparked(): bool
{
return $this->request instanceof CLIRequest && $this->request->isSparked();
}

/**
* Set our Response instance to "pretend" mode so that things like
* cookies and headers are not actually sent, allowing PHP 7.2+ to
Expand Down Expand Up @@ -385,7 +390,7 @@ protected function handleRequest(?RouteCollectionInterface $routes, Cache $cache
}

// 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');
Expand Down Expand Up @@ -426,7 +431,7 @@ protected function handleRequest(?RouteCollectionInterface $routes, Cache $cache
$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
Expand Down Expand Up @@ -822,7 +827,7 @@ protected function createController()
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);
Expand Down
25 changes: 24 additions & 1 deletion system/HTTP/CLIRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@ class CLIRequest extends Request
*/
protected $method = 'cli';

/**
* Invoked via spark command?
*
* @var bool
*/
protected $sparked = false;

/**
* Constructor
*/
Expand Down Expand Up @@ -195,6 +202,22 @@ protected function parseCommand()
*/
public function isCLI(): bool
{
return is_cli();
return true;
}

/**
* Set sparked true.
*/
public function sparked(): void
{
$this->sparked = true;
}

/**
* Invoked via spark command?
*/
public function isSparked(): bool
{
return $this->sparked;
}
}
2 changes: 1 addition & 1 deletion system/HTTP/IncomingRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ public function negotiate(string $type, array $supported, bool $strictMatch = fa
*/
public function isCLI(): bool
{
return is_cli();
return false;
}

/**
Expand Down
9 changes: 9 additions & 0 deletions tests/system/HTTP/CLIRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -641,4 +641,13 @@ public function testMethodIsCliReturnsAlwaysTrue()
{
$this->assertTrue($this->request->isCLI());
}

public function testIsSparked()
{
$this->assertFalse($this->request->isSparked());

$this->request->sparked();

$this->assertTrue($this->request->isCLI());
}
}
3 changes: 1 addition & 2 deletions tests/system/HTTP/IncomingRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -439,8 +439,7 @@ public function testCanGrabGetRawInput()

public function testIsCLI()
{
// this should be the case in unit testing
$this->assertTrue($this->request->isCLI());
$this->assertFalse($this->request->isCLI());
}

public function testIsAJAX()
Expand Down