Passed
Push — 1.0.0-dev ( 93958a...e1c8ef )
by nguereza
02:26
created

Module::setLogger()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 3
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 extends BaseStaticClass{
28
		
29
		/**
30
		 * list of loaded module
31
		 * @var array
32
		 */
33
		private static $list = array();
34
35
		/**
36
		 * Initialise the module list by scanning the directory MODULE_PATH
37
		 */
38
		public function init(){
39
			$logger = self::getLogger();
40
			$logger->debug('Check if the application contains the modules ...');
41
			$moduleDir = opendir(MODULE_PATH);
42
			if (is_resource($moduleDir)){
43
				while(($module = readdir($moduleDir)) !== false){
44
					if (preg_match('/^([a-z0-9-_]+)$/i', $module) && is_dir(MODULE_PATH . $module)){
45
						self::$list[] = $module;
46
					}
47
					else{
48
						$logger->info('Skipping [' .$module. '], may be this is not a directory or does not exists or is invalid name');
49
					}
50
				}
51
				closedir($moduleDir);
52
			}
53
			ksort(self::$list);
54
			
55
			if (! empty(self::$list)){
56
				$logger->info('The application contains the module below [' . implode(', ', self::getModuleList()) . ']');
57
			}
58
		}
59
		
60
61
		/**
62
		 * Add new module in the list
63
		 * @param string $name the name of the module
64
		 *
65
		 * @return object the current instance
66
		 */
67
		public function add($name){
68
			self::$list[] = $name;
69
			return $this;
70
		}
71
		
72
		/**
73
		 * Get the list of the custom autoload configuration from module if exists
74
		 * @return array|boolean the autoload configurations list or false if no module contains the autoload configuration values
75
		 */
76
		public static function getModulesAutoloadConfig(){
77
			$logger = self::getLogger();
78
			if (empty(self::$list)){
79
				$logger->info('No module was loaded skipping.');
80
				return false;
81
			}
82
			$autoloads = array();
83
			$autoloads['libraries'] = array();
84
			$autoloads['config']    = array();
85
			$autoloads['models']    = array();
86
			$autoloads['functions'] = array();
87
			$autoloads['languages'] = array();
88
			
89
			foreach (self::$list as $module) {
90
				$file = MODULE_PATH . $module . DS . 'config' . DS . 'autoload.php';
91
				if (file_exists($file)){
92
					$autoload = array();
93
					require_once $file;
94
					if (! empty($autoload) && is_array($autoload)){
95
						$autoloads = array_merge_recursive($autoloads, $autoload);
96
						unset($autoload);
97
					}
98
				}
99
			}
100
			return $autoloads;
101
		}
102
103
		/**
104
		 * Get the list of the custom routes configuration from module if exists
105
		 * @return array|boolean the routes list or false if no module contains the routes configuration
106
		 */
107
		public static function getModulesRoutesConfig(){
108
			$logger = self::getLogger();
109
			if (empty(self::$list)){
110
				$logger->info('No module was loaded skipping.');
111
				return false;
112
			}
113
			$routes = array();
114
			foreach (self::$list as $module) {
115
				$file = MODULE_PATH . $module . DS . 'config' . DS . 'routes.php';
116
				if (file_exists($file)){
117
					$route = array();
118
					require_once $file;
119
					if (! empty($route) && is_array($route)){
120
						$routes = array_merge($routes, $route);
121
						unset($route);
122
					}
123
				}
124
			}
125
			return $routes;
126
		}
127
128
129
		/**
130
		 * Check if in module list can have this controller
131
		 * @see Module::findClassInModuleFullFilePath
132
		 * @return boolean|string  false or null if no module have this controller, path the full path of the controller
133
		 */
134
		public static function findControllerFullPath($class, $module = null){
135
			return self::findClassInModuleFullFilePath($class, $module, 'controllers');
136
		}
137
138
		/**
139
		 * Check if in module list can have this model
140
		 * @see Module::findClassInModuleFullFilePath
141
		 * @return boolean|string  false or null if no module have this model, return the full path of this model
142
		 */
143
		public static function findModelFullPath($class, $module = null){
144
			return self::findClassInModuleFullFilePath($class, $module, 'models');
145
		}
146
147
		/**
148
		 * Check if in module list can have this library
149
		 * @see Module::findClassInModuleFullFilePath
150
		 * @return boolean|string  false or null if no module have this library,  return the full path of this library
151
		 */
152
		public static function findLibraryFullPath($class, $module = null){
153
			return self::findClassInModuleFullFilePath($class, $module, 'libraries');
154
		}
155
156
		
157
		/**
158
		 * Check if in module list can have this config
159
		 * @see  Module::findNonClassInModuleFullFilePath
160
		 * @return boolean|string  false or null if no module have this configuration,  return the full path of this configuration
161
		 */
162
		public static function findConfigFullPath($configuration, $module = null){
163
			return self::findNonClassInModuleFullFilePath($configuration, $module, 'config');
164
		}
165
166
		/**
167
		 * Check if in module list can have this helper
168
		 * @see  Module::findNonClassInModuleFullFilePath
169
		 * @return boolean|string  false or null if no module have this helper,  return the full path of this helper
170
		 */
171
		public static function findFunctionFullPath($helper, $module = null){
172
			return self::findNonClassInModuleFullFilePath($helper, $module, 'functions');
173
		}
174
175
		/**
176
		 * Check if in module list can have this view
177
		 * @see  Module::findNonClassInModuleFullFilePath
178
		 * @return boolean|string  false or null if no module have this view, path the full path of the view
179
		 */
180
		public static function findViewFullPath($view, $module = null){
181
			return self::findNonClassInModuleFullFilePath($view, $module, 'views');
182
		}
183
184
		/**
185
		 * Check if in module list can have this language
186
		 * @see  Module::findNonClassInModuleFullFilePath
187
		 * @return boolean|string  false or null if no module have this language,  return the full path of this language
188
		 */
189
		public static function findLanguageFullPath($language, $appLang, $module = null){
190
			return self::findNonClassInModuleFullFilePath($language, $module, 'lang', $appLang);
191
		}
192
193
		/**
194
		 * Get the list of module loaded
195
		 * @return array the module list
196
		 */
197
		public static function getModuleList(){
198
			return self::$list;
199
		}
200
201
		/**
202
		 * Check if the application has an module
203
		 * @return boolean
204
		 */
205
		public static function hasModule(){
206
			return !empty(self::$list);
207
		}
208
209
		/**
210
		 * Check if in module list can have the model, controller, library
211
		 * @param  string $class the class name of library, model, controller
212
		 * @param string $module the module name
213
		 * @param string $type the name of the type "controllers", "libraries", "models"
214
		 * @return boolean|string  false or null if no module 
215
		 * have this class, return the full path of the class
216
		 */
217
		protected static function findClassInModuleFullFilePath($class, $module, $type){
218
			$logger = self::getLogger();
219
		    $class = str_ireplace('.php', '', $class);
220
		    $class = ucfirst($class);
221
		    $classFile = $class.'.php';
222
		    $logger->debug('Checking the class [' . $class . '] in module [' . $module . '] for [' . $type . '] ...');
223
		    $filePath = MODULE_PATH . $module . DS . $type . DS . $classFile;
224
		    if (file_exists($filePath)){
225
		        $logger->info('Found class [' . $class . '] in module [' . $module . '] for [' . $type . '] the file path is [' .$filePath. ']');
226
		        return $filePath;
227
		    }
228
		    $logger->info('Class [' . $class . '] does not exist in the module [' .$module. '] for [' . $type . ']');
229
		    return false;
230
		}
231
232
		/**
233
		 * Check if in module list can have the config, view, helper, language
234
		 * @param string $name the name of config, view, helper, language
235
		 * @param string $module the module name
236
		 * @param string $type the name of the type "config", "functions", "views", "lang"
237
		 * @param string|null $appLang the application language. This is use only when $type = "lang"
238
		 * @return boolean|string  false or null if no module 
239
		 * have this resource, return the full path of the resource
240
		 */
241
		protected static function findNonClassInModuleFullFilePath($name, $module, $type, $appLang = null){
242
		    $logger = self::getLogger();
243
		    $name = str_ireplace('.php', '', $name);
244
		    $file = $name.'.php';
245
		    $filePath = MODULE_PATH . $module . DS . $type . DS . $file;
246
		    switch($type){
247
		        case 'functions':
248
		            $name = str_ireplace('function_', '', $name);
249
		            $file = 'function_'.$name.'.php';
250
		            $filePath = MODULE_PATH . $module . DS . $type . DS . $file;
251
		        break;
252
		        case 'views':
253
		            $name = trim($name, '/\\');
254
		            $name = str_ireplace('/', DS, $name);
255
		            $file = $name . '.php';
256
		            $filePath = MODULE_PATH . $module . DS . $type . DS . $file;
257
		        break;
258
		        case 'lang':
259
		            $name = str_ireplace('lang_', '', $name);
260
		            $file = 'lang_'.$name.'.php';
261
		            $filePath = MODULE_PATH . $module . DS . $type . DS . $appLang . DS . $file;
262
		        break;
263
		    }
264
		    $logger->debug('Checking resource [' . $name . '] in module [' .$module. '] for [' . $type . '] ...');
265
		    if (file_exists($filePath)){
266
		        $logger->info('Found resource [' . $name . '] in module [' .$module. '] for [' . $type . '] the file path is [' .$filePath. ']');
267
		        return $filePath;
268
		    }
269
		    $logger->info('Resource [' . $name . '] does not exist in the module [' .$module. '] for [' . $type . ']');
270
		    return false;
271
		}
272
273
	}
274