X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2FBaseObject.php;h=20481884517e565123d8b17f7038a6033250525a;hb=fd706cf9a7a0c4700838a1f00b12d8fd37323b7c;hp=4a6fa12d24b03ad18f4542114f1302d8a9e3573e;hpb=3aa77685fcccff9739384136b4894de43200fa8c;p=friendica.git diff --git a/src/BaseObject.php b/src/BaseObject.php index 4a6fa12d24..2048188451 100644 --- a/src/BaseObject.php +++ b/src/BaseObject.php @@ -4,18 +4,34 @@ */ namespace Friendica; -require_once 'boot.php'; +require_once __DIR__ . '/../boot.php'; +use Dice\Dice; use Friendica\Network\HTTPException\InternalServerErrorException; /** * Basic object * * Contains what is useful to any object + * + * Act's like a global registry for classes */ class BaseObject { - private static $app = null; + /** + * @var Dice The Dependency Injection library + */ + private static $dice; + + /** + * Set's the dependency injection library for a global usage + * + * @param Dice $dice The dependency injection library + */ + public static function setDependencyInjection(Dice $dice) + { + self::$dice = $dice; + } /** * Get the app @@ -23,26 +39,31 @@ class BaseObject * Same as get_app from boot.php * * @return App - * @throws \Exception */ public static function getApp() { - if (empty(self::$app)) { - throw new InternalServerErrorException('App isn\'t initialized.'); - } - - return self::$app; + return self::getClass(App::class); } /** - * Set the app + * Returns the initialized class based on it's name + * + * @param string $name The name of the class * - * @param App $app App + * @return object The initialized name * - * @return void + * @throws InternalServerErrorException */ - public static function setApp(App $app) + public static function getClass(string $name) { - self::$app = $app; + if (empty(self::$dice)) { + throw new InternalServerErrorException('DICE isn\'t initialized.'); + } + + if (class_exists($name) || interface_exists($name)) { + return self::$dice->create($name); + } else { + throw new InternalServerErrorException('Class \'' . $name . '\' isn\'t valid.'); + } } }