Completed
Push — master ( b93a9c...e7c2e1 )
by Alexey
05:14
created

Module::sitemap()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 1
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
            return $event['eventObject'];
78
        });
79
    }
80
81
    /**
82
     * Get all posible directorys for module files
83
     * 
84
     * @param string $moduleName
85
     * @return array
86
     */
87
    public static function getModulePaths($moduleName)
88
    {
89
        $moduleName = ucfirst($moduleName);
90
        $paths = [];
91
        if (App::$cur !== App::$primary) {
92
            $paths['primaryAppPath'] = App::$primary->path . '/modules/' . $moduleName;
93
        }
94
        $paths['curAppPath'] = App::$cur->path . '/modules/' . $moduleName;
95
        $paths['systemPath'] = INJI_SYSTEM_DIR . '/modules/' . $moduleName;
96
        return $paths;
97
    }
98
99
    /**
100
     * Return directory where places module file
101
     * 
102
     * @param string $moduleName
103
     * @return string
104
     */
105
    public static function getModulePath($moduleName)
106
    {
107
        $moduleName = ucfirst($moduleName);
108
        $paths = Module::getModulePaths($moduleName);
109
        foreach ($paths as $path) {
110
            if (file_exists($path . '/' . $moduleName . '.php')) {
111
                return $path;
112
            }
113
        }
114
    }
115
116
    /**
117
     * Check module for installed
118
     * 
119
     * @param string $moduleName
120
     * @param \App $app
121
     * @return boolean
122
     */
123
    public static function installed($moduleName, $app)
124
    {
125
        if (in_array($moduleName, self::getInstalled($app))) {
126
            return true;
127
        }
128
        return FALSE;
129
    }
130
131
    /**
132
     * Get installed modules for app
133
     * 
134
     * @param \App $app
135
     * @param boolean|\App $primary
136
     * @return array
137
     */
138
    public static function getInstalled($app, $primary = false)
139
    {
140
        if (!$primary) {
141
            $primary = \App::$primary;
142
        }
143
        $system = !empty(Inji::$config['modules']) ? Inji::$config['modules'] : [];
144
        $primary = !empty($primary->config['modules']) ? $primary->config['modules'] : [];
145
        $actual = $app !== $primary && !empty($app->config['modules']) ? $app->config['modules'] : [];
146
        $modules = array_unique(array_merge($system, $primary, $actual));
147
        return $modules;
148
    }
149
150
    /**
151
     * Find module controllers
152
     * 
153
     * @param string $moduleName
154
     * @return array
155
     */
156
    public static function getModuleControllers($moduleName)
157
    {
158
        $controllers = [];
159
        $moduleDirs = static::getModulePaths($moduleName);
160
        foreach ($moduleDirs as $moduleDir) {
161
            if (is_dir($moduleDir)) {
162
                foreach (scandir($moduleDir) as $dir) {
163
                    if (preg_match('!Controllers$!', $dir) && is_dir($moduleDir . '/' . $dir)) {
164
                        $path = $moduleDir . '/' . $dir;
165
                        foreach (scandir($path) as $file) {
166
                            if (preg_match('!Controller\.php$!', $file) && is_file($path . '/' . $file)) {
167
                                $controllerName = preg_replace('!Controller\.php$!', '', $file);
168
                                $controllers[preg_replace('!Controllers$!', '', $dir)][$controllerName] = $path . '/' . $file;
169
                            }
170
                        }
171
                    }
172
                }
173
            }
174
        }
175
        return $controllers;
176
    }
177
178
    /**
179
     * Find module by request
180
     * 
181
     * @param \App $app
182
     * @return \Module
183
     */
184
    public static function resolveModule($app)
185
    {
186 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...
187
            $module = $app->{$app->params[0]};
188
            $module->params = array_slice($app->params, 1);
189
            return $module;
190
        }
191 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...
192
            $module = $app->{$app->config['defaultModule']};
193
            $module->params = $app->params;
194
            return $module;
195
        }
196
197
        if ($app->Main) {
198
            $module = $app->Main;
199
            $module->params = $app->params;
200
            return $module;
201
        }
202
        return null;
203
    }
204
205
    /**
206
     * Get posible path for controller
207
     * 
208
     * @return array
209
     */
210
    public function getControllerPaths()
211
    {
212
        $paths = [];
213
        if (App::$cur != App::$primary) {
214
            if (!empty($this->params[0]) && strtolower($this->params[0]) != strtolower($this->moduleName)) {
215
                $paths['primaryAppAppTypePath_slice'] = App::$primary->path . '/modules/' . $this->moduleName . '/' . $this->app->type . 'Controllers/' . ucfirst($this->params[0]) . 'Controller.php';
216
                if (App::$primary->{$this->moduleName}) {
217
                    $paths['primaryAppAppTypePath_slice'] = App::$primary->{$this->moduleName}->path . '/' . $this->app->type . 'Controllers/' . ucfirst($this->params[0]) . 'Controller.php';
218
                }
219
            }
220
            $paths['primaryAppAppAppTypePath'] = App::$primary->path . '/modules/' . $this->moduleName . '/' . $this->app->type . 'Controllers/' . $this->moduleName . 'Controller.php';
221
            if (App::$primary->{$this->moduleName}) {
222
                $paths['primaryAppAppTypePath'] = App::$primary->{$this->moduleName}->path . '/' . $this->app->type . 'Controllers/' . $this->moduleName . 'Controller.php';
223
            }
224
            $paths['curAppAppTypePath'] = $this->app->{$this->moduleName}->path . '/' . $this->app->type . 'Controllers/' . $this->moduleName . 'Controller.php';
225
        }
226
227
        if (!empty($this->params[0]) && strtolower($this->params[0]) != strtolower($this->moduleName)) {
228
            $paths['appAppTypePath_slice'] = $this->app->path . '/modules/' . $this->moduleName . '/' . $this->app->type . 'Controllers/' . ucfirst($this->params[0]) . 'Controller.php';
229
            $paths['appTypePath_slice'] = $this->path . '/' . $this->app->type . 'Controllers/' . ucfirst($this->params[0]) . 'Controller.php';
230
        }
231
        $paths['appAppTypePath'] = $this->app->path . '/modules/' . $this->moduleName . '/' . $this->app->type . 'Controllers/' . $this->moduleName . 'Controller.php';
232
        $paths['appTypePath'] = $this->path . '/' . $this->app->type . 'Controllers/' . $this->moduleName . 'Controller.php';
233
234
        if (!empty($this->params[0]) && strtolower($this->params[0]) != strtolower($this->moduleName)) {
235
            $paths['appUniversalPath_slice'] = $this->app->path . '/modules/' . $this->moduleName . '/Controllers/' . ucfirst($this->params[0]) . 'Controller.php';
236
            $paths['universalPath_slice'] = $this->path . '/Controllers/' . ucfirst($this->params[0]) . 'Controller.php';
237
        }
238
        $paths['appUniversalPath'] = $this->app->path . '/modules/' . $this->moduleName . '/Controllers/' . $this->moduleName . 'Controller.php';
239
        $paths['universalPath'] = $this->path . '/Controllers/' . $this->moduleName . 'Controller.php';
240
241
        return $paths;
242
    }
243
244
    /**
245
     * Find controller by request
246
     * 
247
     * @return \Controller
248
     */
249
    public function findController()
250
    {
251
        $paths = $this->getControllerPaths();
252
        foreach ($paths as $pathName => $path) {
253
            if (file_exists($path)) {
254
                include $path;
255
                if (strpos($pathName, 'slice')) {
256
                    $controllerName = ucfirst($this->params[0]);
257
                    $params = array_slice($this->params, 1);
258
                } else {
259
                    $controllerName = $this->moduleName;
260
                    $params = $this->params;
261
                }
262
                $fullControllerName = $controllerName . 'Controller';
263
                $controller = new $fullControllerName();
264
                $controller->params = $params;
265
                $controller->module = $this;
266
                $controller->path = pathinfo($path, PATHINFO_DIRNAME);
267
                $controller->name = $controllerName;
268
                return $controller;
269
            }
270
        }
271
    }
272
273
    /**
274
     * Return module info
275
     * 
276
     * @param string $moduleName
277
     * @return array
278
     */
279
    public static function getInfo($moduleName = '')
280
    {
281
        if (!$moduleName && get_called_class()) {
282
            $moduleName = get_called_class();
283
        } elseif (!$moduleName) {
284
            return [];
285
        }
286
        $paths = Module::getModulePaths($moduleName);
287
        foreach ($paths as $path) {
288
            if (file_exists($path . '/info.php')) {
289
                return include $path . '/info.php';
290
            }
291
        }
292
        return [];
293
    }
294
295
    /**
296
     * Return snippets by name
297
     * 
298
     * @param string $snippetsPath
299
     * @param boolean $extensions
300
     * @param string $dir
301
     * @param string $moduleName
302
     * @return array
303
     */
304
    public function getSnippets($snippetsPath, $extensions = true, $dir = '/snippets', $moduleName = '')
305
    {
306
        $moduleName = $moduleName ? $moduleName : $this->moduleName;
307
        $modulePaths = Module::getModulePaths($moduleName);
308
        $modulePaths['templatePath'] = App::$cur->view->template->path . '/modules/' . ucfirst($moduleName);
309
        $snippets = [];
310
        foreach ($modulePaths as $path) {
311
            if (file_exists($path . $dir . '/' . $snippetsPath)) {
312
                $snippetsPaths = array_slice(scandir($path . $dir . '/' . $snippetsPath), 2);
313
                foreach ($snippetsPaths as $snippetPath) {
314
                    if (is_dir($path . $dir . '/' . $snippetsPath . '/' . $snippetPath)) {
315
                        $snippets[$snippetPath] = include $path . $dir . '/' . $snippetsPath . '/' . $snippetPath . '/info.php';
316
                    } else {
317
                        $snippets[pathinfo($snippetPath, PATHINFO_FILENAME)] = include $path . $dir . '/' . $snippetsPath . '/' . $snippetPath;
318
                    }
319
                }
320
            }
321
        }
322
        if ($extensions) {
323
            $snippets = array_merge($snippets, $this->getExtensions('snippets', $snippetsPath));
324
        }
325
        return $snippets;
326
    }
327
328
    /**
329
     * Return extensions for type
330
     * 
331
     * @param string $extensionType
332
     * @param string $request
333
     * @return array
334
     */
335
    public function getExtensions($extensionType, $request)
336
    {
337
        $extensions = [];
338
        $modules = Module::getInstalled(App::$cur);
339
        $method = 'get' . ucfirst($extensionType);
340
        foreach ($modules as $module) {
341
            $extensions = array_merge($extensions, $this->{$method}($request, false, "/extensions/{$this->moduleName}/" . $extensionType, $module));
342
        }
343
        return $extensions;
344
    }
345
346
    function sitemap()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
347
    {
348
        return [];
349
    }
350
351
}
352