Completed
Push — master ( 3029f1...135fa8 )
by Alexey
05:33
created

Module::getInfo()   B

Complexity

Conditions 6
Paths 7

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 10
nc 7
nop 1
dl 0
loc 15
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Module
5
 *
6
 * @author Alexey Krupskiy <[email protected]>
7
 * @link http://inji.ru/
8
 * @copyright 2015 Alexey Krupskiy
9
 * @license https://github.com/injitools/cms-Inji/blob/master/LICENSE
10
 */
11
class Module
12
{
13
    /**
14
     * Storage of cur requested module
15
     * 
16
     * @var \Module
17
     */
18
    public static $cur = null;
19
20
    /**
21
     * Module name
22
     * 
23
     * @var string
24
     */
25
    public $moduleName = '';
26
27
    /**
28
     * Module config
29
     * 
30
     * @var array
31
     */
32
    public $config = [];
33
34
    /**
35
     * Module info
36
     * 
37
     * @var array
38
     */
39
    public $info = [];
40
41
    /**
42
     * Requested module params
43
     * 
44
     * @var array
45
     */
46
    public $params = [];
47
48
    /**
49
     * Module directory path
50
     * 
51
     * @var string 
52
     */
53
    public $path = '';
54
55
    /**
56
     * Module app
57
     * 
58
     * @var \App
59
     */
60
    public $app = null;
61
62
    /**
63
     * Parse cur module
64
     * 
65
     * @param \App $app
66
     */
67
    public function __construct($app)
68
    {
69
        $this->app = $app;
70
        $this->moduleName = get_class($this);
71
        $this->path = Router::getLoadedClassPath($this->moduleName);
72
        $this->info = $this->getInfo();
73
        $this->config = Config::module($this->moduleName, !empty($this->info['systemConfig']));
74
        $that = $this;
75
        Inji::$inst->listen('Config-change-module-' . $this->app->name . '-' . $this->moduleName, $this->app->name . '-' . $this->moduleName . 'config', function($event) use ($that) {
76
            $that->config = $event['eventObject'];
77
        });
78
    }
79
80
    /**
81
     * Get all posible directorys for module files
82
     * 
83
     * @param string $moduleName
84
     * @return array
85
     */
86
    public static function getModulePaths($moduleName)
87
    {
88
        $moduleName = ucfirst($moduleName);
89
        $paths = [];
90
        if (App::$cur !== App::$primary) {
91
            $paths['primaryAppPath'] = App::$primary->path . '/modules/' . $moduleName;
92
        }
93
        $paths['curAppPath'] = App::$cur->path . '/modules/' . $moduleName;
94
        $paths['systemPath'] = INJI_SYSTEM_DIR . '/modules/' . $moduleName;
95
        return $paths;
96
    }
97
98
    /**
99
     * Return directory where places module file
100
     * 
101
     * @param string $moduleName
102
     * @return string
103
     */
104
    public static function getModulePath($moduleName)
105
    {
106
        $moduleName = ucfirst($moduleName);
107
        $paths = Module::getModulePaths($moduleName);
108
        foreach ($paths as $path) {
109
            if (file_exists($path . '/' . $moduleName . '.php')) {
110
                return $path;
111
            }
112
        }
113
    }
114
115
    /**
116
     * Check module for installed
117
     * 
118
     * @param string $moduleName
119
     * @param \App $app
120
     * @return boolean
121
     */
122
    public static function installed($moduleName, $app)
123
    {
124
        if (in_array($moduleName, self::getInstalled($app))) {
125
            return true;
126
        }
127
        return FALSE;
128
    }
129
130
    /**
131
     * Get installed modules for app
132
     * 
133
     * @param \App $app
134
     * @param boolean|\App $primary
135
     * @return array
136
     */
137
    public static function getInstalled($app, $primary = false)
138
    {
139
        if (!$primary) {
140
            $primary = \App::$primary;
141
        }
142
        $system = !empty(Inji::$config['modules']) ? Inji::$config['modules'] : [];
143
        $primary = !empty($primary->config['modules']) ? $primary->config['modules'] : [];
144
        $actual = $app !== $primary && !empty($app->config['modules']) ? $app->config['modules'] : [];
145
        $modules = array_unique(array_merge($system, $primary, $actual));
146
        return $modules;
147
    }
148
149
    /**
150
     * Find module controllers
151
     * 
152
     * @param string $moduleName
153
     * @return array
154
     */
155
    public static function getModuleControllers($moduleName)
156
    {
157
        $controllers = [];
158
        $moduleDirs = static::getModulePaths($moduleName);
159
        foreach ($moduleDirs as $moduleDir) {
160
            if (is_dir($moduleDir)) {
161
                foreach (scandir($moduleDir) as $dir) {
162
                    if (preg_match('!Controllers$!', $dir) && is_dir($moduleDir . '/' . $dir)) {
163
                        $path = $moduleDir . '/' . $dir;
164
                        foreach (scandir($path) as $file) {
165
                            if (preg_match('!Controller\.php$!', $file) && is_file($path . '/' . $file)) {
166
                                $controllerName = preg_replace('!Controller\.php$!', '', $file);
167
                                $controllers[preg_replace('!Controllers$!', '', $dir)][$controllerName] = $path . '/' . $file;
168
                            }
169
                        }
170
                    }
171
                }
172
            }
173
        }
174
        return $controllers;
175
    }
176
177
    /**
178
     * Find module by request
179
     * 
180
     * @param \App $app
181
     * @return \Module
182
     */
183
    public static function resolveModule($app)
184
    {
185 View Code Duplication
        if (!empty($app->params[0]) && $app->{$app->params[0]}) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
186
            $module = $app->{$app->params[0]};
187
            $module->params = array_slice($app->params, 1);
188
            return $module;
189
        }
190 View Code Duplication
        if (!empty($app->config['defaultModule']) && $app->{$app->config['defaultModule']}) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
191
            $module = $app->{$app->config['defaultModule']};
192
            $module->params = $app->params;
193
            return $module;
194
        }
195
196
        if ($app->Main) {
197
            $module = $app->Main;
198
            $module->params = $app->params;
199
            return $module;
200
        }
201
        return null;
202
    }
203
204
    /**
205
     * Get posible path for controller
206
     * 
207
     * @return array
208
     */
209
    public function getControllerPaths()
210
    {
211
        $paths = [];
212
        if (App::$cur != App::$primary) {
213
            if (!empty($this->params[0]) && strtolower($this->params[0]) != strtolower($this->moduleName)) {
214
                $paths['primaryAppAppTypePath_slice'] = App::$primary->path . '/modules/' . $this->moduleName . '/' . $this->app->type . 'Controllers/' . ucfirst($this->params[0]) . 'Controller.php';
215
                if (App::$primary->{$this->moduleName}) {
216
                    $paths['primaryAppAppTypePath_slice'] = App::$primary->{$this->moduleName}->path . '/' . $this->app->type . 'Controllers/' . ucfirst($this->params[0]) . 'Controller.php';
217
                }
218
            }
219
            $paths['primaryAppAppAppTypePath'] = App::$primary->path . '/modules/' . $this->moduleName . '/' . $this->app->type . 'Controllers/' . $this->moduleName . 'Controller.php';
220
            if (App::$primary->{$this->moduleName}) {
221
                $paths['primaryAppAppTypePath'] = App::$primary->{$this->moduleName}->path . '/' . $this->app->type . 'Controllers/' . $this->moduleName . 'Controller.php';
222
            }
223
            $paths['curAppAppTypePath'] = $this->app->{$this->moduleName}->path . '/' . $this->app->type . 'Controllers/' . $this->moduleName . 'Controller.php';
224
        }
225
226
        if (!empty($this->params[0]) && strtolower($this->params[0]) != strtolower($this->moduleName)) {
227
            $paths['appAppTypePath_slice'] = $this->app->path . '/modules/' . $this->moduleName . '/' . $this->app->type . 'Controllers/' . ucfirst($this->params[0]) . 'Controller.php';
228
            $paths['appTypePath_slice'] = $this->path . '/' . $this->app->type . 'Controllers/' . ucfirst($this->params[0]) . 'Controller.php';
229
        }
230
        $paths['appAppTypePath'] = $this->app->path . '/modules/' . $this->moduleName . '/' . $this->app->type . 'Controllers/' . $this->moduleName . 'Controller.php';
231
        $paths['appTypePath'] = $this->path . '/' . $this->app->type . 'Controllers/' . $this->moduleName . 'Controller.php';
232
233
        if (!empty($this->params[0]) && strtolower($this->params[0]) != strtolower($this->moduleName)) {
234
            $paths['appUniversalPath_slice'] = $this->app->path . '/modules/' . $this->moduleName . '/Controllers/' . ucfirst($this->params[0]) . 'Controller.php';
235
            $paths['universalPath_slice'] = $this->path . '/Controllers/' . ucfirst($this->params[0]) . 'Controller.php';
236
        }
237
        $paths['appUniversalPath'] = $this->app->path . '/modules/' . $this->moduleName . '/Controllers/' . $this->moduleName . 'Controller.php';
238
        $paths['universalPath'] = $this->path . '/Controllers/' . $this->moduleName . 'Controller.php';
239
240
        return $paths;
241
    }
242
243
    /**
244
     * Find controller by request
245
     * 
246
     * @return \Controller
247
     */
248
    public function findController()
249
    {
250
        $paths = $this->getControllerPaths();
251
        foreach ($paths as $pathName => $path) {
252
            if (file_exists($path)) {
253
                include $path;
254
                if (strpos($pathName, 'slice')) {
255
                    $controllerName = ucfirst($this->params[0]);
256
                    $params = array_slice($this->params, 1);
257
                } else {
258
                    $controllerName = $this->moduleName;
259
                    $params = $this->params;
260
                }
261
                $fullControllerName = $controllerName . 'Controller';
262
                $controller = new $fullControllerName();
263
                $controller->params = $params;
264
                $controller->module = $this;
265
                $controller->path = pathinfo($path, PATHINFO_DIRNAME);
266
                $controller->name = $controllerName;
267
                return $controller;
268
            }
269
        }
270
    }
271
272
    /**
273
     * Return module info
274
     * 
275
     * @param string $moduleName
276
     * @return array
277
     */
278
    public static function getInfo($moduleName = '')
279
    {
280
        if (!$moduleName && get_called_class()) {
281
            $moduleName = get_called_class();
282
        } elseif (!$moduleName) {
283
            return [];
284
        }
285
        $paths = Module::getModulePaths($moduleName);
286
        foreach ($paths as $path) {
287
            if (file_exists($path . '/info.php')) {
288
                return include $path . '/info.php';
289
            }
290
        }
291
        return [];
292
    }
293
294
    /**
295
     * Return snippets by name
296
     * 
297
     * @param string $snippetsPath
298
     * @param boolean $extensions
299
     * @param string $dir
300
     * @param string $moduleName
301
     * @return array
302
     */
303
    public function getSnippets($snippetsPath, $extensions = true, $dir = '/snippets', $moduleName = '')
304
    {
305
        $moduleName = $moduleName ? $moduleName : $this->moduleName;
306
        $modulePaths = Module::getModulePaths($moduleName);
307
        $modulePaths['templatePath'] = App::$cur->view->template->path . '/modules/' . ucfirst($moduleName);
308
        $snippets = [];
309
        foreach ($modulePaths as $path) {
310
            if (file_exists($path . $dir . '/' . $snippetsPath)) {
311
                $snippetsPaths = array_slice(scandir($path . $dir . '/' . $snippetsPath), 2);
312
                foreach ($snippetsPaths as $snippetPath) {
313
                    if (is_dir($path . $dir . '/' . $snippetsPath . '/' . $snippetPath)) {
314
                        $snippets[$snippetPath] = include $path . $dir . '/' . $snippetsPath . '/' . $snippetPath . '/info.php';
315
                    } else {
316
                        $snippets[pathinfo($snippetPath, PATHINFO_FILENAME)] = include $path . $dir . '/' . $snippetsPath . '/' . $snippetPath;
317
                    }
318
                }
319
            }
320
        }
321
        if ($extensions) {
322
            $snippets = array_merge($snippets, $this->getExtensions('snippets', $snippetsPath));
323
        }
324
        return $snippets;
325
    }
326
327
    /**
328
     * Return extensions for type
329
     * 
330
     * @param string $extensionType
331
     * @param string $request
332
     * @return array
333
     */
334
    public function getExtensions($extensionType, $request)
335
    {
336
        $extensions = [];
337
        $modules = Module::getInstalled(App::$cur);
338
        $method = 'get' . ucfirst($extensionType);
339
        foreach ($modules as $module) {
340
            $extensions = array_merge($extensions, $this->{$method}($request, false, "/extensions/{$this->moduleName}/" . $extensionType, $module));
341
        }
342
        return $extensions;
343
    }
344
345
}
346