mirror of
https://github.com/friendica/friendica
synced 2025-04-27 15:10:11 +00:00
Move Container logic into own class
This commit is contained in:
parent
6d77eb9b10
commit
40fbb02149
14 changed files with 147 additions and 88 deletions
|
@ -10,6 +10,7 @@ namespace Friendica\Core;
|
|||
use Dice\Dice;
|
||||
use Friendica;
|
||||
use Friendica\App;
|
||||
use Friendica\Core\Logger\Capability\LogChannel;
|
||||
|
||||
/**
|
||||
* Description of Console
|
||||
|
@ -23,7 +24,7 @@ class Console extends \Asika\SimpleConsole\Console
|
|||
/**
|
||||
* @var Dice The DI library
|
||||
*/
|
||||
protected $dice;
|
||||
protected $container;
|
||||
|
||||
protected function getHelp()
|
||||
{
|
||||
|
@ -106,14 +107,18 @@ HELP;
|
|||
/**
|
||||
* 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
|
||||
|
@ -172,8 +177,14 @@ HELP;
|
|||
|
||||
$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);
|
||||
|
@ -181,5 +192,4 @@ HELP;
|
|||
|
||||
return $subconsole;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
74
src/Core/Container.php
Normal file
74
src/Core/Container.php
Normal file
|
@ -0,0 +1,74 @@
|
|||
<?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');
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue