TrishulApi\Core\App

Namespace: TrishulApi\Core
Author: Shyam Dubey
Since: v1.0.0

Overview

The App class is the main entry point for the Trishul API framework. It is responsible for bootstrapping the application, initializing core components such as routing, exception handling, CORS security, logging, and Swagger documentation. This class should be instantiated and configured in your application's index.php or main bootstrap file.

Usage Example


use TrishulApi\Core\App;

$app = new App();

// Optionally set a custom global exception handler
$app->set_global_exception_handler(\App\Exception\CustomExceptionHandler::class);

// Optionally set allowed CORS domains
$app->set_allowed_domains(['https://example.com', 'https://api.example.com']);

// Optionally set log directory
$app->set_log_dir('/var/log/trishul');

// Optionally set .env file path
$app->set_env_path(__DIR__ . '/.env');

// Start the application (must be called to handle requests)
$app->start();
        

Methods

public function start(): void

Starts the application by initializing global exception handling, CORS security, and routing. This should be called after all configuration is done.

public function set_global_exception_handler($exception_class): void

Sets a custom global exception handler class. By default, uses ExceptionHandler::class. Call this before start() for custom error handling and logging.

ParameterTypeDescription
$exception_class string Fully qualified class name of the exception handler.
public function set_allowed_domains($domains): void

Sets the allowed domains for CORS (Cross-Origin Resource Sharing). Accepts an array of domain strings.

ParameterTypeDescription
$domains array List of allowed domains.
public function get_allowed_domains(): array

Returns the list of currently allowed CORS domains.

public function set_log_dir($dir): void

Sets the directory where logs will be stored. Throws InvalidArgumentException if the directory is invalid.

ParameterTypeDescription
$dir string Path to the log directory.
public function get_swagger()

Returns the singleton instance of the Swagger documentation generator (TrishulSwagger).

public function set_env_path($path): void

Sets the path to the environment file (.env) for configuration.

ParameterTypeDescription
$path string Path to the .env file.
public function get_env_path(): string

Returns the path to the environment file currently set.

Notes

Related Classes

Changelog