Passed
Push — master ( 7c91ce...412da6 )
by Andreas
22:01
created

midcom::get_registered_service_classes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
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
     * Mapping of service names to classes implementing the service
30
     */
31
    private static $_service_classes = [];
32
33
    /**
34
     * @throws midcom_error
35
     */
36
    public static function init(string $environment = 'prod', bool $debug = false) : midcom_application
37
    {
38
        ///////////////////////////////////
39
        // Try to be smart about the paths:
40
        // Define default constants
41
        if (!defined('MIDCOM_ROOT')) {
42
            define('MIDCOM_ROOT', __DIR__);
43
        }
44
45
        if (!defined('MIDCOM_STATIC_ROOT')) {
46
            $pos = strrpos(MIDCOM_ROOT, '/');
47
            if ($pos === false) {
48
                // No slash, this is strange
49
                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.');
50
            }
51
            define('MIDCOM_STATIC_ROOT', substr(MIDCOM_ROOT, 0, $pos) . '/static');
52
        }
53
        if (!defined('MIDCOM_STATIC_URL')) {
54
            define('MIDCOM_STATIC_URL', '/midcom-static');
55
        }
56
57
        if (!defined('OPENPSA2_THEME_ROOT')) {
58
            define('OPENPSA2_THEME_ROOT', MIDCOM_ROOT . '/../var/themes/');
59
        }
60
61
        // Instantiate the MidCOM main class
62
        self::$_application = new midcom_application($environment, $debug);
63
        self::$_application->boot();
64
        return self::$_application;
65
    }
66
67
    /**
68
     * Get midcom_application instance
69
     *
70
     * Services can also be loaded this way by passing their name as an argument,
71
     * but this feature will likely be removed at some point
72
     *
73
     * @param string $name The service name as listed in the _service_classes array or null to get midcom_application
74
     * @return midcom_application The midcom application instance
75
     */
76 735
    public static function get($name = null)
77
    {
78 735
        if (!self::$_application) {
79
            self::init();
80
        }
81
82 735
        if (null === $name) {
83 735
            return self::$_application;
84
        }
85
86
        return self::$_application->getContainer()->get($name);
87
    }
88
89
    /**
90
     * Register a service class
91
     *
92
     * (Experimental, use with caution)
93
     */
94
    public static function register_service_class(string $name, string $class)
95
    {
96
        self::$_service_classes[$name] = $class;
97
    }
98
99
    /**
100
     * @internal
101
     */
102
    public static function get_registered_service_classes() : array
103
    {
104
        return self::$_service_classes;
105
    }
106
}
107