final class Main extends Singleton

The main container class managing API singletons and running apps

Main is also a Singleton so it can be fetched anywhere with GetInstance(). A Main handles a single request, with an arbitrary number of appruns.

Properties

static private $instances from  Singleton
private DBStats $construct_stats
private RunContext[] $action_stats
private DBStats[] $commit_stats
private DBStats $total_stats
private RequestLog $reqlog
private float $time
private BaseApp> $apps
private RunContext[] $stack
private Config $config
private ObjectDatabase $database
private ErrorManager $error_manager
private IOInterface $interface
private bool $dirty
private $dbException
private $cfgException
private $rolledback

Methods

static Singleton
GetInstance()

Get the instance of the singleton

__construct(IOInterface $interface, ErrorManager $errorman)

Initializes the Main API

float
GetTime()

Gets the timestamp of when the request was started

array
GetApps()

Gets an array of instantiated apps

bool
HasDatabase()

Returns true if the global ObjectDatabase instance is valid

GetDatabase()

Returns the global ObjectDatabase instance or throws if not configured

Config|null
TryGetConfig()

Returns the global config object or null if not installed

GetConfig()

Returns the global config object if installed else throws

GetInterface()

Returns the interface used for the current request

GetErrorManager()

Returns a reference to the global error manager

RunContext|null
GetContext()

Returns the RunContext that is currently being executed

bool
TryLoadApp(string $app)

Loads the main include file for an app and constructs it

LoadApp(string $app)

Loads the main include file for an app and constructs it

Run(Input $input)

Calls into an app to run the given Input command

RunRemote(string $url, Input $input)

Calls into a remote API to run the given Input command

static 
LoggedTry(callable $func)

Runs the given $func in try/catch and logs if exception

void
rollback(Throwable|null $e = null)

Rolls back the current transaction. Internal only, do not call via apps.

void
commit()

Commits the current transaction. Internal only, do not call via apps.

void
innerCommit(bool $apps)

Commits the database or does a rollback if readOnly/dryrun

bool
isEnabled()

if false, requests are not allowed (always true for privileged interfaces)

int
GetDebugLevel()

Returns the configured debug level, or the interface's default

void
FinalizeOutput(Output $output, bool $isError = false)

Compiles performance metrics and adds them to the given output

Details

in Singleton at line 21
static Singleton GetInstance()

Get the instance of the singleton

Return Value

Singleton

Exceptions

MissingSingletonException

if not yet constructed

at line 165
__construct(IOInterface $interface, ErrorManager $errorman)

Initializes the Main API

Creates the error manager, initializes the interface, initializes the database, loads global config, loads and constructs registered apps

Parameters

IOInterface $interface

the interface that began the request

ErrorManager $errorman

the error manager instance

Exceptions

MaintenanceException

if the server is not enabled

FailedAppLoadException

if a registered app fails to load

at line 105
float GetTime()

Gets the timestamp of when the request was started

Return Value

float

at line 111
array GetApps()

Gets an array of instantiated apps

Return Value

array

at line 114
bool HasDatabase()

Returns true if the global ObjectDatabase instance is valid

Return Value

bool

at line 117
ObjectDatabase GetDatabase()

Returns the global ObjectDatabase instance or throws if not configured

Return Value

ObjectDatabase

at line 128
Config|null TryGetConfig()

Returns the global config object or null if not installed

Return Value

Config|null

at line 131
Config GetConfig()

Returns the global config object if installed else throws

Return Value

Config

at line 144
IOInterface GetInterface()

Returns the interface used for the current request

Return Value

IOInterface

at line 147
ErrorManager GetErrorManager()

Returns a reference to the global error manager

Return Value

ErrorManager

at line 150
RunContext|null GetContext()

Returns the RunContext that is currently being executed

Return Value

RunContext|null

at line 228
protected bool TryLoadApp(string $app)

Loads the main include file for an app and constructs it

Parameters

string $app

Return Value

bool

at line 247
Main LoadApp(string $app)

Loads the main include file for an app and constructs it

Parameters

string $app

Return Value

Main

at line 263
Run(Input $input)

Calls into an app to run the given Input command

Calls Run() on the requested app and then saves (but does not commit) any modified objects. These calls can be nested - apps can call Run for other apps but should always do so via the API, not directly to the app

Parameters

Input $input

the command to run

Exceptions

UnknownAppException

if the requested app is invalid

at line 305
RunRemote(string $url, Input $input)

Calls into a remote API to run the given Input command

Note that this breaks transactions - the remote API will commit before we get the response to this remote call.

Parameters

string $url

the base URL of the remote API

Input $input

See also

Main::Run

at line 315
static LoggedTry(callable $func)

Runs the given $func in try/catch and logs if exception

Parameters

callable $func

at line 327
void rollback(Throwable|null $e = null)

Rolls back the current transaction. Internal only, do not call via apps.

First rolls back each app, then the database, then saves mandatorySave objects if not a server error

Parameters

Throwable|null $e

the exception that caused the rollback (or null)

Return Value

void

at line 362
void commit()

Commits the current transaction. Internal only, do not call via apps.

First commits each app, then the database. Does a rollback instead if the request was specified as a dry run.

Return Value

void

Exceptions

CommitAfterRollbackException

if a rollback was previously performed

at line 394
private void innerCommit(bool $apps)

Commits the database or does a rollback if readOnly/dryrun

Parameters

bool $apps

if true, commit/rollback apps also

Return Value

void

at line 407
private bool isEnabled()

if false, requests are not allowed (always true for privileged interfaces)

Return Value

bool

at line 415
int GetDebugLevel()

Returns the configured debug level, or the interface's default

Return Value

int

at line 434
void FinalizeOutput(Output $output, bool $isError = false)

Compiles performance metrics and adds them to the given output

Parameters

Output $output

the output object to add metrics to

bool $isError

if true, the output is an error response

Return Value

void