|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yiisoft\Db\Connection; |
|
6
|
|
|
|
|
7
|
|
|
use Psr\Log\LoggerInterface; |
|
8
|
|
|
use RuntimeException; |
|
9
|
|
|
use Yiisoft\Db\Cache\QueryCache; |
|
10
|
|
|
use Yiisoft\Db\Cache\SchemaCache; |
|
11
|
|
|
use Yiisoft\Factory\DependencyResolver; |
|
12
|
|
|
use Yiisoft\Factory\Exception\InvalidConfigException; |
|
13
|
|
|
use Yiisoft\Profiler\ProfilerInterface; |
|
14
|
|
|
|
|
15
|
|
|
final class LazyConnectionDependencies |
|
16
|
|
|
{ |
|
17
|
|
|
private DependencyResolver $dependencyResolver; |
|
18
|
|
|
private ?LoggerInterface $logger = null; |
|
19
|
|
|
private ?ProfilerInterface $profiler = null; |
|
20
|
|
|
private ?QueryCache $queryCache = null; |
|
21
|
|
|
private ?SchemaCache $schemaCache = null; |
|
22
|
|
|
|
|
23
|
3016 |
|
public function __construct(DependencyResolver $dependencyResolver) |
|
24
|
|
|
{ |
|
25
|
3016 |
|
$this->dependencyResolver = $dependencyResolver; |
|
26
|
3016 |
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Get `LoggerInterface` instance. |
|
30
|
|
|
* |
|
31
|
|
|
* @throws InvalidConfigException |
|
32
|
|
|
* |
|
33
|
|
|
* @psalm-suppress InvalidReturnStatement |
|
34
|
|
|
* @psalm-suppress InvalidReturnType |
|
35
|
|
|
* |
|
36
|
|
|
* @return LoggerInterface |
|
37
|
|
|
*/ |
|
38
|
1853 |
|
public function logger(): LoggerInterface |
|
39
|
|
|
{ |
|
40
|
1853 |
|
if ($this->logger !== null) { |
|
41
|
1853 |
|
return $this->logger; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
1853 |
|
$this->logger = $this->create(LoggerInterface::class); |
|
45
|
|
|
|
|
46
|
1853 |
|
return $this->logger; |
|
|
|
|
|
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Get `ProfilerInterface` instance. |
|
51
|
|
|
* |
|
52
|
|
|
* @throws InvalidConfigException |
|
53
|
|
|
* |
|
54
|
|
|
* @psalm-suppress InvalidReturnStatement |
|
55
|
|
|
* @psalm-suppress InvalidReturnType |
|
56
|
|
|
* |
|
57
|
|
|
* @return ProfilerInterface |
|
58
|
|
|
*/ |
|
59
|
1853 |
|
public function profiler(): ProfilerInterface |
|
60
|
|
|
{ |
|
61
|
1853 |
|
if ($this->profiler !== null) { |
|
62
|
1853 |
|
return $this->profiler; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
1853 |
|
$this->profiler = $this->create(ProfilerInterface::class); |
|
66
|
1853 |
|
return $this->profiler; |
|
|
|
|
|
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Get `QueryCache` instance. |
|
71
|
|
|
* |
|
72
|
|
|
* @throws InvalidConfigException |
|
73
|
|
|
* |
|
74
|
|
|
* @psalm-suppress InvalidReturnStatement |
|
75
|
|
|
* @psalm-suppress InvalidReturnType |
|
76
|
|
|
* |
|
77
|
|
|
* @return QueryCache |
|
78
|
|
|
*/ |
|
79
|
1697 |
|
public function queryCache(): QueryCache |
|
80
|
|
|
{ |
|
81
|
1697 |
|
if ($this->queryCache !== null) { |
|
82
|
1397 |
|
return $this->queryCache; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
1697 |
|
$this->queryCache = $this->create(QueryCache::class); |
|
86
|
1697 |
|
return $this->queryCache; |
|
|
|
|
|
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Get `SchemaCache` instance. |
|
91
|
|
|
* |
|
92
|
|
|
* @throws InvalidConfigException |
|
93
|
|
|
* |
|
94
|
|
|
* @psalm-suppress InvalidReturnStatement |
|
95
|
|
|
* @psalm-suppress InvalidReturnType |
|
96
|
|
|
* |
|
97
|
|
|
* @return SchemaCache |
|
98
|
|
|
*/ |
|
99
|
1627 |
|
public function schemaCache(): SchemaCache |
|
100
|
|
|
{ |
|
101
|
1627 |
|
if ($this->schemaCache !== null) { |
|
102
|
1567 |
|
return $this->schemaCache; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
1627 |
|
$this->schemaCache = $this->create(SchemaCache::class); |
|
106
|
1627 |
|
return $this->schemaCache; |
|
|
|
|
|
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Creates an instance of the specified class. |
|
111
|
|
|
* |
|
112
|
|
|
* @param string $class |
|
113
|
|
|
* |
|
114
|
|
|
* @throws InvalidConfigException |
|
115
|
|
|
* @throws RuntimeException If the created object is not an instance of the `LoggerInterface`. |
|
116
|
|
|
* |
|
117
|
|
|
* @return LoggerInterface|ProfilerInterface|QueryCache|SchemaCache The created instance. |
|
118
|
|
|
*/ |
|
119
|
1873 |
|
private function create(string $class): object |
|
120
|
|
|
{ |
|
121
|
1873 |
|
$instance = $this->dependencyResolver->get($class); |
|
122
|
|
|
|
|
123
|
1873 |
|
if (!($instance instanceof $class)) { |
|
124
|
|
|
throw new RuntimeException(sprintf( |
|
125
|
|
|
'The "%s" is not an instance of the "%s".', |
|
126
|
|
|
(is_object($instance) ? get_class($instance) : gettype($instance)), |
|
127
|
|
|
$class |
|
128
|
|
|
)); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
1873 |
|
return $instance; |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
|