MagratheaPHP2
Up to date Narrative last written —; no source changes since. Method signatures below are reflected live.

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

PropertyTypeDescription
$appRootstringRoot directory of the application
$magRootstringRoot directory of the Magrathea framework itself
$codeFolderarray<string>Folders registered for PSR-4 autoloading
$versionRequired?stringMinimum 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"

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

MethodPHP ErrorsQuery LoggingException Display
Dev()OnOffVerbose
Debug()OnOnVerbose
Prod()OffOffFile log only

Notes

Class Reference — MagratheaPHP

Magrathea2\MagratheaPHP extends Singleton /home/platypusweb/platypusweb.com.br/site/magratheaphp2/src/MagratheaPHP.php

Base class for a Magrathea project. It handles paths, configurations, database connections, and environments.

AddCodeFolder(...$folder): Magrathea2\MagratheaPHP

Adds one or more code folders for the autoloader.

ParamTypeDefault
$folder mixed
AddFeature(...$features): Magrathea2\MagratheaPHP

Adds feature folders based on the application root. For each feature, it adds `features/[name]` and `features/[name]/Base`.

ParamTypeDefault
$features mixed
AddRootCodeFolder(...$folder): Magrathea2\MagratheaPHP
ParamTypeDefault
$folder mixed
AppNamespace(string $namespace): Magrathea2\MagratheaPHP

Sets the project's root namespace, used to scope autoloader errors. Only classes within this namespace will trigger a MagratheaException when not found.

ParamTypeDefault
$namespace string required
AppPath(string $path): Magrathea2\MagratheaPHP

Sets the application's root path. Also defines the `magRoot` as the parent directory.

ParamTypeDefault
$path string required
AppVersion(): string

Gets the version of the application from the `version` file in the project root.

Connect()

Connects to the database using credentials from the configuration.

Debug(): Magrathea2\MagratheaPHP

Enables the debugger. This will also log database queries.

Dev(): Magrathea2\MagratheaPHP

Sets the environment to development mode. Enables error reporting and display.

GetAppRoot(): string

Returns the root path of the application.

GetConfigRoot()

Gets the path to the configuration directory.

GetDB(): Magrathea2\DB\Database

Gets the singleton instance of the Database object.

GetMagratheaRoot(): string

Gets the root directory of the MagratheaPHP library.

Load()

Starts Magrathea

MinVersion(string $version, bool $throwEx = false)

Checks if the current Magrathea version meets a minimum requirement. Otherwise, it displays an error message.

ParamTypeDefault
$version string required
$throwEx bool false
Prod(): Magrathea2\MagratheaPHP

Sets the environment to production mode. Errors will be sent to the logger instead of being displayed.

StartDB()

Connects to the database. Alias for `Connect()`.

StartSession(): Magrathea2\MagratheaPHP

Starts a PHP session if one is not already active.

static LoadVendor(): void

Includes the Composer autoloader.

static Test(): void

A simple test function to check if MagratheaPHP is working.

static Version(): string

Gets Magrathea Version

Examples