$dice = (new Dice())->addRules(require(dirname(__FILE__, 2) . '/static/dependencies.config.php'));
-$app = \Friendica\App::fromDice($dice);
+$container = \Friendica\Core\Container::fromDice($dice);
+$app = \Friendica\App::fromContainer($container);
$app->processEjabberd();
$dice = (new Dice())->addRules(require(dirname(__DIR__) . '/static/dependencies.config.php'));
-$app = \Friendica\App::fromDice($dice);
-
-$app->processConsole($_SERVER['argv'] ?? []);
+$container = \Friendica\Core\Container::fromDice($dice);
+\Friendica\Core\Console::create($container, $_SERVER['argv'] ?? [])->execute();
$dice = (new Dice())->addRules(require(dirname(__DIR__) . '/static/dependencies.config.php'));
-$app = \Friendica\App::fromDice($dice);
-
$argv = $_SERVER['argv'] ?? [];
array_splice($argv, 1, 0, "daemon");
-$app->processConsole($argv);
+$container = \Friendica\Core\Container::fromDice($dice);
+\Friendica\Core\Console::create($container, $_SERVER['argv'] ?? [])->execute();
$dice = (new Dice())->addRules(require(dirname(__DIR__) . '/static/dependencies.config.php'));
-$app = \Friendica\App::fromDice($dice);
-
$argv = $_SERVER['argv'] ?? [];
array_splice($argv, 1, 0, "jetstream");
-$app->processConsole($argv);
+$container = \Friendica\Core\Container::fromDice($dice);
+\Friendica\Core\Console::create($container, $_SERVER['argv'] ?? [])->execute();
$dice = (new Dice())->addRules(require(dirname(__DIR__) . '/static/dependencies.config.php'));
-$app = \Friendica\App::fromDice($dice);
-
$argv = $_SERVER['argv'] ?? [];
array_splice($argv, 1, 0, "worker");
-$app->processConsole($argv);
+$container = \Friendica\Core\Container::fromDice($dice);
+\Friendica\Core\Console::create($container, $_SERVER['argv'] ?? [])->execute();
$dice = (new Dice())->addRules(require(__DIR__ . '/static/dependencies.config.php'));
-$app = \Friendica\App::fromDice($dice);
+$container = \Friendica\Core\Container::fromDice($dice);
+$app = \Friendica\App::fromContainer($container);
$app->processRequest($request, $start_time);
use Friendica\Capabilities\ICanHandleRequests;
use Friendica\Content\Nav;
use Friendica\Core\Config\Factory\Config;
+use Friendica\Core\Container;
use Friendica\Core\Renderer;
use Friendica\Core\Session\Capability\IHandleUserSessions;
use Friendica\Database\Definition\DbaDefinition;
const CODENAME = 'Interrupted Fern';
const VERSION = '2025.02-dev';
- public static function fromDice(Dice $dice): self
+ public static function fromContainer(Container $container): self
{
- return new self($dice);
+ return new self($container);
}
/**
- * @var Dice
+ * @var Container
*/
private $container;
*/
private $appHelper;
- private function __construct(Dice $container)
+ private function __construct(Container $container)
{
$this->container = $container;
}
public function processRequest(ServerRequestInterface $request, float $start_time): void
{
- $this->setupContainerForAddons();
-
- $this->setupContainerForLogger(LogChannel::DEFAULT);
-
- $this->container = $this->container->addRule(Mode::class, [
+ $this->container->addRule(Mode::class, [
'call' => [
['determineRunMode', [false, $request->getServerParams()], Dice::CHAIN_CALL],
],
]);
- $this->setupLegacyServiceLocator();
-
- $this->registerErrorHandler();
+ $this->container->setup(LogChannel::APP, false);
$this->requestId = $this->container->create(Request::class)->getRequestId();
$this->auth = $this->container->create(Authentication::class);
public function processEjabberd(): void
{
- $this->setupContainerForAddons();
-
- $this->setupContainerForLogger(LogChannel::AUTH_JABBERED);
-
- $this->setupLegacyServiceLocator();
-
- $this->registerErrorHandler();
+ $this->container->setup(LogChannel::AUTH_JABBERED, false);
/** @var BasePath */
$basePath = $this->container->create(BasePath::class);
}
}
- public function processConsole(array $argv): void
- {
- $this->setupContainerForAddons();
-
- $this->setupContainerForLogger(LogChannel::CONSOLE);
-
- $this->setupLegacyServiceLocator();
-
- $this->registerErrorHandler();
-
- $this->registerTemplateEngine();
-
- (new \Friendica\Core\Console($this->container, $argv))->execute();
- }
-
- private function setupContainerForAddons(): void
- {
- /** @var \Friendica\Core\Addon\Capability\ICanLoadAddons $addonLoader */
- $addonLoader = $this->container->create(\Friendica\Core\Addon\Capability\ICanLoadAddons::class);
-
- $this->container = $this->container->addRules($addonLoader->getActiveAddonConfig('dependencies'));
- }
-
- private function setupContainerForLogger(string $logChannel): void
- {
- $this->container = $this->container->addRule(LoggerInterface::class, [
- 'constructParams' => [$logChannel],
- ]);
- }
-
- private function setupLegacyServiceLocator(): void
- {
- DI::init($this->container);
- }
-
- private function registerErrorHandler(): void
- {
- \Friendica\Core\Logger\Handler\ErrorHandler::register($this->container->create(LoggerInterface::class));
- }
-
private function registerTemplateEngine(): void
{
Renderer::registerTemplateEngine('Friendica\Render\FriendicaSmartyEngine');
--- /dev/null
+<?php
+
+namespace Friendica\Console;
+
+use Asika\SimpleConsole\Console;
+use Friendica\Core\Logger\Capability\LogChannel;
+
+/**
+ * Abstract Console class for common settings
+ */
+abstract class AbstractConsole extends Console
+{
+ /**
+ * Overwrite this const in case you want to switch the LogChannel for this console command
+ *
+ * @var string
+ */
+ public const LOG_CHANNEL = LogChannel::class;
+}
namespace Friendica\Console;
+use Asika\SimpleConsole\CommandArgsException;
use Friendica\App\Mode;
use Friendica\Core\Config\Capability\IManageConfigValues;
-use Asika\SimpleConsole\Console;
use Friendica\Core\KeyValueStorage\Capability\IManageKeyValuePairs;
+use Friendica\Core\Logger\Capability\LogChannel;
use Friendica\Core\System;
use Friendica\Core\Update;
use Friendica\Core\Worker;
/**
* Console command for interacting with the daemon
*/
-final class Daemon extends Console
+final class Daemon extends AbstractConsole
{
+ public const LOG_CHANNEL = LogChannel::DAEMON;
+
private Mode $mode;
private IManageConfigValues $config;
private IManageKeyValuePairs $keyValue;
throw new RuntimeException("Friendica isn't properly installed yet");
}
+ $this->logger->warning('blah!');
+
$this->mode->setExecutor(Mode::DAEMON);
$this->config->reload();
$foreground = $this->getOption(['f', 'foreground']) ?? false;
if (empty($daemonMode)) {
- throw new RuntimeException("Please use either 'start', 'stop' or 'status'");
+ throw new CommandArgsException("Please use either 'start', 'stop' or 'status'");
}
$this->daemon->init($pidfile);
use Friendica\App\Mode;
use Friendica\Core\Addon;
use Friendica\Core\Config\Capability\IManageConfigValues;
-use Asika\SimpleConsole\Console;
use Friendica\Core\Hook;
use Friendica\Core\KeyValueStorage\Capability\IManageKeyValuePairs;
+use Friendica\Core\Logger\Capability\LogChannel;
use Friendica\Protocol\ATProtocol\Jetstream;
use Friendica\System\Daemon as SysDaemon;
use RuntimeException;
/**
* Console command for interacting with the daemon
*/
-final class JetstreamDaemon extends Console
+final class JetstreamDaemon extends AbstractConsole
{
+ public const LOG_CHANNEL = LogChannel::AUTH_JABBERED;
+
private Mode $mode;
private IManageConfigValues $config;
private IManageKeyValuePairs $keyValue;
namespace Friendica\Console;
use Friendica\App\Mode;
-use Asika\SimpleConsole\Console;
+use Friendica\Core\Logger\Capability\LogChannel;
use Friendica\Core\Update;
use Friendica\Core\Worker as CoreWorker;
use Friendica\Core\Worker\Repository\Process as ProcessRepository;
/**
* Console command for starting worker
*/
-final class Worker extends Console
+final class Worker extends AbstractConsole
{
+ public const LOG_CHANNEL = LogChannel::WORKER;
+
private Mode $mode;
private BasePath $basePath;
private ProcessRepository $processRepo;
use Dice\Dice;
use Friendica;
use Friendica\App;
+use Friendica\Core\Logger\Capability\LogChannel;
/**
* Description of Console
/**
* @var Dice The DI library
*/
- protected $dice;
+ protected $container;
protected function getHelp()
{
/**
* CliInput Friendica constructor.
*
- * @param Dice $dice The DI library
- * @param array $argv
+ * @param Container $container The Friendica container
*/
- public function __construct(Dice $dice, array $argv = null)
+ public function __construct(Container $container, array $argv = null)
{
parent::__construct($argv);
- $this->dice = $dice;
+ $this->container = $container;
+ }
+
+ public static function create(Container $container, array $argv = null): Console
+ {
+ return new static($container, $argv);
}
protected function doExecute(): int
$className = $this->subConsoles[$command];
+ if (is_subclass_of($className, Friendica\Console\AbstractConsole::class)) {
+ $this->container->setup($className::LOG_CHANNEL);
+ } else {
+ $this->container->setup(LogChannel::CONSOLE);
+ }
+
/** @var Console $subconsole */
- $subconsole = $this->dice->create($className, [$subargs]);
+ $subconsole = $this->container->create($className, [$subargs]);
foreach ($this->options as $name => $value) {
$subconsole->setOption($name, $value);
return $subconsole;
}
-
}
--- /dev/null
+<?php
+
+namespace Friendica\Core;
+
+use Dice\Dice;
+use Friendica\Core\Logger\Capability\LogChannel;
+use Friendica\DI;
+use Psr\Log\LoggerInterface;
+
+final class Container
+{
+ private Dice $container;
+
+ protected function __construct(Dice $container)
+ {
+ $this->container = $container;
+ }
+
+ public static function fromDice(Dice $container): self {
+ return new static($container);
+ }
+
+ public function setup(string $logChannel = LogChannel::DEFAULT, bool $withTemplateEngine = true)
+ {
+ $this->setupContainerForAddons();
+ $this->setupContainerForLogger($logChannel);
+ $this->setupLegacyServiceLocator();
+ $this->registerErrorHandler();
+
+ if ($withTemplateEngine) {
+ $this->registerTemplateEngine();
+ }
+ }
+
+ public function create(string $name, array $args = [], array $share = []): object
+ {
+ return $this->container->create($name, $args, $share);
+ }
+
+ public function addRule(string $name, array $rule):void
+ {
+ $this->container = $this->container->addRule($name, $rule);
+ }
+
+ private function setupContainerForAddons(): void
+ {
+ /** @var \Friendica\Core\Addon\Capability\ICanLoadAddons $addonLoader */
+ $addonLoader = $this->container->create(\Friendica\Core\Addon\Capability\ICanLoadAddons::class);
+
+ $this->container = $this->container->addRules($addonLoader->getActiveAddonConfig('dependencies'));
+ }
+
+ private function setupContainerForLogger(string $logChannel): void
+ {
+ $this->container = $this->container->addRule(LoggerInterface::class, [
+ 'constructParams' => [$logChannel],
+ ]);
+ }
+
+ private function setupLegacyServiceLocator(): void
+ {
+ DI::init($this->container);
+ }
+
+ private function registerErrorHandler(): void
+ {
+ \Friendica\Core\Logger\Handler\ErrorHandler::register($this->container->create(LoggerInterface::class));
+ }
+
+ private function registerTemplateEngine(): void
+ {
+ Renderer::registerTemplateEngine('Friendica\Render\FriendicaSmartyEngine');
+ }
+}
$dice = $this->createMock(Dice::class);
$dice->expects($this->never())->method('create');
- $app = App::fromDice($dice);
+ $app = App::fromContainer($dice);
$this->assertInstanceOf(App::class, $app);
}