Loader   A
last analyzed

Complexity

Total Complexity 23

Size/Duplication

Total Lines 255
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 255
rs 10
c 0
b 0
f 0
wmc 23
lcom 1
cbo 0

17 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A action() 0 6 1
A controller() 0 9 2
A hasController() 0 4 1
A addController() 0 6 1
A getController() 0 4 1
A getControllerName() 0 6 2
A model() 0 9 2
A hasModel() 0 4 1
A addModel() 0 6 1
A getModel() 0 4 1
A getModelName() 0 6 2
A middleware() 0 9 2
A hasMiddleware() 0 4 1
A addMiddleware() 0 6 1
A getMiddleware() 0 4 1
A getMiddlewareName() 0 6 2
1
<?php
2
3
namespace System;
4
5
class Loader
6
{
7
    /**
8
     * Application Object
9
     *
10
     * @var \System\Application
11
     */
12
    private $app;
13
14
    /**
15
     * Controllers container
16
     *
17
     * @var array
18
     */
19
    private $controllers = [];
20
21
    /**
22
     * Models container
23
     *
24
     * @var array
25
     */
26
    private $models = [];
27
28
    /**
29
     * Models container
30
     *
31
     * @var array
32
     */
33
    private $middlewares = [];
34
35
    /**
36
     * Constructor
37
     *
38
     * @param \System\Application $app
39
     */
40
    public function __construct(Application $app)
41
    {
42
        $this->app = $app;
43
    }
44
45
    /**
46
     * Call the given controller with the given method
47
     * and pass the given arguments to the controller method
48
     *
49
     * @param string $controller
50
     * @param string $method
51
     * @param array $arguments
52
     * @return mixed
53
     */
54
    public function action(string $controller, string $method, array $arguments)
55
    {
56
        $object = $this->controller($controller);
57
58
        return call_user_func([$object, $method], $arguments);
59
    }
60
61
    /**
62
     * Call the given controller
63
     *
64
     * @param string $controller
65
     * @return object
66
     */
67
    public function controller(string $controller)
68
    {
69
        $controller = $this->getControllerName($controller);
70
71
        if (!$this->hasController($controller)) {
72
            $this->addController($controller);
73
        }
74
        return $this->getController($controller);
75
    }
76
77
    /**
78
     * Determine if the given class|controller exists
79
     * in the controllers container
80
     *
81
     * @param string $controller
82
     * @return bool
83
     */
84
    private function hasController($controller)
85
    {
86
        return array_key_exists($controller, $this->controllers);
87
    }
88
89
    /**
90
     * Create new object for the given controller and store it
91
     * in controllers container
92
     *
93
     * @param string $controller
94
     * @return void
95
     */
96
    private function addController($controller)
97
    {
98
        $object = new $controller($this->app);
99
100
        $this->controllers[$controller] = $object;
101
    }
102
103
    /**
104
     * Get the controller object
105
     *
106
     * @param string $controller
107
     * @return object
108
     */
109
    private function getController($controller)
110
    {
111
        return $this->controllers[$controller];
112
    }
113
114
115
    /**
116
     * Get the full class name for the given controller
117
     *
118
     * @param string $controller
119
     * @return string
120
     */
121
    private function getControllerName($controller)
122
    {
123
        $controller .= strpos($controller, 'Controller') ? '' : 'Controller';
124
125
        return 'app\\Controllers\\' . $controller;
126
    }
127
128
    /**
129
     * Call the given model
130
     *
131
     * @param string $model
132
     * @return object
133
     */
134
    public function model(string $model)
135
    {
136
        $model = $this->getModelName($model);
137
138
        if (!$this->hasModel($model)) {
139
            $this->addModel($model);
140
        }
141
        return $this->getModel($model);
142
    }
143
144
    /**
145
     * Determine if the given class|model exists
146
     * in the models container
147
     *
148
     * @param string $model
149
     * @return bool
150
     */
151
    private function hasModel($model)
152
    {
153
        return array_key_exists($model, $this->models);
154
    }
155
156
    /**
157
     * Create new object for the given model and store it
158
     * in models container
159
     *
160
     * @param string $model
161
     * @return void
162
     */
163
    private function addModel($model)
164
    {
165
        $object = new $model($this->app);
166
167
        $this->models[$model] = $object;
168
    }
169
170
    /**
171
     * Get the model object
172
     *
173
     * @param string $model
174
     * @return object
175
     */
176
    private function getModel($model)
177
    {
178
        return $this->models[$model];
179
    }
180
181
    /**
182
     * Get the full class name for the given model
183
     *
184
     * @param string $model
185
     * @return string
186
     */
187
    private function getModelName($model)
188
    {
189
        $model .= strpos($model, 'Model') ? '' : 'Model';
190
191
        return 'app\\Models\\' . $model;
192
    }
193
194
    /**
195
     * Call the given middleware
196
     *
197
     * @param string $middleware
198
     * @return object
199
     */
200
    public function middleware(string $middleware)
201
    {
202
        $middleware = $this->getMiddlewareName($middleware);
203
204
        if (!$this->hasMiddleware($middleware)) {
205
            $this->addMiddleware($middleware);
206
        }
207
        return $this->getMiddleware($middleware);
208
    }
209
210
    /**
211
     * Determine if the given class|middleware exists
212
     * in the middlewares container
213
     *
214
     * @param string $middleware
215
     * @return bool
216
     */
217
    private function hasMiddleware($middleware)
218
    {
219
        return array_key_exists($middleware, $this->middlewares);
220
    }
221
222
    /**
223
     * Create new object for the given middleware and store it
224
     * in middlewares container
225
     *
226
     * @param string $middleware
227
     * @return void
228
     */
229
    private function addMiddleware($middleware)
230
    {
231
        $object = new $middleware($this->app);
232
233
        $this->middlewares[$middleware] = $object;
234
    }
235
236
    /**
237
     * Get the middleware object
238
     *
239
     * @param string $middleware
240
     * @return object
241
     */
242
    private function getMiddleware($middleware)
243
    {
244
        return $this->middlewares[$middleware];
245
    }
246
247
    /**
248
     * Get the full class name for the given middleware
249
     *
250
     * @param string $middleware
251
     * @return string
252
     */
253
    private function getMiddlewareName($middleware)
254
    {
255
        $middleware .= strpos($middleware, 'Middleware') ? '' : 'Middleware';
256
257
        return 'app\\Middlewares\\' . $middleware;
258
    }
259
}
260