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
}
Menu
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
AdminManagermust be initialized before calling any other admin method.StartDefault()is the fastest way to get a functional admin panel. UseStart()for full customization.- Asset methods (
GetJs(),GetCss()) compile files on first call and cache the result for the request. - Feature routing is URL-based — the first path segment after
/admin/maps to a feature ID.
Class Reference — AdminManager
Class for managing Magrathea's Admin
Adds a css file to admin
| Param | Type | Default |
|---|---|---|
$file |
mixed | required |
Adds a javascript file to admin
| Param | Type | Default |
|---|---|---|
$file |
mixed | required |
Permission verification
display error page
| Param | Type | Default |
|---|---|---|
$message |
mixed | required |
returns active feature (the one from "magrathea_feature" data)
returns the current admin.
Gets CSS compressor
Gets color
Gets Js code
gets admin feature id and returns its object
| Param | Type | Default |
|---|---|---|
$featureId |
mixed | required |
Gets Js compressor
Gets Js code
Return logged user
Gets the AdminMenu
Gets title
Log an action
| Param | Type | Default |
|---|---|---|
$action |
mixed | required |
$victim |
mixed | null |
$data |
mixed | null |
$user_id |
mixed | false |
Show permission denied page
Prints the logo
| Param | Type | Default |
|---|---|---|
$logoSize |
?int | 200 |
Sets the menu
| Param | Type | Default |
|---|---|---|
$m |
Magrathea2\Admin\AdminMenu | required |
Starts Admin
| Param | Type | Default |
|---|---|---|
$admin |
Magrathea2\Admin\Admin | required |
Starts a default admin
| Param | Type | Default |
|---|---|---|
$title |
?string | null |
$color |
?string | null |