2017-11-16 19:05:41 +01:00
|
|
|
<?php
|
|
|
|
/**
|
2017-11-19 22:50:49 +01:00
|
|
|
* @file src/BaseObject.php
|
2017-11-16 19:05:41 +01:00
|
|
|
*/
|
2017-11-19 22:50:49 +01:00
|
|
|
namespace Friendica;
|
2017-11-16 19:05:41 +01:00
|
|
|
|
2019-03-10 05:21:19 +01:00
|
|
|
require_once __DIR__ . '/../boot.php';
|
2018-12-30 21:42:56 +01:00
|
|
|
|
2019-07-21 20:24:16 +02:00
|
|
|
use Dice\Dice;
|
2019-02-05 21:54:55 +01:00
|
|
|
use Friendica\Network\HTTPException\InternalServerErrorException;
|
2018-12-30 21:42:56 +01:00
|
|
|
|
2017-11-16 19:05:41 +01:00
|
|
|
/**
|
|
|
|
* Basic object
|
|
|
|
*
|
|
|
|
* Contains what is useful to any object
|
2019-07-21 20:24:16 +02:00
|
|
|
*
|
|
|
|
* Act's like a global registry for classes
|
2017-11-16 19:05:41 +01:00
|
|
|
*/
|
|
|
|
class BaseObject
|
|
|
|
{
|
2019-03-26 22:04:31 +01:00
|
|
|
/**
|
2019-07-21 20:24:16 +02:00
|
|
|
* @var Dice The Dependency Injection library
|
2019-03-26 22:04:31 +01:00
|
|
|
*/
|
2019-07-21 20:24:16 +02:00
|
|
|
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;
|
|
|
|
}
|
2017-11-16 19:05:41 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the app
|
|
|
|
*
|
|
|
|
* Same as get_app from boot.php
|
2017-11-19 20:15:25 +01:00
|
|
|
*
|
2017-12-17 17:34:43 +01:00
|
|
|
* @return App
|
2017-11-16 19:05:41 +01:00
|
|
|
*/
|
2017-11-19 22:50:49 +01:00
|
|
|
public static function getApp()
|
2017-11-16 19:05:41 +01:00
|
|
|
{
|
2019-07-26 15:54:14 +02:00
|
|
|
return self::getClass(App::class);
|
2017-11-16 19:05:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-07-21 20:24:16 +02:00
|
|
|
* Returns the initialized class based on it's name
|
2017-11-16 19:05:41 +01:00
|
|
|
*
|
2019-07-21 20:24:16 +02:00
|
|
|
* @param string $name The name of the class
|
2017-11-19 20:15:25 +01:00
|
|
|
*
|
2019-07-21 20:24:16 +02:00
|
|
|
* @return object The initialized name
|
|
|
|
*
|
|
|
|
* @throws InternalServerErrorException
|
2017-11-16 19:05:41 +01:00
|
|
|
*/
|
2019-10-23 00:14:47 +02:00
|
|
|
public static function getClass(string $name)
|
2017-11-16 19:05:41 +01:00
|
|
|
{
|
2019-07-26 15:54:14 +02:00
|
|
|
if (empty(self::$dice)) {
|
|
|
|
throw new InternalServerErrorException('DICE isn\'t initialized.');
|
|
|
|
}
|
|
|
|
|
2019-10-16 14:58:09 +02:00
|
|
|
if (class_exists($name) || interface_exists($name)) {
|
2019-07-21 20:24:16 +02:00
|
|
|
return self::$dice->create($name);
|
|
|
|
} else {
|
|
|
|
throw new InternalServerErrorException('Class \'' . $name . '\' isn\'t valid.');
|
|
|
|
}
|
2017-11-16 19:05:41 +01:00
|
|
|
}
|
|
|
|
}
|