From 37ffeb81218f1ef13328ead46e7f7238cbb80004 Mon Sep 17 00:00:00 2001
From: Art4 <art4@wlabs.de>
Date: Wed, 8 Jan 2025 22:35:27 +0000
Subject: [PATCH] Move creation of Dice into Container class

---
 bin/auth_ejabberd.php  | 11 ++++-------
 bin/console.php        |  6 +-----
 bin/daemon.php         |  6 +-----
 bin/jetstream.php      |  6 +-----
 bin/worker.php         |  6 +-----
 index.php              |  6 +-----
 src/Core/Container.php | 11 ++++++++++-
 7 files changed, 19 insertions(+), 33 deletions(-)

diff --git a/bin/auth_ejabberd.php b/bin/auth_ejabberd.php
index 68ee53a8b0..7a279b079b 100755
--- a/bin/auth_ejabberd.php
+++ b/bin/auth_ejabberd.php
@@ -44,15 +44,12 @@ if (php_sapi_name() !== 'cli') {
 	exit();
 }
 
-use Dice\Dice;
+chdir(dirname(__DIR__));
 
-chdir(dirname(__FILE__, 2));
+require dirname(__DIR__) . '/vendor/autoload.php';
 
-require dirname(__FILE__, 2) . '/vendor/autoload.php';
+$container = \Friendica\Core\Container::fromBasePath(dirname(__DIR__));
 
-$dice = (new Dice())->addRules(require(dirname(__FILE__, 2) . '/static/dependencies.config.php'));
-
-$container = \Friendica\Core\Container::fromDice($dice);
-$app       = \Friendica\App::fromContainer($container);
+$app = \Friendica\App::fromContainer($container);
 
 $app->processEjabberd();
diff --git a/bin/console.php b/bin/console.php
index 404f327868..94738dbf8c 100755
--- a/bin/console.php
+++ b/bin/console.php
@@ -13,13 +13,9 @@ if (php_sapi_name() !== 'cli') {
 	exit();
 }
 
-use Dice\Dice;
-
 require dirname(__DIR__) . '/vendor/autoload.php';
 
-$dice = (new Dice())->addRules(require(dirname(__DIR__) . '/static/dependencies.config.php'));
-
-$container = \Friendica\Core\Container::fromDice($dice);
+$container = \Friendica\Core\Container::fromBasePath(dirname(__DIR__));
 
 $app = \Friendica\App::fromContainer($container);
 
diff --git a/bin/daemon.php b/bin/daemon.php
index a1c0abbfaf..04e00ccf0d 100755
--- a/bin/daemon.php
+++ b/bin/daemon.php
@@ -19,19 +19,15 @@ if (php_sapi_name() !== 'cli') {
 	exit();
 }
 
-use Dice\Dice;
-
 // Ensure that daemon.php is executed from the base path of the installation
 chdir(dirname(__DIR__));
 
 require dirname(__DIR__) . '/vendor/autoload.php';
 
-$dice = (new Dice())->addRules(require(dirname(__DIR__) . '/static/dependencies.config.php'));
-
 $argv = $_SERVER['argv'] ?? [];
 array_splice($argv, 1, 0, "daemon");
 
-$container = \Friendica\Core\Container::fromDice($dice);
+$container = \Friendica\Core\Container::fromBasePath(dirname(__DIR__));
 
 $app = \Friendica\App::fromContainer($container);
 
diff --git a/bin/jetstream.php b/bin/jetstream.php
index ac33e8ff2a..72d13c3e1f 100755
--- a/bin/jetstream.php
+++ b/bin/jetstream.php
@@ -9,8 +9,6 @@
  * @deprecated 2025.02 use bin/console.php jetstream instead
  */
 
-use Dice\Dice;
-
 if (php_sapi_name() !== 'cli') {
 	header($_SERVER["SERVER_PROTOCOL"] . ' 403 Forbidden');
 	exit();
@@ -21,12 +19,10 @@ chdir(dirname(__DIR__));
 
 require dirname(__DIR__) . '/vendor/autoload.php';
 
-$dice = (new Dice())->addRules(require(dirname(__DIR__) . '/static/dependencies.config.php'));
-
 $argv = $_SERVER['argv'] ?? [];
 array_splice($argv, 1, 0, "jetstream");
 
-$container = \Friendica\Core\Container::fromDice($dice);
+$container = \Friendica\Core\Container::fromBasePath(dirname(__DIR__));
 
 $app = \Friendica\App::fromContainer($container);
 
diff --git a/bin/worker.php b/bin/worker.php
index fa238b66a2..54b36e392f 100755
--- a/bin/worker.php
+++ b/bin/worker.php
@@ -16,19 +16,15 @@ if (php_sapi_name() !== 'cli') {
 	exit();
 }
 
-use Dice\Dice;
-
 // Ensure that worker.php is executed from the base path of the installation
 chdir(dirname(__DIR__));
 
 require dirname(__DIR__) . '/vendor/autoload.php';
 
-$dice = (new Dice())->addRules(require(dirname(__DIR__) . '/static/dependencies.config.php'));
-
 $argv = $_SERVER['argv'] ?? [];
 array_splice($argv, 1, 0, "worker");
 
-$container = \Friendica\Core\Container::fromDice($dice);
+$container = \Friendica\Core\Container::fromBasePath(dirname(__DIR__));
 
 $app = \Friendica\App::fromContainer($container);
 
diff --git a/index.php b/index.php
index 6654dcb515..906d91bc3c 100644
--- a/index.php
+++ b/index.php
@@ -5,8 +5,6 @@
 //
 // SPDX-License-Identifier: AGPL-3.0-or-later
 
-use Dice\Dice;
-
 $start_time = microtime(true);
 
 if (!file_exists(__DIR__ . '/vendor/autoload.php')) {
@@ -17,9 +15,7 @@ require __DIR__ . '/vendor/autoload.php';
 
 $request = \GuzzleHttp\Psr7\ServerRequest::fromGlobals();
 
-$dice = (new Dice())->addRules(require(__DIR__ . '/static/dependencies.config.php'));
-
-$container = \Friendica\Core\Container::fromDice($dice);
+$container = \Friendica\Core\Container::fromBasePath(__DIR__);
 $app       = \Friendica\App::fromContainer($container);
 
 $app->processRequest($request, $start_time);
diff --git a/src/Core/Container.php b/src/Core/Container.php
index 12a9b625a7..0da94935d8 100644
--- a/src/Core/Container.php
+++ b/src/Core/Container.php
@@ -21,9 +21,18 @@ use Psr\Log\LoggerInterface;
  */
 class Container
 {
+	public static function fromBasePath(string $basePath): self
+	{
+		$path = $basePath . '/static/dependencies.config.php';
+
+		$dice = (new Dice())->addRules(require($path));
+
+		return static::fromDice($dice);
+	}
+
 	private Dice $container;
 
-	protected function __construct(Dice $container)
+	private function __construct(Dice $container)
 	{
 		$this->container = $container;
 	}