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 Webmozart\Glob\Glob; |
10
|
|
|
use Webmozart\Glob\Iterator\GlobIterator; |
11
|
|
|
use Yiisoft\Aliases\Aliases; |
12
|
|
|
use Yiisoft\Yii\Cycle\Exception\SchemaFileNotFoundException; |
13
|
|
|
use Yiisoft\Yii\Cycle\Schema\Provider\Support\SchemaMerger; |
14
|
|
|
use Yiisoft\Yii\Cycle\Schema\SchemaProviderInterface; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Be careful, using this class may be insecure. |
18
|
|
|
*/ |
19
|
|
|
final class FromFilesSchemaProvider implements SchemaProviderInterface |
20
|
|
|
{ |
21
|
|
|
/** @var array Schema files */ |
22
|
|
|
private array $files = []; |
23
|
|
|
|
24
|
|
|
/** @var bool Throw exception if file not found */ |
25
|
|
|
private bool $strict = false; |
26
|
|
|
|
27
|
|
|
private Aliases $aliases; |
28
|
|
|
|
29
|
20 |
|
public function __construct(Aliases $aliases) |
30
|
|
|
{ |
31
|
20 |
|
$this->aliases = $aliases; |
32
|
|
|
} |
33
|
|
|
|
34
|
17 |
|
public function withConfig(array $config): self |
35
|
|
|
{ |
36
|
17 |
|
$files = $config['files'] ?? []; |
37
|
17 |
|
if (!is_array($files)) { |
38
|
1 |
|
throw new InvalidArgumentException('The "files" parameter must be an array.'); |
39
|
|
|
} |
40
|
16 |
|
if (count($files) === 0) { |
41
|
2 |
|
throw new InvalidArgumentException('Schema file list is not set.'); |
42
|
|
|
} |
43
|
|
|
|
44
|
14 |
|
$strict = $config['strict'] ?? $this->strict; |
45
|
14 |
|
if (!is_bool($strict)) { |
46
|
1 |
|
throw new InvalidArgumentException('The "strict" parameter must be a boolean.'); |
47
|
|
|
} |
48
|
|
|
|
49
|
13 |
|
$files = array_map( |
50
|
13 |
|
function ($file) { |
51
|
13 |
|
if (!is_string($file)) { |
52
|
5 |
|
throw new InvalidArgumentException('The "files" parameter must contain string values.'); |
53
|
|
|
} |
54
|
8 |
|
return $this->aliases->get($file); |
55
|
13 |
|
}, |
56
|
13 |
|
$files |
57
|
13 |
|
); |
58
|
|
|
|
59
|
8 |
|
$new = clone $this; |
60
|
8 |
|
$new->files = $files; |
61
|
8 |
|
$new->strict = $strict; |
62
|
8 |
|
return $new; |
63
|
|
|
} |
64
|
|
|
|
65
|
10 |
|
public function read(?SchemaProviderInterface $nextProvider = null): ?array |
66
|
|
|
{ |
67
|
10 |
|
$schema = (new SchemaMerger())->merge(...$this->readFiles()); |
|
|
|
|
68
|
|
|
|
69
|
8 |
|
return $schema !== null || $nextProvider === null ? $schema : $nextProvider->read(); |
70
|
|
|
} |
71
|
|
|
|
72
|
1 |
|
public function clear(): bool |
73
|
|
|
{ |
74
|
1 |
|
return false; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Read schema from each file |
79
|
|
|
* |
80
|
|
|
* @return Generator<int, array|null> |
81
|
|
|
*/ |
82
|
10 |
|
private function readFiles(): Generator |
83
|
|
|
{ |
84
|
10 |
|
foreach ($this->files as $path) { |
85
|
8 |
|
$path = \str_replace('\\', '/', $path); |
86
|
8 |
|
if (!Glob::isDynamic($path)) { |
87
|
7 |
|
yield $this->loadFile($path); |
88
|
7 |
|
continue; |
89
|
|
|
} |
90
|
1 |
|
foreach (new GlobIterator($path) as $file) { |
91
|
1 |
|
yield $this->loadFile($file); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
96
|
8 |
|
private function loadFile(string $path): ?array |
97
|
|
|
{ |
98
|
8 |
|
$isFile = is_file($path); |
99
|
|
|
|
100
|
8 |
|
if (!$isFile && $this->strict) { |
101
|
1 |
|
throw new SchemaFileNotFoundException($path); |
102
|
|
|
} |
103
|
|
|
|
104
|
8 |
|
return $isFile ? require $path : null; |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
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.