mirror of
https://github.com/friendica/friendica
synced 2025-04-27 15:10:11 +00:00
Merge branch 'develop' into refactor-logger
This commit is contained in:
commit
b40db06ef3
17 changed files with 237 additions and 226 deletions
|
@ -9,7 +9,6 @@ namespace Friendica\Core;
|
|||
|
||||
use Friendica;
|
||||
use Friendica\App;
|
||||
use Friendica\Core\Logger\Capability\LogChannel;
|
||||
|
||||
/**
|
||||
* Description of Console
|
||||
|
@ -187,12 +186,6 @@ 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->container->create($className, [$subargs]);
|
||||
|
||||
|
|
|
@ -9,57 +9,11 @@ declare(strict_types=1);
|
|||
|
||||
namespace Friendica\Core;
|
||||
|
||||
use Dice\Dice;
|
||||
use Friendica\Core\Addon\Capability\ICanLoadAddons;
|
||||
use Friendica\Core\Logger\Capability\LogChannel;
|
||||
use Friendica\Core\Logger\Handler\ErrorHandler;
|
||||
use Friendica\DI;
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
||||
/**
|
||||
* Wrapper for the Dice class to make some basic setups
|
||||
* Dependency Injection Container
|
||||
*/
|
||||
class Container
|
||||
interface Container
|
||||
{
|
||||
private Dice $container;
|
||||
|
||||
protected function __construct(Dice $container)
|
||||
{
|
||||
$this->container = $container;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates an instance with Dice
|
||||
*
|
||||
* @param Dice $container
|
||||
*
|
||||
* @return self
|
||||
*/
|
||||
public static function fromDice(Dice $container): self
|
||||
{
|
||||
return new self($container);
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize the container with the given parameters
|
||||
*
|
||||
* @param string $logChannel The Log Channel of this call
|
||||
* @param bool $withTemplateEngine true, if the template engine should be set too
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function setup(string $logChannel = LogChannel::DEFAULT, bool $withTemplateEngine = true)
|
||||
{
|
||||
$this->setupContainerForAddons();
|
||||
$this->setupContainerForLogger($logChannel);
|
||||
$this->setupLegacyServiceLocator();
|
||||
$this->registerErrorHandler();
|
||||
|
||||
if ($withTemplateEngine) {
|
||||
$this->registerTemplateEngine();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a fully constructed object based on $name using $args and $share as constructor arguments if supplied
|
||||
* @param string $name name The name of the class to instantiate
|
||||
|
@ -69,10 +23,7 @@ class Container
|
|||
*
|
||||
* @see Dice::create()
|
||||
*/
|
||||
public function create(string $name, array $args = [], array $share = []): object
|
||||
{
|
||||
return $this->container->create($name, $args, $share);
|
||||
}
|
||||
public function create(string $name, array $args = [], array $share = []): object;
|
||||
|
||||
/**
|
||||
* Add a rule $rule to the class $name
|
||||
|
@ -81,38 +32,5 @@ class Container
|
|||
*
|
||||
* @see Dice::addRule()
|
||||
*/
|
||||
public function addRule(string $name, array $rule): void
|
||||
{
|
||||
$this->container = $this->container->addRule($name, $rule);
|
||||
}
|
||||
|
||||
private function setupContainerForAddons(): void
|
||||
{
|
||||
/** @var ICanLoadAddons $addonLoader */
|
||||
$addonLoader = $this->container->create(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
|
||||
{
|
||||
ErrorHandler::register($this->container->create(LoggerInterface::class));
|
||||
}
|
||||
|
||||
private function registerTemplateEngine(): void
|
||||
{
|
||||
Renderer::registerTemplateEngine('Friendica\Render\FriendicaSmartyEngine');
|
||||
}
|
||||
public function addRule(string $name, array $rule): void;
|
||||
}
|
||||
|
|
74
src/Core/DiceContainer.php
Normal file
74
src/Core/DiceContainer.php
Normal file
|
@ -0,0 +1,74 @@
|
|||
<?php
|
||||
|
||||
// Copyright (C) 2010-2024, the Friendica project
|
||||
// SPDX-FileCopyrightText: 2010-2024 the Friendica project
|
||||
//
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Friendica\Core;
|
||||
|
||||
use Dice\Dice;
|
||||
|
||||
/**
|
||||
* Wrapper for the Dice class to make some basic setups
|
||||
*/
|
||||
final class DiceContainer implements Container
|
||||
{
|
||||
public static function fromBasePath(string $basePath): self
|
||||
{
|
||||
$path = $basePath . '/static/dependencies.config.php';
|
||||
|
||||
$dice = (new Dice())->addRules(require($path));
|
||||
|
||||
return new self($dice);
|
||||
}
|
||||
|
||||
private Dice $container;
|
||||
|
||||
private function __construct(Dice $container)
|
||||
{
|
||||
$this->container = $container;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a fully constructed object based on $name using $args and $share as constructor arguments if supplied
|
||||
* @param string $name name The name of the class to instantiate
|
||||
* @param array $args An array with any additional arguments to be passed into the constructor upon instantiation
|
||||
* @param array $share a list of defined in shareInstances for objects higher up the object graph, should only be used internally
|
||||
* @return object A fully constructed object based on the specified input arguments
|
||||
*
|
||||
* @see Dice::create()
|
||||
*/
|
||||
public function create(string $name, array $args = [], array $share = []): object
|
||||
{
|
||||
return $this->container->create($name, $args, $share);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a rule $rule to the class $name
|
||||
* @param string $name The name of the class to add the rule for
|
||||
* @param array $rule The container can be fully configured using rules provided by associative arrays. See {@link https://r.je/dice.html#example3} for a description of the rules.
|
||||
*
|
||||
* @see Dice::addRule()
|
||||
*/
|
||||
public function addRule(string $name, array $rule): void
|
||||
{
|
||||
$this->container = $this->container->addRule($name, $rule);
|
||||
}
|
||||
|
||||
/**
|
||||
* Only used to inject Dice into DI class
|
||||
*
|
||||
* @see \Friendica\DI
|
||||
*
|
||||
* @internal
|
||||
*
|
||||
* @deprecated
|
||||
*/
|
||||
public function getDice(): Dice
|
||||
{
|
||||
return $this->container;
|
||||
}
|
||||
}
|
|
@ -10,7 +10,6 @@ namespace Friendica\Core;
|
|||
use Friendica\DI;
|
||||
use Friendica\Core\Logger\Type\WorkerLogger;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Psr\Log\LogLevel;
|
||||
|
||||
/**
|
||||
* Logger functions
|
||||
|
@ -191,21 +190,4 @@ class Logger
|
|||
{
|
||||
self::getInstance()->debug($message, $context);
|
||||
}
|
||||
|
||||
/**
|
||||
* An alternative logger for development.
|
||||
*
|
||||
* Works largely as log() but allows developers
|
||||
* to isolate particular elements they are targeting
|
||||
* personally without background noise
|
||||
*
|
||||
* @param string $message Message to log
|
||||
* @param string $level Logging level
|
||||
* @return void
|
||||
* @throws \Exception
|
||||
*/
|
||||
public static function devLog(string $message, string $level = LogLevel::DEBUG)
|
||||
{
|
||||
DI::devLogger()->log($level, $message);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue