MagratheaPHP — Main Entry Point
File: src/MagratheaPHP.php Namespace: Magrathea2 Extends: Singleton
The central bootstrap class for any MagratheaPHP2 application. Manages the app root path, autoloading, environment mode, configuration loading, and database connection. Uses the Singleton pattern — always access via MagratheaPHP::Instance().
Properties
| Property | Type | Description |
|---|---|---|
$appRoot | string | Root directory of the application |
$magRoot | string | Root directory of the Magrathea framework itself |
$codeFolder | array<string> | Folders registered for PSR-4 autoloading |
$versionRequired | ?string | Minimum required Magrathea version |
Static Methods
LoadVendor(): void
Loads Magrathea's internal autoloader. Call this before anything else.
MagratheaPHP::LoadVendor();
Version(): string
Returns the current framework version string.
echo MagratheaPHP::Version(); // "2.1.24"
GetDocumentationLink(): string
Returns a link to the official online documentation.
Test(): void
Runs internal self-test of the framework.
Instance Methods (Fluent Interface)
All instance methods return $this for chaining unless otherwise stated.
AppPath(string $path): MagratheaPHP
Sets the application root directory. Must be called first.
MagratheaPHP::Instance()->AppPath(__DIR__);
GetAppRoot(): string
Returns the currently set application root path.
GetMagratheaRoot(): string
Returns the framework's own root path.
MinVersion(string $version, bool $throwEx = false): MagratheaPHP
Asserts the running framework version is at least $version. If $throwEx is true, throws an exception on failure.
MagratheaPHP::Instance()->MinVersion("2.1.0");
AddCodeFolder(...$folder): MagratheaPHP
Registers additional folders (relative to $appRoot) for the autoloader.
$app->AddCodeFolder("models", "controls", "services");
AddRootCodeFolder(...$folder): MagratheaPHP
Same as AddCodeFolder but paths are relative to the filesystem root.
AddFeature(...$features): MagratheaPHP
Registers Admin panel feature classes.
Dev(): MagratheaPHP
Enables development mode: verbose PHP errors and warnings.
Debug(): MagratheaPHP
Enables debug mode: same as Dev + database query logging via Debugger.
Prod(): MagratheaPHP
Enables production mode: suppresses error display, uses file logging.
Load(): MagratheaPHP
Loads the configuration file. Must be called after AppPath() and the desired mode.
GetConfigRoot(): string
Returns the path to the configuration directory (typically <appRoot>/config/).
StartDB(): MagratheaPHP / Connect(): MagratheaPHP
Connects to the database using credentials from the loaded config. StartDB is an alias for Connect.
$app->Connect();
// or
$app->StartDB();
GetDB(): Database
Returns the active Database singleton instance.
$db = MagratheaPHP::Instance()->GetDB();
StartSession(): MagratheaPHP
Calls session_start() if no session is active.
AppVersion(): string
Reads the app version from <magRoot>/version and removes trailing line breaks (\n / \r\n).
Full Bootstrap Chain
<?php
require_once __DIR__ . '/vendor/autoload.php';
use Magrathea2\MagratheaPHP;
MagratheaPHP::LoadVendor();
$app = MagratheaPHP::Instance()
->AppPath(__DIR__)
->MinVersion("2.1.0")
->AddCodeFolder("models", "controls", "api")
->Dev() // swap for ->Prod() in production
->Load()
->StartSession()
->Connect();
Environment Modes Comparison
| Method | PHP Errors | Query Logging | Exception Display |
|---|---|---|---|
Dev() | On | Off | Verbose |
Debug() | On | On | Verbose |
Prod() | Off | Off | File log only |
Notes
MagratheaPHPitself extendsSingleton, soMagratheaPHP::Instance()always returns the same object.- Config is expected at
<appRoot>/config/magrathea.confby default. - The autoloader scans every folder registered via
AddCodeFolderto resolve class names.
Class Reference — MagratheaPHP
Base class for a Magrathea project. It handles paths, configurations, database connections, and environments.
Adds one or more code folders for the autoloader.
| Param | Type | Default |
|---|---|---|
$folder |
mixed | — |
Adds feature folders based on the application root. For each feature, it adds `features/[name]` and `features/[name]/Base`.
| Param | Type | Default |
|---|---|---|
$features |
mixed | — |
| Param | Type | Default |
|---|---|---|
$folder |
mixed | — |
Sets the project's root namespace, used to scope autoloader errors. Only classes within this namespace will trigger a MagratheaException when not found.
| Param | Type | Default |
|---|---|---|
$namespace |
string | required |
Sets the application's root path. Also defines the `magRoot` as the parent directory.
| Param | Type | Default |
|---|---|---|
$path |
string | required |
Gets the version of the application from the `version` file in the project root.
Connects to the database using credentials from the configuration.
Enables the debugger. This will also log database queries.
Sets the environment to development mode. Enables error reporting and display.
Returns the root path of the application.
Gets the path to the configuration directory.
Gets the singleton instance of the Database object.
Gets the root directory of the MagratheaPHP library.
Starts Magrathea
Checks if the current Magrathea version meets a minimum requirement. Otherwise, it displays an error message.
| Param | Type | Default |
|---|---|---|
$version |
string | required |
$throwEx |
bool | false |
Sets the environment to production mode. Errors will be sent to the logger instead of being displayed.
Connects to the database. Alias for `Connect()`.
Starts a PHP session if one is not already active.
Returns the link to Magrathea's official documentation.
Includes the Composer autoloader.
A simple test function to check if MagratheaPHP is working.
Gets Magrathea Version