|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of graze/sprout. |
|
4
|
|
|
* |
|
5
|
|
|
* Copyright © 2018 Nature Delivered Ltd. <https://www.graze.com> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
* |
|
10
|
|
|
* @license https://github.com/graze/sprout/blob/master/LICENSE.md |
|
11
|
|
|
* @link https://github.com/graze/sprout |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace Graze\Sprout\Parser; |
|
15
|
|
|
|
|
16
|
|
|
use League\Flysystem\AdapterInterface; |
|
17
|
|
|
|
|
18
|
|
|
class FileTablePopulator implements TablePopulatorInterface |
|
19
|
|
|
{ |
|
20
|
|
|
/** @var AdapterInterface */ |
|
21
|
|
|
private $filesystem; |
|
22
|
|
|
/** @var TableFilterer */ |
|
23
|
|
|
private $tableFilterer; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* SchemaParser constructor. |
|
27
|
|
|
* |
|
28
|
|
|
* @param AdapterInterface $filesystem |
|
29
|
|
|
* @param TableFilterer|null $tableFilterer |
|
30
|
|
|
*/ |
|
31
|
6 |
|
public function __construct(AdapterInterface $filesystem, TableFilterer $tableFilterer = null) |
|
32
|
|
|
{ |
|
33
|
6 |
|
$this->filesystem = $filesystem; |
|
34
|
6 |
|
$this->tableFilterer = $tableFilterer ?: new TableFilterer(); |
|
35
|
6 |
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @param ParsedSchema $parsedSchema |
|
39
|
|
|
* |
|
40
|
|
|
* @return ParsedSchema|null |
|
41
|
|
|
*/ |
|
42
|
6 |
|
public function populateTables(ParsedSchema $parsedSchema) |
|
43
|
|
|
{ |
|
44
|
6 |
|
if (count($parsedSchema->getTables()) === 0) { |
|
45
|
5 |
|
if ($parsedSchema->getPath() === '' || $this->filesystem->has($parsedSchema->getPath()) === false) { |
|
46
|
2 |
|
return null; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
// find existing tables |
|
50
|
3 |
|
$files = $this->filesystem->listContents($parsedSchema->getPath()); |
|
51
|
3 |
|
$files = array_values(array_filter( |
|
52
|
3 |
|
$files, |
|
53
|
|
|
function (array $file) { |
|
54
|
|
|
// ignore empty file names (`.bla`) files |
|
55
|
2 |
|
return (pathinfo($file['path'], PATHINFO_FILENAME) !== ''); |
|
56
|
3 |
|
} |
|
57
|
|
|
)); |
|
58
|
|
|
|
|
59
|
|
|
// sort by file size, largest first |
|
60
|
3 |
|
usort( |
|
61
|
3 |
|
$files, |
|
62
|
|
|
function (array $a, array $b) { |
|
63
|
2 |
|
return ($a['size'] == $b['size']) ? 0 : (($a['size'] > $b['size']) ? -1 : 1); |
|
64
|
3 |
|
} |
|
65
|
|
|
); |
|
66
|
|
|
|
|
67
|
3 |
|
$tables = array_map( |
|
68
|
3 |
|
function (array $file) { |
|
69
|
2 |
|
return pathinfo($file['path'], PATHINFO_FILENAME); |
|
70
|
3 |
|
}, |
|
71
|
3 |
|
$files |
|
72
|
|
|
); |
|
73
|
|
|
|
|
74
|
3 |
|
if (count($parsedSchema->getSchemaConfig()->getExcludes()) > 0) { |
|
75
|
1 |
|
$tables = $this->tableFilterer->filter( |
|
76
|
1 |
|
$tables, |
|
77
|
1 |
|
$parsedSchema->getSchemaConfig()->getExcludes() |
|
78
|
|
|
); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
// remove the file extensions to get the table names |
|
82
|
3 |
|
$parsedSchema->setTables($tables); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
4 |
|
if (count($parsedSchema->getTables()) === 0) { |
|
86
|
1 |
|
return null; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
3 |
|
return $parsedSchema; |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|