1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Yii\Cycle\Schema\Provider; |
6
|
|
|
|
7
|
|
|
use Psr\Container\ContainerInterface; |
8
|
|
|
use Yiisoft\Yii\Cycle\Exception\BadDeclarationException; |
9
|
|
|
use Yiisoft\Yii\Cycle\Schema\SchemaProviderInterface; |
10
|
|
|
|
11
|
|
|
final class DeferredSchemaProviderDecorator implements SchemaProviderInterface |
12
|
|
|
{ |
13
|
|
|
/** @var SchemaProviderInterface|string */ |
14
|
|
|
private $provider; |
15
|
|
|
private array $config = []; |
16
|
|
|
private ?SchemaProviderInterface $nextProvider; |
17
|
|
|
private bool $resolved = false; |
18
|
|
|
private ContainerInterface $container; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @param ContainerInterface $container |
22
|
|
|
* @param $provider |
23
|
|
|
* @param null|SchemaProviderInterface $nextProvider |
24
|
|
|
*/ |
25
|
12 |
|
public function __construct(ContainerInterface $container, $provider, ?SchemaProviderInterface $nextProvider) |
26
|
|
|
{ |
27
|
12 |
|
$this->provider = $provider; |
28
|
12 |
|
$this->container = $container; |
29
|
12 |
|
$this->nextProvider = $nextProvider; |
30
|
12 |
|
} |
31
|
|
|
/** |
32
|
|
|
* @psalm-suppress InvalidReturnType,InvalidReturnStatement |
33
|
|
|
*/ |
34
|
12 |
|
private function getProvider(): SchemaProviderInterface |
35
|
|
|
{ |
36
|
12 |
|
if ($this->resolved) { |
37
|
|
|
return $this->provider; |
|
|
|
|
38
|
|
|
} |
39
|
12 |
|
$provider = $this->provider; |
40
|
12 |
|
if (is_string($provider)) { |
41
|
5 |
|
$provider = $this->container->get($provider); |
42
|
|
|
} |
43
|
12 |
|
if (!$provider instanceof SchemaProviderInterface) { |
44
|
1 |
|
throw new BadDeclarationException('Provider', SchemaProviderInterface::class, $provider); |
45
|
|
|
} |
46
|
11 |
|
$this->provider = count($this->config) > 0 ? $provider->withConfig($this->config) : $provider; |
47
|
11 |
|
$this->resolved = true; |
48
|
11 |
|
return $this->provider; |
49
|
|
|
} |
50
|
12 |
|
public function withConfig(array $config): SchemaProviderInterface |
51
|
|
|
{ |
52
|
12 |
|
$provider = !$this->resolved && count($this->config) === 0 ? $this->provider : $this->getProvider(); |
53
|
12 |
|
$new = new self($this->container, $provider, $this->nextProvider); |
54
|
12 |
|
$new->config = $config; |
55
|
12 |
|
return $new; |
56
|
|
|
} |
57
|
9 |
|
public function read(?SchemaProviderInterface $nextProvider = null): ?array |
58
|
|
|
{ |
59
|
9 |
|
return $this->getProvider()->read($this->nextProvider); |
60
|
|
|
} |
61
|
3 |
|
public function clear(): bool |
62
|
|
|
{ |
63
|
3 |
|
return $this->getProvider()->clear(); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|