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

AdminManager — Admin Runtime Singleton

File: src/Admin/AdminManager.php Namespace: Magrathea2\Admin Extends: Singleton

The runtime singleton for the admin panel. Manages initialization, authentication, session state, asset compilation, feature routing, and UI helpers. You interact with AdminManager throughout an admin request lifecycle.


Starting the Admin

Start(Admin $admin): AdminManager

Initialize with a custom configured Admin object.

$admin = new Admin();
$admin->SetTitle("My App Admin")
      ->SetPrimaryColor("#3498DB")
      ->AddFeaturesArray([
          new ProductAdminFeature(),
          new OrderAdminFeature(),
      ]);

AdminManager::Instance()->Start($admin);

StartDefault(?string $title = null, ?string $color = null): AdminManager

Start with default configuration and all built-in features enabled.

AdminManager::Instance()->StartDefault("My App", "#e74c3c");

Built-in features included by default: user management, user logs, app config, cache, file editor, API explorer.

GetAdmin(): ?Admin

Returns the current Admin configuration object.


Authentication

Auth(): bool

Check whether the current HTTP session contains a valid admin user.

if (!AdminManager::Instance()->Auth()) {
    header("Location: /admin/login");
    exit;
}

GetLoggedUser(): AdminUser|null

Return the currently logged-in AdminUser object.

$user = AdminManager::Instance()->GetLoggedUser();
if ($user) {
    echo "Logged in as: " . $user->name;
}

PermissionDenied(): void

Render the permission-denied error page and exit.

if (!AdminManager::Instance()->Auth()) {
    AdminManager::Instance()->PermissionDenied();
    exit;
}

ErrorPage(string $message): void

Render a generic error page with a custom message.

AdminManager::Instance()->ErrorPage("Database connection failed.");

UI Helpers

GetTitle(): string

Returns the panel title string.

echo "<title>" . AdminManager::Instance()->GetTitle() . "</title>";

GetColor(): string

Returns the primary color as an RGB decimal string (e.g., "58, 123, 213").

$color = AdminManager::Instance()->GetColor();
echo "style='color: rgb($color)'";

PrintLogo(?int $logoSize = 200): void

Outputs the admin logo, sized to $logoSize px. The markup depends on the file extension of Admin::$adminLogo: .jpeg/.jpg/.png render as an <img> tag, while .svg inlines the SVG file's contents inside a sized wrapper <div>.

AdminManager::Instance()->PrintLogo(150);
// PNG/JPEG: <img src="/assets/logo.png" width="150" ...>
// SVG:      <div class="logo_image" style="...width: 150px; height: 150px">...inline <svg>...</div>

GetFaviconTag(): string

Returns the <link rel="icon"> tag string for the favicon.

echo AdminManager::Instance()->GetFaviconTag();

Feature Management

GetFeature(string $featureId): AdminFeature|null

Retrieve a registered feature by its ID.

$cacheFeature = AdminManager::Instance()->GetFeature("cache");
if ($cacheFeature) {
    $cacheFeature->Handle();
}

GetActiveFeature(): AdminFeature|null

Returns the feature matching the current URL segment. Used by the admin router.

$feature = AdminManager::Instance()->GetActiveFeature();
if ($feature) {
    $feature->Handle();
    $feature->Render();
} else {
    // show dashboard
}

SetMenu(AdminMenu $m): AdminManager

Set the current admin menu.

GetMenu(): AdminMenu

Returns the current AdminMenu object for rendering.

$menu = AdminManager::Instance()->GetMenu();
$menu->Render();

Asset Management

AddJs(string $file): AdminManager

Add a JavaScript file to the admin bundle.

AdminManager::Instance()->AddJs("/admin/assets/charts.js");

GetJs(): string

Returns all registered JS files bundled and minified.

echo "<script>" . AdminManager::Instance()->GetJs() . "</script>";

GetJSManager(): JavascriptCompressor

Returns the underlying JavascriptCompressor instance for advanced use.

AddCss(string $file): AdminManager

Add a CSS/SCSS file to the admin bundle.

AdminManager::Instance()->AddCss("/admin/assets/custom.scss");

GetCss(): string

Returns all CSS/SCSS compiled and minified.

echo "<style>" . AdminManager::Instance()->GetCss() . "</style>";

GetCSSManager(): CssCompressor

Returns the underlying CssCompressor instance.


Action Logging

Log(string $action, mixed $victim = null, mixed $data = null, mixed $user_id = false): void

Record an admin action. All calls appear in the UserLogs feature.

AdminManager::Instance()->Log(
    "product_deleted",
    $product->id,
    $product->ToArray()
);

Full Admin Entry Point Example

<?php
// admin/index.php
require_once __DIR__ . '/../vendor/autoload.php';

use Magrathea2\MagratheaPHP;
use Magrathea2\Admin\AdminManager;
use App\Admin\ProductAdminFeature;
use App\Admin\OrderAdminFeature;

MagratheaPHP::LoadVendor();

MagratheaPHP::Instance()
    ->AppPath(__DIR__ . '/..')
    ->AddCodeFolder("models", "controls", "admin")
    ->Prod()
    ->Load()
    ->Connect()
    ->StartSession();

$manager = AdminManager::Instance()
    ->StartDefault("My App Admin", "#2c3e50");

// Custom features
$manager->GetAdmin()->AddFeaturesArray([
    new ProductAdminFeature(),
    new OrderAdminFeature(),
]);

// Authentication gate
if (!$manager->Auth()) {
    header("Location: /admin/login.php");
    exit;
}

// Route to the active feature or show dashboard
$feature = $manager->GetActiveFeature();
if ($feature) {
    $feature->Handle();
}

// Build menu
$menu = $manager->GetAdmin()->BuildMenu();
$manager->SetMenu($menu);

// Render layout
include __DIR__ . '/layout.php';

Notes

Class Reference — AdminManager

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

Class for managing Magrathea's Admin

AddCss($file): Magrathea2\Admin\AdminManager

Adds a css file to admin

ParamTypeDefault
$file mixed required
AddJs($file): Magrathea2\Admin\AdminManager

Adds a javascript file to admin

ParamTypeDefault
$file mixed required
Auth(): bool

Permission verification

ErrorPage($message)

display error page

ParamTypeDefault
$message mixed required
GetActiveFeature(): ?Magrathea2\Admin\AdminFeature

returns active feature (the one from "magrathea_feature" data)

GetAdmin(): ?Magrathea2\Admin\Admin

returns the current admin.

GetCSSManager(): Magrathea2\Compressors\CssCompressor

Gets CSS compressor

GetColor(): string

Gets color

GetCss(): string

Gets Js code

GetFaviconTag(): string
GetFeature($featureId): ?Magrathea2\Admin\AdminFeature

gets admin feature id and returns its object

ParamTypeDefault
$featureId mixed required
GetJSManager(): Magrathea2\Compressors\JavascriptCompressor

Gets Js compressor

GetJs(): string

Gets Js code

GetLoggedUser(): ?Magrathea2\Admin\Features\User\AdminUser

Return logged user

GetMenu(): Magrathea2\Admin\AdminMenu

Gets the AdminMenu

GetTitle(): string

Gets title

Initialize()
Log($action, $victim = null, $data = null, $user_id = false): void

Log an action

ParamTypeDefault
$action mixed required
$victim mixed null
$data mixed null
$user_id mixed false
PermissionDenied()

Show permission denied page

SetMenu(Magrathea2\Admin\AdminMenu $m): Magrathea2\Admin\AdminManager

Sets the menu

ParamTypeDefault
$m Magrathea2\Admin\AdminMenu required
Start(Magrathea2\Admin\Admin $admin): Magrathea2\Admin\AdminManager

Starts Admin

ParamTypeDefault
$admin Magrathea2\Admin\Admin required
StartDefault(?string $title = null, ?string $color = null): Magrathea2\Admin\AdminManager

Starts a default admin

ParamTypeDefault
$title ?string null
$color ?string null

Examples