X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2FBaseObject.php;h=20481884517e565123d8b17f7038a6033250525a;hb=d7c832748289b24741e34bc44e4906fc71a45d84;hp=33ed67754674b34731bef4eab345df47be755468;hpb=7efde8e334d6ca52fd1608fb9a78babcea4bdc9f;p=friendica.git diff --git a/src/BaseObject.php b/src/BaseObject.php index 33ed677546..2048188451 100644 --- a/src/BaseObject.php +++ b/src/BaseObject.php @@ -4,14 +4,34 @@ */ namespace Friendica; +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 @@ -22,22 +42,28 @@ class BaseObject */ public static function getApp() { - if (empty(self::$app)) { - self::$app = new App(dirname(__DIR__)); - } - - return self::$app; + return self::getClass(App::class); } /** - * Set the app + * Returns the initialized class based on it's name * - * @param App $app App + * @param string $name The name of the class * - * @return void + * @return object The initialized name + * + * @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.'); + } } }