Passed
Push — master ( 4ef552...d90a37 )
by Andreas
21:17
created

  A

Complexity

Conditions 3
Paths 4

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3.0416

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 4
nop 1
dl 0
loc 11
ccs 5
cts 6
cp 0.8333
crap 3.0416
rs 10
c 0
b 0
f 0
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
    /**
57
     * Get component name associated with a class name to get its DBA classes defined
58
     */
59 33
    public function get_component_for_class(string $classname) : ?string
60
    {
61 33
        $class_parts = array_filter(explode('_', $classname));
62
        // Fix for incorrectly named classes
63
        $component_map = [
64
            'midgard' => 'midcom',
65
            'org.openpsa.campaign' => 'org.openpsa.directmarketing',
66
            'org.openpsa.link' => 'org.openpsa.directmarketing',
67
            'org.openpsa.document' => 'org.openpsa.documents',
68
            'org.openpsa.organization' => 'org.openpsa.contacts',
69
            'org.openpsa.person' => 'org.openpsa.contacts',
70
            'org.openpsa.member' => 'org.openpsa.contacts',
71
            'org.openpsa.salesproject' => 'org.openpsa.sales',
72
            'org.openpsa.offer' => 'org.openpsa.sales',
73
            'org.openpsa.event' => 'org.openpsa.calendar',
74
            'org.openpsa.eventmember' => 'org.openpsa.calendar',
75
            'org.openpsa.invoice' => 'org.openpsa.invoices',
76
            'org.openpsa.billing' => 'org.openpsa.invoices',
77
            'org.openpsa.query' => 'org.openpsa.reports',
78
            'org.openpsa.task' => 'org.openpsa.projects',
79
            'org.openpsa.project' => 'org.openpsa.projects',
80
            'org.openpsa.role' => 'org.openpsa.projects',
81
            'org.openpsa.hour' => 'org.openpsa.expenses'
82
        ];
83
84 33
        while (!empty($class_parts)) {
85 33
            $component = implode('.', $class_parts);
86 33
            if (array_key_exists($component, $component_map)) {
87 22
                $component = $component_map[$component];
88
            }
89
90 33
            if (midcom::get()->componentloader->is_installed($component)) {
91 32
                return $component;
92
            }
93 31
            array_pop($class_parts);
94
        }
95
96 1
        return null;
97
    }
98
99
    /**
100
     * Get a MidCOM DB class name for a MgdSchema Object.
101
     * We also ensure that the corresponding component has been loaded.
102
     *
103
     * @param string|object $object The object (or classname) to check
104
     */
105 318
    public function get_midcom_class_name_for_mgdschema_object($object) : ?string
106
    {
107 318
        static $dba_classes_by_mgdschema = [];
108
109 318
        if (is_string($object)) {
110
            // In some cases we get a class name instead
111 250
            $classname = $object;
112 160
        } elseif (is_object($object)) {
113 160
            $classname = get_class($object);
114
        } else {
115
            debug_print_r("Invalid input provided", $object, MIDCOM_LOG_WARN);
116
            return null;
117
        }
118
119 318
        if (array_key_exists($classname, $dba_classes_by_mgdschema)) {
120 316
            return $dba_classes_by_mgdschema[$classname];
121
        }
122
123 4
        if (!is_subclass_of($classname, mgdobject::class)) {
124
            debug_add("{$classname} is not an MgdSchema object, not resolving to MidCOM DBA class", MIDCOM_LOG_WARN);
125
            $dba_classes_by_mgdschema[$classname] = null;
126
            return null;
127
        }
128
129 4
        if ($classname == midcom::get()->config->get('person_class')) {
130
            $component = 'midcom';
131
        } else {
132 4
            $component = $this->get_component_for_class($classname);
133 4
            if (!$component) {
134
                debug_add("Component for class {$classname} cannot be found", MIDCOM_LOG_WARN);
135
                $dba_classes_by_mgdschema[$classname] = null;
136
                return null;
137
            }
138
        }
139 4
        $definitions = $this->get_component_classes($component);
140
141
        //TODO: This allows components to override midcom classes fx. Do we want that?
142 4
        $dba_classes_by_mgdschema = array_merge($dba_classes_by_mgdschema, $definitions);
143
144 4
        if (array_key_exists($classname, $dba_classes_by_mgdschema)) {
145 4
            return $dba_classes_by_mgdschema[$classname];
146
        }
147
148
        debug_add("{$classname} cannot be resolved to any DBA class name");
149
        $dba_classes_by_mgdschema[$classname] = null;
150
        return null;
151
    }
152
153
    /**
154
     * Get an MgdSchema class name for a MidCOM DBA class name
155
     *
156
     * @return string The corresponding MidCOM DBA class name, false otherwise.
157
     */
158 492
    public function get_mgdschema_class_name_for_midcom_class(string $classname)
159
    {
160 492
        static $mapping = [];
161
162 492
        if (!array_key_exists($classname, $mapping)) {
163 15
            $mapping[$classname] = false;
164
165 15
            if (class_exists($classname)) {
166 15
                $dummy_object = new $classname();
167 15
                if ($this->is_midcom_db_object($dummy_object)) {
168 15
                    $mapping[$classname] = $dummy_object->__mgdschema_class_name__;
169
                }
170
            }
171
        }
172
173 492
        return $mapping[$classname];
174
    }
175
176
    /**
177
     * Simple helper to check whether we are dealing with a MidCOM Database object
178
     * or a subclass thereof.
179
     *
180
     * @param object|string $object The object (or classname) to check
181
     */
182 519
    public function is_midcom_db_object($object) : bool
183
    {
184 519
        return is_subclass_of($object, midcom_core_dbaobject::class)
185 519
            || is_a($object, midcom_core_dbaproxy::class, true);
186
    }
187
188 10
    public function get_component_classes(string $component) : array
189
    {
190 10
        $map = midcom::get()->componentloader->get_manifest($component)->class_mapping;
191 10
        if ($component == 'midcom') {
192
            $map[midcom::get()->config->get('person_class')] = midcom_db_person::class;
193
        }
194 10
        return $map;
195
    }
196
}
197