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