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 RuntimeException; |
9
|
|
|
use SplFixedArray; |
10
|
|
|
use Yiisoft\Yii\Cycle\Exception\CumulativeException; |
11
|
|
|
use Yiisoft\Yii\Cycle\Schema\SchemaProviderInterface; |
12
|
|
|
|
13
|
|
|
abstract class BaseProviderCollector implements SchemaProviderInterface |
14
|
|
|
{ |
15
|
|
|
protected const IS_SEQUENCE_PIPELINE = true; |
16
|
|
|
|
17
|
|
|
/** @var SplFixedArray<DeferredSchemaProviderDecorator>|null */ |
18
|
|
|
protected ?SplFixedArray $providers = null; |
19
|
|
|
private ContainerInterface $container; |
20
|
|
|
|
21
|
44 |
|
public function __construct(ContainerInterface $container) |
22
|
|
|
{ |
23
|
44 |
|
$this->container = $container; |
24
|
44 |
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @return $this |
28
|
|
|
* |
29
|
|
|
* @psalm-immutable |
30
|
|
|
*/ |
31
|
40 |
|
public function withConfig(array $config): self |
32
|
|
|
{ |
33
|
40 |
|
$new = clone $this; |
34
|
40 |
|
$new->providers = $this->createSequence($new->container, $config); |
35
|
40 |
|
return $new; |
36
|
|
|
} |
37
|
|
|
|
38
|
13 |
|
public function clear(): bool |
39
|
|
|
{ |
40
|
13 |
|
if ($this->providers === null) { |
41
|
2 |
|
throw new RuntimeException(self::class . ' is not configured.'); |
42
|
|
|
} |
43
|
11 |
|
$exceptions = []; |
44
|
11 |
|
$result = false; |
45
|
11 |
|
foreach ($this->providers as $provider) { |
46
|
|
|
try { |
47
|
9 |
|
$result = $provider->clear() || $result; |
48
|
2 |
|
} catch (\Throwable $e) { |
49
|
2 |
|
$exceptions[] = $e; |
50
|
|
|
} |
51
|
|
|
} |
52
|
11 |
|
if (count($exceptions)) { |
53
|
2 |
|
throw new CumulativeException(...$exceptions); |
54
|
|
|
} |
55
|
9 |
|
return $result; |
56
|
|
|
} |
57
|
|
|
|
58
|
40 |
|
private function createSequence(ContainerInterface $container, array $providers): SplFixedArray |
59
|
|
|
{ |
60
|
40 |
|
$size = count($providers); |
61
|
40 |
|
$stack = new SplFixedArray($size); |
62
|
40 |
|
$nextProvider = null; |
63
|
40 |
|
foreach (array_reverse($providers) as $key => $definition) { |
64
|
32 |
|
$config = []; |
65
|
32 |
|
if (is_array($definition)) { |
66
|
6 |
|
if (is_string($key)) { |
67
|
3 |
|
$config = $definition; |
68
|
3 |
|
$definition = $key; |
69
|
|
|
} else { |
70
|
3 |
|
$config = $definition[1] ?? []; |
71
|
3 |
|
$definition = $definition[0]; |
72
|
|
|
} |
73
|
|
|
} |
74
|
32 |
|
$nextProvider = (new DeferredSchemaProviderDecorator( |
75
|
32 |
|
$container, |
76
|
|
|
$definition, |
77
|
32 |
|
static::IS_SEQUENCE_PIPELINE ? $nextProvider : null |
78
|
32 |
|
))->withConfig($config); |
79
|
32 |
|
$stack[--$size] = $nextProvider; |
80
|
|
|
} |
81
|
40 |
|
return $stack; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|