Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1102: Expose LoggerChannel attribute
- Loading branch information
Showing
5 changed files
with
203 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Spiral\Logger\Attribute; | ||
|
||
use Psr\Log\LoggerInterface; | ||
|
||
/** | ||
* Used to specify the channel name for the logger when it injected as {@see LoggerInterface} on auto-wiring. | ||
* | ||
* Note: {@see \Spiral\Logger\LoggerInjector} should be registered in the container to support this attribute. | ||
*/ | ||
#[\Attribute(\Attribute::TARGET_PARAMETER)] | ||
final class LoggerChannel | ||
{ | ||
/** | ||
* @param non-empty-string $name | ||
*/ | ||
public function __construct(public readonly string $name) | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Spiral\Logger\Bootloader; | ||
|
||
use Psr\Log\LoggerInterface; | ||
use Spiral\Boot\Bootloader\Bootloader; | ||
use Spiral\Core\Container; | ||
use Spiral\Logger\Attribute\LoggerChannel; | ||
use Spiral\Logger\LogFactory; | ||
use Spiral\Logger\LoggerInjector; | ||
use Spiral\Logger\LogsInterface; | ||
use Spiral\Logger\NullLogger; | ||
|
||
/** | ||
* Register {@see LoggerInterface} injector with support for {@see LoggerChannel} attribute. | ||
* Register default {@see LogsInterface} implementation that produces {@see NullLogger}. | ||
*/ | ||
final class LoggerBootloader extends Bootloader | ||
{ | ||
protected const SINGLETONS = [ | ||
LogsInterface::class => LogFactory::class, | ||
]; | ||
|
||
public function init(Container $container): void | ||
{ | ||
$container->bindInjector(LoggerInterface::class, LoggerInjector::class); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Spiral\Logger; | ||
|
||
use Psr\Log\LoggerInterface; | ||
use Spiral\Core\Container\InjectorInterface; | ||
use Spiral\Logger\Attribute\LoggerChannel; | ||
|
||
/** | ||
* Container injector for {@see LoggerInterface}. | ||
* Supports {@see LoggerChannel} attribute. | ||
* | ||
* @implements InjectorInterface<LoggerInterface> | ||
*/ | ||
final class LoggerInjector implements InjectorInterface | ||
{ | ||
public const DEFAULT_CHANNEL = 'default'; | ||
|
||
public function __construct( | ||
private readonly LogsInterface $factory, | ||
) { | ||
} | ||
|
||
/** | ||
* @param \ReflectionParameter|string|null $context may use extended context if possible. | ||
*/ | ||
public function createInjection( | ||
\ReflectionClass $class, | ||
\ReflectionParameter|null|string $context = null, | ||
): LoggerInterface { | ||
$channel = \is_object($context) ? $this->extractChannelAttribute($context) : null; | ||
|
||
if ($channel === null) { | ||
/** | ||
* Array of flags to check if the logger allows null argument | ||
* | ||
* @var array<class-string<LogsInterface>, bool> $cache | ||
*/ | ||
static $cache = []; | ||
|
||
$cache[$this->factory::class] = (new \ReflectionMethod($this->factory, 'getLogger')) | ||
->getParameters()[0]->allowsNull(); | ||
|
||
$channel = $cache[$this->factory::class] ? null : self::DEFAULT_CHANNEL; | ||
} | ||
|
||
return $this->factory->getLogger($channel); | ||
} | ||
|
||
/** | ||
* @return non-empty-string|null | ||
*/ | ||
private function extractChannelAttribute(\ReflectionParameter $parameter): ?string | ||
{ | ||
/** @var \ReflectionAttribute<LoggerChannel>[] $attributes */ | ||
$attributes = $parameter->getAttributes(LoggerChannel::class); | ||
|
||
return $attributes[0]?->newInstance()->name; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters