Skip to content

Commit 829a6e1

Browse files
committed
Add runningInConsole() method to Application
Implements Laravel-compatible runningInConsole() for Swoole's coroutine architecture where PHP_SAPI detection doesn't work (both HTTP server and CLI commands run via CLI). - Add runningInConsole() and markAsRunningInConsole() to Application - Kernel::handle() sets the console context flag for CLI entry points - Command::execute() preserves context across coroutine boundaries - Uses __foundation.running_in_console Context key (framework convention) Semantics: - CLI commands via Kernel::handle(): true - Artisan::call() from HTTP: false (inherits caller context) - Nested commands: inherit parent context - Scheduled commands: true (inherited from schedule:run) - Queue jobs: false
1 parent 4094802 commit 829a6e1

File tree

5 files changed

+417
-1
lines changed

5 files changed

+417
-1
lines changed

src/console/src/Command.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,17 @@ protected function execute(InputInterface $input, OutputInterface $output): int
4444
$this->replaceOutput();
4545
$method = method_exists($this, 'handle') ? 'handle' : '__invoke';
4646

47-
$callback = function () use ($method): int {
47+
// Capture console state before potentially spawning a new coroutine.
48+
// This preserves the context set by Kernel::handle() across coroutine boundaries.
49+
$shouldRunInConsole = $this->app->runningInConsole();
50+
51+
$callback = function () use ($method, $shouldRunInConsole): int {
52+
// Re-establish console context in the new coroutine if it was set
53+
// by Kernel::handle() (CLI entry point) before spawning.
54+
if ($shouldRunInConsole) {
55+
$this->app->markAsRunningInConsole();
56+
}
57+
4858
try {
4959
$this->eventDispatcher?->dispatch(new BeforeHandle($this));
5060
/* @phpstan-ignore-next-line */

src/foundation/src/Application.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use Closure;
88
use Hyperf\Collection\Arr;
9+
use Hyperf\Context\Context;
910
use Hyperf\Di\Definition\DefinitionSourceInterface;
1011
use Hyperf\Macroable\Macroable;
1112
use Hypervel\Container\Container;
@@ -685,4 +686,27 @@ public function getNamespace(): string
685686

686687
throw new RuntimeException('Unable to detect application namespace.');
687688
}
689+
690+
/**
691+
* Determine if the application is running in the console.
692+
*
693+
* In Swoole's architecture, both HTTP servers and console commands run via CLI,
694+
* so PHP_SAPI detection doesn't work. This method checks a coroutine-local
695+
* Context flag that is set when console commands are executed.
696+
*/
697+
public function runningInConsole(): bool
698+
{
699+
return Context::get('__foundation.running_in_console', false) === true;
700+
}
701+
702+
/**
703+
* Mark the application as running in the console.
704+
*
705+
* This sets a coroutine-local Context flag. Called by the Console Kernel
706+
* before executing commands.
707+
*/
708+
public function markAsRunningInConsole(): void
709+
{
710+
Context::set('__foundation.running_in_console', true);
711+
}
688712
}

src/foundation/src/Console/Kernel.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ public function __construct(
9494
*/
9595
public function handle(InputInterface $input, ?OutputInterface $output = null): mixed
9696
{
97+
$this->app->markAsRunningInConsole();
98+
9799
return $this->getArtisan()->run($input, $output);
98100
}
99101

src/foundation/src/Contracts/Application.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,4 +204,21 @@ public function setLocale(string $locale): void;
204204
* @throws RuntimeException
205205
*/
206206
public function getNamespace(): string;
207+
208+
/**
209+
* Determine if the application is running in the console.
210+
*
211+
* In Swoole's architecture, both HTTP servers and console commands run via CLI,
212+
* so PHP_SAPI detection doesn't work. This method checks a coroutine-local
213+
* Context flag that is set when console commands are executed.
214+
*/
215+
public function runningInConsole(): bool;
216+
217+
/**
218+
* Mark the application as running in the console.
219+
*
220+
* This sets a coroutine-local Context flag. Called by the Console Kernel
221+
* before executing commands.
222+
*/
223+
public function markAsRunningInConsole(): void;
207224
}

0 commit comments

Comments
 (0)