|
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
|
|
|
public function __construct(ContainerInterface $container, $provider, ?SchemaProviderInterface $nextProvider) |
|
26
|
|
|
{ |
|
27
|
|
|
$this->provider = $provider; |
|
28
|
|
|
$this->container = $container; |
|
29
|
|
|
$this->nextProvider = $nextProvider; |
|
30
|
|
|
} |
|
31
|
|
|
/** |
|
32
|
|
|
* @psalm-suppress InvalidReturnType,InvalidReturnStatement |
|
33
|
|
|
*/ |
|
34
|
|
|
private function getProvider(): SchemaProviderInterface |
|
35
|
|
|
{ |
|
36
|
|
|
if ($this->resolved) { |
|
37
|
|
|
return $this->provider; |
|
|
|
|
|
|
38
|
|
|
} |
|
39
|
|
|
$provider = $this->provider; |
|
40
|
|
|
if (is_string($provider)) { |
|
41
|
|
|
$provider = $this->container->get($provider); |
|
42
|
|
|
} |
|
43
|
|
|
if (!$provider instanceof SchemaProviderInterface) { |
|
44
|
|
|
throw new BadDeclarationException('Provider', SchemaProviderInterface::class, $provider); |
|
45
|
|
|
} |
|
46
|
|
|
$this->provider = count($this->config) > 0 ? $provider->withConfig($this->config) : $provider; |
|
47
|
|
|
$this->resolved = true; |
|
48
|
|
|
return $this->provider; |
|
49
|
|
|
} |
|
50
|
|
|
public function withConfig(array $config): SchemaProviderInterface |
|
51
|
|
|
{ |
|
52
|
|
|
$provider = !$this->resolved && count($this->config) === 0 ? $this->provider : $this->getProvider(); |
|
53
|
|
|
$new = new self($this->container, $provider, $this->nextProvider); |
|
54
|
|
|
$new->config = $config; |
|
55
|
|
|
return $new; |
|
56
|
|
|
} |
|
57
|
|
|
public function read(?SchemaProviderInterface $nextProvider = null): ?array |
|
58
|
|
|
{ |
|
59
|
|
|
return $this->getProvider()->read($this->nextProvider); |
|
60
|
|
|
} |
|
61
|
|
|
public function clear(): bool |
|
62
|
|
|
{ |
|
63
|
|
|
return $this->getProvider()->clear(); |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|