Passed
Push — master ( 8de60e...02d2e0 )
by Andreas
17:40
created

midcom   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Test Coverage

Coverage 23.53%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 16
c 2
b 0
f 0
dl 0
loc 75
ccs 4
cts 17
cp 0.2353
rs 10
wmc 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A register_service_class() 0 3 1
A init() 0 11 2
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
        // Instantiate the MidCOM main class
39
        self::$_application = new midcom_application($environment, $debug);
40
41
        // Define default constants
42
        if (!defined('MIDCOM_STATIC_URL')) {
43
            define('MIDCOM_STATIC_URL', '/midcom-static');
44
        }
45
        define('MIDCOM_STATIC_ROOT', self::$_application->getProjectDir() . '/web' . MIDCOM_STATIC_URL);
46
        return self::$_application;
47
    }
48
49
    /**
50
     * Get midcom_application instance
51
     *
52
     * Services can also be loaded this way by passing their name as an argument,
53
     * but this feature will likely be removed at some point
54
     *
55
     * @param string $name The service name as listed in the _service_classes array or null to get midcom_application
56
     * @return midcom_application The midcom application instance
57
     */
58 712
    public static function get(string $name = null)
59
    {
60 712
        if (!self::$_application) {
61
            self::init();
62
        }
63
64 712
        if (null === $name) {
65 712
            return self::$_application;
66
        }
67
68
        return self::$_application->getContainer()->get($name);
69
    }
70
71
    /**
72
     * Register a service class
73
     *
74
     * (Experimental, use with caution)
75
     */
76
    public static function register_service_class(string $name, string $class)
77
    {
78
        self::$_service_classes[$name] = $class;
79
    }
80
81
    /**
82
     * @internal
83
     */
84
    public static function get_registered_service_classes() : array
85
    {
86
        return self::$_service_classes;
87
    }
88
}
89