Adding DependencyFactory
authorPhilipp Holzer <admin@philipp.info>
Tue, 12 Feb 2019 19:12:25 +0000 (20:12 +0100)
committerPhilipp Holzer <admin@philipp.info>
Sun, 17 Feb 2019 19:45:39 +0000 (20:45 +0100)
bin/auth_ejabberd.php
bin/console.php
bin/daemon.php
bin/worker.php
index.php
src/Factory/DependencyFactory.php [new file with mode: 0644]

index 003faae..bf6d069 100755 (executable)
  *
  */
 
-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();
index 2b0d588..410eabd 100755 (executable)
@@ -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();
index f2970a5..257896c 100755 (executable)
@@ -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]);
        }
 }
 
index 5303331..c7174d8 100755 (executable)
@@ -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);
index 66f0239..6bbd70a 100644 (file)
--- 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 (file)
index 0000000..041202e
--- /dev/null
@@ -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);
+       }
+}