|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @copyright CONTENT CONTROL GmbH, http://www.contentcontrol-berlin.de |
|
4
|
|
|
*/ |
|
5
|
|
|
|
|
6
|
|
|
namespace midcom\datamanager; |
|
7
|
|
|
|
|
8
|
|
|
use midcom_error; |
|
9
|
|
|
use midcom_helper_misc; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Experimental schemadb class |
|
13
|
|
|
*/ |
|
14
|
|
|
class schemadb |
|
15
|
|
|
{ |
|
16
|
|
|
private array $schemas = []; |
|
17
|
|
|
|
|
18
|
172 |
|
public static function from_path(string $path) : self |
|
19
|
|
|
{ |
|
20
|
172 |
|
return new static(midcom_helper_misc::load_snippet($path)); |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
192 |
|
public function __construct(array $data = []) |
|
24
|
|
|
{ |
|
25
|
192 |
|
$this->check_inheritance($data); |
|
26
|
192 |
|
foreach ($data as $name => $config) { |
|
27
|
189 |
|
$this->add($name, new schema($config)); |
|
28
|
|
|
} |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
192 |
|
private function check_inheritance(array &$data) |
|
32
|
|
|
{ |
|
33
|
192 |
|
foreach ($data as $schema_name => $schema) { |
|
34
|
189 |
|
if (!isset($schema['extends'])) { |
|
35
|
189 |
|
continue; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
// Default extended schema is with the same name |
|
39
|
|
|
$extended_schema_name = $schema_name; |
|
40
|
|
|
$path = ''; |
|
41
|
|
|
|
|
42
|
|
|
if (is_array($schema['extends'])) { |
|
43
|
|
|
if (isset($schema['extends']['path'])) { |
|
44
|
|
|
$path = $schema['extends']['path']; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
// Override schema name |
|
48
|
|
|
if (isset($schema['extends']['name'])) { |
|
49
|
|
|
$extended_schema_name = $schema['extends']['name']; |
|
50
|
|
|
} |
|
51
|
|
|
} elseif (isset($data[$schema['extends']])) { |
|
52
|
|
|
$schema['extends'] = [ |
|
53
|
|
|
'name' => $schema['extends'], |
|
54
|
|
|
]; |
|
55
|
|
|
} else { |
|
56
|
|
|
$path = $schema['extends']; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
if ($path === '') { |
|
60
|
|
|
// Infinite loop, set an UI message and stop executing |
|
61
|
|
|
if ( !isset($schema['extends']['name']) |
|
62
|
|
|
|| $schema['extends']['name'] === $schema_name) { |
|
63
|
|
|
throw new midcom_error('schema ' . $schema_name . ' extends itself'); |
|
64
|
|
|
} |
|
65
|
|
|
$extended_schema_name = $schema['extends']['name']; |
|
66
|
|
|
$extended_schemadb = [$extended_schema_name => $data[$extended_schema_name]]; |
|
67
|
|
|
} else { |
|
68
|
|
|
$extended_schemadb = midcom_helper_misc::load_snippet($path); |
|
69
|
|
|
if (!isset($extended_schemadb[$extended_schema_name])) { |
|
70
|
|
|
throw new midcom_error('extended schema ' . $path . ':' . $schema_name . ' was not found'); |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
// Override the extended schema with fields from the new schema |
|
75
|
|
|
foreach ($schema as $key => $value) { |
|
76
|
|
|
if ($key === 'extends') { |
|
77
|
|
|
continue; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
// This is probably either fields or operations |
|
81
|
|
|
if (is_array($value)) { |
|
82
|
|
|
$extended_schemadb[$extended_schema_name][$key] ??= []; |
|
83
|
|
|
|
|
84
|
|
|
foreach ($value as $name => $field) { |
|
85
|
|
|
if (!$field) { |
|
86
|
|
|
unset($extended_schemadb[$extended_schema_name][$key][$name]); |
|
87
|
|
|
continue; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
$extended_schemadb[$extended_schema_name][$key][$name] = $field; |
|
91
|
|
|
} |
|
92
|
|
|
} else { |
|
93
|
|
|
$extended_schemadb[$extended_schema_name][$key] = $value; |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
// Replace the new schema with extended schema |
|
98
|
|
|
$data[$schema_name] = $extended_schemadb[$extended_schema_name]; |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
192 |
|
public function add(string $name, schema $schema) |
|
103
|
|
|
{ |
|
104
|
192 |
|
$this->schemas[$name] = $schema; |
|
105
|
192 |
|
$schema->set_name($name); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* @return schema[] |
|
110
|
|
|
*/ |
|
111
|
34 |
|
public function all() : array |
|
112
|
|
|
{ |
|
113
|
34 |
|
return $this->schemas; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
118 |
|
public function get_first() : schema |
|
117
|
|
|
{ |
|
118
|
118 |
|
if (empty($this->schemas)) { |
|
119
|
|
|
throw new midcom_error('Schema DB is empty'); |
|
120
|
|
|
} |
|
121
|
118 |
|
return reset($this->schemas); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
61 |
|
public function has(string $name) : bool |
|
125
|
|
|
{ |
|
126
|
61 |
|
return array_key_exists($name, $this->schemas); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
78 |
|
public function get(string $name) : schema |
|
130
|
|
|
{ |
|
131
|
78 |
|
if (!array_key_exists($name, $this->schemas)) { |
|
132
|
|
|
throw new midcom_error('Schema ' . $name . ' not found in schemadb'); |
|
133
|
|
|
} |
|
134
|
78 |
|
return $this->schemas[$name]; |
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
|
|
|