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