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