Skip to content

Commit 34385ef

Browse files
authored
Merge pull request #591 from armanist/issue/538-runtime-stage-events
Issue/538 Group the stages by adapter and give the runtime stages lifecycle events
2 parents 926909c + 5b652d1 commit 34385ef

38 files changed

Lines changed: 351 additions & 140 deletions

‎CHANGELOG.md‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ The format is based on Keep a Changelog.
88

99
### Added
1010
- Added `Request::expectsJson()`: true when the `Accept` header lists a JSON media type (`application/json` or a `+json` type, parameters and case ignored), or for an AJAX request that accepts any content type (#581)
11-
- Added boot stage lifecycle events: stages may declare `BEFORE`/`AFTER` event names and the boot `Pipeline` dispatches them with the `AppContext` as a named payload. The environment, config, error handler, HTTP, and modules stages declare `boot.<stage>.before` and `boot.<stage>.after` (#536)
11+
- Added lifecycle events for the web and console runtime: the runtime stages declare `web.preflight`, `web.route`, `web.not_found`, `web.prepare`, `web.dispatch`, `web.send` and `console.command`, each with `.before` and `.after`, and the `Pipeline` dispatches them with the runtime context as a named `context` payload. A stage that does not run dispatches nothing, a stage that throws dispatches no `.after`, and the closing `web.send` events also follow an OPTIONS or not found response. Boot stages declare no events. `console.command.after` fires only with a Symfony application that does not exit after the command (#536, #538)
1212
- Added adapter-based Lang provider support with `DeepL` and `Google Translate` adapters plus shared remote request/caching infrastructure (#533)
1313

1414
### Changed
15-
- **BREAKING:** Reworked the app boot and runtime into pipelines of stages that the adapters run in explicit steps (#537, #575, #576, #579, #583, #589):
15+
- **BREAKING:** Reworked the app boot and runtime into pipelines of stages that the adapters run in explicit steps (#537, #538, #575, #576, #579, #583, #589):
1616
- `AppInterface` gains `boot()`. The adapter constructors only store the context, and `AppFactory` calls `boot()` right after creating the adapter, so `AppFactory::create(...)->start()` is unchanged. Code that creates an adapter directly must call `boot()` before `start()`.
17-
- Removed the `AppAdapter` base class and the `WebAppTrait` and `ConsoleAppTrait` traits: `WebAppAdapter` and `ConsoleAppAdapter` implement `AppInterface` directly and moved to `Quantum\App\Adapters\Web` and `Quantum\App\Adapters\Console`. The boot stages moved to `Quantum\App\Stages\Boot`. Code that goes through `AppFactory` is unaffected; code that references these classes directly must update its imports.
17+
- Removed the `AppAdapter` base class and the `WebAppTrait` and `ConsoleAppTrait` traits: `WebAppAdapter` and `ConsoleAppAdapter` implement `AppInterface` directly and moved to `Quantum\App\Adapters\Web` and `Quantum\App\Adapters\Console`. The stages are grouped by the adapter that runs them: the boot stages both adapters run in `Quantum\App\Stages\Boot`, the others in `Quantum\App\Adapters\Web\Stages\Boot`, `...\Web\Stages\Runtime`, `...\Console\Stages\Boot` and `...\Console\Stages\Runtime`. Code that goes through `AppFactory` is unaffected; code that references these classes directly must update its imports.
1818
- Replaced `BootPipeline` and `BootStageInterface` with one generic `Quantum\App\Pipeline<TContext>`, which runs `StageInterface<TContext>` stages in order and dispatches the `BEFORE`/`AFTER` events they declare. The context type is a PHPDoc template checked by PHPStan: a stage states it with `@implements StageInterface<AppContext>` (or `WebContext`, `ConsoleContext`) and takes `process(AppContext $context)`. A stage that calls `stop()` on the context ends the run; an optional closing stage, the second constructor argument, runs last, also after a stop, but not after a stage throws.
1919
- `AppContext` keeps `getBaseDir()`, `getContainer()`, `getEnvironment()` and `getConfig()`, and carries the state of a run: `stop()`, `isStopped()`, `getExitCode()` and `setExitCode()`. Removed `AppContext::getRequest()`, `getResponse()` and `getRoutes()`: use `WebContext::getRequest()` and `WebContext::getRoutes()`, and `response()` for the shared response.
2020
- The new `WebContext` and `ConsoleContext` extend `AppContext` with the same base directory and container. `WebContext` reads the request, the routes and the view cache from the container and stores the response to send; `ConsoleContext` reads the Symfony `Application`, the input and the output from the container.
@@ -26,7 +26,7 @@ The format is based on Keep a Changelog.
2626
- `hook()` is now `event()`; `on()` and `fire()` are now `listen()` and `dispatch()`, and no aliases remain. Listeners receive one named payload array, which is empty when none is given.
2727
- Event names no longer need to be registered, so `shared/config/hooks.php` is no longer read; dispatching a name without listeners does nothing and an empty name throws `EventException`.
2828
- Listeners stay registered after `dispatch()` instead of being removed after the first call.
29-
- Listeners can be registered from app helper files, which load before config and before any boot event.
29+
- Listeners can be registered from app helper files, which load before the config and before any runtime event.
3030
- Removed the unused `hooks_dir()` helper and renamed the debugger tab constant `Debugger::HOOKS` to `Debugger::EVENTS`; the debugbar tab is now `events` and lists only events that have listeners.
3131
- **BREAKING:** Removed `Quantum\Loader` and moved its responsibilities to their owning packages (#535):
3232
- Moved `Setup` to `Quantum\Config\Setup`; required config failures now use `ConfigException`, and optional imports use `Config::importIfExists()`.

‎src/App/Adapters/Console/ConsoleAppAdapter.php‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@
1010

1111
namespace Quantum\App\Adapters\Console;
1212

13-
use Quantum\App\Stages\Boot\SetupConsoleApplicationStage;
14-
use Quantum\App\Stages\Boot\RegisterCoreCommandsStage;
15-
use Quantum\App\Stages\Boot\RegisterAppCommandsStage;
16-
use Quantum\App\Stages\Boot\ResolveEnvironmentStage;
13+
use Quantum\App\Adapters\Console\Stages\Boot\SetupConsoleApplicationStage;
14+
use Quantum\App\Adapters\Console\Stages\Boot\RegisterCoreCommandsStage;
15+
use Quantum\App\Adapters\Console\Stages\Boot\RegisterAppCommandsStage;
16+
use Quantum\App\Adapters\Console\Stages\Boot\ResolveEnvironmentStage;
17+
use Quantum\App\Adapters\Console\Stages\Runtime\RunCommandStage;
18+
use Quantum\App\Adapters\Console\Stages\Boot\InitConsoleStage;
1719
use Quantum\App\Stages\Boot\SetupErrorHandlerStage;
1820
use Quantum\App\Stages\Boot\LoadEnvironmentStage;
19-
use Quantum\App\Stages\Console\RunCommandStage;
2021
use Quantum\App\Stages\Boot\LoadAppConfigStage;
21-
use Quantum\App\Stages\Boot\InitConsoleStage;
2222
use Quantum\App\Stages\Boot\LoadHelpersStage;
2323
use Quantum\App\Contracts\AppInterface;
2424
use Quantum\App\AppContext;

src/App/Stages/Boot/InitConsoleStage.php renamed to src/App/Adapters/Console/Stages/Boot/InitConsoleStage.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* @link https://quantumphp.io
99
*/
1010

11-
namespace Quantum\App\Stages\Boot;
11+
namespace Quantum\App\Adapters\Console\Stages\Boot;
1212

1313
use Symfony\Component\Console\Output\OutputInterface;
1414
use Symfony\Component\Console\Output\ConsoleOutput;

src/App/Stages/Boot/RegisterAppCommandsStage.php renamed to src/App/Adapters/Console/Stages/Boot/RegisterAppCommandsStage.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* @link https://quantumphp.io
99
*/
1010

11-
namespace Quantum\App\Stages\Boot;
11+
namespace Quantum\App\Adapters\Console\Stages\Boot;
1212

1313
use Symfony\Component\Console\Application;
1414
use Quantum\App\Contracts\StageInterface;

src/App/Stages/Boot/RegisterCoreCommandsStage.php renamed to src/App/Adapters/Console/Stages/Boot/RegisterCoreCommandsStage.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* @link https://quantumphp.io
99
*/
1010

11-
namespace Quantum\App\Stages\Boot;
11+
namespace Quantum\App\Adapters\Console\Stages\Boot;
1212

1313
use Symfony\Component\Console\Application;
1414
use Quantum\App\Contracts\StageInterface;

src/App/Stages/Boot/ResolveEnvironmentStage.php renamed to src/App/Adapters/Console/Stages/Boot/ResolveEnvironmentStage.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* @link https://quantumphp.io
99
*/
1010

11-
namespace Quantum\App\Stages\Boot;
11+
namespace Quantum\App\Adapters\Console\Stages\Boot;
1212

1313
use Symfony\Component\Console\Input\InputInterface;
1414
use Quantum\Environment\Exceptions\EnvException;

src/App/Stages/Boot/SetupConsoleApplicationStage.php renamed to src/App/Adapters/Console/Stages/Boot/SetupConsoleApplicationStage.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* @link https://quantumphp.io
99
*/
1010

11-
namespace Quantum\App\Stages\Boot;
11+
namespace Quantum\App\Adapters\Console\Stages\Boot;
1212

1313
use Symfony\Component\Console\Application;
1414
use Quantum\App\Contracts\StageInterface;

src/App/Stages/Console/RunCommandStage.php renamed to src/App/Adapters/Console/Stages/Runtime/RunCommandStage.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* @link https://quantumphp.io
99
*/
1010

11-
namespace Quantum\App\Stages\Console;
11+
namespace Quantum\App\Adapters\Console\Stages\Runtime;
1212

1313
use Quantum\App\Adapters\Console\ConsoleContext;
1414
use Quantum\App\Contracts\StageInterface;
@@ -25,6 +25,10 @@
2525
*/
2626
class RunCommandStage implements StageInterface
2727
{
28+
public const BEFORE = 'console.command.before';
29+
30+
public const AFTER = 'console.command.after';
31+
2832
/**
2933
* @param ConsoleContext $context
3034
* @throws Exception

src/App/Stages/Boot/InitDebuggerStage.php renamed to src/App/Adapters/Web/Stages/Boot/InitDebuggerStage.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* @link https://quantumphp.io
99
*/
1010

11-
namespace Quantum\App\Stages\Boot;
11+
namespace Quantum\App\Adapters\Web\Stages\Boot;
1212

1313
use Quantum\App\Contracts\StageInterface;
1414
use Quantum\App\AppContext;

src/App/Stages/Boot/InitHttpStage.php renamed to src/App/Adapters/Web/Stages/Boot/InitHttpStage.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* @link https://quantumphp.io
99
*/
1010

11-
namespace Quantum\App\Stages\Boot;
11+
namespace Quantum\App\Adapters\Web\Stages\Boot;
1212

1313
use Quantum\App\Contracts\StageInterface;
1414
use Quantum\App\AppContext;
@@ -23,10 +23,6 @@
2323
*/
2424
class InitHttpStage implements StageInterface
2525
{
26-
public const BEFORE = 'boot.http.before';
27-
28-
public const AFTER = 'boot.http.after';
29-
3026
public function process(AppContext $context): void
3127
{
3228
if (!Di::isRegistered(Request::class)) {

0 commit comments

Comments
 (0)