|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Graze\Sprout\Parser; |
|
4
|
|
|
|
|
5
|
|
|
use Graze\Sprout\Config; |
|
6
|
|
|
use SplFileInfo; |
|
7
|
|
|
|
|
8
|
|
|
class SchemaParser |
|
9
|
|
|
{ |
|
10
|
|
|
/** @var Config */ |
|
11
|
|
|
private $config; |
|
12
|
|
|
/** @var null|string */ |
|
13
|
|
|
private $group; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* SchemaParser constructor. |
|
17
|
|
|
* |
|
18
|
|
|
* @param Config $config |
|
19
|
|
|
* @param string|null $group |
|
20
|
|
|
*/ |
|
21
|
|
|
public function __construct(Config $config, string $group = null) |
|
22
|
|
|
{ |
|
23
|
|
|
$this->config = $config; |
|
24
|
|
|
$this->group = $group; |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @param array $schemaTables |
|
29
|
|
|
* |
|
30
|
|
|
* @return ParsedSchema[] |
|
31
|
|
|
*/ |
|
32
|
|
|
public function extractSchemas(array $schemaTables = []) |
|
33
|
|
|
{ |
|
34
|
|
|
if (count($schemaTables) === 0) { |
|
35
|
|
|
$schemaTables = array_map( |
|
36
|
|
|
function (Config\SchemaConfigInterface $schemaConfig) { |
|
37
|
|
|
return $schemaConfig->getSchema(); |
|
38
|
|
|
}, |
|
39
|
|
|
$this->config->get(Config::CONFIG_SCHEMAS) |
|
40
|
|
|
); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
$parsedSchemas = []; |
|
44
|
|
|
foreach ($schemaTables as $schemaTable) { |
|
45
|
|
|
if (preg_match('/^([a-z_]+):(.+)$/i', $schemaTable, $matches)) { |
|
46
|
|
|
$schema = $matches[1]; |
|
47
|
|
|
$tables = $matches[2] === '*' ? [] : explode(',', $matches[2]); |
|
48
|
|
|
} else { |
|
49
|
|
|
$schema = $schemaTable; |
|
50
|
|
|
$tables = []; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
$parsedSchemas[] = new ParsedSchema( |
|
54
|
|
|
$this->config->getSchemaConfiguration($schema), |
|
55
|
|
|
$this->config->getSchemaPath($this->config->getSchemaConfiguration($schema), $this->group), |
|
56
|
|
|
$tables |
|
57
|
|
|
); |
|
58
|
|
|
}; |
|
59
|
|
|
|
|
60
|
|
|
return array_filter(array_map([$this, 'populateTables'], $parsedSchemas)); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @param ParsedSchema $parsedSchema |
|
65
|
|
|
* |
|
66
|
|
|
* @return ParsedSchema|null |
|
67
|
|
|
*/ |
|
68
|
|
|
public function populateTables(ParsedSchema $parsedSchema) |
|
69
|
|
|
{ |
|
70
|
|
|
if (!is_dir($parsedSchema->getPath())) { |
|
71
|
|
|
return null; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
if (count($parsedSchema->getTables()) === 0) { |
|
75
|
|
|
// find existing tables |
|
76
|
|
|
$files = iterator_to_array(new \FilesystemIterator($parsedSchema->getPath())); |
|
77
|
|
|
$files = array_values(array_filter( |
|
78
|
|
|
$files, |
|
79
|
|
|
function (SplFileInfo $file) { |
|
80
|
|
|
// ignore empty file names (`.bla`) files |
|
81
|
|
|
return (!in_array($file->getFilename(), ['.', '..']) |
|
82
|
|
|
&& pathinfo($file, PATHINFO_FILENAME) !== ''); |
|
83
|
|
|
} |
|
84
|
|
|
)); |
|
85
|
|
|
|
|
86
|
|
|
// sort by file size, largest first |
|
87
|
|
|
usort( |
|
88
|
|
|
$files, |
|
89
|
|
|
function (SplFileInfo $a, SplFileInfo $b) { |
|
90
|
|
|
$left = $a->getSize(); |
|
91
|
|
|
$right = $b->getSize(); |
|
92
|
|
|
return ($left == $right) ? 0 : (($left > $right) ? -1 : 1); |
|
93
|
|
|
} |
|
94
|
|
|
); |
|
95
|
|
|
|
|
96
|
|
|
// remove the file extensions to get the table names |
|
97
|
|
|
$parsedSchema->setTables(array_map( |
|
98
|
|
|
function (SplFileInfo $file) { |
|
99
|
|
|
return pathinfo($file, PATHINFO_FILENAME); |
|
100
|
|
|
}, |
|
101
|
|
|
$files |
|
102
|
|
|
)); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
if (count($parsedSchema->getTables()) === 0) { |
|
106
|
|
|
return null; |
|
107
|
|
|
} |
|
108
|
|
|
return $parsedSchema; |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|