]> git.mxchange.org Git - friendica.git/blob - src/BaseObject.php
Merge pull request #7957 from annando/issue-7953
[friendica.git] / src / BaseObject.php
1 <?php
2 /**
3  * @file src/BaseObject.php
4  */
5 namespace Friendica;
6
7 require_once __DIR__ . '/../boot.php';
8
9 use Dice\Dice;
10 use Friendica\Network\HTTPException\InternalServerErrorException;
11
12 /**
13  * Basic object
14  *
15  * Contains what is useful to any object
16  *
17  * Act's like a global registry for classes
18  */
19 class BaseObject
20 {
21         /**
22          * @var Dice The Dependency Injection library
23          */
24         private static $dice;
25
26         /**
27          * Set's the dependency injection library for a global usage
28          *
29          * @param Dice $dice The dependency injection library
30          */
31         public static function setDependencyInjection(Dice $dice)
32         {
33                 self::$dice = $dice;
34         }
35
36         /**
37          * Get the app
38          *
39          * Same as get_app from boot.php
40          *
41          * @return App
42          */
43         public static function getApp()
44         {
45                 return self::getClass(App::class);
46         }
47
48         /**
49          * Returns the initialized class based on it's name
50          *
51          * @param string $name The name of the class
52          *
53          * @return object The initialized name
54          *
55          * @throws InternalServerErrorException
56          */
57         public static function getClass(string $name)
58         {
59                 if (empty(self::$dice)) {
60                         throw new InternalServerErrorException('DICE isn\'t initialized.');
61                 }
62
63                 if (class_exists($name) || interface_exists($name)) {
64                         return self::$dice->create($name);
65                 } else {
66                         throw new InternalServerErrorException('Class \'' . $name . '\' isn\'t valid.');
67                 }
68         }
69 }