|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yiisoft\Db\Factory; |
|
6
|
|
|
|
|
7
|
|
|
use Psr\Container\ContainerInterface; |
|
8
|
|
|
use Psr\Log\LoggerInterface; |
|
9
|
|
|
use RuntimeException; |
|
10
|
|
|
use Yiisoft\Factory\Definitions\DefinitionInterface; |
|
11
|
|
|
use Yiisoft\Factory\Definitions\Normalizer; |
|
12
|
|
|
use Yiisoft\Factory\Factory; |
|
13
|
|
|
|
|
14
|
|
|
final class LoggerFactory extends Factory |
|
15
|
|
|
{ |
|
16
|
|
|
private static ?ContainerInterface $container = null; |
|
17
|
|
|
private static ?DefinitionInterface $definition = null; |
|
18
|
|
|
private static Factory $loggerFactory; |
|
19
|
|
|
|
|
20
|
|
|
private function __construct(ContainerInterface $container = null) |
|
21
|
|
|
{ |
|
22
|
|
|
self::$container = $container; |
|
23
|
|
|
self::$definition = Normalizer::normalize(LoggerInterface::class); |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
public static function initialize(ContainerInterface $container = null): void |
|
27
|
|
|
{ |
|
28
|
|
|
self::$loggerFactory = new self($container); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Creates a `LoggerInterface` instance. |
|
33
|
|
|
* |
|
34
|
|
|
* @throws RuntimeException If the created object is not an instance of the `LoggerInterface`. |
|
35
|
|
|
* |
|
36
|
|
|
* @return LoggerInterface The `LoggerInterface` instance. |
|
37
|
|
|
* |
|
38
|
|
|
* @psalm-suppress RedundantConditionGivenDocblockType |
|
39
|
|
|
* @psalm-suppress DocblockTypeContradiction |
|
40
|
|
|
*/ |
|
41
|
|
|
public static function run(): LoggerInterface |
|
42
|
|
|
{ |
|
43
|
|
|
$logger = self::$definition->resolve(self::$container); |
|
|
|
|
|
|
44
|
|
|
|
|
45
|
|
|
if (!($logger instanceof LoggerInterface)) { |
|
46
|
|
|
throw new RuntimeException(sprintf( |
|
47
|
|
|
'The "%s" is not an instance of the "Psr\Log\LoggerInterface".', |
|
48
|
|
|
(is_object($logger) ? get_class($logger) : gettype($logger)) |
|
49
|
|
|
)); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
return $logger; |
|
53
|
|
|
} |
|
54
|
|
|
} |
|
55
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.