]> git.mxchange.org Git - friendica.git/blobdiff - src/BaseObject.php
Group selection: Respect "pubmail" and ignore atchived or blocked contacts
[friendica.git] / src / BaseObject.php
index 01957164c10bd664fbd536b2f6c0680b5af8e9c9..20481884517e565123d8b17f7038a6033250525a 100644 (file)
@@ -4,44 +4,66 @@
  */
 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
         *
         * Same as get_app from boot.php
         *
-        * @return object
+        * @return App
         */
        public static function getApp()
        {
-               if (self::$app) {
-                       return self::$app;
-               }
-
-               self::$app = get_app();
-
-               return self::$app;
+               return self::getClass(App::class);
        }
 
        /**
-        * Set the app
+        * Returns the initialized class based on it's name
         *
-        * @param object $app App
+        * @param string $name The name of the class
         *
-        * @return void
+        * @return object The initialized name
+        *
+        * @throws InternalServerErrorException
         */
-       public static function setApp($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.');
+               }
        }
 }