src/Phase.php source

1 <?php
2
3 namespace mini;
4
5 /**
6 * Application lifecycle phases
7 *
8 * Represents the application's overall state, not individual request state.
9 * The Ready phase can handle many concurrent requests while remaining in Ready state.
10 *
11 * Usage:
12 * ```php
13 * // Check current phase
14 * echo Mini::$mini->phase->getCurrentState()->value; // "bootstrap"
15 *
16 * // Transition to next phase
17 * Mini::$mini->phase->trigger(Phase::Ready);
18 *
19 * // Subscribe to phase transitions
20 * Mini::$mini->phase->onEnteringState(Phase::Ready, function($old, $new) {
21 * // Called when entering Ready phase
22 * });
23 * ```
24 *
25 * Typical flows:
26 * - Traditional SAPI: Initializing → Bootstrap → Ready → Shutdown
27 * - Long-running: Initializing → Bootstrap → Ready (handles requests) → Shutdown
28 * - Failure: Any phase → Failed → Shutdown
29 */
30 enum Phase: string
31 {
32 /**
33 * Initializing phase - before framework bootstrap begins
34 *
35 * In this phase:
36 * - Mini singleton is being constructed
37 * - Hooks can be registered to observe Bootstrap entry
38 * - Very brief transitional state
39 *
40 * This is the initial phase when Mini::$mini is first created.
41 */
42 case Initializing = 'initializing';
43
44 /**
45 * Bootstrap phase - framework initialization
46 *
47 * In this phase:
48 * - Services are being registered (addService, etc.)
49 * - Configuration is being loaded
50 * - Lifecycle hooks are being set up
51 * - Request handling is NOT yet available
52 *
53 * After bootstrap completes, transitions to Ready.
54 */
55 case Bootstrap = 'bootstrap';
56
57 /**
58 * Ready phase - application is ready to handle requests
59 *
60 * In this phase:
61 * - Framework is fully initialized and locked
62 * - Services CANNOT be registered (container is locked)
63 * - Application can handle requests (one or many, concurrent or sequential)
64 * - This is the normal operating state
65 *
66 * Traditional SAPI: Stays in Ready while handling the single request, then → Shutdown
67 * Long-running: Stays in Ready indefinitely, handling many requests, until → Shutdown
68 *
69 * Note: Individual request contexts are managed separately via getRequestScope(),
70 * not via phase transitions. The application remains in Ready phase throughout.
71 */
72 case Ready = 'ready';
73
74 /**
75 * Failed phase - unrecoverable error occurred
76 *
77 * In this phase:
78 * - An unrecoverable error has occurred during initialization
79 * - Application cannot continue normal operation
80 * - Will transition to Shutdown
81 *
82 * This is used when Bootstrap fails or other critical errors occur.
83 */
84 case Failed = 'failed';
85
86 /**
87 * Shutdown phase - application is shutting down
88 *
89 * In this phase:
90 * - All resources are being released
91 * - Connections are being closed
92 * - Process will terminate
93 *
94 * This is the terminal phase before process death.
95 */
96 case Shutdown = 'shutdown';
97 }
98