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