Completed
Push — master ( b9fa67...e31f65 )
by Andreas
24:00 queued 04:20
created

midcom::register_service_class()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 6
ccs 0
cts 4
cp 0
crap 6
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
use midcom\events\dispatcher;
10
use Symfony\Component\HttpKernel\Controller\ControllerResolver;
11
use Symfony\Component\HttpKernel\Controller\ArgumentResolver;
12
use Symfony\Component\HttpKernel\HttpKernel;
13
use Symfony\Component\HttpFoundation\RequestStack;
14
use midcom\httpkernel\subscriber;
15
use Symfony\Component\HttpKernel\EventListener\StreamedResponseListener;
16
17
/**
18
 * @package midcom
19
 */
20
class midcom
21
{
22
    /**
23
     * MidCOM version
24
     *
25
     * @var string
26
     */
27
    const VERSION = '9.7.0+git';
28
29
    /**
30
     * Main application singleton
31
     *
32
     * @var midcom_application
33
     */
34
    private static $_application;
35
36
    /**
37
     * This is the interface to MidCOMs Object Services.
38
     *
39
     * Each service is indexed by its string-name (for example "i18n"
40
     * for all i18n stuff).
41
     *
42
     * @var Array
43
     */
44
    private static $_services = [];
45
46
    /**
47
     * Mapping of service names to classes implementing the service
48
     */
49
    private static $_service_classes = [
50
        'auth' => midcom_services_auth::class,
51
        'componentloader' => midcom_helper__componentloader::class,
52
        'cache' => midcom_services_cache::class,
53
        'config' => midcom_config::class,
54
        'dbclassloader' => midcom_services_dbclassloader::class,
55
        'dbfactory' => midcom_helper__dbfactory::class,
56
        'dispatcher' => dispatcher::class,
57
        'debug' => midcom_debug::class,
58
        'head' => midcom_helper_head::class,
59
        'i18n' => midcom_services_i18n::class,
60
        'indexer' => midcom_services_indexer::class,
61
        'metadata' => midcom_services_metadata::class,
62
        'permalinks' => midcom_services_permalinks::class,
63
        'rcs' => midcom_services_rcs::class,
64
        'session' => midcom_services__sessioning::class,
65
        'style' => midcom_helper__styleloader::class,
66
        'toolbars' => midcom_services_toolbars::class,
67
        'uimessages' => midcom_services_uimessages::class,
68
    ];
69
70
    /**
71
     * @throws midcom_error
72
     */
73 1
    public static function init() : HttpKernel
74
    {
75
        ///////////////////////////////////
76
        // Try to be smart about the paths:
77
        // Define default constants
78 1
        if (!defined('MIDCOM_ROOT')) {
79
            define('MIDCOM_ROOT', __DIR__);
80
        }
81
82 1
        if (!defined('MIDCOM_STATIC_ROOT')) {
83
            $pos = strrpos(MIDCOM_ROOT, '/');
84
            if ($pos === false) {
85
                // No slash, this is strange
86
                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.');
87
            }
88
            define('MIDCOM_STATIC_ROOT', substr(MIDCOM_ROOT, 0, $pos) . '/static');
89
        }
90 1
        if (!defined('MIDCOM_STATIC_URL')) {
91
            define('MIDCOM_STATIC_URL', '/midcom-static');
92
        }
93
94 1
        if (!defined('OPENPSA2_THEME_ROOT')) {
95
            define('OPENPSA2_THEME_ROOT', MIDCOM_ROOT . '/../var/themes/');
96
        }
97
98 1
        self::$_services['dispatcher'] = new dispatcher;
99 1
        self::$_services['dispatcher']->addSubscriber(new subscriber);
100 1
        self::$_services['dispatcher']->addSubscriber(new StreamedResponseListener);
101 1
        $c_resolver = new ControllerResolver;
102 1
        $a_resolver = new ArgumentResolver;
103 1
        $kernel = new HttpKernel(self::$_services['dispatcher'], $c_resolver, new RequestStack, $a_resolver);
104
105
        // Instantiate the MidCOM main class
106 1
        self::$_application = new midcom_application($kernel);
107 1
        return $kernel;
108
    }
109
110
    /**
111
     * Get midcom_application instance
112
     *
113
     * Services can also be loaded this way by passing their name as an argument,
114
     * but this feature will likely be removed at some point
115
     *
116
     * @param string $name The service name as listed in the _service_classes array or null to get midcom_application
117
     * @return midcom_application The midcom application instance
118
     */
119 735
    public static function get($name = null)
120
    {
121 735
        if (!self::$_application) {
122
            self::init();
123
        }
124
125 735
        if (null === $name) {
126 735
            return self::$_application;
127
        }
128
129 735
        if (!isset(self::$_services[$name])) {
130 3
            if (!isset(self::$_service_classes[$name])) {
131
                throw new midcom_error("Requested service '$name' is not available.");
132
            }
133 3
            $service_class = self::$_service_classes[$name];
134 3
            self::$_services[$name] = new $service_class;
135
        }
136 735
        return self::$_services[$name];
137
    }
138
139
    /**
140
     * Register a service class
141
     *
142
     * (Experimental, use with caution)
143
     *
144
     * @param string $name
145
     * @param string $class
146
     * @throws midcom_error
147
     */
148
    public static function register_service_class($name, $class)
149
    {
150
        if (isset(self::$_services[$name])) {
151
            throw new midcom_error("Can't change service $name after initialization");
152
        }
153
        self::$_service_classes[$name] = $class;
154
    }
155
}
156