Passed
Push — master ( 8be1b6...9df40e )
by Andreas
47:25 queued 25:20
created

midcom::get()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3.3332

Importance

Changes 0
Metric Value
cc 3
eloc 5
c 0
b 0
f 0
nc 4
nop 1
dl 0
loc 11
ccs 4
cts 6
cp 0.6667
crap 3.3332
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
        define('MIDCOM_ROOT', __DIR__);
43
        if (!defined('MIDCOM_STATIC_URL')) {
44
            define('MIDCOM_STATIC_URL', '/midcom-static');
45
        }
46
        define('MIDCOM_STATIC_ROOT', self::$_application->getProjectDir() . '/web' . MIDCOM_STATIC_URL);
47
        define('OPENPSA2_THEME_ROOT', self::$_application->getProjectDir() . '/var/themes/');
48
        return self::$_application;
49
    }
50
51
    /**
52
     * Get midcom_application instance
53
     *
54
     * Services can also be loaded this way by passing their name as an argument,
55
     * but this feature will likely be removed at some point
56
     *
57
     * @param string $name The service name as listed in the _service_classes array or null to get midcom_application
58
     * @return midcom_application The midcom application instance
59
     */
60 680
    public static function get(string $name = null)
61
    {
62 680
        if (!self::$_application) {
63
            self::init();
64
        }
65
66 680
        if (null === $name) {
67 680
            return self::$_application;
68
        }
69
70
        return self::$_application->getContainer()->get($name);
71
    }
72
73
    /**
74
     * Register a service class
75
     *
76
     * (Experimental, use with caution)
77
     */
78
    public static function register_service_class(string $name, string $class)
79
    {
80
        self::$_service_classes[$name] = $class;
81
    }
82
83
    /**
84
     * @internal
85
     */
86
    public static function get_registered_service_classes() : array
87
    {
88
        return self::$_service_classes;
89
    }
90
}
91