|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yiisoft\Yii\Cycle\Schema\Provider; |
|
6
|
|
|
|
|
7
|
|
|
use InvalidArgumentException; |
|
8
|
|
|
use Yiisoft\Aliases\Aliases; |
|
9
|
|
|
use Yiisoft\Yii\Cycle\Exception\DuplicateRoleException; |
|
10
|
|
|
use Yiisoft\Yii\Cycle\Exception\SchemaFileNotFoundException; |
|
11
|
|
|
use Yiisoft\Yii\Cycle\Schema\SchemaProviderInterface; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Be careful, using this class may be insecure. |
|
15
|
|
|
*/ |
|
16
|
|
|
final class FromFilesSchemaProvider implements SchemaProviderInterface |
|
17
|
|
|
{ |
|
18
|
|
|
/** @var array Schema files */ |
|
19
|
|
|
private array $files = []; |
|
20
|
|
|
|
|
21
|
|
|
/** @var bool Throw exception if file not found */ |
|
22
|
|
|
private bool $strict = false; |
|
23
|
|
|
|
|
24
|
|
|
private Aliases $aliases; |
|
25
|
|
|
|
|
26
|
19 |
|
public function __construct(Aliases $aliases) |
|
27
|
|
|
{ |
|
28
|
19 |
|
$this->aliases = $aliases; |
|
29
|
19 |
|
} |
|
30
|
|
|
|
|
31
|
16 |
|
public function withConfig(array $config): self |
|
32
|
|
|
{ |
|
33
|
16 |
|
$files = $config['files'] ?? []; |
|
34
|
16 |
|
if (!is_array($files)) { |
|
35
|
1 |
|
throw new InvalidArgumentException('The "files" parameter must be an array.'); |
|
36
|
|
|
} |
|
37
|
15 |
|
if (count($files) === 0) { |
|
38
|
2 |
|
throw new InvalidArgumentException('Schema file list is not set.'); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
13 |
|
$strict = $config['strict'] ?? $this->strict; |
|
42
|
13 |
|
if (!is_bool($strict)) { |
|
43
|
1 |
|
throw new InvalidArgumentException('The "strict" parameter must be a boolean.'); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
12 |
|
$files = array_map( |
|
47
|
12 |
|
function ($file) { |
|
48
|
12 |
|
if (!is_string($file)) { |
|
49
|
5 |
|
throw new InvalidArgumentException('The "files" parameter must contain string values.'); |
|
50
|
|
|
} |
|
51
|
7 |
|
return $this->aliases->get($file); |
|
52
|
12 |
|
}, |
|
53
|
|
|
$files |
|
54
|
|
|
); |
|
55
|
|
|
|
|
56
|
7 |
|
$new = clone $this; |
|
57
|
7 |
|
$new->files = $files; |
|
58
|
7 |
|
$new->strict = $strict; |
|
59
|
7 |
|
return $new; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
9 |
|
public function read(?SchemaProviderInterface $nextProvider = null): ?array |
|
63
|
|
|
{ |
|
64
|
9 |
|
$schema = null; |
|
65
|
9 |
|
foreach ($this->files as $file) { |
|
66
|
7 |
|
if (is_file($file)) { |
|
67
|
6 |
|
$schema = $schema ?? []; |
|
68
|
6 |
|
foreach (require $file as $role => $definition) { |
|
69
|
5 |
|
if (array_key_exists($role, $schema)) { |
|
70
|
1 |
|
throw new DuplicateRoleException($role); |
|
71
|
|
|
} |
|
72
|
5 |
|
$schema[$role] = $definition; |
|
73
|
|
|
} |
|
74
|
3 |
|
} elseif ($this->strict) { |
|
75
|
1 |
|
throw new SchemaFileNotFoundException($file); |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
7 |
|
return $schema !== null || $nextProvider === null ? $schema : $nextProvider->read(); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
1 |
|
public function clear(): bool |
|
83
|
|
|
{ |
|
84
|
1 |
|
return false; |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|