Introduce DICE
- Adding dice library - Adding dependency config - Removing Factories - Refactoring App\Mode constructor - Refactoring App\Router constructor - Refactoring BasePath for DI usage - Refactoring ConfigFileLoader constructor - Refactoring Profiler constructor - Adjust entrypoints (index, console, worker, ..) - Adding functional test for DI - Fix tests because of refactorings
This commit is contained in:
parent
5887b9c499
commit
55999730e0
28 changed files with 563 additions and 308 deletions
|
@ -5,9 +5,9 @@ namespace Friendica\Factory;
|
|||
use Friendica\Core;
|
||||
use Friendica\Core\Config;
|
||||
use Friendica\Core\Config\Cache;
|
||||
use Friendica\Util\ConfigFileLoader;
|
||||
use Friendica\Model\Config\Config as ConfigModel;
|
||||
use Friendica\Model\Config\PConfig as PConfigModel;
|
||||
use Friendica\Util\ConfigFileLoader;
|
||||
|
||||
class ConfigFactory
|
||||
{
|
||||
|
@ -16,7 +16,7 @@ class ConfigFactory
|
|||
*
|
||||
* @return Cache\ConfigCache
|
||||
*/
|
||||
public static function createCache(ConfigFileLoader $loader)
|
||||
public function createCache(ConfigFileLoader $loader)
|
||||
{
|
||||
$configCache = new Cache\ConfigCache();
|
||||
$loader->setupCache($configCache);
|
||||
|
@ -26,11 +26,11 @@ class ConfigFactory
|
|||
|
||||
/**
|
||||
* @param Cache\ConfigCache $configCache The config cache of this adapter
|
||||
* @param ConfigModel $configModel The configuration model
|
||||
* @param ConfigModel $configModel The configuration model
|
||||
*
|
||||
* @return Config\Configuration
|
||||
*/
|
||||
public static function createConfig(Cache\ConfigCache $configCache, ConfigModel $configModel)
|
||||
public function createConfig(Cache\ConfigCache $configCache, ConfigModel $configModel)
|
||||
{
|
||||
if ($configCache->get('system', 'config_adapter') === 'preload') {
|
||||
$configuration = new Config\PreloadConfiguration($configCache, $configModel);
|
||||
|
@ -46,13 +46,13 @@ class ConfigFactory
|
|||
}
|
||||
|
||||
/**
|
||||
* @param Cache\ConfigCache $configCache The config cache
|
||||
* @param Cache\PConfigCache $pConfigCache The personal config cache
|
||||
* @param PConfigModel $configModel The configuration model
|
||||
* @param Cache\ConfigCache $configCache The config cache
|
||||
* @param Cache\PConfigCache $pConfigCache The personal config cache
|
||||
* @param PConfigModel $configModel The configuration model
|
||||
*
|
||||
* @return Config\PConfiguration
|
||||
*/
|
||||
public static function createPConfig(Cache\ConfigCache $configCache, Cache\PConfigCache $pConfigCache, PConfigModel $configModel)
|
||||
public function createPConfig(Cache\ConfigCache $configCache, Cache\PConfigCache $pConfigCache, PConfigModel $configModel)
|
||||
{
|
||||
if ($configCache->get('system', 'config_adapter') === 'preload') {
|
||||
$configuration = new Config\PreloadPConfiguration($pConfigCache, $configModel);
|
||||
|
|
|
@ -1,61 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Friendica\Factory;
|
||||
|
||||
use Friendica\Core\Config\Cache;
|
||||
use Friendica\Database;
|
||||
use Friendica\Util\Logger\VoidLogger;
|
||||
use Friendica\Util\Profiler;
|
||||
use ParagonIE\HiddenString\HiddenString;
|
||||
|
||||
class DBFactory
|
||||
{
|
||||
/**
|
||||
* Initialize the DBA connection
|
||||
*
|
||||
* @param Cache\ConfigCache $configCache The configuration cache
|
||||
* @param Profiler $profiler The profiler
|
||||
* @param array $server The $_SERVER variables
|
||||
*
|
||||
* @return Database\Database
|
||||
* @throws \Exception if connection went bad
|
||||
*/
|
||||
public static function init(Cache\ConfigCache $configCache, Profiler $profiler, array $server)
|
||||
{
|
||||
$db_host = $configCache->get('database', 'hostname');
|
||||
$db_user = $configCache->get('database', 'username');
|
||||
$db_pass = $configCache->get('database', 'password');
|
||||
$db_data = $configCache->get('database', 'database');
|
||||
$charset = $configCache->get('database', 'charset');
|
||||
|
||||
// Use environment variables for mysql if they are set beforehand
|
||||
if (!empty($server['MYSQL_HOST'])
|
||||
&& !empty($server['MYSQL_USERNAME'] || !empty($server['MYSQL_USER']))
|
||||
&& $server['MYSQL_PASSWORD'] !== false
|
||||
&& !empty($server['MYSQL_DATABASE']))
|
||||
{
|
||||
$db_host = $server['MYSQL_HOST'];
|
||||
if (!empty($server['MYSQL_PORT'])) {
|
||||
$db_host .= ':' . $server['MYSQL_PORT'];
|
||||
}
|
||||
if (!empty($server['MYSQL_USERNAME'])) {
|
||||
$db_user = $server['MYSQL_USERNAME'];
|
||||
} else {
|
||||
$db_user = $server['MYSQL_USER'];
|
||||
}
|
||||
$db_pass = new HiddenString((string) $server['MYSQL_PASSWORD']);
|
||||
$db_data = $server['MYSQL_DATABASE'];
|
||||
}
|
||||
|
||||
$database = new Database\Database($configCache, $profiler, new VoidLogger(), $db_host, $db_user, $db_pass, $db_data, $charset);
|
||||
|
||||
if ($database->connected()) {
|
||||
// Loads DB_UPDATE_VERSION constant
|
||||
Database\DBStructure::definition($configCache->get('system', 'basepath'), false);
|
||||
}
|
||||
|
||||
unset($db_host, $db_user, $db_pass, $db_data, $charset);
|
||||
|
||||
return $database;
|
||||
}
|
||||
}
|
|
@ -2,13 +2,10 @@
|
|||
|
||||
namespace Friendica\Factory;
|
||||
|
||||
use Dice\Dice;
|
||||
use Friendica\App;
|
||||
use Friendica\Core\Config\Cache\PConfigCache;
|
||||
use Friendica\Core\L10n\L10n;
|
||||
use Friendica\Factory;
|
||||
use Friendica\Util\BasePath;
|
||||
use Friendica\Util\BaseURL;
|
||||
use Friendica\Util\ConfigFileLoader;
|
||||
use Friendica\Core\Config\PConfiguration;
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
||||
class DependencyFactory
|
||||
{
|
||||
|
@ -16,34 +13,18 @@ class DependencyFactory
|
|||
* Setting all default-dependencies of a friendica execution
|
||||
*
|
||||
* @param string $channel The channel of this execution
|
||||
* @param string $directory The base directory
|
||||
* @param bool $isBackend True, if it's a backend execution, otherwise false (Default true)
|
||||
*
|
||||
* @return App The application
|
||||
*
|
||||
* @throws \Exception
|
||||
*/
|
||||
public static function setUp($channel, $directory, $isBackend = true)
|
||||
public static function setUp($channel, Dice $dice, $isBackend = true)
|
||||
{
|
||||
$basePath = BasePath::create($directory, $_SERVER);
|
||||
$mode = new App\Mode($basePath);
|
||||
$router = new App\Router();
|
||||
$configLoader = new ConfigFileLoader($basePath, $mode);
|
||||
$configCache = Factory\ConfigFactory::createCache($configLoader);
|
||||
$profiler = Factory\ProfilerFactory::create($configCache);
|
||||
$database = Factory\DBFactory::init($configCache, $profiler, $_SERVER);
|
||||
$configModel = new \Friendica\Model\Config\Config($database);
|
||||
$config = Factory\ConfigFactory::createConfig($configCache, $configModel);
|
||||
// needed to call PConfig::init()
|
||||
$pconfigModel = new \Friendica\Model\Config\PConfig($database);
|
||||
Factory\ConfigFactory::createPConfig($configCache, new PConfigCache(), $pconfigModel);
|
||||
$logger = Factory\LoggerFactory::create($channel, $database, $config, $profiler);
|
||||
Factory\LoggerFactory::createDev($channel, $config, $profiler);
|
||||
$baseURL = new BaseURL($config, $_SERVER);
|
||||
$l10n = new L10n(L10n::detectLanguage($config->get('system', 'language')),
|
||||
$database,
|
||||
$logger);
|
||||
$pConfig = $dice->create(PConfiguration::class);
|
||||
$logger = $dice->create(LoggerInterface::class, [$channel]);
|
||||
$devLogger = $dice->create('$devLogger', [$channel]);
|
||||
|
||||
return new App($database, $config, $mode, $router, $baseURL, $logger, $profiler, $l10n, $isBackend);
|
||||
return $dice->create(App::class, [$isBackend]);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -48,7 +48,7 @@ class LoggerFactory
|
|||
* @throws \Exception
|
||||
* @throws InternalServerErrorException
|
||||
*/
|
||||
public static function create($channel, Database $database, Configuration $config, Profiler $profiler)
|
||||
public function create($channel, Database $database, Configuration $config, Profiler $profiler)
|
||||
{
|
||||
if (empty($config->get('system', 'debugging', false))) {
|
||||
$logger = new VoidLogger();
|
||||
|
|
|
@ -1,26 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Friendica\Factory;
|
||||
|
||||
use Friendica\Core\Config\Cache\ConfigCache;
|
||||
use Friendica\Util\Profiler;
|
||||
|
||||
class ProfilerFactory
|
||||
{
|
||||
/**
|
||||
* Creates a Profiler for the current execution
|
||||
*
|
||||
* @param ConfigCache $configCache The configuration cache
|
||||
*
|
||||
* @return Profiler
|
||||
*/
|
||||
public static function create(ConfigCache $configCache)
|
||||
{
|
||||
$enabled = $configCache->get('system', 'profiler');
|
||||
$enabled = isset($enabled) && $enabled !== '0';
|
||||
$renderTime = $configCache->get('rendertime', 'callstack');
|
||||
$renderTime = isset($renderTime) && $renderTime !== '0';
|
||||
|
||||
return new Profiler($enabled, $renderTime);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue