Module   A
last analyzed

Complexity

Total Complexity 38

Size/Duplication

Total Lines 282
Duplicated Lines 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
eloc 104
c 3
b 1
f 0
dl 0
loc 282
rs 9.36
wmc 38

18 Methods

Rating   Name   Duplication   Size   Complexity  
A findLanguageFullPath() 0 2 1
A findModelFullPath() 0 2 1
A findControllerFullPath() 0 2 1
A __construct() 0 5 2
A add() 0 7 2
A remove() 0 10 2
A removeAll() 0 3 1
A findLibraryFullPath() 0 2 1
A findConfigFullPath() 0 2 1
A findFunctionFullPath() 0 2 1
A getModulesAutoloadConfig() 0 24 6
A findViewFullPath() 0 2 1
A getModuleList() 0 2 1
A hasModule() 0 2 1
A getModulesRoutesConfig() 0 18 6
A findClassInModuleFullFilePath() 0 13 2
A init() 0 8 3
A findNonClassInModuleFullFilePath() 0 30 5
1
<?php
2
    defined('ROOT_PATH') || exit('Access denied');
3
    /**
4
     * TNH Framework
5
     *
6
     * A simple PHP framework using HMVC architecture
7
     *
8
     * This content is released under the MIT License (MIT)
9
     *
10
     * Copyright (c) 2017 TNH Framework
11
     *
12
     * Permission is hereby granted, free of charge, to any person obtaining a copy
13
     * of this software and associated documentation files (the "Software"), to deal
14
     * in the Software without restriction, including without limitation the rights
15
     * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16
     * copies of the Software, and to permit persons to whom the Software is
17
     * furnished to do so, subject to the following conditions:
18
     *
19
     * The above copyright notice and this permission notice shall be included in all
20
     * copies or substantial portions of the Software.
21
     *
22
     * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23
     * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24
     * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25
     * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26
     * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27
     * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28
     * SOFTWARE.
29
     */
30
   
31
    class Module extends BaseClass {
32
		
33
        /**
34
         * list of loaded module
35
         * @var array
36
         */
37
        private $list = array();
38
39
        /**
40
         * Construct new module
41
         * Initialize the module list by scanning the directory MODULE_PATH
42
         *
43
         * @param boolean $init whether to load the module list
44
         */
45
        public function __construct($init = true) {
46
            parent::__construct();
47
48
            if ($init) {
49
                $this->init();
50
            }
51
        }
52
		
53
        /**
54
         * Add new module in the list
55
         * @param string $name the name of the module
56
         *
57
         * @return object the current instance
58
         */
59
        public function add($name) {
60
            if (in_array($name, $this->list)) {
61
               $this->logger->warning('The module [' .$name. '] already added skipping.');
62
               return $this;
63
            }
64
            $this->list[] = $name;
65
            return $this;
66
        }
67
68
        /**
69
         * Remove the module from list
70
         * @param  string   $name the module name
71
         *
72
         * @return object the current instance
73
         */
74
        public function remove($name) {
75
            $this->logger->debug('Removing of the module [' . $name . '] ...');
76
            $index = array_search($name, $this->list, true);
77
            if ($index !== false) {
78
                $this->logger->info('Found the module at index [' . $index . '] remove it');
79
                unset($this->list[$index]);
80
                return $this;
81
            }
82
            $this->logger->warning('Cannot found this module in the list');
83
            return $this;
84
        }
85
        
86
        /**
87
         * Remove all the module. 
88
         */
89
        public function removeAll() {
90
            $this->logger->debug('Removing of all module ...');
91
            $this->list = array();
92
        }
93
94
         /**
95
         * Get the list of module loaded
96
         * @return array the module list
97
         */
98
        public function getModuleList() {
99
            return $this->list;
100
        }
101
102
        /**
103
         * Check if the application has an module
104
         * @return boolean
105
         */
106
        public function hasModule() {
107
            return !empty($this->list);
108
        }
109
		
110
        /**
111
         * Get the list of the custom autoload configuration from module if exists
112
         * @return array|boolean the autoload configurations list or false if no module contains the autoload configuration values
113
         */
114
        public function getModulesAutoloadConfig() {
115
            if (empty($this->list)) {
116
                $this->logger->info('No module was loaded skipping.');
117
                return false;
118
            }
119
            $autoloads = array();
120
            $autoloads['libraries'] = array();
121
            $autoloads['config']    = array();
122
            $autoloads['models']    = array();
123
            $autoloads['functions'] = array();
124
            $autoloads['languages'] = array();
125
			
126
            foreach ($this->list as $module) {
127
                $file = MODULE_PATH . $module . DS . 'config' . DS . 'autoload.php';
128
                if (file_exists($file)) {
129
                    $autoload = array();
130
                    require $file;
131
                    if (!empty($autoload) && is_array($autoload)) {
132
                        $autoloads = array_merge_recursive($autoloads, $autoload);
133
                        unset($autoload);
134
                    }
135
                }
136
            }
137
            return $autoloads;
138
        }
139
140
        /**
141
         * Get the list of the custom routes configuration from module if exists
142
         * @return array|boolean the routes list or false if no module 
143
         * contains the routes configuration
144
         */
145
        public function getModulesRoutesConfig() {
146
            if (empty($this->list)) {
147
                $this->logger->info('No module was loaded skipping.');
148
                return false;
149
            }
150
            $routes = array();
151
            foreach ($this->list as $module) {
152
                $file = MODULE_PATH . $module . DS . 'config' . DS . 'routes.php';
153
                if (file_exists($file)) {
154
                    $route = array();
155
                    require $file;
156
                    if (!empty($route) && is_array($route)) {
157
                        $routes = array_merge($routes, $route);
158
                        unset($route);
159
                    }
160
                }
161
            }
162
            return $routes;
163
        }
164
165
166
        /**
167
         * Check if in module list can have this controller
168
         * @see Module::findClassInModuleFullFilePath
169
         * @return boolean|string  false or null if no module have this 
170
         * controller, path the full path of the controller
171
         */
172
        public function findControllerFullPath($class, $module = null) {
173
            return $this->findClassInModuleFullFilePath($class, $module, 'controllers');
174
        }
175
176
        /**
177
         * Check if in module list can have this model
178
         * @see Module::findClassInModuleFullFilePath
179
         * @return boolean|string  false or null if no module 
180
         * have this model, return the full path of this model
181
         */
182
        public function findModelFullPath($class, $module = null) {
183
            return $this->findClassInModuleFullFilePath($class, $module, 'models');
184
        }
185
186
        /**
187
         * Check if in module list can have this library
188
         * @see Module::findClassInModuleFullFilePath
189
         * @return boolean|string  false or null if no module have 
190
         * this library,  return the full path of this library
191
         */
192
        public function findLibraryFullPath($class, $module = null) {
193
            return $this->findClassInModuleFullFilePath($class, $module, 'libraries');
194
        }
195
196
		
197
        /**
198
         * Check if in module list can have this config
199
         * @see  Module::findNonClassInModuleFullFilePath
200
         * @return boolean|string  false or null if no module have this configuration,
201
         *   return the full path of this configuration
202
         */
203
        public function findConfigFullPath($configuration, $module = null) {
204
            return $this->findNonClassInModuleFullFilePath($configuration, $module, 'config');
205
        }
206
207
        /**
208
         * Check if in module list can have this helper
209
         * @see  Module::findNonClassInModuleFullFilePath
210
         * @return boolean|string  false or null if no module have this 
211
         * helper,  return the full path of this helper
212
         */
213
        public function findFunctionFullPath($helper, $module = null) {
214
            return $this->findNonClassInModuleFullFilePath($helper, $module, 'functions');
215
        }
216
217
        /**
218
         * Check if in module list can have this view
219
         * @see  Module::findNonClassInModuleFullFilePath
220
         * @return boolean|string  false or null if no module have 
221
         * this view, path the full path of the view
222
         */
223
        public function findViewFullPath($view, $module = null) {
224
            return $this->findNonClassInModuleFullFilePath($view, $module, 'views');
225
        }
226
227
        /**
228
         * Check if in module list can have this language
229
         * @see  Module::findNonClassInModuleFullFilePath
230
         * @return boolean|string  false or null if no module have
231
         *  this language,  return the full path of this language
232
         */
233
        public function findLanguageFullPath($language, $appLang, $module = null) {
234
            return $this->findNonClassInModuleFullFilePath($language, $module, 'lang', $appLang);
235
        }
236
237
        /**
238
         * Load the module list
239
         */
240
        protected function init() {
241
            $this->logger->debug('Check if the application contains the modules ...');
242
            $dirList = glob(MODULE_PATH . '*', GLOB_ONLYDIR);
243
            if ($dirList !== false) {
244
               $this->list = array_map('basename', $dirList);
245
            }
246
            if (!empty($this->list)) {
247
                $this->logger->info('The application contains the module below [' . implode(', ', $this->list) . ']');
248
            }
249
        }
250
251
        /**
252
         * Check if in module list can have the model, controller, library
253
         * @param  string $class the class name of library, model, controller
254
         * @param string $module the module name
255
         * @param string $type the name of the type "controllers", "libraries", "models"
256
         * @return boolean|string  false or null if no module 
257
         * have this class, return the full path of the class
258
         */
259
        protected function findClassInModuleFullFilePath($class, $module, $type) {
260
            $class = str_ireplace('.php', '', $class);
261
            $class = ucfirst($class);
262
            $classFile = $class . '.php';
263
            $this->logger->debug('Checking the class [' . $class . '] in module [' . $module . '] for [' . $type . '] ...');
264
            $filePath = MODULE_PATH . $module . DS . $type . DS . $classFile;
265
            if (file_exists($filePath)) {
266
                $this->logger->info('Found class [' . $class . '] in module [' . $module . '] '
267
                                     . 'for [' . $type . '] the file path is [' . $filePath . ']');
268
                return $filePath;
269
            }
270
            $this->logger->info('Class [' . $class . '] does not exist in the module [' . $module . '] for [' . $type . ']');
271
            return false;
272
        }
273
274
        /**
275
         * Check if in module list can have the config, view, helper, language
276
         * @param string $name the name of config, view, helper, language
277
         * @param string $module the module name
278
         * @param string $type the name of the type "config", "functions", "views", "lang"
279
         * @param string|null $appLang the application language. This is use only when $type = "lang"
280
         * @return boolean|string  false or null if no module 
281
         * have this resource, return the full path of the resource
282
         */
283
        protected function findNonClassInModuleFullFilePath($name, $module, $type, $appLang = null) {
284
            $name = str_ireplace('.php', '', $name);
285
            $file = $name . '.php';
286
            $filePath = MODULE_PATH . $module . DS . $type . DS . $file;
287
            switch ($type) {
288
                case 'functions':
289
                    $name = str_ireplace('function_', '', $name);
290
                    $file = 'function_' . $name . '.php';
291
                    $filePath = MODULE_PATH . $module . DS . $type . DS . $file;
292
                break;
293
                case 'views':
294
                    $name = trim($name, '/\\');
295
                    $name = str_ireplace('/', DS, $name);
296
                    $file = $name . '.php';
297
                    $filePath = MODULE_PATH . $module . DS . $type . DS . $file;
298
                break;
299
                case 'lang':
300
                    $name = str_ireplace('lang_', '', $name);
301
                    $file = 'lang_' . $name . '.php';
302
                    $filePath = MODULE_PATH . $module . DS . $type . DS . $appLang . DS . $file;
303
                break;
304
            }
305
            $this->logger->debug('Checking resource [' . $name . '] in module [' . $module . '] for [' . $type . '] ...');
306
            if (file_exists($filePath)) {
307
                $this->logger->info('Found resource [' . $name . '] in module [' . $module . '] '
308
                                    . 'for [' . $type . '] the file path is [' . $filePath . ']');
309
                return $filePath;
310
            }
311
            $this->logger->info('Resource [' . $name . '] does not exist in the module [' . $module . '] for [' . $type . ']');
312
            return false;
313
        }
314
315
    }
316