|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yiisoft\Yii\Cycle\Schema\Provider; |
|
6
|
|
|
|
|
7
|
|
|
use Generator; |
|
8
|
|
|
use InvalidArgumentException; |
|
9
|
|
|
use Yiisoft\Aliases\Aliases; |
|
10
|
|
|
use Yiisoft\Yii\Cycle\Exception\SchemaFileNotFoundException; |
|
11
|
|
|
use Yiisoft\Yii\Cycle\Schema\Provider\Support\SchemaMerger; |
|
12
|
|
|
use Yiisoft\Yii\Cycle\Schema\SchemaProviderInterface; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Be careful, using this class may be insecure. |
|
16
|
|
|
*/ |
|
17
|
|
|
final class FromFilesSchemaProvider implements SchemaProviderInterface |
|
18
|
|
|
{ |
|
19
|
|
|
/** @var array Schema files */ |
|
20
|
|
|
private array $files = []; |
|
21
|
|
|
|
|
22
|
|
|
/** @var bool Throw exception if file not found */ |
|
23
|
|
|
private bool $strict = false; |
|
24
|
|
|
|
|
25
|
|
|
private Aliases $aliases; |
|
26
|
|
|
|
|
27
|
19 |
|
public function __construct(Aliases $aliases) |
|
28
|
|
|
{ |
|
29
|
19 |
|
$this->aliases = $aliases; |
|
30
|
19 |
|
} |
|
31
|
|
|
|
|
32
|
16 |
|
public function withConfig(array $config): self |
|
33
|
|
|
{ |
|
34
|
16 |
|
$files = $config['files'] ?? []; |
|
35
|
16 |
|
if (!is_array($files)) { |
|
36
|
1 |
|
throw new InvalidArgumentException('The "files" parameter must be an array.'); |
|
37
|
|
|
} |
|
38
|
15 |
|
if (count($files) === 0) { |
|
39
|
2 |
|
throw new InvalidArgumentException('Schema file list is not set.'); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
13 |
|
$strict = $config['strict'] ?? $this->strict; |
|
43
|
13 |
|
if (!is_bool($strict)) { |
|
44
|
1 |
|
throw new InvalidArgumentException('The "strict" parameter must be a boolean.'); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
12 |
|
$files = array_map( |
|
48
|
12 |
|
function ($file) { |
|
49
|
12 |
|
if (!is_string($file)) { |
|
50
|
5 |
|
throw new InvalidArgumentException('The "files" parameter must contain string values.'); |
|
51
|
|
|
} |
|
52
|
7 |
|
return $this->aliases->get($file); |
|
53
|
12 |
|
}, |
|
54
|
|
|
$files |
|
55
|
|
|
); |
|
56
|
|
|
|
|
57
|
7 |
|
$new = clone $this; |
|
58
|
7 |
|
$new->files = $files; |
|
59
|
7 |
|
$new->strict = $strict; |
|
60
|
7 |
|
return $new; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
9 |
|
public function read(?SchemaProviderInterface $nextProvider = null): ?array |
|
64
|
|
|
{ |
|
65
|
9 |
|
$schema = (new SchemaMerger())->merge(...$this->readFiles()); |
|
|
|
|
|
|
66
|
|
|
|
|
67
|
7 |
|
return $schema !== null || $nextProvider === null ? $schema : $nextProvider->read(); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
1 |
|
public function clear(): bool |
|
71
|
|
|
{ |
|
72
|
1 |
|
return false; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Read schema from each file |
|
77
|
|
|
* |
|
78
|
|
|
* @return Generator<int, array|null> |
|
79
|
|
|
*/ |
|
80
|
9 |
|
private function readFiles(): Generator |
|
81
|
|
|
{ |
|
82
|
9 |
|
foreach ($this->files as $file) { |
|
83
|
7 |
|
if (is_file($file)) { |
|
84
|
6 |
|
yield require $file; |
|
85
|
3 |
|
} elseif ($this->strict) { |
|
86
|
1 |
|
throw new SchemaFileNotFoundException($file); |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
8 |
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|
This check looks for function or method calls that always return null and whose return value is assigned to a variable.
The method
getObject()can return nothing but null, so it makes no sense to assign that value to a variable.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.