Passed
Push — master ( 8de60e...02d2e0 )
by Andreas
17:40
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.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