Passed
Push — 1.0.0-dev ( bb6d0b...e3bc99 )
by nguereza
03:11
created

Module::getModulesAutoloadConfig()   A

Complexity

Conditions 6
Paths 5

Size

Total Lines 24
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 6
eloc 18
nc 5
nop 0
dl 0
loc 24
rs 9.0444
c 1
b 1
f 0
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->info('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
            if (false !== $index = array_search($name, $this->list, true)) {
77
                $this->logger->info('Found the module at index [' . $index . '] remove it');
78
                unset($this->list[$index]);
79
            } else {
80
                $this->logger->info('Cannot found this module in the list');
81
            }
82
            return $this;
83
        }
84
        
85
        /**
86
         * Remove all the module. 
87
         */
88
        public function removeAll() {
89
            $this->logger->debug('Removing of all module ...');
90
            $this->list = array();
91
        }
92
93
         /**
94
         * Get the list of module loaded
95
         * @return array the module list
96
         */
97
        public function getModuleList() {
98
            return $this->list;
99
        }
100
101
        /**
102
         * Check if the application has an module
103
         * @return boolean
104
         */
105
        public function hasModule() {
106
            return !empty($this->list);
107
        }
108
		
109
        /**
110
         * Get the list of the custom autoload configuration from module if exists
111
         * @return array|boolean the autoload configurations list or false if no module contains the autoload configuration values
112
         */
113
        public function getModulesAutoloadConfig() {
114
            if (empty($this->list)) {
115
                $this->logger->info('No module was loaded skipping.');
116
                return false;
117
            }
118
            $autoloads = array();
119
            $autoloads['libraries'] = array();
120
            $autoloads['config']    = array();
121
            $autoloads['models']    = array();
122
            $autoloads['functions'] = array();
123
            $autoloads['languages'] = array();
124
			
125
            foreach ($this->list as $module) {
126
                $file = MODULE_PATH . $module . DS . 'config' . DS . 'autoload.php';
127
                if (file_exists($file)) {
128
                    $autoload = array();
129
                    require_once $file;
130
                    if (!empty($autoload) && is_array($autoload)) {
131
                        $autoloads = array_merge_recursive($autoloads, $autoload);
132
                        unset($autoload);
133
                    }
134
                }
135
            }
136
            return $autoloads;
137
        }
138
139
        /**
140
         * Get the list of the custom routes configuration from module if exists
141
         * @return array|boolean the routes list or false if no module contains the routes configuration
142
         */
143
        public function getModulesRoutesConfig() {
144
            if (empty($this->list)) {
145
                $this->logger->info('No module was loaded skipping.');
146
                return false;
147
            }
148
            $routes = array();
149
            foreach ($this->list as $module) {
150
                $file = MODULE_PATH . $module . DS . 'config' . DS . 'routes.php';
151
                if (file_exists($file)) {
152
                    $route = array();
153
                    require_once $file;
154
                    if (!empty($route) && is_array($route)) {
155
                        $routes = array_merge($routes, $route);
156
                        unset($route);
157
                    }
158
                }
159
            }
160
            return $routes;
161
        }
162
163
164
        /**
165
         * Check if in module list can have this controller
166
         * @see Module::findClassInModuleFullFilePath
167
         * @return boolean|string  false or null if no module have this controller, path the full path of the controller
168
         */
169
        public function findControllerFullPath($class, $module = null) {
170
            return $this->findClassInModuleFullFilePath($class, $module, 'controllers');
171
        }
172
173
        /**
174
         * Check if in module list can have this model
175
         * @see Module::findClassInModuleFullFilePath
176
         * @return boolean|string  false or null if no module have this model, return the full path of this model
177
         */
178
        public function findModelFullPath($class, $module = null) {
179
            return $this->findClassInModuleFullFilePath($class, $module, 'models');
180
        }
181
182
        /**
183
         * Check if in module list can have this library
184
         * @see Module::findClassInModuleFullFilePath
185
         * @return boolean|string  false or null if no module have this library,  return the full path of this library
186
         */
187
        public function findLibraryFullPath($class, $module = null) {
188
            return $this->findClassInModuleFullFilePath($class, $module, 'libraries');
189
        }
190
191
		
192
        /**
193
         * Check if in module list can have this config
194
         * @see  Module::findNonClassInModuleFullFilePath
195
         * @return boolean|string  false or null if no module have this configuration,  return the full path of this configuration
196
         */
197
        public function findConfigFullPath($configuration, $module = null) {
198
            return $this->findNonClassInModuleFullFilePath($configuration, $module, 'config');
199
        }
200
201
        /**
202
         * Check if in module list can have this helper
203
         * @see  Module::findNonClassInModuleFullFilePath
204
         * @return boolean|string  false or null if no module have this helper,  return the full path of this helper
205
         */
206
        public function findFunctionFullPath($helper, $module = null) {
207
            return $this->findNonClassInModuleFullFilePath($helper, $module, 'functions');
208
        }
209
210
        /**
211
         * Check if in module list can have this view
212
         * @see  Module::findNonClassInModuleFullFilePath
213
         * @return boolean|string  false or null if no module have this view, path the full path of the view
214
         */
215
        public function findViewFullPath($view, $module = null) {
216
            return $this->findNonClassInModuleFullFilePath($view, $module, 'views');
217
        }
218
219
        /**
220
         * Check if in module list can have this language
221
         * @see  Module::findNonClassInModuleFullFilePath
222
         * @return boolean|string  false or null if no module have this language,  return the full path of this language
223
         */
224
        public function findLanguageFullPath($language, $appLang, $module = null) {
225
            return $this->findNonClassInModuleFullFilePath($language, $module, 'lang', $appLang);
226
        }
227
228
        /**
229
         * Load the module list
230
         */
231
        protected function init() {
232
            $this->logger->debug('Check if the application contains the modules ...');
233
            $dirList = glob(MODULE_PATH . '*', GLOB_ONLYDIR);
234
            if ($dirList !== false) {
235
               $this->list = array_map('basename', $dirList);
236
            }
237
            if (!empty($this->list)) {
238
                $this->logger->info('The application contains the module below [' . implode(', ', $this->list) . ']');
239
            }
240
        }
241
242
        /**
243
         * Check if in module list can have the model, controller, library
244
         * @param  string $class the class name of library, model, controller
245
         * @param string $module the module name
246
         * @param string $type the name of the type "controllers", "libraries", "models"
247
         * @return boolean|string  false or null if no module 
248
         * have this class, return the full path of the class
249
         */
250
        protected function findClassInModuleFullFilePath($class, $module, $type) {
251
            $class = str_ireplace('.php', '', $class);
252
            $class = ucfirst($class);
253
            $classFile = $class . '.php';
254
            $this->logger->debug('Checking the class [' . $class . '] in module [' . $module . '] for [' . $type . '] ...');
255
            $filePath = MODULE_PATH . $module . DS . $type . DS . $classFile;
256
            if (file_exists($filePath)) {
257
                $this->logger->info('Found class [' . $class . '] in module [' . $module . '] for [' . $type . '] the file path is [' . $filePath . ']');
258
                return $filePath;
259
            }
260
            $this->logger->info('Class [' . $class . '] does not exist in the module [' . $module . '] for [' . $type . ']');
261
            return false;
262
        }
263
264
        /**
265
         * Check if in module list can have the config, view, helper, language
266
         * @param string $name the name of config, view, helper, language
267
         * @param string $module the module name
268
         * @param string $type the name of the type "config", "functions", "views", "lang"
269
         * @param string|null $appLang the application language. This is use only when $type = "lang"
270
         * @return boolean|string  false or null if no module 
271
         * have this resource, return the full path of the resource
272
         */
273
        protected function findNonClassInModuleFullFilePath($name, $module, $type, $appLang = null) {
274
            $name = str_ireplace('.php', '', $name);
275
            $file = $name . '.php';
276
            $filePath = MODULE_PATH . $module . DS . $type . DS . $file;
277
            switch ($type) {
278
                case 'functions':
279
                    $name = str_ireplace('function_', '', $name);
280
                    $file = 'function_' . $name . '.php';
281
                    $filePath = MODULE_PATH . $module . DS . $type . DS . $file;
282
                break;
283
                case 'views':
284
                    $name = trim($name, '/\\');
285
                    $name = str_ireplace('/', DS, $name);
286
                    $file = $name . '.php';
287
                    $filePath = MODULE_PATH . $module . DS . $type . DS . $file;
288
                break;
289
                case 'lang':
290
                    $name = str_ireplace('lang_', '', $name);
291
                    $file = 'lang_' . $name . '.php';
292
                    $filePath = MODULE_PATH . $module . DS . $type . DS . $appLang . DS . $file;
293
                break;
294
            }
295
            $this->logger->debug('Checking resource [' . $name . '] in module [' . $module . '] for [' . $type . '] ...');
296
            if (file_exists($filePath)) {
297
                $this->logger->info('Found resource [' . $name . '] in module [' . $module . '] for [' . $type . '] the file path is [' . $filePath . ']');
298
                return $filePath;
299
            }
300
            $this->logger->info('Resource [' . $name . '] does not exist in the module [' . $module . '] for [' . $type . ']');
301
            return false;
302
        }
303
304
    }
305