8000 Function to check if the kernel is loaded via the console or a web request. · Issue #51340 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

Function to check if the kernel is loaded via the console or a web request. #51340

New issue

Have a question about 8000 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

Closed
y4roc opened this issue Aug 10, 2023 · 3 comments · Fixed by #52079
Closed

Function to check if the kernel is loaded via the console or a web request. #51340

y4roc opened this issue Aug 10, 2023 · 3 comments · Fixed by #52079
Labels

Comments

@y4roc
Copy link
y4roc commented Aug 10, 2023

Description

I had the problem that I had to check in a service if the function is called via CLI or web.

Depending on that, I had to make different calculations. That's where my idea comes from, that you could put a function isCLI() or isWeb() into the kernel.

Example

class MyService {
  public function __constructor(
    private readonly KernelInterface $kernel
  ){}
  
  public function myFunction(): void {
    if(!$this->kernel->isCLI())
    {
      return;
    }
    // Do things in CLI without Request()
  }
}
@derrabus
Copy link
Member

I don't think that the kernel should be injected into anything unless you want to perform subrequests. While I understand your motivation, I disagree with the proposed implementation.

@nicolas-grekas
Copy link
Member
nicolas-grekas commented Aug 10, 2023

I also thought this could be useful. I think the can be done with an envvar (APP_RUNTIME_MODE?) that we define as \in_array(\PHP_SAPI, ['cli', 'phpdbg'], true) ? 'cli' : 'web' by default. Long running servers could advertise themselves as 'web' by setting this envvar. The env var might be defined by symfony/runtime so that all runtimes could leverage the concept.

@BaptisteContreras
Copy link
Contributor

I tried to implement @nicolas-grekas idea.

@fabpot fabpot closed this as completed Oct 20, 2023
fabpot added a commit that referenced this issue Oct 20, 2023
…`kernel.runtime_mode.*`, all set from env var `APP_RUNTIME_MODE` (nicolas-grekas)

This PR was merged into the 6.4 branch.

Discussion
----------

[HttpKernel] Add parameters `kernel.runtime_mode` and `kernel.runtime_mode.*`, all set from env var `APP_RUNTIME_MODE`

| Q             | A
| ------------- | ---
| Branch?       | 6.4
| Bug fix?      | no
| New feature?  | yes
| Deprecations? | no
| Tickets       | Fix #51340
| License       | MIT
| Doc PR        | TODO

Alternative to #51408. I think this approach is simpler and more powerful.

Here, we ensure that the kernel always provides a new `kernel.runtime_mode` parameter. This parameter is an array derived by default from the `APP_RUNTIME_MODE` env var, using the `query_string` processor.

This also creates 3 new parameters that should be the most common: `kernel.runtime_mode.web`, `kernel.runtime_mode.cli`, and `kernel.runtime_mode.worker`.

A long-running server would typically set `APP_RUNTIME_MODE` to `web=1&worker=1` or `web=0&worker=1` when appropriate (eg https://github.com/php-runtime/frankenphp-symfony/ should do so when `FRANKENPHP_WORKER` is set.)

I screened the codebase and updated them all except cache pools (where the SAPI is used to enable/disable locking) and error renderers (where the SAPI is used to turn html-rendering on/off.) These require more work that could be done later on. There are a few other remaining usages of `PHP_SAPI` but these look not appropriate for the new flag.

Commits
-------

7c70aec [HttpKernel] Add parameters `kernel.runtime_mode` and `kernel.runtime_mode.*`, all set from env var `APP_RUNTIME_MODE`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
0