|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yiisoft\Db\Factory; |
|
6
|
|
|
|
|
7
|
|
|
use Psr\Container\ContainerInterface; |
|
8
|
|
|
use RuntimeException; |
|
9
|
|
|
use Yiisoft\Factory\Definitions\DefinitionInterface; |
|
10
|
|
|
use Yiisoft\Factory\Definitions\Normalizer; |
|
11
|
|
|
use Yiisoft\Factory\Factory; |
|
12
|
|
|
use Yiisoft\Profiler\ProfilerInterface; |
|
13
|
|
|
|
|
14
|
|
|
final class ProfilerFactory extends Factory |
|
15
|
|
|
{ |
|
16
|
|
|
private static ?ContainerInterface $container = null; |
|
17
|
|
|
private static ?DefinitionInterface $definition = null; |
|
18
|
|
|
private static Factory $profilerFactory; |
|
19
|
|
|
|
|
20
|
|
|
private function __construct(ContainerInterface $container = null) |
|
21
|
|
|
{ |
|
22
|
|
|
self::$container = $container; |
|
23
|
|
|
self::$definition = Normalizer::normalize(ProfilerInterface::class); |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
public static function initialize(ContainerInterface $container = null): void |
|
27
|
|
|
{ |
|
28
|
|
|
self::$profilerFactory = new self($container); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Creates a `ProfilerInterface` instance. |
|
33
|
|
|
* |
|
34
|
|
|
* @throws RuntimeException If the created object is not an instance of the `ProfilerInterface`. |
|
35
|
|
|
* |
|
36
|
|
|
* @return ProfilerInterface The `ProfilerInterface` instance. |
|
37
|
|
|
* |
|
38
|
|
|
* @psalm-suppress RedundantConditionGivenDocblockType |
|
39
|
|
|
* @psalm-suppress DocblockTypeContradiction |
|
40
|
|
|
*/ |
|
41
|
|
|
public static function run(): ProfilerInterface |
|
42
|
|
|
{ |
|
43
|
|
|
$profiler = self::$definition->resolve(self::$container); |
|
|
|
|
|
|
44
|
|
|
|
|
45
|
|
|
if (!($profiler instanceof ProfilerInterface)) { |
|
46
|
|
|
throw new RuntimeException(sprintf( |
|
47
|
|
|
'The "%s" is not an instance of the "Psr\Log\LoggerInterface".', |
|
48
|
|
|
(is_object($profiler) ? get_class($profiler) : gettype($profiler)) |
|
49
|
|
|
)); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
return $profiler; |
|
53
|
|
|
} |
|
54
|
|
|
} |
|
55
|
|
|
|