Passed
Push — master ( 52d6d0...ade603 )
by Andreas
33:03
created

midcom::get()   A

Complexity

Conditions 6
Paths 10

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 6.1308

Importance

Changes 0
Metric Value
cc 6
eloc 12
nc 10
nop 1
dl 0
loc 22
ccs 11
cts 13
cp 0.8462
crap 6.1308
rs 9.2222
c 0
b 0
f 0
1
<?php
2
/**
3
 * @package midcom
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
/**
10
 * @package midcom
11
 */
12
class midcom
13
{
14
    /**
15
     * MidCOM version
16
     *
17
     * @var string
18
     */
19
    const VERSION = '9.7.0+git';
20
21
    /**
22
     * Main application singleton
23
     *
24
     * @var midcom_application
25
     */
26
    private static $_application;
27
28
    /**
29
     * This is the interface to MidCOMs Object Services.
30
     *
31
     * Each service is indexed by its string-name (for example "i18n"
32
     * for all i18n stuff).
33
     *
34
     * @var Array
35
     */
36
    private static $_services = [];
37
38
    /**
39
     * Mapping of service names to classes implementing the service
40
     */
41
    private static $_service_classes = [
42
        'auth' => midcom_services_auth::class,
43
        'componentloader' => midcom_helper__componentloader::class,
44
        'cache' => midcom_services_cache::class,
45
        'config' => midcom_config::class,
46
        'dbclassloader' => midcom_services_dbclassloader::class,
47
        'dbfactory' => midcom_helper__dbfactory::class,
48
        'debug' => midcom_debug::class,
49
        'head' => midcom_helper_head::class,
50
        'i18n' => midcom_services_i18n::class,
51
        'indexer' => midcom_services_indexer::class,
52
        'metadata' => midcom_services_metadata::class,
53
        'permalinks' => midcom_services_permalinks::class,
54
        'rcs' => midcom_services_rcs::class,
55
        'session' => midcom_services__sessioning::class,
56
        'style' => midcom_helper__styleloader::class,
57
        'toolbars' => midcom_services_toolbars::class,
58
        'uimessages' => midcom_services_uimessages::class,
59
    ];
60
61
    /**
62
     * @throws midcom_error
63
     */
64 1
    public static function init(string $environment = 'prod', bool $debug = false) : midcom_application
65
    {
66
        ///////////////////////////////////
67
        // Try to be smart about the paths:
68
        // Define default constants
69 1
        if (!defined('MIDCOM_ROOT')) {
70
            define('MIDCOM_ROOT', __DIR__);
71
        }
72
73 1
        if (!defined('MIDCOM_STATIC_ROOT')) {
74
            $pos = strrpos(MIDCOM_ROOT, '/');
75
            if ($pos === false) {
76
                // No slash, this is strange
77
                throw new midcom_error('MIDCOM_ROOT did not contain a slash, this should not happen and is most probably the cause of a configuration error.');
78
            }
79
            define('MIDCOM_STATIC_ROOT', substr(MIDCOM_ROOT, 0, $pos) . '/static');
80
        }
81 1
        if (!defined('MIDCOM_STATIC_URL')) {
82
            define('MIDCOM_STATIC_URL', '/midcom-static');
83
        }
84
85 1
        if (!defined('OPENPSA2_THEME_ROOT')) {
86
            define('OPENPSA2_THEME_ROOT', MIDCOM_ROOT . '/../var/themes/');
87
        }
88
89
        // Instantiate the MidCOM main class
90 1
        self::$_application = new midcom_application($environment, $debug);
91 1
        self::$_application->boot();
92 1
        return self::$_application;
93
    }
94
95
    /**
96
     * Get midcom_application instance
97
     *
98
     * Services can also be loaded this way by passing their name as an argument,
99
     * but this feature will likely be removed at some point
100
     *
101
     * @param string $name The service name as listed in the _service_classes array or null to get midcom_application
102
     * @return midcom_application The midcom application instance
103
     */
104 738
    public static function get($name = null)
105
    {
106 738
        if (!self::$_application) {
107
            self::init();
108
        }
109
110 738
        if (null === $name) {
111 738
            return self::$_application;
112
        }
113
114 738
        if ($name === 'dispatcher') {
115 309
            return self::$_application->getContainer()->get('event_dispatcher');
116
        }
117
118 738
        if (!isset(self::$_services[$name])) {
119 3
            if (!isset(self::$_service_classes[$name])) {
120
                throw new midcom_error("Requested service '$name' is not available.");
121
            }
122 3
            $service_class = self::$_service_classes[$name];
123 3
            self::$_services[$name] = new $service_class;
124
        }
125 738
        return self::$_services[$name];
126
    }
127
128
    /**
129
     * Register a service class
130
     *
131
     * (Experimental, use with caution)
132
     *
133
     * @param string $name
134
     * @param string $class
135
     * @throws midcom_error
136
     */
137
    public static function register_service_class($name, $class)
138
    {
139
        if (isset(self::$_services[$name])) {
140
            throw new midcom_error("Can't change service $name after initialization");
141
        }
142
        self::$_service_classes[$name] = $class;
143
    }
144
}
145