Browse Source

4) Adding Factories to other entrypoints

tags/2019.03^2
Philipp Holzer 7 months ago
parent
commit
1e0e1674f2
No account linked to committer's email address

+ 8
- 3
bin/auth_ejabberd.php View File

@@ -33,8 +33,10 @@
33 33
  */
34 34
 
35 35
 use Friendica\App;
36
+use Friendica\Core\Config;
37
+use Friendica\Factory;
38
+use Friendica\Util\BasePath;
36 39
 use Friendica\Util\ExAuth;
37
-use Friendica\Util\LoggerFactory;
38 40
 
39 41
 if (sizeof($_SERVER["argv"]) == 0) {
40 42
 	die();
@@ -52,9 +54,12 @@ chdir($directory);
52 54
 
53 55
 require dirname(__DIR__) . '/vendor/autoload.php';
54 56
 
55
-$logger = LoggerFactory::create('auth_ejabberd');
57
+$basedir = BasePath::create(dirname(__DIR__));
58
+$configLoader = new Config\ConfigCacheLoader($basedir);
59
+$config = Factory\ConfigFactory::createCache($configLoader);
60
+$logger = Factory\LoggerFactory::create('auth_ejabberd', $config);
56 61
 
57
-$a = new App(dirname(__DIR__), $logger);
62
+$a = new App($config, $logger);
58 63
 
59 64
 if ($a->getMode()->isNormal()) {
60 65
 	$oAuth = new ExAuth();

+ 8
- 3
bin/console.php View File

@@ -3,11 +3,16 @@
3 3
 
4 4
 require dirname(__DIR__) . '/vendor/autoload.php';
5 5
 
6
-use Friendica\Util\LoggerFactory;
6
+use Friendica\Core\Config;
7
+use Friendica\Factory;
8
+use Friendica\Util\BasePath;
7 9
 
8
-$logger = LoggerFactory::create('console');
10
+$basedir = BasePath::create(dirname(__DIR__));
11
+$configLoader = new Config\ConfigCacheLoader($basedir);
12
+$config = Factory\ConfigFactory::createCache($configLoader);
13
+$logger = Factory\LoggerFactory::create('console', $config);
9 14
 
10
-$a = new Friendica\App(dirname(__DIR__), $logger);
15
+$a = new Friendica\App($config, $logger);
11 16
 \Friendica\BaseObject::setApp($a);
12 17
 
13 18
 (new Friendica\Core\Console($argv))->execute();

+ 7
- 3
bin/daemon.php View File

@@ -11,7 +11,8 @@ use Friendica\App;
11 11
 use Friendica\Core\Config;
12 12
 use Friendica\Core\Worker;
13 13
 use Friendica\Database\DBA;
14
-use Friendica\Util\LoggerFactory;
14
+use Friendica\Factory;
15
+use Friendica\Util\BasePath;
15 16
 
16 17
 // Get options
17 18
 $shortopts = 'f';
@@ -32,9 +33,12 @@ if (!file_exists("boot.php") && (sizeof($_SERVER["argv"]) != 0)) {
32 33
 
33 34
 require dirname(__DIR__) . '/vendor/autoload.php';
34 35
 
35
-$logger = LoggerFactory::create('daemon');
36
+$basedir = BasePath::create(dirname(__DIR__));
37
+$configLoader = new Config\ConfigCacheLoader($basedir);
38
+$config = Factory\ConfigFactory::createCache($configLoader);
39
+$logger = Factory\LoggerFactory::create('daemon', $config);
36 40
 
37
-$a = new App(dirname(__DIR__), $logger);
41
+$a = new App($config, $logger);
38 42
 
39 43
 if ($a->getMode()->isInstall()) {
40 44
 	die("Friendica isn't properly installed yet.\n");

+ 10
- 5
bin/worker.php View File

@@ -4,11 +4,13 @@
4 4
  * @file bin/worker.php
5 5
  * @brief Starts the background processing
6 6
  */
7
+
7 8
 use Friendica\App;
8 9
 use Friendica\Core\Config;
9
-use Friendica\Core\Worker;
10 10
 use Friendica\Core\Update;
11
-use Friendica\Util\LoggerFactory;
11
+use Friendica\Core\Worker;
12
+use Friendica\Factory;
13
+use Friendica\Util\BasePath;
12 14
 
13 15
 // Get options
14 16
 $shortopts = 'sn';
@@ -29,12 +31,15 @@ if (!file_exists("boot.php") && (sizeof($_SERVER["argv"]) != 0)) {
29 31
 
30 32
 require dirname(__DIR__) . '/vendor/autoload.php';
31 33
 
32
-$logger = LoggerFactory::create('worker');
34
+$basedir = BasePath::create(dirname(__DIR__));
35
+$configLoader = new Config\ConfigCacheLoader($basedir);
36
+$config = Factory\ConfigFactory::createCache($configLoader);
37
+$logger = Factory\LoggerFactory::create('worker', $config);
33 38
 
34
-$a = new App(dirname(__DIR__), $logger);
39
+$a = new App($config, $logger);
35 40
 
36 41
 // Check the database structure and possibly fixes it
37
-Update::check(true);
42
+Update::check($a->getBasePath(), true);
38 43
 
39 44
 // Quit when in maintenance
40 45
 if (!$a->getMode()->has(App\Mode::MAINTENANCEDISABLED)) {

+ 2
- 2
mod/admin.php View File

@@ -916,7 +916,7 @@ function admin_page_summary(App $a)
916 916
 	}
917 917
 
918 918
 	if (Config::get('system', 'dbupdate', DBStructure::UPDATE_NOT_CHECKED) == DBStructure::UPDATE_NOT_CHECKED) {
919
-		DBStructure::update(false, true);
919
+		DBStructure::update($a->getBasePath(), false, true);
920 920
 	}
921 921
 	if (Config::get('system', 'dbupdate') == DBStructure::UPDATE_FAILED) {
922 922
 		$showwarning = true;
@@ -1725,7 +1725,7 @@ function admin_page_dbsync(App $a)
1725 1725
 	}
1726 1726
 
1727 1727
 	if (($a->argc > 2) && (intval($a->argv[2]) || ($a->argv[2] === 'check'))) {
1728
-		$retval = DBStructure::update(false, true);
1728
+		$retval = DBStructure::update($a->getBasePath(), false, true);
1729 1729
 		if ($retval === '') {
1730 1730
 			$o .= L10n::t("Database structure update %s was successfully applied.", DB_UPDATE_VERSION) . "<br />";
1731 1731
 			Config::set('database', 'last_successful_update', DB_UPDATE_VERSION);

+ 1
- 1
src/App.php View File

@@ -1288,7 +1288,7 @@ class App
1288 1288
 			$this->module = 'maintenance';
1289 1289
 		} else {
1290 1290
 			$this->checkURL();
1291
-			Core\Update::check(false);
1291
+			Core\Update::check($this->basePath, false);
1292 1292
 			Core\Addon::loadAddons();
1293 1293
 			Core\Hook::loadHooks();
1294 1294
 		}

+ 8
- 3
src/BaseObject.php View File

@@ -6,7 +6,9 @@ namespace Friendica;
6 6
 
7 7
 require_once 'boot.php';
8 8
 
9
-use Friendica\Util\LoggerFactory;
9
+use Friendica\Core\Config;
10
+use Friendica\Factory;
11
+use Friendica\Util\BasePath;
10 12
 
11 13
 /**
12 14
  * Basic object
@@ -28,8 +30,11 @@ class BaseObject
28 30
 	public static function getApp()
29 31
 	{
30 32
 		if (empty(self::$app)) {
31
-			$logger = $logger = LoggerFactory::create('app');
32
-			self::$app = new App(dirname(__DIR__), $logger);
33
+			$basedir = BasePath::create(dirname(__DIR__));
34
+			$configLoader = new Config\ConfigCacheLoader($basedir);
35
+			$config = Factory\ConfigFactory::createCache($configLoader);
36
+			$logger = Factory\LoggerFactory::create('app', $config);
37
+			self::$app = new App($config, $logger);
33 38
 		}
34 39
 
35 40
 		return self::$app;

+ 1
- 1
src/Core/Console/AutomaticInstallation.php View File

@@ -158,7 +158,7 @@ HELP;
158 158
 
159 159
 		$installer->resetChecks();
160 160
 
161
-		if (!$installer->installDatabase()) {
161
+		if (!$installer->installDatabase($a->getBasePath())) {
162 162
 			$errorMessage = $this->extractErrors($installer->getChecks());
163 163
 			throw new RuntimeException($errorMessage);
164 164
 		}

+ 5
- 3
src/Core/Console/DatabaseStructure.php View File

@@ -61,17 +61,19 @@ HELP;
61 61
 
62 62
 		Core\Config::load();
63 63
 
64
+		$a = get_app();
65
+
64 66
 		switch ($this->getArgument(0)) {
65 67
 			case "dryrun":
66
-				$output = DBStructure::update(true, false);
68
+				$output = DBStructure::update($a->getBasePath(), true, false);
67 69
 				break;
68 70
 			case "update":
69 71
 				$force = $this->getOption(['f', 'force'], false);
70
-				$output = Update::run($force, true, false);
72
+				$output = Update::run($a->getBasePath(), $force, true, false);
71 73
 				break;
72 74
 			case "dumpsql":
73 75
 				ob_start();
74
-				DBStructure::printStructure();
76
+				DBStructure::printStructure($a->getBasePath());
75 77
 				$output = ob_get_clean();
76 78
 				break;
77 79
 			case "toinnodb":

+ 2
- 2
src/Core/Console/PostUpdate.php View File

@@ -2,8 +2,8 @@
2 2
 
3 3
 namespace Friendica\Core\Console;
4 4
 
5
-use Friendica\Core\L10n;
6 5
 use Friendica\Core\Config;
6
+use Friendica\Core\L10n;
7 7
 use Friendica\Core\Update;
8 8
 
9 9
 /**
@@ -56,7 +56,7 @@ HELP;
56 56
 		}
57 57
 
58 58
 		echo L10n::t('Check for pending update actions.') . "\n";
59
-		Update::run(true, true, false);
59
+		Update::run($a->getBasePath(), true, true, false);
60 60
 		echo L10n::t('Done.') . "\n";
61 61
 
62 62
 		echo L10n::t('Execute pending post updates.') . "\n";

+ 4
- 2
src/Core/Installer.php View File

@@ -168,12 +168,14 @@ class Installer
168 168
 	/***
169 169
 	 * Installs the DB-Scheme for Friendica
170 170
 	 *
171
+	 * @param string $basePath The base path of this application
172
+	 *
171 173
 	 * @return bool true if the installation was successful, otherwise false
172 174
 	 * @throws Exception
173 175
 	 */
174
-	public function installDatabase()
176
+	public function installDatabase($basePath)
175 177
 	{
176
-		$result = DBStructure::update(false, true, true);
178
+		$result = DBStructure::update($basePath, false, true, true);
177 179
 
178 180
 		if ($result) {
179 181
 			$txt = L10n::t('You may need to import the file "database.sql" manually using phpmyadmin or mysql.') . EOL;

+ 10
- 8
src/Core/Update.php View File

@@ -14,10 +14,11 @@ class Update
14 14
 	/**
15 15
 	 * @brief Function to check if the Database structure needs an update.
16 16
 	 *
17
+	 * @param string $basePath The base path of this application
17 18
 	 * @param boolean $via_worker boolean Is the check run via the worker?
18 19
 	 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
19 20
 	 */
20
-	public static function check($via_worker)
21
+	public static function check($basePath, $via_worker)
21 22
 	{
22 23
 		if (!DBA::connected()) {
23 24
 			return;
@@ -38,7 +39,7 @@ class Update
38 39
 		if ($build < DB_UPDATE_VERSION) {
39 40
 			// When we cannot execute the database update via the worker, we will do it directly
40 41
 			if (!Worker::add(PRIORITY_CRITICAL, 'DBUpdate') && $via_worker) {
41
-				self::run();
42
+				self::run($basePath);
42 43
 			}
43 44
 		}
44 45
 	}
@@ -46,14 +47,15 @@ class Update
46 47
 	/**
47 48
 	 * Automatic database updates
48 49
 	 *
49
-	 * @param bool $force    Force the Update-Check even if the lock is set
50
-	 * @param bool $verbose  Run the Update-Check verbose
51
-	 * @param bool $sendMail Sends a Mail to the administrator in case of success/failure
50
+	 * @param string $basePath The base path of this application
51
+	 * @param bool $force      Force the Update-Check even if the lock is set
52
+	 * @param bool $verbose    Run the Update-Check verbose
53
+	 * @param bool $sendMail   Sends a Mail to the administrator in case of success/failure
52 54
 	 *
53 55
 	 * @return string Empty string if the update is successful, error messages otherwise
54 56
 	 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
55 57
 	 */
56
-	public static function run($force = false, $verbose = false, $sendMail = true)
58
+	public static function run($basePath, $force = false, $verbose = false, $sendMail = true)
57 59
 	{
58 60
 		// In force mode, we release the dbupdate lock first
59 61
 		// Necessary in case of an stuck update
@@ -91,7 +93,7 @@ class Update
91 93
 					}
92 94
 
93 95
 					// update the structure in one call
94
-					$retval = DBStructure::update($verbose, true);
96
+					$retval = DBStructure::update($basePath, $verbose, true);
95 97
 					if ($retval) {
96 98
 						if ($sendMail) {
97 99
 							self::updateFailed(
@@ -125,7 +127,7 @@ class Update
125 127
 				}
126 128
 			}
127 129
 		} elseif ($force) {
128
-			DBStructure::update($verbose, true);
130
+			DBStructure::update($basePath, $verbose, true);
129 131
 		}
130 132
 
131 133
 		return '';

+ 3
- 3
src/Database/DBA.php View File

@@ -2,7 +2,7 @@
2 2
 
3 3
 namespace Friendica\Database;
4 4
 
5
-use Friendica\Core\Config\ConfigCache;
5
+use Friendica\Core\Config\IConfigCache;
6 6
 use Friendica\Core\Logger;
7 7
 use Friendica\Core\System;
8 8
 use Friendica\Util\DateTimeFormat;
@@ -32,7 +32,7 @@ class DBA
32 32
 	public static $connected = false;
33 33
 
34 34
 	/**
35
-	 * @var ConfigCache
35
+	 * @var IConfigCache
36 36
 	 */
37 37
 	private static $config;
38 38
 	private static $server_info = '';
@@ -1031,7 +1031,7 @@ class DBA
1031 1031
 	 * This process must only be started once, since the value is cached.
1032 1032
 	 */
1033 1033
 	private static function buildRelationData() {
1034
-		$definition = DBStructure::definition();
1034
+		$definition = DBStructure::definition(self::$config->get('system', 'basepath'));
1035 1035
 
1036 1036
 		foreach ($definition AS $table => $structure) {
1037 1037
 			foreach ($structure['fields'] AS $field => $field_struct) {

+ 13
- 11
src/Database/DBStructure.php View File

@@ -74,9 +74,9 @@ class DBStructure
74 74
 		return L10n::t('Errors encountered performing database changes: ') . $message . EOL;
75 75
 	}
76 76
 
77
-	public static function printStructure()
77
+	public static function printStructure($basePath)
78 78
 	{
79
-		$database = self::definition(false);
79
+		$database = self::definition($basePath, false);
80 80
 
81 81
 		echo "-- ------------------------------------------\n";
82 82
 		echo "-- " . FRIENDICA_PLATFORM . " " . FRIENDICA_VERSION . " (" . FRIENDICA_CODENAME, ")\n";
@@ -98,14 +98,15 @@ class DBStructure
98 98
 	 *
99 99
 	 * @see config/dbstructure.config.php
100 100
 	 * @param boolean $with_addons_structure Whether to tack on addons additional tables
101
+	 * @param string  $basePath              The base path of this application
101 102
 	 * @return array
102 103
 	 * @throws Exception
103 104
 	 */
104
-	public static function definition($basepath, $with_addons_structure = true)
105
+	public static function definition($basePath, $with_addons_structure = true)
105 106
 	{
106 107
 		if (!self::$definition) {
107 108
 
108
-			$filename = $basepath . '/config/dbstructure.config.php';
109
+			$filename = $basePath . '/config/dbstructure.config.php';
109 110
 
110 111
 			if (!is_readable($filename)) {
111 112
 				throw new Exception('Missing database structure config file config/dbstructure.config.php');
@@ -246,15 +247,16 @@ class DBStructure
246 247
 	/**
247 248
 	 * Updates DB structure and returns eventual errors messages
248 249
 	 *
249
-	 * @param bool  $verbose
250
-	 * @param bool  $action     Whether to actually apply the update
251
-	 * @param bool  $install    Is this the initial update during the installation?
252
-	 * @param array $tables     An array of the database tables
253
-	 * @param array $definition An array of the definition tables
250
+	 * @param string $basePath   The base path of this application
251
+	 * @param bool   $verbose
252
+	 * @param bool   $action     Whether to actually apply the update
253
+	 * @param bool   $install    Is this the initial update during the installation?
254
+	 * @param array  $tables     An array of the database tables
255
+	 * @param array  $definition An array of the definition tables
254 256
 	 * @return string Empty string if the update is successful, error messages otherwise
255 257
 	 * @throws Exception
256 258
 	 */
257
-	public static function update($verbose, $action, $install = false, array $tables = null, array $definition = null)
259
+	public static function update($basePath, $verbose, $action, $install = false, array $tables = null, array $definition = null)
258 260
 	{
259 261
 		if ($action && !$install) {
260 262
 			Config::set('system', 'maintenance', 1);
@@ -283,7 +285,7 @@ class DBStructure
283 285
 
284 286
 		// Get the definition
285 287
 		if (is_null($definition)) {
286
-			$definition = self::definition();
288
+			$definition = self::definition($basePath);
287 289
 		}
288 290
 
289 291
 		// MySQL >= 5.7.4 doesn't support the IGNORE keyword in ALTER TABLE statements

+ 7
- 0
src/Factory/ConfigFactory.php View File

@@ -6,6 +6,11 @@ use Friendica\Core\Config;
6 6
 
7 7
 class ConfigFactory
8 8
 {
9
+	/**
10
+	 * @param Config\ConfigCacheLoader $loader The Config Cache loader (INI/config/.htconfig)
11
+	 *
12
+	 * @return Config\ConfigCache
13
+	 */
9 14
 	public static function createCache(Config\ConfigCacheLoader $loader)
10 15
 	{
11 16
 		$configCache = new Config\ConfigCache();
@@ -17,6 +22,7 @@ class ConfigFactory
17 22
 	/**
18 23
 	 * @param string              $type   The adapter type
19 24
 	 * @param Config\IConfigCache $config The config cache of this adapter
25
+	 *
20 26
 	 * @return Config\IConfigAdapter
21 27
 	 */
22 28
 	public static function createConfig($type, $config)
@@ -32,6 +38,7 @@ class ConfigFactory
32 38
 	 * @param string               $type   The adapter type
33 39
 	 * @param int                  $uid    The UID of the current user
34 40
 	 * @param Config\IPConfigCache $config The config cache of this adapter
41
+	 *
35 42
 	 * @return Config\IPConfigAdapter
36 43
 	 */
37 44
 	public static function createPConfig($type, $uid, $config)

+ 3
- 3
src/Model/Attach.php View File

@@ -7,15 +7,15 @@
7 7
 namespace Friendica\Model;
8 8
 
9 9
 use Friendica\BaseObject;
10
-use Friendica\Core\System;
11 10
 use Friendica\Core\StorageManager;
11
+use Friendica\Core\System;
12 12
 use Friendica\Database\DBA;
13 13
 use Friendica\Database\DBStructure;
14 14
 use Friendica\Model\Storage\IStorage;
15 15
 use Friendica\Object\Image;
16
-use Friendica\Util\Security;
17 16
 use Friendica\Util\DateTimeFormat;
18 17
 use Friendica\Util\Mimetype;
18
+use Friendica\Util\Security;
19 19
 
20 20
 /**
21 21
  * Class to handle attach dabatase table
@@ -31,7 +31,7 @@ class Attach extends BaseObject
31 31
 	 */
32 32
 	private static function getFields()
33 33
 	{
34
-		$allfields = DBStructure::definition(false);
34
+		$allfields = DBStructure::definition(self::getApp()->getBasePath(), false);
35 35
 		$fields = array_keys($allfields['attach']['fields']);
36 36
 		array_splice($fields, array_search('data', $fields), 1);
37 37
 		return $fields;

+ 1
- 1
src/Module/Install.php View File

@@ -103,7 +103,7 @@ class Install extends BaseModule
103 103
 					return;
104 104
 				}
105 105
 
106
-				self::$installer->installDatabase();
106
+				self::$installer->installDatabase($a->getBasePath());
107 107
 
108 108
 				break;
109 109
 		}

+ 3
- 2
src/Worker/DBUpdate.php View File

@@ -5,12 +5,13 @@
5 5
  */
6 6
 namespace Friendica\Worker;
7 7
 
8
+use Friendica\BaseObject;
8 9
 use Friendica\Core\Update;
9 10
 
10
-class DBUpdate
11
+class DBUpdate extends BaseObject
11 12
 {
12 13
 	public static function execute()
13 14
 	{
14
-		Update::run();
15
+		Update::run(self::getApp()->getBasePath());
15 16
 	}
16 17
 }

+ 3
- 1
tests/DatabaseTest.php View File

@@ -36,7 +36,9 @@ abstract class DatabaseTest extends MockedTest
36 36
 			$this->markTestSkipped('Please set the MYSQL_* environment variables to your test database credentials.');
37 37
 		}
38 38
 
39
-		DBA::connect(getenv('MYSQL_HOST'),
39
+		DBA::connect(
40
+			__DIR__,
41
+			getenv('MYSQL_HOST'),
40 42
 			getenv('MYSQL_USERNAME'),
41 43
 			getenv('MYSQL_PASSWORD'),
42 44
 			getenv('MYSQL_DATABASE'));

+ 1
- 1
tests/include/ApiTest.php View File

@@ -10,8 +10,8 @@ use Friendica\Core\Config;
10 10
 use Friendica\Core\PConfig;
11 11
 use Friendica\Core\Protocol;
12 12
 use Friendica\Core\System;
13
+use Friendica\Factory\LoggerFactory;
13 14
 use Friendica\Network\HTTPException;
14
-use Friendica\Util\LoggerFactory;
15 15
 use Monolog\Handler\TestHandler;
16 16
 
17 17
 require_once __DIR__ . '/../../include/api.php';

Loading…
Cancel
Save