Passed
Push — master ( 39dd55...cb4ea4 )
by Mikael
02:08
created

DI::getActiveServices()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
3
namespace Anax\DI;
4
5
use Anax\DI\Exception\NotFoundException;
6
use Anax\DI\Exception\Exception;
7
8
/**
9
 * Anax base class implementing Dependency Injection / Service Locator of
10
 * the services used by the framework, using lazy loading.
11
 */
12
class DI implements DIInterface
13
{
14
    /**
15
     * @var array $loaded  Store all lazy loaded services, ready to be
16
     *                     instantiated.
17
     * @var array $active  A service is instantiated into this array,
18
     *                     once its accessed.
19
     */
20
    public $loaded = [];
21
    public $active = [];
22
23
24
25
    /**
26
     * Finds an entry in the container by its identifier and returns it.
27
     * If the service is active/singelton then that instance is returned,
28
     * else the service is loaded and a new instance is returned.
29
     *
30
     * @param string $service     Identifier of the entry to look for.
31
     *
32
     * @throws NotFoundException  No entry was found for this identifier.
33
     * @throws Exception          Error while retrieving the entry.
34
     *
35
     * @return mixed Entry.
36
     */
37 5
    public function get($service)
38
    {
39 5
        if (!$this->has($service)) {
40
            throw new NotFoundException("The service '$service' is not loaded in the DI-container.");
41
        }
42
43 5
        if (isset($this->active[$service])
44 5
            && $this->loaded[$service]['singleton']) {
45
            return $this->active[$service];
46
        }
47
48 5
        return $this->load($service);
49
    }
50
51
52
53
    /**
54
     * Load a prepared service object and create an instance of it.
55
     *
56
     * @param string $service as a service label, naming this service.
57
     *
58
     * @throws Exception when service could not be loaded.
59
     *
60
     * @return object as instance of the service object.
61
     */
62 5
    protected function load($service)
63
    {
64 5
        $sol = isset($this->loaded[$service]['loader'])
65 5
            ? $this->loaded[$service]['loader']
66 5
            : null;
67
68 5
        if (is_callable($sol)) {
69
            // Load by calling a function
70
            try {
71 3
                $this->active[$service] = $sol();
72 3
            } catch (\Exception $e) {
73
                throw new Exception(
74
                    "Could not load service '$service'."
75
                    . "Failed in the callback that instantiates the service. "
76
                    . $e->getMessage()
77
                );
78
            }
79 5
        } elseif (is_object($sol)) {
80
            // Load by pre-instantiated object
81 1
            $this->active[$service] = $sol;
82 2
        } elseif (is_string($sol)) {
83
            // Load by creating a new object from class-string
84 1
            if (!class_exists($sol)) {
85
                throw new Exception(
86
                    "Could not load service '$service'."
87
                    . "Class '$sol' does not exists."
88
                );
89
            }
90 1
            $this->active[$service] = new $sol();
91 1
        } else {
92
            throw new Exception("The service '$service' has no init mechanism.");
93
        }
94
95 5
        $this->$service = $this->active[$service];
96 5
        return $this->active[$service];
97
    }
98
99
100
101
    /**
102
     * Returns true if the container can return an entry for the given
103
     * identifier, otherwise false
104
     *
105
     * @param string $service Identifier of the entry to look for.
106
     *
107
     * @return boolean
108
     */
109 5
    public function has($service)
110
    {
111 5
        return isset($this->loaded[$service])
112 5
            ? true
113 5
            : false;
114
    }
115
116
117
118
    /**
119
     * Set a service and connect it to a task which creates the object
120
     * (lazy loading).
121
     *
122
     * @param string  $service   as a service label, naming this service.
123
     * @param mixed   $loader    contains a pre-defined object, a string with
124
     *                           class name or an callable which returns an
125
     *                           instance of the service object. Its the way
126
     *                           to actually load, insantiate, the service
127
     *                           object.
128
     *
129
     * @return void
130
     */
131 5
    public function set($service, $loader)
132
    {
133 5
        $this->loaded[$service]['loader'] = $loader;
134 5
        $this->loaded[$service]['singleton'] = false;
135 5
    }
136
137
138
139
    /**
140
     * Set a singleton service and connect it to a task which creates the
141
     * object (lazy loading).
142
     *
143
     * @param string $service as a service label, naming this service.
144
     * @param mixed   $loader    contains a pre-defined object, a string with
145
     *                           class name or an callable which returns an
146
     *                           instance of the service object. Its the way
147
     *                           to actually load, insantiate, the service
148
     *                           object.
149
     *
150
     * @return void
151
     */
152 4
    public function setShared($service, $loader)
153
    {
154 4
        $this->loaded[$service]['loader'] = $loader;
155 4
        $this->loaded[$service]['singleton'] = true;
156 4
    }
157
158
159
160
    /**
161
     * Return an arry with all loaded services names.
162
     *
163
     * @return void
164
     */
165 2
    public function getServices()
166
    {
167 2
        return array_keys($this->loaded);
168
    }
169
170
171
172
    /**
173
     * Return an arry with all active services names.
174
     *
175
     * @return void
176
     */
177
    public function getActiveServices()
178
    {
179
        return array_keys($this->active);
180
    }
181
182
183
184
    /**
185
     * Return an array with all loaded services that are controllers.
186
     *
187
     * @return void
188
     */
189
/*
190
    public function getControllers()
191
    {
192
        return array_filter(
193
            array_keys($this->loaded),
194
            function ($val) {
195
                return strpos($val, "Controller") !== false;
196
            }
197
        );
198
    }
199
*/
200
201
202
    /**
203
     * Magic method to get and create services.
204
     * When created it is also stored as a parameter of this object.
205
     *
206
     * @param string $service name of class property not existing.
207
     *
208
     * @return class as the service requested.
209
     */
210
/*    
211
    public function __get($service)
212
    {
213
        return $this->get($service);
214
    }
215
*/
216
217
218
    /**
219
     * Magic method to get and create services.
220
     * When created it is also stored as a parameter of this object.
221
     *
222
     * @param string $service   name of class property not existing.
223
     * @param array  $arguments currently NOT USED.
224
     *
225
     * @return class as the service requested.
226
     */
227
/*
228
    public function __call($service, $arguments = [])
229
    {
230
        return $this->get($service);
231
    }
232
*/
233
}
234