feat: add runningInConsole() method to Application#341
feat: add runningInConsole() method to Application#341binaryfire wants to merge 2 commits intohypervel:mainfrom
runningInConsole() method to Application#341Conversation
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
- Remove queue job test that didn't actually test the PR's functionality - Remove CaptureRunningInConsoleJob fixture (100+ lines of boilerplate) - Rename test method to accurately reflect what it tests - Regenerate App facade docblocks to include new methods
|
Hi @binaryfire , the current implementation of this PR seems to return false when using |
| $this->app->markAsRunningInConsole(); | ||
| $this->app->markAsRunningInConsole(); |
There was a problem hiding this comment.
Are these two lines duplicated code?
|
@albertcht Closing this, because I've implemented a cleaner solution in the refactor branch. The new approach defaults to
Files to review in the
This is a much cleaner and less brittle implementation that returns the same value as Laravel for every scenario (http server, unit tests, tinker, commands, queue workers etc) |
Summary
Implements Laravel-compatible
app()->runningInConsole()detection for Hypervel, using coroutine-local Context to correctly distinguish between CLI commands and HTTP/queue contextsProblem
In traditional Laravel,
app()->runningInConsole()checksPHP_SAPI === 'cli'. This doesn't work in Swoole/Hypervel because both HTTP servers and console commands run via CLI.Common use cases that need this detection:
Solution
Uses
Hyperf\Context\Contextto store a coroutine-local flag that tracks whether code is executing within a console command context.How it works
Kernel::handle()(the CLI entry point, e.g.,php artisan foo) callsmarkAsRunningInConsole()before executing any commandKernel::call()(programmatic command execution) does NOT set the flag - it inherits whatever context already existsCommand::execute()propagates the flag across coroutine boundaries when commands spawn new coroutinesBehavior matrix
runningInConsole()php artisan migratetruetrueArtisan::call()from CLItrueschedule:run)truefalseArtisan::call()from HTTP controllerfalsefalseChanges
Application.php- AddedrunningInConsole()andmarkAsRunningInConsole()methods using ContextApplication.php(Contract) - Added method signatures to interfaceKernel.php- CallsmarkAsRunningInConsole()inhandle()(CLI entry point only)Command.php- Propagates console context across coroutine boundariesApp.php(Facade) - Regenerated docblocks to include new methodsTests
10 tests covering:
falsemarkAsRunningInConsole()sets state totrueKernel::handle()sets the flagArtisan::call()alone doesn't set the flagtrueKernel::call()without priorhandle()seesfalseapp()helper reflects stateschedule:run