X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2FBaseObject.php;h=fcec89bb4228b5f3faae23beb535b7b8aee91c79;hb=f010beaa95fafbb0228ef4b569621a64b51afa50;hp=d006c249defde79fc07243794a13d61ade733a8e;hpb=83ead5ec483041c2751c5de0d45cd63a598fb02c;p=friendica.git diff --git a/src/BaseObject.php b/src/BaseObject.php index d006c249de..fcec89bb42 100644 --- a/src/BaseObject.php +++ b/src/BaseObject.php @@ -4,16 +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 @@ -24,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) + protected 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.'); + } } }