|
1 | 1 | --- |
2 | 2 | sidebar_position: 1 |
3 | | ---- |
| 3 | +--- |
| 4 | + |
| 5 | +:::danger Important |
| 6 | +Certain features of the `App` class are either under development or may undergo significant changes in future updates. Please refer to the official documentation regularly to stay informed about the latest updates and modifications. |
| 7 | +::: |
| 8 | + |
| 9 | +# App Class Overview |
| 10 | + |
| 11 | +The `App` class is the cornerstone of the **Fire Framework**, designed to streamline application management. It handles configuration, routing, middleware, services, and orchestrates the entire application lifecycle with a robust and cohesive structure. |
| 12 | + |
| 13 | +--- |
| 14 | + |
| 15 | +## 📖 Overview |
| 16 | + |
| 17 | +### Namespace |
| 18 | + |
| 19 | +```php |
| 20 | +Framework\Fire |
| 21 | +``` |
| 22 | + |
| 23 | +### Description |
| 24 | + |
| 25 | +The `App` class is implemented as a singleton to ensure a single instance of the application is maintained. It orchestrates environment configuration, routing, middleware, and database connections, providing a cohesive structure for application management. |
| 26 | + |
| 27 | +--- |
| 28 | + |
| 29 | +## 🛠️ Properties |
| 30 | + |
| 31 | +### `static ?App $instance` |
| 32 | + |
| 33 | +- **Description**: Holds the singleton instance of the `App` class. |
| 34 | +- **Visibility**: Protected. |
| 35 | + |
| 36 | +### `IRouter $router` |
| 37 | + |
| 38 | +- **Description**: Manages HTTP request routing. |
| 39 | +- **Visibility**: Protected. |
| 40 | + |
| 41 | +### `string $envPath` |
| 42 | + |
| 43 | +- **Description**: Specifies the path to the `.env` file. |
| 44 | +- **Visibility**: Protected. |
| 45 | + |
| 46 | +### `string $basePath` |
| 47 | + |
| 48 | +- **Description**: Defines the base directory of the application. |
| 49 | +- **Visibility**: Protected. |
| 50 | + |
| 51 | +### `IEnvironmentLoader $environmentLoader` |
| 52 | + |
| 53 | +- **Description**: Handles the loading of environment variables. |
| 54 | +- **Visibility**: Protected. |
| 55 | + |
| 56 | +### `IDatabaseManager $databaseManager` |
| 57 | + |
| 58 | +- **Description**: Manages database connections and interactions. |
| 59 | +- **Visibility**: Protected. |
| 60 | + |
| 61 | +--- |
| 62 | + |
| 63 | +## 🔧 Methods |
| 64 | + |
| 65 | +### `static getInstance(): self` |
| 66 | + |
| 67 | +- **Description**: Retrieves the singleton instance of the `App` class. |
| 68 | +- **Throws**: `RuntimeException` if the instance is not initialized. |
| 69 | +- **Returns**: `App`. |
| 70 | + |
| 71 | +--- |
| 72 | + |
| 73 | +### `static configure(string $basePath, ?IEnvironmentLoader $environmentLoader = null, ?IRouter $router = null, ?IDatabaseManager $databaseManager = null): self` |
| 74 | + |
| 75 | +- **Description**: Configures the application with essential components such as the base path, environment loader, router, and database manager. |
| 76 | +- **Parameters**: |
| 77 | + - `string $basePath`: The base directory of the application. |
| 78 | + - `?IEnvironmentLoader $environmentLoader`: (Optional) The environment loader instance. |
| 79 | + - `?IRouter $router`: (Optional) The router instance. |
| 80 | + - `?IDatabaseManager $databaseManager`: (Optional) The database manager instance. |
| 81 | +- **Returns**: `App`. |
| 82 | + |
| 83 | +--- |
| 84 | + |
| 85 | +### `loadRoutes(): self` |
| 86 | + |
| 87 | +- **Description**: Loads application routes from the `/routes` directory. |
| 88 | +- **Throws**: `RuntimeException` if the routes directory is missing. |
| 89 | +- **Returns**: `App`. |
| 90 | + |
| 91 | +--- |
| 92 | + |
| 93 | +### `loadMiddlewares(): self` |
| 94 | + |
| 95 | +- **Description**: Loads middleware components from the `/app/middlewares` directory. |
| 96 | +- **Returns**: `App`. |
| 97 | + |
| 98 | +--- |
| 99 | + |
| 100 | +### `loadServices(): self` |
| 101 | + |
| 102 | +- **Description**: Loads additional services from a specified directory (to be implemented). |
| 103 | +- **Returns**: `App`. |
| 104 | + |
| 105 | +--- |
| 106 | + |
| 107 | +### `getBasePath(): string` |
| 108 | + |
| 109 | +- **Description**: Retrieves the base directory of the application. |
| 110 | +- **Returns**: `string`. |
| 111 | + |
| 112 | +--- |
| 113 | + |
| 114 | +### `setNotFoundHandler(callable $handler): self` |
| 115 | + |
| 116 | +- **Description**: Configures a custom handler for "404 Not Found" errors. |
| 117 | +- **Parameters**: |
| 118 | + - `callable $handler`: The callback function to handle 404 errors. |
| 119 | +- **Returns**: `App`. |
| 120 | + |
| 121 | +--- |
| 122 | + |
| 123 | +### `run(): void` |
| 124 | + |
| 125 | +- **Description**: Executes the application by resolving the current HTTP request through the router. Handles exceptions by returning a 500 error response. |
| 126 | +- **Returns**: `void`. |
| 127 | + |
| 128 | +--- |
| 129 | + |
| 130 | +## 📂 Usage Example |
| 131 | + |
| 132 | +```php |
| 133 | +use Framework\Fire\App; |
| 134 | +use Framework\Fire\Environment\EnvironmentLoader; |
| 135 | +use Framework\Fire\Routing\Router; |
| 136 | + |
| 137 | +// Configure the application |
| 138 | +$app = App::configure( |
| 139 | + __DIR__, |
| 140 | + new EnvironmentLoader(__DIR__), |
| 141 | + new Router() |
| 142 | +); |
| 143 | + |
| 144 | +// Load routes and middleware |
| 145 | +$app->loadRoutes() |
| 146 | + ->loadMiddlewares(); |
| 147 | + |
| 148 | +// Run the application |
| 149 | +$app->run(); |
| 150 | +``` |
| 151 | + |
| 152 | +--- |
| 153 | + |
| 154 | +## 🔗 Notes |
| 155 | + |
| 156 | +- The `App` class employs a singleton pattern to ensure a single instance is used throughout the application lifecycle. |
| 157 | +- Additional functionality can be integrated by utilizing the `loadServices()` method. |
| 158 | +- Comprehensive error handling is implemented to manage runtime exceptions effectively, ensuring application stability. |
0 commit comments