Application
Application
The Application.
The main application class is the core of the whole application and is responsible for routing actions to controllers based on request objects extended extended from Hazaar\Application\Request.
Supported request objects are:
- Hazaar\Application\Request\Http - A standard HTTP request. This can be either GET or POST requests. POST requests can optionally have a request body. In this case the Content-Type header will be checked to see how to decode the request body. multipart/form-data and application/json are currently accepted.
- Hazaar\Application\Request\Cli - This is a special request type to allow Hazaar applications to be executed from the command line. This is currently used by the config tool
Example usage:
define('APPLICATION_ENV', 'development');
$config = 'application.ini';
$application = new Hazaar\Application(APPLICATION_ENV);
$application->bootstrap()->run();
Constants
VERSION
public const VERSION = '3.0'
Properties
config
public Config $config
loader
public Loader $loader
router
public Router $router
environment
public string $environment = 'development'
path
public string $path
base
public string $base
timer
public Timer $timer
urlDefaultPart
protected string $urlDefaultPart
instance
public Application $instance
root
private string $root
outputFunctions
private array $outputFunctions
Methods
__construct
The main application constructor.
public __construct(string $env, ?string $path): void
The application is basically the center of the Hazaar universe. Everything hangs off of it and controllers are executed within the context of the application. The main constructor prepares the application to begin processing and is the first piece of code executed within the Hazaar environment.
Because of this is it responsible for setting up the class loader, starting code execution profiling timers, loading the application configuration and setting up the request object.
Hazaar also has a 'run direct' function that allows Hazaar to execute special requests directly without going through the normal application execution path. These requests are used so hazaar can serve static internal content such as error pages, images and style sheets required by these error pages and redistributed JavaScript code libraries.
Parameters
Parameter | Type | Description |
---|---|---|
$env | string | The application environment name. eg: 'development' or 'production' |
$path | string |
shutdown
The main application destructor.
public shutdown(): void
The destructor cleans up any application redirections. ifthe controller hasn't used it in this run then it loses it. This prevents stale redirect URIs from accidentally being used.
getConfigOverridePaths
public getConfigOverridePaths(): void
getDefaultConfig
public getDefaultConfig(): void
configure
public configure(Config $config): void
Parameters
Parameter | Type | Description |
---|---|---|
$config | Config |
getInstance
public getInstance(): ?Application
setRoot
public setRoot(string $value): void
Parameters
Parameter | Type | Description |
---|---|---|
$value | string |
getRoot
public getRoot(): string
getRuntimePath
Returns the application runtime directory.
public getRuntimePath(NULL $suffix, boolean $createDir = false): string
The runtime directory is a place where Hazaar will keep files that it needs to create during normal operation. For example, socket files for background scheduler communication, cached views, and backend applications.
Parameters
Parameter | Type | Description |
---|---|---|
$suffix | NULL | An optional suffix to tack on the end of the path |
$createDir | boolean | automatically create the runtime directory if it does not exist |
getFilePath
Return the requested path in the current application.
public getFilePath(?string $path, bool $forceRealpath = true): string
This method allows access to the raw URL path part, relative to the current application request.
Parameters
Parameter | Type | Description |
---|---|---|
$path | string | path suffix to append to the application path |
$forceRealpath | bool | Return the real path to a file. If the file does not exist, this will return false. |
getRequestedController
Get the currently requested controller name.
public getRequestedController(): string
getApplicationPath
Get the real path to the application on the local filesystem resolving links.
public getApplicationPath(NULL $suffix): string
Parameters
Parameter | Type | Description |
---|---|---|
$suffix | NULL | application path suffix |
getBasePath
Get the base path.
public getBasePath(NULL $suffix): string
The base path is the root your application which contains the application, library and public directories
Parameters
Parameter | Type | Description |
---|---|---|
$suffix | NULL | application base path suffix |
bootstrap
Initialise the application ready for execution.
public bootstrap(): Application
Bootstrap is the first step in running an application. It will run some checks to make sure sure the server has any required modules loaded as requested by the application (via the config). It will then execute the application bootstrap.php script within the context of the application. Once that step succeeds the requested (or the default) controller will be loaded and initialised so that it is ready for execution by the application.
run
Executes the application.
public run(?Controller $controller): int
Once the application has been initialised and a controller loaded, it can be executed via the run() method. This will execute the loaded controller and check that it returns a valid Hazaar\Controller\Response object. ifa valid response is not returned an exception will be raised.
Once a valid response object is returned it will be used to write output back to the web server to be returned to the user.
This method also has protection against error loops. ifan exception is thrown while processing a Hazaar\Controller\Error controller object then a new exception will be thrown outside the application context and will display basic fall-back error output.
Parameters
Parameter | Type | Description |
---|---|---|
$controller | Controller |
getPath
public getPath(?string $path): string
Parameters
Parameter | Type | Description |
---|---|---|
$path | string |
getURL
Generate a URL relative to the application.
public getURL(): URL
This is the base method for generating URLs in your application. URLs generated directly from here are relative to the application base path. For URLs that are relative to the current controller see Controller::url()
Parameters are dynamic and depend on what you are trying to generate.
For examples see: Generating URLs
getVersion
Return the current Hazaar framework version.
public getVersion(): string
setResponseType
Set the response type override for a request.
public setResponseType(string $type): void
The response type should be set in the response object itself. However, setting this allows that to be overridden. This should be used sparingly but can be used from a controller to force reponses to a certain type, such as application/json.
Parameters
Parameter | Type | Description |
---|---|---|
$type | string |
registerOutputFunction
Register an output function.
public registerOutputFunction(callable $function): void
Parameters
Parameter | Type | Description |
---|---|---|
$function | callable |
errorHandler
Custom error handler function.
public errorHandler(int $errno, string $errstr, ?string $errfile, ?int $errline): bool
This function is responsible for handling PHP errors and displaying appropriate error messages.
Parameters
Parameter | Type | Description |
---|---|---|
$errno | int | the error number |
$errstr | string | the error message |
$errfile | string | the file where the error occurred |
$errline | int | the line number where the error occurred |
shutdownHandler
Shutdown handler function.
public shutdownHandler(): void
This function is responsible for executing the shutdown tasks registered in the global variable $__shutdownTasks. It checks if the script is running in CLI mode or if headers have already been sent before executing the tasks.
exceptionHandler
Exception handler function.
public exceptionHandler(\Throwable $e, ?int $responseType): void
This function is responsible for handling exceptions thrown in the application. If the exception code is greater than or equal to 500, it logs the error message along with the code, line number, and file name. Then it calls the errorAndDie()
function to handle the error further.
Parameters
Parameter | Type | Description |
---|---|---|
$e | \Throwable | |
$responseType | int |
getBase
public getBase(): string
findApplicationPath
public findApplicationPath(?string $search_path): string
Parameters
Parameter | Type | Description |
---|---|---|
$search_path | string |
Generated by Hazaar API Doc Generator