Passed
Push — 1.0.0-dev ( 024223...5d8234 )
by nguereza
02:46
created

Module::findClassInModuleFullFilePath()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

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