CDI   A
last analyzed

Complexity

Total Complexity 19

Size/Duplication

Total Lines 196
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 196
rs 10
wmc 19
lcom 1
cbo 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getServices() 0 4 1
A getActiveServices() 0 4 1
A set() 0 5 1
A setShared() 0 4 1
A get() 0 16 4
A __get() 0 4 1
A has() 0 6 2
A __call() 0 4 1
B load() 0 30 6
1
<?php
2
3
namespace Anax\DI;
4
5
/**
6
 * Anax base class implementing Dependency Injection / Service Locator of the services used by the 
7
 * framework, using lazy loading.
8
 *
9
 */
10
class CDI implements IDI
11
{
12
13
    /**
14
     * Properties
15
     *
16
     */
17
    public $loaded = [];  // Store all lazy loaded services, ready to be instantiated
18
    public $active = [];  // A service is instantiated into this array, once its accessed
19
20
21
22
    /**
23
     * Construct.
24
     *
25
     */
26
    public function __construct()
27
    {
28
        ;
29
    }
30
31
32
33
    /**
34
     * Return an arry with all loaded services names.
35
     *
36
     * @return void
37
     */
38
    public function getServices()
39
    {
40
        return array_keys($this->loaded);
41
    }
42
43
44
45
    /**
46
     * Return an arry with all active services names.
47
     *
48
     * @return void
49
     */
50
    public function getActiveServices()
51
    {
52
        return array_keys($this->active);
53
    }
54
55
56
57
    /**
58
     * Set a service and connect it to a task which creates the object (lazy loading).
59
     *
60
     * @param string  $service   as a service label, naming this service.
61
     * @param mixed   $loader    contains a pre-defined object, a string with classname or an
62
     *      callable which returns an instance of the service object. Its the way to 
63
     *      actually load, insantiate, the serviceobject.
64
     * @param boolean $singleton set if service is to act as singleton or not, default is false.
65
     *
66
     * @return nothing.
0 ignored issues
show
Documentation introduced by
The doc-type nothing. could not be parsed: Unknown type name "nothing." at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
67
     */
68
    public function set($service, $loader, $singleton = false)
69
    {
70
        $this->loaded[$service]['loader'] = $loader;
71
        $this->loaded[$service]['singleton'] = $singleton;
72
    }
73
74
75
76
    /**
77
     * Set a singleton service and connect it to a task which creates the object (lazy loading).
78
     *
79
     * @param string $service as a service label, naming this service.
80
     * @param mixed  $loader  contains a pre-defined object, a string with classname or an
81
     *      callable which returns an instance of the service object. Its the way to 
82
     *      actually load, insantiate, the serviceobject.
83
     *
84
     * @return nothing.
0 ignored issues
show
Documentation introduced by
The doc-type nothing. could not be parsed: Unknown type name "nothing." at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
85
     */
86
    public function setShared($service, $loader)
87
    {
88
        $this->set($service, $loader, true);
89
    }
90
91
92
93
    /**
94
     * Get an instance of the service object, managing singletons.
95
     *
96
     * @param string $service as a service label, naming this service.
97
     *
98
     * @return object as instance of the service object.
99
     * @throws Exception when service accessed is not loaded. 
100
     */
101
    public function get($service)
102
    {
103
        // Is the service active?
104
        if (isset($this->active[$service])) {
105
            if ($this->loaded[$service]['singleton']) {
106
                return $this->active[$service];
107
            } else {
108
                return $this->load($service);
109
            }
110
        } elseif (isset($this->loaded[$service])) {
111
            // Is the service loaded?
112
            return $this->load($service);
113
        }
114
115
        throw new \Exception("CDI the service accessed '$service' is not loaded in the DI-container.");
116
    }
117
118
119
120
    /**
121
     * Magic method to get and create services. 
122
     * When created it is also stored as a parameter of this object.
123
     *
124
     * @param string $service name of class property not existing.
125
     *
126
     * @return class as the service requested.
127
     */
128
    public function __get($service)
129
    {
130
        return $this->get($service);
131
    }
132
133
134
135
    /**
136
     * Check if service exists by name.
137
     *
138
     * @param string $service as a service label, naming this service.
139
     *
140
     * @return boolean true if the service exists, otherwise false.
141
     */
142
    public function has($service)
143
    {
144
        return isset($this->loaded[$service])
145
            ? true
146
            : false;
147
    }
148
149
150
151
    /**
152
     * Magic method to get and create services. 
153
     * When created it is also stored as a parameter of this object.
154
     *
155
     * @param string $service   name of class property not existing.
156
     * @param array  $arguments currently NOT USED.
157
     *
158
     * @return class as the service requested.
159
     */
160
    public function __call($service, $arguments = [])
161
    {
162
        return $this->get($service);
163
    }
164
165
166
167
    /**
168
     * Lazy load a service object and create an instance of it.
169
     *
170
     * @param string $service as a service label, naming this service.
171
     *
172
     * @return object as instance of the service object.
173
     * @throws Exception when service could not be loaded. 
174
     */
175
    protected function load($service)
176
    {
177
        $sol = isset($this->loaded[$service]['loader'])
178
            ? $this->loaded[$service]['loader']
179
            : null;
180
181
        // Load by calling a function
182
        if (is_callable($sol)) {
183
184
            try {
185
                $this->active[$service] = $sol();
186
            } catch (\Exception $e) {
187
                throw new \Exception("CDI could not load service '$service'. Failed in the callback that instantiates the service. " . $e->getMessage());
188
            }
189
190
        } elseif (is_object($sol)) {
191
            // Load by pre-instantiated object
192
            $this->active[$service] = $sol;
193
194
        } elseif (is_string($sol)) {
195
            // Load by creating a new object from class-string
196
            $this->active[$service] = new $sol();
197
198
        } else {
199
            throw new Exception("CDI could not load service '$service'. It is unknown how to load it.");
200
        }
201
202
        $this->$service = $this->active[$service];
203
        return $this->active[$service];
204
    }
205
}
206