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 Graze\Sprout\Config\Config; |
||
17 | use Graze\Sprout\Config\SchemaConfigInterface; |
||
18 | |||
19 | class SchemaParser |
||
20 | { |
||
21 | /** @var Config */ |
||
22 | private $config; |
||
23 | /** @var null|string */ |
||
24 | private $group; |
||
25 | /** @var TablePopulatorInterface */ |
||
26 | private $populator; |
||
27 | |||
28 | /** |
||
29 | * SchemaParser constructor. |
||
30 | * |
||
31 | * @param TablePopulatorInterface $populator |
||
32 | * @param Config $config |
||
33 | * @param string|null $group |
||
34 | */ |
||
35 | 4 | public function __construct(TablePopulatorInterface $populator, Config $config, string $group = null) |
|
36 | { |
||
37 | 4 | $this->populator = $populator; |
|
38 | 4 | $this->config = $config; |
|
39 | 4 | $this->group = $group; |
|
40 | 4 | } |
|
41 | |||
42 | /** |
||
43 | * @param array $schemaTables |
||
44 | * |
||
45 | * @return ParsedSchema[] |
||
46 | */ |
||
47 | 4 | public function extractSchemas(array $schemaTables = []) |
|
48 | { |
||
49 | 4 | if (count($schemaTables) === 0) { |
|
50 | 1 | $schemaTables = array_map( |
|
51 | 1 | function (SchemaConfigInterface $schemaConfig) { |
|
52 | 1 | return $schemaConfig->getSchema(); |
|
53 | 1 | }, |
|
54 | 1 | $this->config->get(Config::CONFIG_SCHEMAS) |
|
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
55 | ); |
||
56 | } |
||
57 | |||
58 | 4 | $parsedSchemas = []; |
|
59 | 4 | foreach ($schemaTables as $schemaTable) { |
|
60 | 4 | if (preg_match('/^([a-z0-9_]+):(.+)$/i', $schemaTable, $matches)) { |
|
61 | 2 | $schema = $matches[1]; |
|
62 | 2 | $tables = explode(',', $matches[2]); |
|
63 | } else { |
||
64 | 3 | $schema = $schemaTable; |
|
65 | 3 | $tables = []; |
|
66 | } |
||
67 | |||
68 | 4 | $parsedSchemas[] = new ParsedSchema( |
|
69 | 4 | $this->config->getSchemaConfiguration($schema), |
|
70 | 4 | $this->config->getSchemaPath($this->config->getSchemaConfiguration($schema), $this->group), |
|
71 | 4 | $tables |
|
72 | ); |
||
73 | }; |
||
74 | |||
75 | 4 | return array_filter(array_map([$this->populator, 'populateTables'], $parsedSchemas)); |
|
76 | } |
||
77 | } |
||
78 |