|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @package midcom.services |
|
4
|
|
|
* @author The Midgard Project, http://www.midgard-project.org |
|
5
|
|
|
* @copyright The Midgard Project, http://www.midgard-project.org |
|
6
|
|
|
* @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
use midgard\portable\api\mgdobject; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* <b>How to write database class definitions:</b> |
|
13
|
|
|
* |
|
14
|
|
|
* The general idea is to provide MidCOM with a way to hook into every database interaction |
|
15
|
|
|
* between the component and the Midgard core. |
|
16
|
|
|
* |
|
17
|
|
|
* Since PHP does not allow for multiple inheritance (which would be really useful here), |
|
18
|
|
|
* a decorator pattern is used, which connects the class you actually use in your component |
|
19
|
|
|
* and the original MgdSchema class while at the same time routing all function calls through |
|
20
|
|
|
* midcom_core_dbaobject. |
|
21
|
|
|
* |
|
22
|
|
|
* The class loader does not require much information when registering classes: |
|
23
|
|
|
* An example declaration looks like this: |
|
24
|
|
|
* |
|
25
|
|
|
* <code> |
|
26
|
|
|
* [ |
|
27
|
|
|
* 'midgard_article' => 'midcom_db_article' |
|
28
|
|
|
* ] |
|
29
|
|
|
* </code> |
|
30
|
|
|
* |
|
31
|
|
|
* The key is the MgdSchema class name from that you want to use. The class specified must exist. |
|
32
|
|
|
* |
|
33
|
|
|
* The value is the name of the MidCOM base class you intend to create. |
|
34
|
|
|
* It is checked for basic validity against the PHP restrictions on symbol naming, but the |
|
35
|
|
|
* class itself is not checked for existence. You <i>must</i> declare the class as listed at |
|
36
|
|
|
* all times, as typecasting and detection is done using this metadata property in the core. |
|
37
|
|
|
* |
|
38
|
|
|
* <b>Inherited class requirements</b> |
|
39
|
|
|
* |
|
40
|
|
|
* The classes you inherit from the intermediate stub classes must at this time satisfy one |
|
41
|
|
|
* requirement: You have to declare the midcom and mgdschema classnames: |
|
42
|
|
|
* |
|
43
|
|
|
* <code> |
|
44
|
|
|
* class midcom_db_article |
|
45
|
|
|
* extends midcom_core_dbaobject |
|
46
|
|
|
* { |
|
47
|
|
|
* public $__midcom_class_name__ = __CLASS__; |
|
48
|
|
|
* public $__mgdschema_class_name__ = 'midgard_article'; |
|
49
|
|
|
* |
|
50
|
|
|
* </code> |
|
51
|
|
|
* |
|
52
|
|
|
* @package midcom.services |
|
53
|
|
|
*/ |
|
54
|
|
|
class midcom_services_dbclassloader |
|
55
|
|
|
{ |
|
56
|
|
|
private $map; |
|
57
|
|
|
|
|
58
|
2 |
|
public function __construct(array $map) |
|
59
|
|
|
{ |
|
60
|
2 |
|
$this->map = $map; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Get component name associated with a class name to get its DBA classes defined |
|
65
|
|
|
*/ |
|
66
|
31 |
|
public function get_component_for_class(string $classname) : ?string |
|
67
|
|
|
{ |
|
68
|
31 |
|
$class_parts = array_filter(explode('_', $classname)); |
|
69
|
|
|
// Fix for incorrectly named classes |
|
70
|
|
|
$component_map = [ |
|
71
|
|
|
'midgard' => 'midcom', |
|
72
|
|
|
'org.openpsa.campaign' => 'org.openpsa.directmarketing', |
|
73
|
|
|
'org.openpsa.link' => 'org.openpsa.directmarketing', |
|
74
|
|
|
'org.openpsa.document' => 'org.openpsa.documents', |
|
75
|
|
|
'org.openpsa.organization' => 'org.openpsa.contacts', |
|
76
|
|
|
'org.openpsa.person' => 'org.openpsa.contacts', |
|
77
|
|
|
'org.openpsa.member' => 'org.openpsa.contacts', |
|
78
|
|
|
'org.openpsa.salesproject' => 'org.openpsa.sales', |
|
79
|
|
|
'org.openpsa.offer' => 'org.openpsa.sales', |
|
80
|
|
|
'org.openpsa.event' => 'org.openpsa.calendar', |
|
81
|
|
|
'org.openpsa.eventmember' => 'org.openpsa.calendar', |
|
82
|
|
|
'org.openpsa.invoice' => 'org.openpsa.invoices', |
|
83
|
|
|
'org.openpsa.billing' => 'org.openpsa.invoices', |
|
84
|
|
|
'org.openpsa.query' => 'org.openpsa.reports', |
|
85
|
|
|
'org.openpsa.task' => 'org.openpsa.projects', |
|
86
|
|
|
'org.openpsa.project' => 'org.openpsa.projects', |
|
87
|
|
|
'org.openpsa.role' => 'org.openpsa.projects', |
|
88
|
|
|
'org.openpsa.hour' => 'org.openpsa.expenses' |
|
89
|
|
|
]; |
|
90
|
|
|
|
|
91
|
31 |
|
while (!empty($class_parts)) { |
|
92
|
31 |
|
$component = implode('.', $class_parts); |
|
93
|
31 |
|
if (array_key_exists($component, $component_map)) { |
|
94
|
21 |
|
$component = $component_map[$component]; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
31 |
|
if (array_key_exists($component, $this->map)) { |
|
98
|
30 |
|
return $component; |
|
99
|
|
|
} |
|
100
|
29 |
|
array_pop($class_parts); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
1 |
|
return null; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Get a MidCOM DB class name for a MgdSchema Object. |
|
108
|
|
|
* We also ensure that the corresponding component has been loaded. |
|
109
|
|
|
* |
|
110
|
|
|
* @param string|object $classname The classname (or object) to check |
|
111
|
|
|
*/ |
|
112
|
318 |
|
public function get_midcom_class_name_for_mgdschema_object($classname) : ?string |
|
113
|
|
|
{ |
|
114
|
318 |
|
static $dba_classes_by_mgdschema = []; |
|
115
|
|
|
|
|
116
|
318 |
|
if (is_object($classname)) { |
|
117
|
160 |
|
$classname = get_class($classname); |
|
118
|
250 |
|
} elseif (!is_string($classname)) { |
|
|
|
|
|
|
119
|
|
|
debug_print_r("Invalid input provided", $classname, MIDCOM_LOG_WARN); |
|
120
|
|
|
return null; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
318 |
|
if (!array_key_exists($classname, $dba_classes_by_mgdschema)) { |
|
124
|
11 |
|
foreach ($this->map as $mapping) { |
|
125
|
11 |
|
if (array_key_exists($classname, $mapping)) { |
|
126
|
11 |
|
$dba_classes_by_mgdschema[$classname] = $mapping[$classname]; |
|
127
|
11 |
|
break; |
|
128
|
|
|
} |
|
129
|
|
|
} |
|
130
|
11 |
|
if (!array_key_exists($classname, $dba_classes_by_mgdschema)) { |
|
131
|
|
|
debug_add("{$classname} cannot be resolved to any DBA class name"); |
|
132
|
|
|
$dba_classes_by_mgdschema[$classname] = null; |
|
133
|
|
|
} |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
318 |
|
return $dba_classes_by_mgdschema[$classname]; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* Get an MgdSchema class name for a MidCOM DBA class name |
|
141
|
|
|
* |
|
142
|
|
|
* @return string The corresponding MidCOM DBA class name, false otherwise. |
|
143
|
|
|
*/ |
|
144
|
492 |
|
public function get_mgdschema_class_name_for_midcom_class(string $classname) |
|
145
|
|
|
{ |
|
146
|
492 |
|
static $mapping = []; |
|
147
|
|
|
|
|
148
|
492 |
|
if (!array_key_exists($classname, $mapping)) { |
|
149
|
15 |
|
$mapping[$classname] = false; |
|
150
|
|
|
|
|
151
|
15 |
|
if (class_exists($classname)) { |
|
152
|
15 |
|
if ($this->is_midcom_db_object($classname)) { |
|
153
|
15 |
|
$mapping[$classname] = (new $classname)->__mgdschema_class_name__; |
|
154
|
|
|
} |
|
155
|
|
|
} |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
492 |
|
return $mapping[$classname]; |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* Simple helper to check whether we are dealing with a MidCOM Database object |
|
163
|
|
|
* or a subclass thereof. |
|
164
|
|
|
* |
|
165
|
|
|
* @param object|string $object The object (or classname) to check |
|
166
|
|
|
*/ |
|
167
|
519 |
|
public function is_midcom_db_object($object) : bool |
|
168
|
|
|
{ |
|
169
|
519 |
|
return is_subclass_of($object, midcom_core_dbaobject::class) |
|
170
|
519 |
|
|| is_a($object, midcom_core_dbaproxy::class, true); |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
6 |
|
public function get_component_classes(string $component) : array |
|
174
|
|
|
{ |
|
175
|
6 |
|
return $this->map[$component]; |
|
176
|
|
|
} |
|
177
|
|
|
} |
|
178
|
|
|
|