1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Yii\Cycle\Schema\Provider; |
6
|
|
|
|
7
|
|
|
use Cycle\ORM\Schema; |
8
|
|
|
use RuntimeException; |
9
|
|
|
use Yiisoft\Aliases\Aliases; |
10
|
|
|
use Yiisoft\Yii\Cycle\Schema\Converter\SchemaToPHP; |
11
|
|
|
use Yiisoft\Yii\Cycle\Schema\SchemaProviderInterface; |
12
|
|
|
|
13
|
|
|
final class PhpFileSchemaProvider implements SchemaProviderInterface |
14
|
|
|
{ |
15
|
|
|
public const MODE_READ_AND_WRITE = 0; |
16
|
|
|
public const MODE_WRITE_ONLY = 1; |
17
|
|
|
|
18
|
|
|
private string $file = ''; |
19
|
|
|
private int $mode = self::MODE_READ_AND_WRITE; |
20
|
|
|
|
21
|
|
|
private Aliases $aliases; |
22
|
|
|
|
23
|
9 |
|
public function __construct(Aliases $aliases) |
24
|
|
|
{ |
25
|
9 |
|
$this->aliases = $aliases; |
26
|
9 |
|
} |
27
|
|
|
|
28
|
8 |
|
public function withConfig(array $config): self |
29
|
|
|
{ |
30
|
8 |
|
$new = clone $this; |
31
|
|
|
|
32
|
|
|
// required option |
33
|
8 |
|
if ($this->file === '' && !array_key_exists('file', $config)) { |
34
|
1 |
|
throw new \InvalidArgumentException('The "file" parameter is required.'); |
35
|
|
|
} |
36
|
7 |
|
$new->file = $this->aliases->get($config['file']); |
37
|
|
|
|
38
|
7 |
|
$new->mode = $config['mode'] ?? $this->mode; |
39
|
|
|
|
40
|
7 |
|
return $new; |
41
|
|
|
} |
42
|
|
|
|
43
|
3 |
|
public function read(): ?array |
44
|
|
|
{ |
45
|
3 |
|
if ($this->mode === self::MODE_WRITE_ONLY) { |
46
|
1 |
|
throw new RuntimeException(__CLASS__ . ' can not read schema.'); |
47
|
|
|
} |
48
|
2 |
|
if (!is_file($this->file)) { |
49
|
2 |
|
return null; |
50
|
|
|
} |
51
|
1 |
|
return include $this->file; |
52
|
|
|
} |
53
|
|
|
|
54
|
1 |
|
public function write(array $schema): bool |
55
|
|
|
{ |
56
|
1 |
|
if (basename($this->file) === '') { |
57
|
|
|
throw new RuntimeException('The "file" parameter must not be empty.'); |
58
|
|
|
} |
59
|
1 |
|
$dirname = dirname($this->file); |
60
|
1 |
|
if ($dirname !== '' && !is_dir($dirname)) { |
61
|
|
|
mkdir($dirname, 0777, true); |
62
|
|
|
} |
63
|
|
|
|
64
|
1 |
|
$content = (new SchemaToPHP(new Schema($schema)))->convert(); |
65
|
1 |
|
file_put_contents($this->file, $content, LOCK_EX); |
66
|
1 |
|
return true; |
67
|
|
|
} |
68
|
|
|
|
69
|
3 |
|
private function removeFile(): void |
70
|
|
|
{ |
71
|
3 |
|
if (!file_exists($this->file)) { |
72
|
1 |
|
return; |
73
|
|
|
} |
74
|
2 |
|
if (!is_file($this->file)) { |
75
|
1 |
|
throw new RuntimeException("`$this->file` is not a file."); |
76
|
|
|
} |
77
|
1 |
|
if (!is_writable($this->file)) { |
78
|
|
|
throw new RuntimeException("File `$this->file` is not writeable."); |
79
|
|
|
} |
80
|
1 |
|
unlink($this->file); |
81
|
1 |
|
} |
82
|
|
|
|
83
|
3 |
|
public function clear(): bool |
84
|
|
|
{ |
85
|
|
|
try { |
86
|
3 |
|
$this->removeFile(); |
87
|
1 |
|
} catch (\Throwable $e) { |
88
|
1 |
|
return false; |
89
|
|
|
} |
90
|
2 |
|
return true; |
91
|
|
|
} |
92
|
|
|
|
93
|
2 |
|
public function isWritable(): bool |
94
|
|
|
{ |
95
|
2 |
|
return true; |
96
|
|
|
} |
97
|
|
|
|
98
|
2 |
|
public function isReadable(): bool |
99
|
|
|
{ |
100
|
2 |
|
return $this->mode !== self::MODE_WRITE_ONLY; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|