Adds minimal app services API for plugins

main
Yasen Pramatarov 2026-01-13 11:53:31 +02:00
parent f4238f205f
commit 1228abe6f2
1 changed files with 96 additions and 0 deletions

96
app/core/App.php 100644
View File

@ -0,0 +1,96 @@
<?php
namespace App;
/**
* Minimal application API
* we use it expose and access core services from plugins (and legacy code).
*/
final class App
{
/** @var array<string, mixed> */
private static array $services = [];
/**
* Register or override a service value.
*/
public static function set(string $key, $value): void
{
self::$services[$key] = $value;
}
/**
* Determine whether a value is registered.
*/
public static function has(string $key): bool
{
if (array_key_exists($key, self::$services)) {
return true;
}
return self::fallback($key) !== null;
}
/**
* Retrieve a registered value.
* Falls back to legacy globals when no explicit service was registered.
*/
public static function get(string $key, $default = null)
{
if (array_key_exists($key, self::$services)) {
return self::$services[$key];
}
$fallback = self::fallback($key);
return $fallback !== null ? $fallback : $default;
}
/**
* Convenience accessor for the database connection.
*/
public static function db()
{
return self::get('db');
}
/**
* Convenience accessor for the configuration array.
*/
public static function config(): array
{
$config = self::get('config', []);
return is_array($config) ? $config : [];
}
/**
* Convenience accessor for the authenticated user object, if any.
*/
public static function user()
{
return self::get('user');
}
/**
* Basic fallback bridge for legacy globals.
*/
private static function fallback(string $key)
{
switch ($key) {
case 'config':
return $GLOBALS['config'] ?? null;
case 'config_path':
return $GLOBALS['config_file'] ?? null;
case 'db':
return $GLOBALS['db'] ?? null;
case 'user':
case 'user_object':
return $GLOBALS['userObject'] ?? null;
case 'user_id':
return $GLOBALS['userId'] ?? null;
case 'logger':
return $GLOBALS['logObject'] ?? null;
case 'app_root':
return $GLOBALS['app_root'] ?? null;
default:
return null;
}
}
}