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
|
|
|
public function __construct(Aliases $aliases) |
27
|
|
|
{ |
28
|
|
|
$this->aliases = $aliases; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function withConfig(array $config): self |
32
|
|
|
{ |
33
|
|
|
$files = $config['files'] ?? []; |
34
|
|
|
if (!is_array($files)) { |
35
|
|
|
throw new InvalidArgumentException('The "files" parameter must be an array.'); |
36
|
|
|
} |
37
|
|
|
if (count($files) === 0) { |
38
|
|
|
throw new InvalidArgumentException('Schema file list is not set.'); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
$strict = $config['strict'] ?? $this->strict; |
42
|
|
|
if (!is_bool($strict)) { |
43
|
|
|
throw new InvalidArgumentException('The "strict" parameter must be a boolean.'); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
$files = array_map( |
47
|
|
|
function ($file) { |
48
|
|
|
if (!is_string($file)) { |
49
|
|
|
throw new InvalidArgumentException('The "files" parameter must contain string values.'); |
50
|
|
|
} |
51
|
|
|
return $this->aliases->get($file); |
52
|
|
|
}, |
53
|
|
|
$files |
54
|
|
|
); |
55
|
|
|
|
56
|
|
|
$new = clone $this; |
57
|
|
|
$new->files = $files; |
58
|
|
|
$new->strict = $strict; |
59
|
|
|
return $new; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function read(): ?array |
63
|
|
|
{ |
64
|
|
|
$schema = null; |
65
|
|
|
|
66
|
|
|
foreach ($this->files as $file) { |
67
|
|
|
if (is_file($file)) { |
68
|
|
|
$schema = $schema ?? []; |
69
|
|
|
foreach (require $file as $role => $definition) { |
70
|
|
|
if (array_key_exists($role, $schema)) { |
71
|
|
|
throw new DuplicateRoleException($role); |
72
|
|
|
} |
73
|
|
|
$schema[$role] = $definition; |
74
|
|
|
} |
75
|
|
|
} elseif ($this->strict) { |
76
|
|
|
throw new SchemaFileNotFoundException($file); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return $schema; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function write(array $schema): bool |
84
|
|
|
{ |
85
|
|
|
return false; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function clear(): bool |
89
|
|
|
{ |
90
|
|
|
return false; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function isWritable(): bool |
94
|
|
|
{ |
95
|
|
|
return false; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function isReadable(): bool |
99
|
|
|
{ |
100
|
|
|
return true; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|