Skip to content

Commit

Permalink
Move Container logic into own class
Browse files Browse the repository at this point in the history
  • Loading branch information
nupplaphil committed Jan 5, 2025
1 parent 70070b4 commit 5b0ab3e
Show file tree
Hide file tree
Showing 14 changed files with 147 additions and 88 deletions.
3 changes: 2 additions & 1 deletion bin/auth_ejabberd.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@

$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();
5 changes: 2 additions & 3 deletions bin/console.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,5 @@

$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();
5 changes: 2 additions & 3 deletions bin/daemon.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,8 @@

$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();
5 changes: 2 additions & 3 deletions bin/jetstream.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,8 @@

$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();
5 changes: 2 additions & 3 deletions bin/worker.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,8 @@

$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();
3 changes: 2 additions & 1 deletion index.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

$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);
67 changes: 8 additions & 59 deletions src/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
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;
Expand Down Expand Up @@ -59,13 +60,13 @@ class App
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;

Expand Down Expand Up @@ -120,26 +121,20 @@ public static function fromDice(Dice $dice): self
*/
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);
Expand Down Expand Up @@ -175,13 +170,7 @@ public function processRequest(ServerRequestInterface $request, float $start_tim

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);
Expand All @@ -198,46 +187,6 @@ public function processEjabberd(): void
}
}

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');
Expand Down
19 changes: 19 additions & 0 deletions src/Console/AbstractConsole.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?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;
}
11 changes: 8 additions & 3 deletions src/Console/Daemon.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@

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;
Expand All @@ -26,8 +27,10 @@
/**
* 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;
Expand Down Expand Up @@ -98,6 +101,8 @@ protected function doExecute()
throw new RuntimeException("Friendica isn't properly installed yet");
}

$this->logger->warning('blah!');

$this->mode->setExecutor(Mode::DAEMON);

$this->config->reload();
Expand All @@ -120,7 +125,7 @@ protected function doExecute()
$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);
Expand Down
6 changes: 4 additions & 2 deletions src/Console/JetstreamDaemon.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,20 @@
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;
Expand Down
6 changes: 4 additions & 2 deletions src/Console/Worker.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
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;
Expand All @@ -19,8 +19,10 @@
/**
* 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;
Expand Down
24 changes: 17 additions & 7 deletions src/Core/Console.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Dice\Dice;
use Friendica;
use Friendica\App;
use Friendica\Core\Logger\Capability\LogChannel;

/**
* Description of Console
Expand All @@ -23,7 +24,7 @@ class Console extends \Asika\SimpleConsole\Console
/**
* @var Dice The DI library
*/
protected $dice;
protected $container;

protected function getHelp()
{
Expand Down Expand Up @@ -106,14 +107,18 @@ 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
Expand Down Expand Up @@ -172,14 +177,19 @@ private function getSubConsole($command)

$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;
}

}
Loading

0 comments on commit 5b0ab3e

Please sign in to comment.