From: Philipp Holzer <admin@philipp.info>
Date: Tue, 12 Feb 2019 19:12:25 +0000 (+0100)
Subject: Adding DependencyFactory
X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=cdcf1667d7cb5cdcf960b5939deb558dd3f22233;p=friendica.git

Adding DependencyFactory
---

diff --git a/bin/auth_ejabberd.php b/bin/auth_ejabberd.php
index 003faae1f5..bf6d069d12 100755
--- a/bin/auth_ejabberd.php
+++ b/bin/auth_ejabberd.php
@@ -32,10 +32,7 @@
  *
  */
 
-use Friendica\App;
-use Friendica\Core\Config\Cache;
 use Friendica\Factory;
-use Friendica\Util\BasePath;
 use Friendica\Util\ExAuth;
 
 if (sizeof($_SERVER["argv"]) == 0) {
@@ -54,17 +51,7 @@ chdir($directory);
 
 require dirname(__DIR__) . '/vendor/autoload.php';
 
-$basedir = BasePath::create(dirname(__DIR__), $_SERVER);
-$configLoader = new Cache\ConfigCacheLoader($basedir);
-$configCache = Factory\ConfigFactory::createCache($configLoader);
-Factory\DBFactory::init($configCache, $_SERVER);
-$config = Factory\ConfigFactory::createConfig($configCache);
-// needed to call PConfig::init()
-Factory\ConfigFactory::createPConfig($configCache);
-$logger = Factory\LoggerFactory::create('auth_ejabberd', $config);
-$profiler = Factory\ProfilerFactory::create($logger, $config);
-
-$a = new App($config, $logger, $profiler);
+$a = Factory\DependencyFactory::setUp('auth_ejabbered', dirname(__DIR__));
 
 if ($a->getMode()->isNormal()) {
 	$oAuth = new ExAuth();
diff --git a/bin/console.php b/bin/console.php
index 2b0d588210..410eabda09 100755
--- a/bin/console.php
+++ b/bin/console.php
@@ -3,21 +3,9 @@
 
 require dirname(__DIR__) . '/vendor/autoload.php';
 
-use Friendica\Core\Config\Cache;
 use Friendica\Factory;
-use Friendica\Util\BasePath;
 
-$basedir = BasePath::create(dirname(__DIR__), $_SERVER);
-$configLoader = new Cache\ConfigCacheLoader($basedir);
-$configCache = Factory\ConfigFactory::createCache($configLoader);
-Factory\DBFactory::init($configCache, $_SERVER);
-$config = Factory\ConfigFactory::createConfig($configCache);
-// needed to call PConfig::init()
-Factory\ConfigFactory::createPConfig($configCache);
-$logger = Factory\LoggerFactory::create('console', $config);
-$profiler = Factory\ProfilerFactory::create($logger, $config);
-
-$a = new Friendica\App($config, $logger, $profiler);
+$a = Factory\DependencyFactory::setUp('console', dirname(__DIR__));
 \Friendica\BaseObject::setApp($a);
 
 (new Friendica\Core\Console($argv))->execute();
diff --git a/bin/daemon.php b/bin/daemon.php
index f2970a5180..257896cfac 100755
--- a/bin/daemon.php
+++ b/bin/daemon.php
@@ -7,13 +7,11 @@
  * This script was taken from http://php.net/manual/en/function.pcntl-fork.php
  */
 
-use Friendica\App;
 use Friendica\Core\Config;
-use Friendica\Core\Config\Cache;
+use Friendica\Core\Logger;
 use Friendica\Core\Worker;
 use Friendica\Database\DBA;
 use Friendica\Factory;
-use Friendica\Util\BasePath;
 
 // Get options
 $shortopts = 'f';
@@ -34,17 +32,7 @@ if (!file_exists("boot.php") && (sizeof($_SERVER["argv"]) != 0)) {
 
 require dirname(__DIR__) . '/vendor/autoload.php';
 
-$basedir = BasePath::create(dirname(__DIR__), $_SERVER);
-$configLoader = new Cache\ConfigCacheLoader($basedir);
-$configCache = Factory\ConfigFactory::createCache($configLoader);
-Factory\DBFactory::init($configCache, $_SERVER);
-$config = Factory\ConfigFactory::createConfig($configCache);
-// needed to call PConfig::init()
-Factory\ConfigFactory::createPConfig($configCache);
-$logger = Factory\LoggerFactory::create('daemon', $config);
-$profiler = Factory\ProfilerFactory::create($logger, $config);
-
-$a = new App($config, $logger, $profiler);
+$a = Factory\DependencyFactory::setUp('daemon', dirname(__DIR__));
 
 if ($a->getMode()->isInstall()) {
 	die("Friendica isn't properly installed yet.\n");
@@ -114,7 +102,7 @@ if ($mode == "stop") {
 
 	unlink($pidfile);
 
-	$logger->notice("Worker daemon process was killed", ["pid" => $pid]);
+	Logger::notice("Worker daemon process was killed", ["pid" => $pid]);
 
 	Config::set('system', 'worker_daemon_mode', false);
 	die("Worker daemon process $pid was killed.\n");
@@ -124,7 +112,7 @@ if (!empty($pid) && posix_kill($pid, 0)) {
 	die("Daemon process $pid is already running.\n");
 }
 
-$logger->notice('Starting worker daemon.', ["pid" => $pid]);
+Logger::notice('Starting worker daemon.', ["pid" => $pid]);
 
 if (!$foreground) {
 	echo "Starting worker daemon.\n";
@@ -172,7 +160,7 @@ $last_cron = 0;
 // Now running as a daemon.
 while (true) {
 	if (!$do_cron && ($last_cron + $wait_interval) < time()) {
-		$logger->info('Forcing cron worker call.', ["pid" => $pid]);
+		Logger::info('Forcing cron worker call.', ["pid" => $pid]);
 		$do_cron = true;
 	}
 
@@ -186,7 +174,7 @@ while (true) {
 		$last_cron = time();
 	}
 
-	$logger->info("Sleeping", ["pid" => $pid]);
+	Logger::info("Sleeping", ["pid" => $pid]);
 	$start = time();
 	do {
 		$seconds = (time() - $start);
@@ -203,10 +191,10 @@ while (true) {
 
 	if ($timeout) {
 		$do_cron = true;
-		$logger->info("Woke up after $wait_interval seconds.", ["pid" => $pid, 'sleep' => $wait_interval]);
+		Logger::info("Woke up after $wait_interval seconds.", ["pid" => $pid, 'sleep' => $wait_interval]);
 	} else {
 		$do_cron = false;
-		$logger->info("Worker jobs are calling to be forked.", ["pid" => $pid]);
+		Logger::info("Worker jobs are calling to be forked.", ["pid" => $pid]);
 	}
 }
 
diff --git a/bin/worker.php b/bin/worker.php
index 5303331276..c7174d81e1 100755
--- a/bin/worker.php
+++ b/bin/worker.php
@@ -7,11 +7,9 @@
 
 use Friendica\App;
 use Friendica\Core\Config;
-use Friendica\Core\Config\Cache;
 use Friendica\Core\Update;
 use Friendica\Core\Worker;
 use Friendica\Factory;
-use Friendica\Util\BasePath;
 
 // Get options
 $shortopts = 'sn';
@@ -32,17 +30,7 @@ if (!file_exists("boot.php") && (sizeof($_SERVER["argv"]) != 0)) {
 
 require dirname(__DIR__) . '/vendor/autoload.php';
 
-$basedir = BasePath::create(dirname(__DIR__), $_SERVER);
-$configLoader = new Cache\ConfigCacheLoader($basedir);
-$configCache = Factory\ConfigFactory::createCache($configLoader);
-Factory\DBFactory::init($configCache, $_SERVER);
-$config = Factory\ConfigFactory::createConfig($configCache);
-// needed to call PConfig::init()
-Factory\ConfigFactory::createPConfig($configCache);
-$logger = Factory\LoggerFactory::create('worker', $config);
-$profiler = Factory\ProfilerFactory::create($logger, $config);
-
-$a = new App($config, $logger, $profiler);
+$a = Factory\DependencyFactory::setUp('worker', dirname(__DIR__));
 
 // Check the database structure and possibly fixes it
 Update::check($a->getBasePath(), true);
diff --git a/index.php b/index.php
index 66f0239654..6bbd70a198 100644
--- a/index.php
+++ b/index.php
@@ -4,10 +4,7 @@
  * Friendica
  */
 
-use Friendica\App;
-use Friendica\Core\Config\Cache;
 use Friendica\Factory;
-use Friendica\Util\BasePath;
 
 if (!file_exists(__DIR__ . '/vendor/autoload.php')) {
 	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')) {
 
 require __DIR__ . '/vendor/autoload.php';
 
-$basedir = BasePath::create(__DIR__, $_SERVER);
-$configLoader = new Cache\ConfigCacheLoader($basedir);
-$configCache = Factory\ConfigFactory::createCache($configLoader);
-Factory\DBFactory::init($configCache, $_SERVER);
-$config = Factory\ConfigFactory::createConfig($configCache);
-// needed to call PConfig::init()
-Factory\ConfigFactory::createPConfig($configCache);
-$logger = Factory\LoggerFactory::create('index', $config);
-$profiler = Factory\ProfilerFactory::create($logger, $config);
-
 // We assume that the index.php is called by a frontend process
 // The value is set to "true" by default in App
-$a = new App($config, $logger, $profiler, false);
+$a = Factory\DependencyFactory::setUp('index', __DIR__, true);
 
 $a->runFrontend();
diff --git a/src/Factory/DependencyFactory.php b/src/Factory/DependencyFactory.php
new file mode 100644
index 0000000000..041202e6bf
--- /dev/null
+++ b/src/Factory/DependencyFactory.php
@@ -0,0 +1,36 @@
+<?php
+
+namespace Friendica\Factory;
+
+use Friendica\App;
+use Friendica\Core\Config\Cache;
+use Friendica\Factory;
+use Friendica\Util\BasePath;
+
+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)
+	{
+		$basedir = BasePath::create($directory, $_SERVER);
+		$configLoader = new Cache\ConfigCacheLoader($basedir);
+		$configCache = Factory\ConfigFactory::createCache($configLoader);
+		Factory\DBFactory::init($configCache, $_SERVER);
+		$config = Factory\ConfigFactory::createConfig($configCache);
+		// needed to call PConfig::init()
+		Factory\ConfigFactory::createPConfig($configCache);
+		Factory\LoggerFactory::create($channel, $config);
+
+		return new App($config, $isBackend);
+	}
+}