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