Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updating Monolog bridge code with PHP 8.1 features #612

Merged
merged 5 commits into from
Mar 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 4 additions & 15 deletions src/Bridge/Dotenv/src/Bootloader/DotenvBootloader.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
<?php

/**
* Spiral Framework.
*
* @license MIT
* @author Anton Titov (Wolfy-J)
*/

declare(strict_types=1);

namespace Spiral\DotEnv\Bootloader;
Expand All @@ -18,20 +11,16 @@

final class DotenvBootloader extends Bootloader
{
/**
* @param DirectoriesInterface $dirs
* @param EnvironmentInterface $env
*/
public function boot(DirectoriesInterface $dirs, EnvironmentInterface $env)
public function boot(DirectoriesInterface $dirs, EnvironmentInterface $env): void
{
$dotenvPath = $env->get('DOTENV_PATH', $dirs->get('root') . '.env');

if (!file_exists($dotenvPath)) {
if (!\file_exists($dotenvPath)) {
return;
}

$path = dirname($dotenvPath);
$file = basename($dotenvPath);
$path = \dirname($dotenvPath);
$file = \basename($dotenvPath);

foreach (Dotenv::createImmutable($path, $file)->load() as $key => $value) {
$env->set($key, $value);
Expand Down
24 changes: 6 additions & 18 deletions src/Bridge/Monolog/src/Bootloader/MonologBootloader.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
<?php

/**
* Spiral Framework.
*
* @license MIT
* @author Anton Titov (Wolfy-J)
*/

declare(strict_types=1);

namespace Spiral\Monolog\Bootloader;
Expand Down Expand Up @@ -37,12 +30,9 @@ final class MonologBootloader extends Bootloader implements Container\SingletonI
'log.rotate' => [self::class, 'logRotate'],
];

/** @var ConfiguratorInterface */
private $config;

public function __construct(ConfiguratorInterface $config)
{
$this->config = $config;
public function __construct(
private readonly ConfiguratorInterface $config
) {
}

public function boot(Container $container, FinalizerInterface $finalizer): void
Expand Down Expand Up @@ -75,14 +65,12 @@ public function boot(Container $container, FinalizerInterface $finalizer): void

public function addHandler(string $channel, HandlerInterface $handler): void
{
$name = MonologConfig::CONFIG;

if (!isset($this->config->getConfig($name)['handlers'][$channel])) {
$this->config->modify($name, new Append('handlers', $channel, []));
if (!isset($this->config->getConfig(MonologConfig::CONFIG)['handlers'][$channel])) {
$this->config->modify(MonologConfig::CONFIG, new Append('handlers', $channel, []));
}

$this->config->modify(
$name,
MonologConfig::CONFIG,
new Append(
'handlers.' . $channel,
null,
Expand Down
25 changes: 6 additions & 19 deletions src/Bridge/Monolog/src/Config/MonologConfig.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
<?php

/**
* Spiral Framework.
*
* @license MIT
* @author Anton Titov (Wolfy-J)
*/

declare(strict_types=1);

namespace Spiral\Monolog\Config;
Expand All @@ -26,19 +19,13 @@ final class MonologConfig extends InjectableConfig
'handlers' => [],
];

/**
* @return int
*/
public function getEventLevel(): int
{
return $this->config['globalLevel'] ?? Logger::DEBUG;
}

/**
* @param string $channel
* @return \Generator|Autowire[]
*
* @throws ConfigException
* @return \Generator<int, Autowire>
*/
public function getHandlers(string $channel): \Generator
{
Expand All @@ -47,14 +34,14 @@ public function getHandlers(string $channel): \Generator
}

foreach ($this->config['handlers'][$channel] as $handler) {
if (is_object($handler) && !$handler instanceof Autowire) {
if (\is_object($handler) && !$handler instanceof Autowire) {
yield $handler;
continue;
}

$wire = $this->wire($handler);
if (\is_null($wire)) {
throw new ConfigException("Invalid handler definition for channel `{$channel}`.");
throw new ConfigException(\sprintf('Invalid handler definition for channel `%s`.', $channel));
}

yield $wire;
Expand All @@ -75,20 +62,20 @@ public function getProcessors(string $channel): \Generator

$wire = $this->wire($processor);
if (\is_null($wire)) {
throw new ConfigException("Invalid processor definition for channel `{$channel}`.");
throw new ConfigException(\sprintf('Invalid processor definition for channel `%s`.', $channel));
}

yield $wire;
}
}

private function wire($definition): ?Autowire
private function wire(Autowire|string|array $definition): ?Autowire
{
if ($definition instanceof Autowire) {
return $definition;
}

if (is_string($definition)) {
if (\is_string($definition)) {
return new Autowire($definition);
}

Expand Down
27 changes: 3 additions & 24 deletions src/Bridge/Monolog/src/EventHandler.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
<?php

/**
* Spiral Framework.
*
* @license MIT
* @author Anton Titov (Wolfy-J)
*/

declare(strict_types=1);

namespace Spiral\Monolog;
Expand All @@ -18,40 +11,26 @@

final class EventHandler extends AbstractHandler
{
/** @var ListenerRegistryInterface */
private $listenerRegistry;

/**
* @param ListenerRegistryInterface $listenerRegistry
* @param int $level
* @param bool $bubble
*/
public function __construct(
ListenerRegistryInterface $listenerRegistry,
private readonly ListenerRegistryInterface $listenerRegistry,
int $level = Logger::DEBUG,
bool $bubble = true
) {
$this->listenerRegistry = $listenerRegistry;

parent::__construct($level, $bubble);
}

/**
* @param array $record
* @return bool
*/
public function handle(array $record): bool
{
$e = new LogEvent(
$record['datetime'],
$record['channel'],
strtolower(Logger::getLevelName($record['level'])),
\strtolower(Logger::getLevelName($record['level'])),
$record['message'],
$record['context']
);

foreach ($this->listenerRegistry->getListeners() as $listener) {
call_user_func($listener, $e);
$listener($e);
}

return true;
Expand Down
7 changes: 0 additions & 7 deletions src/Bridge/Monolog/src/Exception/ConfigException.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
<?php

/**
* Spiral Framework.
*
* @license MIT
* @author Anton Titov (Wolfy-J)
*/

declare(strict_types=1);

namespace Spiral\Monolog\Exception;
Expand Down
46 changes: 7 additions & 39 deletions src/Bridge/Monolog/src/LogFactory.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
<?php

/**
* Spiral Framework.
*
* @license MIT
* @author Anton Titov (Wolfy-J)
*/

declare(strict_types=1);

namespace Spiral\Monolog;
Expand All @@ -30,39 +23,20 @@ final class LogFactory implements LogsInterface, InjectorInterface, ResettableIn
// Default logger channel (supplied via injection)
public const DEFAULT = 'default';

/** @var MonologConfig */
private $config;

/** @var LoggerInterface */
private $default;

/** @var FactoryInterface */
private $factory;
private ?LoggerInterface $default = null;
private readonly HandlerInterface $eventHandler;

/** @var HandlerInterface|null */
private $eventHandler;

/**
* @param MonologConfig $config
* @param ListenerRegistryInterface $listenerRegistry
* @param FactoryInterface $factory
*/
public function __construct(
MonologConfig $config,
private readonly MonologConfig $config,
ListenerRegistryInterface $listenerRegistry,
FactoryInterface $factory
private readonly FactoryInterface $factory
) {
$this->config = $config;
$this->factory = $factory;
$this->eventHandler = new EventHandler($listenerRegistry, $config->getEventLevel());
}

/**
* @inheritdoc
*/
public function getLogger(string $channel = null): LoggerInterface
{
if ($channel === null || $channel == self::DEFAULT) {
if ($channel === null || $channel === self::DEFAULT) {
if ($this->default !== null) {
// we should use only one default logger per system
return $this->default;
Expand All @@ -82,16 +56,13 @@ public function getLogger(string $channel = null): LoggerInterface
);
}

/**
* @inheritdoc
*/
public function createInjection(\ReflectionClass $class, string $context = null)
public function createInjection(\ReflectionClass $class, string $context = null): LoggerInterface
{
// always return default logger as injection
return $this->getLogger();
}

public function reset()
public function reset(): void
{
if ($this->default instanceof ResettableInterface) {
$this->default->reset();
Expand All @@ -101,8 +72,6 @@ public function reset()
/**
* Get list of channel specific handlers.
*
* @param string $channel
* @return array
*
* @throws ConfigException
*/
Expand Down Expand Up @@ -132,7 +101,6 @@ protected function getHandlers(string $channel): array
/**
* Get list of channel specific log processors.
*
* @param string $channel
* @return callable[]
*/
protected function getProcessors(string $channel): array
Expand Down