Passed
Push — master ( a58879...e66cf6 )
by Andreas
17:49
created

midcom   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Test Coverage

Coverage 16%

Importance

Changes 0
Metric Value
eloc 24
dl 0
loc 92
ccs 4
cts 25
cp 0.16
rs 10
c 0
b 0
f 0
wmc 11

4 Methods

Rating   Name   Duplication   Size   Complexity  
A register_service_class() 0 3 1
A init() 0 28 6
A get() 0 11 3
A get_registered_service_classes() 0 3 1
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.8.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
        return self::$_application;
64
    }
65
66
    /**
67
     * Get midcom_application instance
68
     *
69
     * Services can also be loaded this way by passing their name as an argument,
70
     * but this feature will likely be removed at some point
71
     *
72
     * @param string $name The service name as listed in the _service_classes array or null to get midcom_application
73
     * @return midcom_application The midcom application instance
74
     */
75 680
    public static function get(string $name = null)
76
    {
77 680
        if (!self::$_application) {
78
            self::init();
79
        }
80
81 680
        if (null === $name) {
82 680
            return self::$_application;
83
        }
84
85
        return self::$_application->getContainer()->get($name);
86
    }
87
88
    /**
89
     * Register a service class
90
     *
91
     * (Experimental, use with caution)
92
     */
93
    public static function register_service_class(string $name, string $class)
94
    {
95
        self::$_service_classes[$name] = $class;
96
    }
97
98
    /**
99
     * @internal
100
     */
101
    public static function get_registered_service_classes() : array
102
    {
103
        return self::$_service_classes;
104
    }
105
}
106