Documentation

WebApp extends AbstractApp
in package

Handles HTTP request/response

Table of Contents

CONFIG_BASE_URL  = 'app.base_url'
CONFIG_ENVIRONMENT  = 'app.environment'
CONFIG_ERROR_PAGES_FOLDER  = 'app.error_pages_folder'
CONFIG_ROOT_PATH  = 'app.root_path'
CONFIG_USE_ROUTE_ATTRIBUTES  = 'app.use_route_attributes'
CONTENT_TYPE_HTML  = 'text/html; charset=UTF-8'
CONTENT_TYPE_JSON  = 'application/json'
ERROR_CONTENT_PLACEHOLDER  = '<!-- content -->'
EVENT_INIT_FINISHED  = 'app:init_finished'
EVENT_ROUTE_MATCHED  = 'webapp:route_matched'
HEADER_CONTENT_TYPE  = 'Content-Type'
HEADER_LOCATION  = 'Location'
PRODUCTION_ENVIRONMENT  = 'prod'
$config  : ConfigInterface|null
$configPaths  : array<string|int, mixed>
$exitOnFinish  : bool
$logger  : LoggerInterface|null
$middlewares  : array<string|int, mixed>
Stores the middleware class names in a list
$response  : ResponseInterface
$router  : RouterInterface
__construct()  : mixed
addMiddleware()  : void
Adds a middleware
finish()  : void
Finishes the application
fullInit()  : void
Fully initializes the application
fullProcess()  : void
Calls the `process()` method within a try/catch, handles exception with the `handleException()` method
init()  : void
Abstract function for initialize the application
process()  : void
Abstract function for processing the application
redirect()  : void
sendContent()  : void
sendError()  : void
Sends an error response
useJwtAuth()  : void
useRouteAttributes()  : void
handleException()  : void
Handles the exception
isWeb()  : bool
Returns true if the call is from the web
loadConfigs()  : void
Loads all the configs by the `$configPaths`
loadErrorPageContent()  : string
If it exists, loads the content of an error HTML page otherwise returns the HTML comment for the error placeholder
runMiddlewares()  : void
Runs all the added middlewares

Constants

CONFIG_BASE_URL

public mixed CONFIG_BASE_URL = 'app.base_url'

CONFIG_ENVIRONMENT

public mixed CONFIG_ENVIRONMENT = 'app.environment'

CONFIG_ERROR_PAGES_FOLDER

public mixed CONFIG_ERROR_PAGES_FOLDER = 'app.error_pages_folder'

CONFIG_ROOT_PATH

public mixed CONFIG_ROOT_PATH = 'app.root_path'

CONFIG_USE_ROUTE_ATTRIBUTES

public mixed CONFIG_USE_ROUTE_ATTRIBUTES = 'app.use_route_attributes'

CONTENT_TYPE_HTML

public mixed CONTENT_TYPE_HTML = 'text/html; charset=UTF-8'

CONTENT_TYPE_JSON

public mixed CONTENT_TYPE_JSON = 'application/json'

ERROR_CONTENT_PLACEHOLDER

public mixed ERROR_CONTENT_PLACEHOLDER = '<!-- content -->'

EVENT_INIT_FINISHED

public mixed EVENT_INIT_FINISHED = 'app:init_finished'

EVENT_ROUTE_MATCHED

public mixed EVENT_ROUTE_MATCHED = 'webapp:route_matched'

HEADER_CONTENT_TYPE

public mixed HEADER_CONTENT_TYPE = 'Content-Type'

HEADER_LOCATION

public mixed HEADER_LOCATION = 'Location'

PRODUCTION_ENVIRONMENT

public mixed PRODUCTION_ENVIRONMENT = 'prod'

Properties

$configPaths

protected array<string|int, mixed> $configPaths

$middlewares

Stores the middleware class names in a list

protected array<string|int, mixed> $middlewares = []

Methods

__construct()

public __construct(array<string|int, mixed> $configPaths) : mixed
Parameters
$configPaths : array<string|int, mixed>
Return values
mixed

addMiddleware()

Adds a middleware

public addMiddleware(string $interface) : void

It adds only if not presents.

Parameters
$interface : string
Return values
void

finish()

Finishes the application

public finish(string|int $content) : void

If the $exitOnFinish true (default) calls the exit, otherwise just prints out the content.

Parameters
$content : string|int

Content for the output. If it's an int, it is the return code of the process.

Return values
void

fullInit()

Fully initializes the application

public fullInit() : void

Creates the Config, loads the configs, creates the Logger, calls the init() method then runs all the middlewares. If an exception happens, handles it with the handleException() method.

Return values
void

fullProcess()

Calls the `process()` method within a try/catch, handles exception with the `handleException()` method

public fullProcess() : void
Return values
void

init()

Abstract function for initialize the application

public init() : void
Return values
void

process()

Abstract function for processing the application

public process() : void
Return values
void

redirect()

public redirect(string $location[, array<string|int, mixed> $params = [] ]) : void
Parameters
$location : string
$params : array<string|int, mixed> = []
Return values
void

sendContent()

public sendContent(mixed $content) : void
Parameters
$content : mixed
Return values
void

sendError()

Sends an error response

public sendError(int $code[, string $content = '' ]) : void
Parameters
$code : int

The error code

$content : string = ''

The error content

Return values
void

useJwtAuth()

public useJwtAuth() : void
Return values
void

useRouteAttributes()

public useRouteAttributes() : void
Return values
void

handleException()

Handles the exception

protected handleException(Exception $e) : void

Calls the parent exception handler, then calls the sendError with HTTP error 500. Sets the content for the error placeholder if the environment is not production.

Parameters
$e : Exception

The exception for handling

Return values
void

isWeb()

Returns true if the call is from the web

protected isWeb() : bool
Return values
bool

loadConfigs()

Loads all the configs by the `$configPaths`

protected loadConfigs() : void
Return values
void

loadErrorPageContent()

If it exists, loads the content of an error HTML page otherwise returns the HTML comment for the error placeholder

protected loadErrorPageContent(int $code) : string
Parameters
$code : int

The HTTP status code for the error

Return values
string

The content of the HTML file or the HTML comment for the error placeholder

runMiddlewares()

Runs all the added middlewares

protected runMiddlewares() : void
Return values
void

Search results