Browse Source

Adding DependencyFactory

tags/2019.03^2
Philipp Holzer 7 months ago
parent
commit
cdcf1667d7
No account linked to committer's email address
6 changed files with 48 additions and 74 deletions
  1. 1
    14
      bin/auth_ejabberd.php
  2. 1
    13
      bin/console.php
  3. 8
    20
      bin/daemon.php
  4. 1
    13
      bin/worker.php
  5. 1
    14
      index.php
  6. 36
    0
      src/Factory/DependencyFactory.php

+ 1
- 14
bin/auth_ejabberd.php View File

@@ -32,10 +32,7 @@
32 32
  *
33 33
  */
34 34
 
35
-use Friendica\App;
36
-use Friendica\Core\Config\Cache;
37 35
 use Friendica\Factory;
38
-use Friendica\Util\BasePath;
39 36
 use Friendica\Util\ExAuth;
40 37
 
41 38
 if (sizeof($_SERVER["argv"]) == 0) {
@@ -54,17 +51,7 @@ chdir($directory);
54 51
 
55 52
 require dirname(__DIR__) . '/vendor/autoload.php';
56 53
 
57
-$basedir = BasePath::create(dirname(__DIR__), $_SERVER);
58
-$configLoader = new Cache\ConfigCacheLoader($basedir);
59
-$configCache = Factory\ConfigFactory::createCache($configLoader);
60
-Factory\DBFactory::init($configCache, $_SERVER);
61
-$config = Factory\ConfigFactory::createConfig($configCache);
62
-// needed to call PConfig::init()
63
-Factory\ConfigFactory::createPConfig($configCache);
64
-$logger = Factory\LoggerFactory::create('auth_ejabberd', $config);
65
-$profiler = Factory\ProfilerFactory::create($logger, $config);
66
-
67
-$a = new App($config, $logger, $profiler);
54
+$a = Factory\DependencyFactory::setUp('auth_ejabbered', dirname(__DIR__));
68 55
 
69 56
 if ($a->getMode()->isNormal()) {
70 57
 	$oAuth = new ExAuth();

+ 1
- 13
bin/console.php View File

@@ -3,21 +3,9 @@
3 3
 
4 4
 require dirname(__DIR__) . '/vendor/autoload.php';
5 5
 
6
-use Friendica\Core\Config\Cache;
7 6
 use Friendica\Factory;
8
-use Friendica\Util\BasePath;
9 7
 
10
-$basedir = BasePath::create(dirname(__DIR__), $_SERVER);
11
-$configLoader = new Cache\ConfigCacheLoader($basedir);
12
-$configCache = Factory\ConfigFactory::createCache($configLoader);
13
-Factory\DBFactory::init($configCache, $_SERVER);
14
-$config = Factory\ConfigFactory::createConfig($configCache);
15
-// needed to call PConfig::init()
16
-Factory\ConfigFactory::createPConfig($configCache);
17
-$logger = Factory\LoggerFactory::create('console', $config);
18
-$profiler = Factory\ProfilerFactory::create($logger, $config);
19
-
20
-$a = new Friendica\App($config, $logger, $profiler);
8
+$a = Factory\DependencyFactory::setUp('console', dirname(__DIR__));
21 9
 \Friendica\BaseObject::setApp($a);
22 10
 
23 11
 (new Friendica\Core\Console($argv))->execute();

+ 8
- 20
bin/daemon.php View File

@@ -7,13 +7,11 @@
7 7
  * This script was taken from http://php.net/manual/en/function.pcntl-fork.php
8 8
  */
9 9
 
10
-use Friendica\App;
11 10
 use Friendica\Core\Config;
12
-use Friendica\Core\Config\Cache;
11
+use Friendica\Core\Logger;
13 12
 use Friendica\Core\Worker;
14 13
 use Friendica\Database\DBA;
15 14
 use Friendica\Factory;
16
-use Friendica\Util\BasePath;
17 15
 
18 16
 // Get options
19 17
 $shortopts = 'f';
@@ -34,17 +32,7 @@ if (!file_exists("boot.php") && (sizeof($_SERVER["argv"]) != 0)) {
34 32
 
35 33
 require dirname(__DIR__) . '/vendor/autoload.php';
36 34
 
37
-$basedir = BasePath::create(dirname(__DIR__), $_SERVER);
38
-$configLoader = new Cache\ConfigCacheLoader($basedir);
39
-$configCache = Factory\ConfigFactory::createCache($configLoader);
40
-Factory\DBFactory::init($configCache, $_SERVER);
41
-$config = Factory\ConfigFactory::createConfig($configCache);
42
-// needed to call PConfig::init()
43
-Factory\ConfigFactory::createPConfig($configCache);
44
-$logger = Factory\LoggerFactory::create('daemon', $config);
45
-$profiler = Factory\ProfilerFactory::create($logger, $config);
46
-
47
-$a = new App($config, $logger, $profiler);
35
+$a = Factory\DependencyFactory::setUp('daemon', dirname(__DIR__));
48 36
 
49 37
 if ($a->getMode()->isInstall()) {
50 38
 	die("Friendica isn't properly installed yet.\n");
@@ -114,7 +102,7 @@ if ($mode == "stop") {
114 102
 
115 103
 	unlink($pidfile);
116 104
 
117
-	$logger->notice("Worker daemon process was killed", ["pid" => $pid]);
105
+	Logger::notice("Worker daemon process was killed", ["pid" => $pid]);
118 106
 
119 107
 	Config::set('system', 'worker_daemon_mode', false);
120 108
 	die("Worker daemon process $pid was killed.\n");
@@ -124,7 +112,7 @@ if (!empty($pid) && posix_kill($pid, 0)) {
124 112
 	die("Daemon process $pid is already running.\n");
125 113
 }
126 114
 
127
-$logger->notice('Starting worker daemon.', ["pid" => $pid]);
115
+Logger::notice('Starting worker daemon.', ["pid" => $pid]);
128 116
 
129 117
 if (!$foreground) {
130 118
 	echo "Starting worker daemon.\n";
@@ -172,7 +160,7 @@ $last_cron = 0;
172 160
 // Now running as a daemon.
173 161
 while (true) {
174 162
 	if (!$do_cron && ($last_cron + $wait_interval) < time()) {
175
-		$logger->info('Forcing cron worker call.', ["pid" => $pid]);
163
+		Logger::info('Forcing cron worker call.', ["pid" => $pid]);
176 164
 		$do_cron = true;
177 165
 	}
178 166
 
@@ -186,7 +174,7 @@ while (true) {
186 174
 		$last_cron = time();
187 175
 	}
188 176
 
189
-	$logger->info("Sleeping", ["pid" => $pid]);
177
+	Logger::info("Sleeping", ["pid" => $pid]);
190 178
 	$start = time();
191 179
 	do {
192 180
 		$seconds = (time() - $start);
@@ -203,10 +191,10 @@ while (true) {
203 191
 
204 192
 	if ($timeout) {
205 193
 		$do_cron = true;
206
-		$logger->info("Woke up after $wait_interval seconds.", ["pid" => $pid, 'sleep' => $wait_interval]);
194
+		Logger::info("Woke up after $wait_interval seconds.", ["pid" => $pid, 'sleep' => $wait_interval]);
207 195
 	} else {
208 196
 		$do_cron = false;
209
-		$logger->info("Worker jobs are calling to be forked.", ["pid" => $pid]);
197
+		Logger::info("Worker jobs are calling to be forked.", ["pid" => $pid]);
210 198
 	}
211 199
 }
212 200
 

+ 1
- 13
bin/worker.php View File

@@ -7,11 +7,9 @@
7 7
 
8 8
 use Friendica\App;
9 9
 use Friendica\Core\Config;
10
-use Friendica\Core\Config\Cache;
11 10
 use Friendica\Core\Update;
12 11
 use Friendica\Core\Worker;
13 12
 use Friendica\Factory;
14
-use Friendica\Util\BasePath;
15 13
 
16 14
 // Get options
17 15
 $shortopts = 'sn';
@@ -32,17 +30,7 @@ if (!file_exists("boot.php") && (sizeof($_SERVER["argv"]) != 0)) {
32 30
 
33 31
 require dirname(__DIR__) . '/vendor/autoload.php';
34 32
 
35
-$basedir = BasePath::create(dirname(__DIR__), $_SERVER);
36
-$configLoader = new Cache\ConfigCacheLoader($basedir);
37
-$configCache = Factory\ConfigFactory::createCache($configLoader);
38
-Factory\DBFactory::init($configCache, $_SERVER);
39
-$config = Factory\ConfigFactory::createConfig($configCache);
40
-// needed to call PConfig::init()
41
-Factory\ConfigFactory::createPConfig($configCache);
42
-$logger = Factory\LoggerFactory::create('worker', $config);
43
-$profiler = Factory\ProfilerFactory::create($logger, $config);
44
-
45
-$a = new App($config, $logger, $profiler);
33
+$a = Factory\DependencyFactory::setUp('worker', dirname(__DIR__));
46 34
 
47 35
 // Check the database structure and possibly fixes it
48 36
 Update::check($a->getBasePath(), true);

+ 1
- 14
index.php View File

@@ -4,10 +4,7 @@
4 4
  * Friendica
5 5
  */
6 6
 
7
-use Friendica\App;
8
-use Friendica\Core\Config\Cache;
9 7
 use Friendica\Factory;
10
-use Friendica\Util\BasePath;
11 8
 
12 9
 if (!file_exists(__DIR__ . '/vendor/autoload.php')) {
13 10
 	die('Vendor path not found. Please execute "bin/composer.phar --no-dev install" on the command line in the web root.');
@@ -15,18 +12,8 @@ if (!file_exists(__DIR__ . '/vendor/autoload.php')) {
15 12
 
16 13
 require __DIR__ . '/vendor/autoload.php';
17 14
 
18
-$basedir = BasePath::create(__DIR__, $_SERVER);
19
-$configLoader = new Cache\ConfigCacheLoader($basedir);
20
-$configCache = Factory\ConfigFactory::createCache($configLoader);
21
-Factory\DBFactory::init($configCache, $_SERVER);
22
-$config = Factory\ConfigFactory::createConfig($configCache);
23
-// needed to call PConfig::init()
24
-Factory\ConfigFactory::createPConfig($configCache);
25
-$logger = Factory\LoggerFactory::create('index', $config);
26
-$profiler = Factory\ProfilerFactory::create($logger, $config);
27
-
28 15
 // We assume that the index.php is called by a frontend process
29 16
 // The value is set to "true" by default in App
30
-$a = new App($config, $logger, $profiler, false);
17
+$a = Factory\DependencyFactory::setUp('index', __DIR__, true);
31 18
 
32 19
 $a->runFrontend();

+ 36
- 0
src/Factory/DependencyFactory.php View File

@@ -0,0 +1,36 @@
1
+<?php
2
+
3
+namespace Friendica\Factory;
4
+
5
+use Friendica\App;
6
+use Friendica\Core\Config\Cache;
7
+use Friendica\Factory;
8
+use Friendica\Util\BasePath;
9
+
10
+class DependencyFactory
11
+{
12
+	/**
13
+	 * Setting all default-dependencies of a friendica execution
14
+	 *
15
+	 * @param string $channel   The channel of this execution
16
+	 * @param string $directory The base directory
17
+	 * @param bool   $isBackend True, if it's a backend execution, otherwise false (Default true)
18
+	 *
19
+	 * @return App The application
20
+	 *
21
+	 * @throws \Exception
22
+	 */
23
+	public static function setUp($channel, $directory, $isBackend = true)
24
+	{
25
+		$basedir = BasePath::create($directory, $_SERVER);
26
+		$configLoader = new Cache\ConfigCacheLoader($basedir);
27
+		$configCache = Factory\ConfigFactory::createCache($configLoader);
28
+		Factory\DBFactory::init($configCache, $_SERVER);
29
+		$config = Factory\ConfigFactory::createConfig($configCache);
30
+		// needed to call PConfig::init()
31
+		Factory\ConfigFactory::createPConfig($configCache);
32
+		Factory\LoggerFactory::create($channel, $config);
33
+
34
+		return new App($config, $isBackend);
35
+	}
36
+}

Loading…
Cancel
Save