Passed
Push — 1.0.0-dev ( 066288...93958a )
by nguereza
09:45
created

Module::findModelFullPath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 1
eloc 1
c 1
b 1
f 0
nc 1
nop 2
dl 0
loc 2
rs 10
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 GNU GPL License (GPL)
9
     *
10
     * Copyright (C) 2017 Tony NGUEREZA
11
     *
12
     * This program is free software; you can redistribute it and/or
13
     * modify it under the terms of the GNU General Public License
14
     * as published by the Free Software Foundation; either version 3
15
     * of the License, or (at your option) any later version.
16
     *
17
     * This program is distributed in the hope that it will be useful,
18
     * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
     * GNU General Public License for more details.
21
     *
22
     * You should have received a copy of the GNU General Public License
23
     * along with this program; if not, write to the Free Software
24
     * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
25
    */
26
   
27
	class Module{
28
		
29
		/**
30
		 * list of loaded module
31
		 * @var array
32
		 */
33
		private static $list = array();
34
35
		/**
36
		 * logger instance
37
		 * @var object
38
		 */
39
		private static $logger;
40
41
		/**
42
		 * Initialise the module list by scanning the directory MODULE_PATH
43
		 */
44
		public function init(){
45
			$logger = self::getLogger();
46
			$logger->debug('Check if the application contains the modules ...');
47
			$moduleDir = opendir(MODULE_PATH);
48
			if (is_resource($moduleDir)){
49
				while(($module = readdir($moduleDir)) !== false){
50
					if (preg_match('/^([a-z0-9-_]+)$/i', $module) && is_dir(MODULE_PATH . $module)){
51
						self::$list[] = $module;
52
					}
53
					else{
54
						$logger->info('Skipping [' .$module. '], may be this is not a directory or does not exists or is invalid name');
55
					}
56
				}
57
				closedir($moduleDir);
58
			}
59
			ksort(self::$list);
60
			
61
			if (! empty(self::$list)){
62
				$logger->info('The application contains the module below [' . implode(', ', self::getModuleList()) . ']');
63
			}
64
		}
65
66
		/**
67
		 * The signleton of the logger
68
		 * @return Object the Log instance
69
		 */
70
		public static function getLogger(){
71
			if (self::$logger == null){
72
				$logger = array();
73
				$logger[0] =& class_loader('Log', 'classes');
74
				$logger[0]->setLogger('Library::Module');
75
				self::$logger = $logger[0];
76
			}
77
			return self::$logger;			
78
		}
79
80
		/**
81
		 * Set the log instance for future use
82
		 * @param object $logger the log object
83
		 * @return object the log instance
84
		 */
85
		public static function setLogger($logger){
86
			self::$logger = $logger;
87
			return self::$logger;
88
		}
89
90
		
91
92
		/**
93
		 * Add new module in the list
94
		 * @param string $name the name of the module
95
		 *
96
		 * @return object the current instance
97
		 */
98
		public function add($name){
99
			self::$list[] = $name;
100
			return $this;
101
		}
102
		
103
		/**
104
		 * Get the list of the custom autoload configuration from module if exists
105
		 * @return array|boolean the autoload configurations list or false if no module contains the autoload configuration values
106
		 */
107
		public static function getModulesAutoloadConfig(){
108
			$logger = self::getLogger();
109
			if (empty(self::$list)){
110
				$logger->info('No module was loaded skipping.');
111
				return false;
112
			}
113
			$autoloads = array();
114
			$autoloads['libraries'] = array();
115
			$autoloads['config']    = array();
116
			$autoloads['models']    = array();
117
			$autoloads['functions'] = array();
118
			$autoloads['languages'] = array();
119
			
120
			foreach (self::$list as $module) {
121
				$file = MODULE_PATH . $module . DS . 'config' . DS . 'autoload.php';
122
				if (file_exists($file)){
123
					$autoload = array();
124
					require_once $file;
125
					if (! empty($autoload) && is_array($autoload)){
126
						$autoloads = array_merge_recursive($autoloads, $autoload);
127
						unset($autoload);
128
					}
129
				}
130
			}
131
			return $autoloads;
132
		}
133
134
		/**
135
		 * Get the list of the custom routes configuration from module if exists
136
		 * @return array|boolean the routes list or false if no module contains the routes configuration
137
		 */
138
		public static function getModulesRoutesConfig(){
139
			$logger = self::getLogger();
140
			if (empty(self::$list)){
141
				$logger->info('No module was loaded skipping.');
142
				return false;
143
			}
144
			$routes = array();
145
			foreach (self::$list as $module) {
146
				$file = MODULE_PATH . $module . DS . 'config' . DS . 'routes.php';
147
				if (file_exists($file)){
148
					$route = array();
149
					require_once $file;
150
					if (! empty($route) && is_array($route)){
151
						$routes = array_merge($routes, $route);
152
						unset($route);
153
					}
154
				}
155
			}
156
			return $routes;
157
		}
158
159
160
		/**
161
		 * Check if in module list can have this controller
162
		 * @see Module::findClassInModuleFullFilePath
163
		 * @return boolean|string  false or null if no module have this controller, path the full path of the controller
164
		 */
165
		public static function findControllerFullPath($class, $module = null){
166
			return self::findClassInModuleFullFilePath($class, $module, 'controllers');
167
		}
168
169
		/**
170
		 * Check if in module list can have this model
171
		 * @see Module::findClassInModuleFullFilePath
172
		 * @return boolean|string  false or null if no module have this model, return the full path of this model
173
		 */
174
		public static function findModelFullPath($class, $module = null){
175
			return self::findClassInModuleFullFilePath($class, $module, 'models');
176
		}
177
178
		/**
179
		 * Check if in module list can have this library
180
		 * @see Module::findClassInModuleFullFilePath
181
		 * @return boolean|string  false or null if no module have this library,  return the full path of this library
182
		 */
183
		public static function findLibraryFullPath($class, $module = null){
184
			return self::findClassInModuleFullFilePath($class, $module, 'libraries');
185
		}
186
187
		
188
		/**
189
		 * Check if in module list can have this config
190
		 * @see  Module::findNonClassInModuleFullFilePath
191
		 * @return boolean|string  false or null if no module have this configuration,  return the full path of this configuration
192
		 */
193
		public static function findConfigFullPath($configuration, $module = null){
194
			return self::findNonClassInModuleFullFilePath($configuration, $module, 'config');
195
		}
196
197
		/**
198
		 * Check if in module list can have this helper
199
		 * @see  Module::findNonClassInModuleFullFilePath
200
		 * @return boolean|string  false or null if no module have this helper,  return the full path of this helper
201
		 */
202
		public static function findFunctionFullPath($helper, $module = null){
203
			return self::findNonClassInModuleFullFilePath($helper, $module, 'functions');
204
		}
205
206
		/**
207
		 * Check if in module list can have this view
208
		 * @see  Module::findNonClassInModuleFullFilePath
209
		 * @return boolean|string  false or null if no module have this view, path the full path of the view
210
		 */
211
		public static function findViewFullPath($view, $module = null){
212
			return self::findNonClassInModuleFullFilePath($view, $module, 'views');
213
		}
214
215
		/**
216
		 * Check if in module list can have this language
217
		 * @see  Module::findNonClassInModuleFullFilePath
218
		 * @return boolean|string  false or null if no module have this language,  return the full path of this language
219
		 */
220
		public static function findLanguageFullPath($language, $appLang, $module = null){
221
			return self::findNonClassInModuleFullFilePath($language, $module, 'lang', $appLang);
222
		}
223
224
		/**
225
		 * Get the list of module loaded
226
		 * @return array the module list
227
		 */
228
		public static function getModuleList(){
229
			return self::$list;
230
		}
231
232
		/**
233
		 * Check if the application has an module
234
		 * @return boolean
235
		 */
236
		public static function hasModule(){
237
			return !empty(self::$list);
238
		}
239
240
		/**
241
		 * Check if in module list can have the model, controller, library
242
		 * @param  string $class the class name of library, model, controller
243
		 * @param string $module the module name
244
		 * @param string $type the name of the type "controllers", "libraries", "models"
245
		 * @return boolean|string  false or null if no module 
246
		 * have this class, return the full path of the class
247
		 */
248
		protected static function findClassInModuleFullFilePath($class, $module, $type){
249
			$logger = self::getLogger();
250
		    $class = str_ireplace('.php', '', $class);
251
		    $class = ucfirst($class);
252
		    $classFile = $class.'.php';
253
		    $logger->debug('Checking the class [' . $class . '] in module [' . $module . '] for [' . $type . '] ...');
254
		    $filePath = MODULE_PATH . $module . DS . $type . DS . $classFile;
255
		    if (file_exists($filePath)){
256
		        $logger->info('Found class [' . $class . '] in module [' . $module . '] for [' . $type . '] the file path is [' .$filePath. ']');
257
		        return $filePath;
258
		    }
259
		    $logger->info('Class [' . $class . '] does not exist in the module [' .$module. '] for [' . $type . ']');
260
		    return false;
261
		}
262
263
		/**
264
		 * Check if in module list can have the config, view, helper, language
265
		 * @param string $name the name of config, view, helper, language
266
		 * @param string $module the module name
267
		 * @param string $type the name of the type "config", "functions", "views", "lang"
268
		 * @param string|null $appLang the application language. This is use only when $type = "lang"
269
		 * @return boolean|string  false or null if no module 
270
		 * have this resource, return the full path of the resource
271
		 */
272
		protected static function findNonClassInModuleFullFilePath($name, $module, $type, $appLang = null){
273
		    $logger = self::getLogger();
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
		    $logger->debug('Checking resource [' . $name . '] in module [' .$module. '] for [' . $type . '] ...');
296
		    if (file_exists($filePath)){
297
		        $logger->info('Found resource [' . $name . '] in module [' .$module. '] for [' . $type . '] the file path is [' .$filePath. ']');
298
		        return $filePath;
299
		    }
300
		    $logger->info('Resource [' . $name . '] does not exist in the module [' .$module. '] for [' . $type . ']');
301
		    return false;
302
		}
303
304
	}
305