From 1e0e1674f2aeef54c52790946cfd61a199cb2042 Mon Sep 17 00:00:00 2001 From: Philipp Holzer Date: Sun, 3 Feb 2019 22:46:50 +0100 Subject: [PATCH] 4) Adding Factories to other entrypoints --- bin/auth_ejabberd.php | 11 +++++++--- bin/console.php | 11 +++++++--- bin/daemon.php | 10 ++++++--- bin/worker.php | 15 +++++++++----- mod/admin.php | 4 ++-- src/App.php | 2 +- src/BaseObject.php | 11 +++++++--- src/Core/Console/AutomaticInstallation.php | 2 +- src/Core/Console/DatabaseStructure.php | 8 +++++--- src/Core/Console/PostUpdate.php | 4 ++-- src/Core/Installer.php | 6 ++++-- src/Core/Update.php | 18 ++++++++-------- src/Database/DBA.php | 6 +++--- src/Database/DBStructure.php | 24 ++++++++++++---------- src/Factory/ConfigFactory.php | 7 +++++++ src/Model/Attach.php | 6 +++--- src/Module/Install.php | 2 +- src/Worker/DBUpdate.php | 5 +++-- tests/DatabaseTest.php | 4 +++- tests/include/ApiTest.php | 2 +- 20 files changed, 100 insertions(+), 58 deletions(-) diff --git a/bin/auth_ejabberd.php b/bin/auth_ejabberd.php index a6f30d201..af728f218 100755 --- a/bin/auth_ejabberd.php +++ b/bin/auth_ejabberd.php @@ -33,8 +33,10 @@ */ use Friendica\App; +use Friendica\Core\Config; +use Friendica\Factory; +use Friendica\Util\BasePath; use Friendica\Util\ExAuth; -use Friendica\Util\LoggerFactory; if (sizeof($_SERVER["argv"]) == 0) { die(); @@ -52,9 +54,12 @@ chdir($directory); require dirname(__DIR__) . '/vendor/autoload.php'; -$logger = LoggerFactory::create('auth_ejabberd'); +$basedir = BasePath::create(dirname(__DIR__)); +$configLoader = new Config\ConfigCacheLoader($basedir); +$config = Factory\ConfigFactory::createCache($configLoader); +$logger = Factory\LoggerFactory::create('auth_ejabberd', $config); -$a = new App(dirname(__DIR__), $logger); +$a = new App($config, $logger); if ($a->getMode()->isNormal()) { $oAuth = new ExAuth(); diff --git a/bin/console.php b/bin/console.php index 9264e3eee..e7b1786de 100755 --- a/bin/console.php +++ b/bin/console.php @@ -3,11 +3,16 @@ require dirname(__DIR__) . '/vendor/autoload.php'; -use Friendica\Util\LoggerFactory; +use Friendica\Core\Config; +use Friendica\Factory; +use Friendica\Util\BasePath; -$logger = LoggerFactory::create('console'); +$basedir = BasePath::create(dirname(__DIR__)); +$configLoader = new Config\ConfigCacheLoader($basedir); +$config = Factory\ConfigFactory::createCache($configLoader); +$logger = Factory\LoggerFactory::create('console', $config); -$a = new Friendica\App(dirname(__DIR__), $logger); +$a = new Friendica\App($config, $logger); \Friendica\BaseObject::setApp($a); (new Friendica\Core\Console($argv))->execute(); diff --git a/bin/daemon.php b/bin/daemon.php index c7b321c11..7e71571cb 100755 --- a/bin/daemon.php +++ b/bin/daemon.php @@ -11,7 +11,8 @@ use Friendica\App; use Friendica\Core\Config; use Friendica\Core\Worker; use Friendica\Database\DBA; -use Friendica\Util\LoggerFactory; +use Friendica\Factory; +use Friendica\Util\BasePath; // Get options $shortopts = 'f'; @@ -32,9 +33,12 @@ if (!file_exists("boot.php") && (sizeof($_SERVER["argv"]) != 0)) { require dirname(__DIR__) . '/vendor/autoload.php'; -$logger = LoggerFactory::create('daemon'); +$basedir = BasePath::create(dirname(__DIR__)); +$configLoader = new Config\ConfigCacheLoader($basedir); +$config = Factory\ConfigFactory::createCache($configLoader); +$logger = Factory\LoggerFactory::create('daemon', $config); -$a = new App(dirname(__DIR__), $logger); +$a = new App($config, $logger); if ($a->getMode()->isInstall()) { die("Friendica isn't properly installed yet.\n"); diff --git a/bin/worker.php b/bin/worker.php index a64b6a833..61fdbed54 100755 --- a/bin/worker.php +++ b/bin/worker.php @@ -4,11 +4,13 @@ * @file bin/worker.php * @brief Starts the background processing */ + use Friendica\App; use Friendica\Core\Config; -use Friendica\Core\Worker; use Friendica\Core\Update; -use Friendica\Util\LoggerFactory; +use Friendica\Core\Worker; +use Friendica\Factory; +use Friendica\Util\BasePath; // Get options $shortopts = 'sn'; @@ -29,12 +31,15 @@ if (!file_exists("boot.php") && (sizeof($_SERVER["argv"]) != 0)) { require dirname(__DIR__) . '/vendor/autoload.php'; -$logger = LoggerFactory::create('worker'); +$basedir = BasePath::create(dirname(__DIR__)); +$configLoader = new Config\ConfigCacheLoader($basedir); +$config = Factory\ConfigFactory::createCache($configLoader); +$logger = Factory\LoggerFactory::create('worker', $config); -$a = new App(dirname(__DIR__), $logger); +$a = new App($config, $logger); // Check the database structure and possibly fixes it -Update::check(true); +Update::check($a->getBasePath(), true); // Quit when in maintenance if (!$a->getMode()->has(App\Mode::MAINTENANCEDISABLED)) { diff --git a/mod/admin.php b/mod/admin.php index 8c4787701..51af86689 100644 --- a/mod/admin.php +++ b/mod/admin.php @@ -916,7 +916,7 @@ function admin_page_summary(App $a) } if (Config::get('system', 'dbupdate', DBStructure::UPDATE_NOT_CHECKED) == DBStructure::UPDATE_NOT_CHECKED) { - DBStructure::update(false, true); + DBStructure::update($a->getBasePath(), false, true); } if (Config::get('system', 'dbupdate') == DBStructure::UPDATE_FAILED) { $showwarning = true; @@ -1725,7 +1725,7 @@ function admin_page_dbsync(App $a) } if (($a->argc > 2) && (intval($a->argv[2]) || ($a->argv[2] === 'check'))) { - $retval = DBStructure::update(false, true); + $retval = DBStructure::update($a->getBasePath(), false, true); if ($retval === '') { $o .= L10n::t("Database structure update %s was successfully applied.", DB_UPDATE_VERSION) . "
"; Config::set('database', 'last_successful_update', DB_UPDATE_VERSION); diff --git a/src/App.php b/src/App.php index caf202634..79e842870 100644 --- a/src/App.php +++ b/src/App.php @@ -1288,7 +1288,7 @@ class App $this->module = 'maintenance'; } else { $this->checkURL(); - Core\Update::check(false); + Core\Update::check($this->basePath, false); Core\Addon::loadAddons(); Core\Hook::loadHooks(); } diff --git a/src/BaseObject.php b/src/BaseObject.php index 84ff19382..4deba1ced 100644 --- a/src/BaseObject.php +++ b/src/BaseObject.php @@ -6,7 +6,9 @@ namespace Friendica; require_once 'boot.php'; -use Friendica\Util\LoggerFactory; +use Friendica\Core\Config; +use Friendica\Factory; +use Friendica\Util\BasePath; /** * Basic object @@ -28,8 +30,11 @@ class BaseObject public static function getApp() { if (empty(self::$app)) { - $logger = $logger = LoggerFactory::create('app'); - self::$app = new App(dirname(__DIR__), $logger); + $basedir = BasePath::create(dirname(__DIR__)); + $configLoader = new Config\ConfigCacheLoader($basedir); + $config = Factory\ConfigFactory::createCache($configLoader); + $logger = Factory\LoggerFactory::create('app', $config); + self::$app = new App($config, $logger); } return self::$app; diff --git a/src/Core/Console/AutomaticInstallation.php b/src/Core/Console/AutomaticInstallation.php index 9a63f56ed..8c3e5ebbe 100644 --- a/src/Core/Console/AutomaticInstallation.php +++ b/src/Core/Console/AutomaticInstallation.php @@ -158,7 +158,7 @@ HELP; $installer->resetChecks(); - if (!$installer->installDatabase()) { + if (!$installer->installDatabase($a->getBasePath())) { $errorMessage = $this->extractErrors($installer->getChecks()); throw new RuntimeException($errorMessage); } diff --git a/src/Core/Console/DatabaseStructure.php b/src/Core/Console/DatabaseStructure.php index 724feea5e..1ec108d2e 100644 --- a/src/Core/Console/DatabaseStructure.php +++ b/src/Core/Console/DatabaseStructure.php @@ -61,17 +61,19 @@ HELP; Core\Config::load(); + $a = get_app(); + switch ($this->getArgument(0)) { case "dryrun": - $output = DBStructure::update(true, false); + $output = DBStructure::update($a->getBasePath(), true, false); break; case "update": $force = $this->getOption(['f', 'force'], false); - $output = Update::run($force, true, false); + $output = Update::run($a->getBasePath(), $force, true, false); break; case "dumpsql": ob_start(); - DBStructure::printStructure(); + DBStructure::printStructure($a->getBasePath()); $output = ob_get_clean(); break; case "toinnodb": diff --git a/src/Core/Console/PostUpdate.php b/src/Core/Console/PostUpdate.php index 17ed231f9..103d0fef7 100644 --- a/src/Core/Console/PostUpdate.php +++ b/src/Core/Console/PostUpdate.php @@ -2,8 +2,8 @@ namespace Friendica\Core\Console; -use Friendica\Core\L10n; use Friendica\Core\Config; +use Friendica\Core\L10n; use Friendica\Core\Update; /** @@ -56,7 +56,7 @@ HELP; } echo L10n::t('Check for pending update actions.') . "\n"; - Update::run(true, true, false); + Update::run($a->getBasePath(), true, true, false); echo L10n::t('Done.') . "\n"; echo L10n::t('Execute pending post updates.') . "\n"; diff --git a/src/Core/Installer.php b/src/Core/Installer.php index a487aec78..7e79f29f1 100644 --- a/src/Core/Installer.php +++ b/src/Core/Installer.php @@ -168,12 +168,14 @@ class Installer /*** * Installs the DB-Scheme for Friendica * + * @param string $basePath The base path of this application + * * @return bool true if the installation was successful, otherwise false * @throws Exception */ - public function installDatabase() + public function installDatabase($basePath) { - $result = DBStructure::update(false, true, true); + $result = DBStructure::update($basePath, false, true, true); if ($result) { $txt = L10n::t('You may need to import the file "database.sql" manually using phpmyadmin or mysql.') . EOL; diff --git a/src/Core/Update.php b/src/Core/Update.php index 368f5f55c..5df0675cd 100644 --- a/src/Core/Update.php +++ b/src/Core/Update.php @@ -14,10 +14,11 @@ class Update /** * @brief Function to check if the Database structure needs an update. * + * @param string $basePath The base path of this application * @param boolean $via_worker boolean Is the check run via the worker? * @throws \Friendica\Network\HTTPException\InternalServerErrorException */ - public static function check($via_worker) + public static function check($basePath, $via_worker) { if (!DBA::connected()) { return; @@ -38,7 +39,7 @@ class Update if ($build < DB_UPDATE_VERSION) { // When we cannot execute the database update via the worker, we will do it directly if (!Worker::add(PRIORITY_CRITICAL, 'DBUpdate') && $via_worker) { - self::run(); + self::run($basePath); } } } @@ -46,14 +47,15 @@ class Update /** * Automatic database updates * - * @param bool $force Force the Update-Check even if the lock is set - * @param bool $verbose Run the Update-Check verbose - * @param bool $sendMail Sends a Mail to the administrator in case of success/failure + * @param string $basePath The base path of this application + * @param bool $force Force the Update-Check even if the lock is set + * @param bool $verbose Run the Update-Check verbose + * @param bool $sendMail Sends a Mail to the administrator in case of success/failure * * @return string Empty string if the update is successful, error messages otherwise * @throws \Friendica\Network\HTTPException\InternalServerErrorException */ - public static function run($force = false, $verbose = false, $sendMail = true) + public static function run($basePath, $force = false, $verbose = false, $sendMail = true) { // In force mode, we release the dbupdate lock first // Necessary in case of an stuck update @@ -91,7 +93,7 @@ class Update } // update the structure in one call - $retval = DBStructure::update($verbose, true); + $retval = DBStructure::update($basePath, $verbose, true); if ($retval) { if ($sendMail) { self::updateFailed( @@ -125,7 +127,7 @@ class Update } } } elseif ($force) { - DBStructure::update($verbose, true); + DBStructure::update($basePath, $verbose, true); } return ''; diff --git a/src/Database/DBA.php b/src/Database/DBA.php index 62789589c..dc3c62abe 100644 --- a/src/Database/DBA.php +++ b/src/Database/DBA.php @@ -2,7 +2,7 @@ namespace Friendica\Database; -use Friendica\Core\Config\ConfigCache; +use Friendica\Core\Config\IConfigCache; use Friendica\Core\Logger; use Friendica\Core\System; use Friendica\Util\DateTimeFormat; @@ -32,7 +32,7 @@ class DBA public static $connected = false; /** - * @var ConfigCache + * @var IConfigCache */ private static $config; private static $server_info = ''; @@ -1031,7 +1031,7 @@ class DBA * This process must only be started once, since the value is cached. */ private static function buildRelationData() { - $definition = DBStructure::definition(); + $definition = DBStructure::definition(self::$config->get('system', 'basepath')); foreach ($definition AS $table => $structure) { foreach ($structure['fields'] AS $field => $field_struct) { diff --git a/src/Database/DBStructure.php b/src/Database/DBStructure.php index 2724670e7..75a5c8624 100644 --- a/src/Database/DBStructure.php +++ b/src/Database/DBStructure.php @@ -74,9 +74,9 @@ class DBStructure return L10n::t('Errors encountered performing database changes: ') . $message . EOL; } - public static function printStructure() + public static function printStructure($basePath) { - $database = self::definition(false); + $database = self::definition($basePath, false); echo "-- ------------------------------------------\n"; echo "-- " . FRIENDICA_PLATFORM . " " . FRIENDICA_VERSION . " (" . FRIENDICA_CODENAME, ")\n"; @@ -98,14 +98,15 @@ class DBStructure * * @see config/dbstructure.config.php * @param boolean $with_addons_structure Whether to tack on addons additional tables + * @param string $basePath The base path of this application * @return array * @throws Exception */ - public static function definition($basepath, $with_addons_structure = true) + public static function definition($basePath, $with_addons_structure = true) { if (!self::$definition) { - $filename = $basepath . '/config/dbstructure.config.php'; + $filename = $basePath . '/config/dbstructure.config.php'; if (!is_readable($filename)) { throw new Exception('Missing database structure config file config/dbstructure.config.php'); @@ -246,15 +247,16 @@ class DBStructure /** * Updates DB structure and returns eventual errors messages * - * @param bool $verbose - * @param bool $action Whether to actually apply the update - * @param bool $install Is this the initial update during the installation? - * @param array $tables An array of the database tables - * @param array $definition An array of the definition tables + * @param string $basePath The base path of this application + * @param bool $verbose + * @param bool $action Whether to actually apply the update + * @param bool $install Is this the initial update during the installation? + * @param array $tables An array of the database tables + * @param array $definition An array of the definition tables * @return string Empty string if the update is successful, error messages otherwise * @throws Exception */ - public static function update($verbose, $action, $install = false, array $tables = null, array $definition = null) + public static function update($basePath, $verbose, $action, $install = false, array $tables = null, array $definition = null) { if ($action && !$install) { Config::set('system', 'maintenance', 1); @@ -283,7 +285,7 @@ class DBStructure // Get the definition if (is_null($definition)) { - $definition = self::definition(); + $definition = self::definition($basePath); } // MySQL >= 5.7.4 doesn't support the IGNORE keyword in ALTER TABLE statements diff --git a/src/Factory/ConfigFactory.php b/src/Factory/ConfigFactory.php index 1b0b66eec..4ab20d1d5 100644 --- a/src/Factory/ConfigFactory.php +++ b/src/Factory/ConfigFactory.php @@ -6,6 +6,11 @@ use Friendica\Core\Config; class ConfigFactory { + /** + * @param Config\ConfigCacheLoader $loader The Config Cache loader (INI/config/.htconfig) + * + * @return Config\ConfigCache + */ public static function createCache(Config\ConfigCacheLoader $loader) { $configCache = new Config\ConfigCache(); @@ -17,6 +22,7 @@ class ConfigFactory /** * @param string $type The adapter type * @param Config\IConfigCache $config The config cache of this adapter + * * @return Config\IConfigAdapter */ public static function createConfig($type, $config) @@ -32,6 +38,7 @@ class ConfigFactory * @param string $type The adapter type * @param int $uid The UID of the current user * @param Config\IPConfigCache $config The config cache of this adapter + * * @return Config\IPConfigAdapter */ public static function createPConfig($type, $uid, $config) diff --git a/src/Model/Attach.php b/src/Model/Attach.php index 7efb56f95..d65e67fe3 100644 --- a/src/Model/Attach.php +++ b/src/Model/Attach.php @@ -7,15 +7,15 @@ namespace Friendica\Model; use Friendica\BaseObject; -use Friendica\Core\System; use Friendica\Core\StorageManager; +use Friendica\Core\System; use Friendica\Database\DBA; use Friendica\Database\DBStructure; use Friendica\Model\Storage\IStorage; use Friendica\Object\Image; -use Friendica\Util\Security; use Friendica\Util\DateTimeFormat; use Friendica\Util\Mimetype; +use Friendica\Util\Security; /** * Class to handle attach dabatase table @@ -31,7 +31,7 @@ class Attach extends BaseObject */ private static function getFields() { - $allfields = DBStructure::definition(false); + $allfields = DBStructure::definition(self::getApp()->getBasePath(), false); $fields = array_keys($allfields['attach']['fields']); array_splice($fields, array_search('data', $fields), 1); return $fields; diff --git a/src/Module/Install.php b/src/Module/Install.php index d1a35c151..327e59422 100644 --- a/src/Module/Install.php +++ b/src/Module/Install.php @@ -103,7 +103,7 @@ class Install extends BaseModule return; } - self::$installer->installDatabase(); + self::$installer->installDatabase($a->getBasePath()); break; } diff --git a/src/Worker/DBUpdate.php b/src/Worker/DBUpdate.php index 48c7e7ce6..05bace146 100644 --- a/src/Worker/DBUpdate.php +++ b/src/Worker/DBUpdate.php @@ -5,12 +5,13 @@ */ namespace Friendica\Worker; +use Friendica\BaseObject; use Friendica\Core\Update; -class DBUpdate +class DBUpdate extends BaseObject { public static function execute() { - Update::run(); + Update::run(self::getApp()->getBasePath()); } } diff --git a/tests/DatabaseTest.php b/tests/DatabaseTest.php index 2cb76dcad..69ee750db 100644 --- a/tests/DatabaseTest.php +++ b/tests/DatabaseTest.php @@ -36,7 +36,9 @@ abstract class DatabaseTest extends MockedTest $this->markTestSkipped('Please set the MYSQL_* environment variables to your test database credentials.'); } - DBA::connect(getenv('MYSQL_HOST'), + DBA::connect( + __DIR__, + getenv('MYSQL_HOST'), getenv('MYSQL_USERNAME'), getenv('MYSQL_PASSWORD'), getenv('MYSQL_DATABASE')); diff --git a/tests/include/ApiTest.php b/tests/include/ApiTest.php index cf7571073..9cf3e2ea5 100644 --- a/tests/include/ApiTest.php +++ b/tests/include/ApiTest.php @@ -10,8 +10,8 @@ use Friendica\Core\Config; use Friendica\Core\PConfig; use Friendica\Core\Protocol; use Friendica\Core\System; +use Friendica\Factory\LoggerFactory; use Friendica\Network\HTTPException; -use Friendica\Util\LoggerFactory; use Monolog\Handler\TestHandler; require_once __DIR__ . '/../../include/api.php';