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, $config);
$application->bootstrap()->run();
- Full name:
\Hazaar\Application
Properties
GLOBALS
The global variables container.
public array $GLOBALS
This is a container for global variables that are available to all controllers and views. This is a way to pass data to all controllers and views without having to pass it through the controller chain.
The following variables are available by default:
- hazaar - Contains the HazaarMVC version and the time the application was started.
- env - The current application environment.
- path - The path to the application root.
- base - The base URL of the application.
- name - The name of the application.
request
public \Hazaar\Application\Request $request
response
public \Hazaar\Controller\Response $response
config
public \Hazaar\Application\Config $config
loader
public \Hazaar\Loader $loader
router
public \Hazaar\Application\Router $router
environment
public string $environment
timer
public ?\Hazaar\Timer $timer
bootstrap
public mixed $bootstrap
urlDefaultPart
protected string $urlDefaultPart
instance
private static ?\Hazaar\Application $instance
- This property is static.
root
private static string $root
- This property is static.
outputFunctions
private callable[] $outputFunctions
Methods
__construct
The main application constructor.
public __construct(string $env): mixed
The application is basically the center of the Hazaar MVC 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 HazaarMVC 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' |
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 static getConfigOverridePaths(): string[]
- This method is static.
Return Value:
$paths
getDefaultConfig
public getDefaultConfig(): array
Return Value:
$config
configure
public configure(\Hazaar\Application\Config $config): void
Parameters:
Parameter | Type | Description |
---|---|---|
$config | \Hazaar\Application\Config |
getInstance
Get the current application instance.
public static getInstance(): \Hazaar\Application
This static function can be used to get a reference to the current application instance from anywhere.
- This method is static.
Return Value:
The application instance
setRoot
public static setRoot(string $value): void
- This method is static.
Parameters:
Parameter | Type | Description |
---|---|---|
$value | string |
getRoot
public static getRoot(): string
- This method is static.
getRuntimePath
Returns the application runtime directory.
public getRuntimePath(string $suffix = null, bool $createDir = false): string
The runtime directory is a place where HazaarMVC 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 | string | An optional suffix to tack on the end of the path |
$createDir | bool | automatically create the runtime directory if it does not exist |
Return Value:
The path to the runtime directory
getFilePath
Return the requested path in the current application.
public static getFilePath(string $path = null, bool $forceRealpath = true): false|string
This method allows access to the raw URL path part, relative to the current application request.
- This method is static.
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
Return Value:
The current controller name
getApplicationPath
Get the real path to the application on the local filesystem resolving links.
public getApplicationPath(string $suffix = null): string
Parameters:
Parameter | Type | Description |
---|---|---|
$suffix | string | application path suffix |
Return Value:
The resolved application path
getBasePath
Get the base path.
public getBasePath(string $suffix = null): string
The base path is the root your application which contains the application, library and public directories
Parameters:
Parameter | Type | Description |
---|---|---|
$suffix | string | application base path suffix |
Return Value:
The resolved base path
bootstrap
Initialise the application ready for execution.
public bootstrap(): \Hazaar\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.
Return Value:
Returns a reference to itself to allow chaining
run
Executes the application.
public run(?\Hazaar\Controller $controller = null): 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 | ?\Hazaar\Controller |
getPath
Return the requested path in the current application.
public static getPath(?string $path = null): string
This method allows access to the raw URL path part, relative to the current application request.
- This method is static.
Parameters:
Parameter | Type | Description |
---|---|---|
$path | ?string |
getURL
Generate a URL relative to the application.
public getURL(): \Hazaar\Application\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
isActive
Test if a URL is active, relative to the application base URL.
public isActive(): bool
Parameters are simply a list of URL 'parts' that will be combined to test against the current URL to see if it is active. Essentially the argument list is the same as Hazaar\Application::url()
except that parameter arrays are not supported.
Unlike Hazaar\Controller::active()
this method tests if the path is active relative to the application base path. If you want to test if a particular controller is active, then it has to be the first argument.
- Example
$application->active('mycontroller');
Return Value:
true if the supplied URL is active as the current URL
getVersion
Return the current Hazaar MVC framework version.
public getVersion(): string
getResponseType
Returns the requested response type.
public getResponseType(): ?string
The requested response type can be set in the request itself. If it is not set, then the default will be 'html' or the X-Requested-With header will be checked to determine the response type.
This method is used internally to determine the response type to send when one has not been explicitly used. Normally the response type is determined by the Controller\Response object type returned by a controller action.
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((object|string)[]|callable $function): void
Parameters:
Parameter | Type | Description |
---|---|---|
$function | (object|string)[]|callable |
Automatically generated on 2024-11-14