2424 */
2525class Loader extends LoaderHelper
2626{
27+
2728 /**
2829 * Directory for loop
2930 *
@@ -109,36 +110,42 @@ protected function __autoload($name)
109110 // Start Site load time needs core performance
110111 $ timeA = start_time ();
111112
112- $ this ->insatnce = $ name ;
113+ $ this ->instance = $ name ;
113114
114- // Get namespace as folder path
115- $ classFilePath = str_replace (array ("\\" , "// " ), "/ " , MBT_DOCUMENT_ROOT . DIRECTORY_SEPARATOR . $ this ->splitInsatnce (true ));
115+ $ classFilePath = '' ;
116+ if (!empty ($ this ->splitInsatnce (true ))) {
117+ // Get namespace as folder path
118+ $ classFilePath = str_replace (array ("\\" , "// " ), "/ " , MBT_DOCUMENT_ROOT . DIRECTORY_SEPARATOR . $ this ->splitInsatnce (true ));
119+ }
116120
117121 // Exists a folder with same path as the namespace and is dir
118- if (file_exists ( $ classFilePath) && is_dir ($ classFilePath ) && ! empty ( $ this -> splitInsatnce ( true ) )) {
122+ if ($ classFilePath && file_exists ($ classFilePath ) && is_dir ( $ classFilePath )) {
119123
120124 ### This is the fastest way ###
121125 // $this->debugInfo['MB_AUTOLOAD_INSTANCE'][] = 'FAST';
122126
123127 /**
128+ * METHOD I
129+ *
124130 * If so, then we create the class file.
125131 * That means we take the website root path and namespace as a folder path and the classname we put together with these.
126132 *
127133 * ### Example ###
128134 *
129- * PATH: /users/username/projects/sites/website/
130- * NAMESPACE: modal
131- * CLASS: AbstractEntity
135+ * PROJECT PATH: /project/site/mywebsite
136+ * NAMESPACE: testclasses
137+ * CLASS: first_class
132138 *
133- * Then the result example /users/username/projects/sites/website/modal/ class.AbstractEntity .php
139+ * Then the result example /project/site/mywebsite/testclasses/ class.first_class .php
134140 */
135- $ classFile = $ classFilePath . DIRECTORY_SEPARATOR . 'class. ' . $ this ->splitInsatnce () . '.php ' ;
141+ $ classFile = '' ;
142+ if (!empty ($ this ->splitInsatnce ())) {
143+ $ classFile = $ classFilePath . DIRECTORY_SEPARATOR . 'class. ' . $ this ->splitInsatnce () . '.php ' ;
144+ }
136145
137146 // Check if class file exists
138- if (file_exists ($ classFile ) && is_file ($ classFile )) {
139-
140- // Is file exists require
141- require_once $ classFile ;
147+ if ($ classFile && file_exists ($ classFile ) && is_file ($ classFile )) {
148+ require_once $ classFile ; // Is file exists load
142149 }
143150 //..
144151 else {
@@ -147,43 +154,61 @@ protected function __autoload($name)
147154 // $this->debugInfo['MB_AUTOLOAD_INSTANCE'][] = 'MIDDLE';
148155
149156 /**
157+ * METHOD II
158+ *
150159 * This function namspace as folder path and force only this path for class file.
151160 * This means every file found in this folder is opened and searched for the classname.
152161 * As soon as the used class exists in a file, this is integrated.
153162 *
163+ * ### Example ###
164+ *
165+ * This file name dont exists as example class.second_class.php we have class.second.php but the name of the class is second_class.
166+ * Then the result example /project/site/mywebsite/testclasses/classes/class.second_class.php
154167 */
155- // Otherwise we scan class filepath dir from namespace and get all files to require
156- $ this ->list = $ this ->loopDirectory ($ classFilePath ); // Get files
157- $ countedFiles = count ($ this ->list ['files ' ]); // Count files
158- for ($ i = 0 ; $ i < $ countedFiles ; $ i ++) { // Loop all files
159- $ this ->readFile ($ this ->list ['files ' ][$ i ]); // Check files for class
168+
169+ // Otherwise we scan class filepath dir from namespace (/project/site/mywebsite/testclasses/classes) and get all files to require
170+ if ($ classFilePath && file_exists ($ classFilePath )) {
171+
172+ $ this ->list = $ this ->loopDirectory ($ classFilePath ); // Get files
173+
174+ if (isset ($ this ->list ['files ' ])) {
175+ $ countedFiles = count ($ this ->list ['files ' ]); // Count files
176+ for ($ i = 0 ; $ i < $ countedFiles ; $ i ++) { // Loop all files
177+ $ this ->readFile ($ this ->list ['files ' ][$ i ]); // Check files for class
178+ }
179+ $ this ->saveIndex (); // Write load file
180+ }
160181 }
161182
162- $ this ->loadWrite (); // Write load file
163- $ this ->loadRead (); // Read and load
183+ $ this ->readIndex (); // Read and load
164184 }
165185 } else {
166186
167187 ### This method is the slowest, but found class anything where ###
168188 // $this->debugInfo['MB_AUTOLOAD_INSTANCE'][] = 'SLOW';
169189
170190 /**
191+ * METHOD III
192+ *
171193 * This method is the slowest, because it scans all your folders.
172194 * No matter how much files you have, all are opened, read and searched for the classname.
173195 *
174196 * The complete path is the directory path, that you give the autoloader
175- * DEFAULT: MBT_DOCUMENT_ROOT
197+ * DEFAULT: MBT_DOCUMENT_ROOT (Project root)
176198 */
199+
177200 // Get all Files from complete path
178201 $ this ->list = $ this ->loopDirectory ();
179202
180- $ countedFiles = count ($ this ->list ['files ' ]); // Count files
181- for ($ i = 0 ; $ i < $ countedFiles ; $ i ++) { // Loop all files
182- $ this ->readFile ($ this ->list ['files ' ][$ i ]); // Check files for class
203+ if (isset ($ this ->list ['files ' ])) {
204+ $ countedFiles = count ($ this ->list ['files ' ]); // Count files
205+ for ($ i = 0 ; $ i < $ countedFiles ; $ i ++) { // Loop all files
206+ $ this ->readFile ($ this ->list ['files ' ][$ i ]); // Check files for class
207+ }
208+ $ this ->saveIndex (); // Write load file
183209 }
184210
185- $ this ->loadWrite (); // Write load file
186- $ this ->loadRead (); // Read and load
211+ $ this ->readIndex (); // Read and load
187212 }
188213
189214 $ endTimeA = end_time ($ timeA );
@@ -206,16 +231,20 @@ protected function __autoload($name)
206231 */
207232 protected function readFile ($ filepath )
208233 {
209- $ arr = array ();
234+ $ arr = [];
235+
210236 $ this ->file = $ this ->getFile ($ filepath );
211- if (false != $ this ->file && is_array ($ this ->file )) {
237+
238+ if (false !== $ this ->file && is_array ($ this ->file )) {
239+
212240 foreach ($ this ->file as $ lineNum => $ line ) {
213241
214242 // Get actually namespace
215243 // $this->getNamespace($line, $arr);
216244
217245 // Get actually class
218246 $ this ->getClass ($ filepath , $ line , $ arr , $ lineNum );
247+
219248 if ($ lineNum > MBT_CORE_AUTOLOAD_READ_MAX_LINES || $ this ->found == true ) {
220249 $ this ->found = false ;
221250 break ; // DEFAULT: 49 lines or by found = true, break the loop
@@ -246,21 +275,66 @@ public function getFile($filepath)
246275 *
247276 * @return array
248277 */
249- public function loopDirectory ($ dir = NULL , array &$ results = array () )
278+ public function loopDirectory ($ dir = NULL , array &$ results = [] )
250279 {
251280 if ($ dir == NULL ) {
252- foreach ($ this ->dir as $ direct ) {
253- $ dir = $ direct ;
254- }
281+ $ dir = $ this ->dir [0 ];
255282 }
256283
284+ $ ignoreDirs = [
285+ 'autoload ' , 'vendor ' , '.git ' , 'node_modules ' , 'cache ' , 'logs ' , 'tests ' , 'build ' ,
286+ 'docs ' , 'documentation ' , 'assets ' , 'temp ' , 'bin ' , 'backup ' , 'config ' ,
287+ 'media ' , 'public ' , 'storage ' , 'resources ' , 'src ' , 'templates ' , 'views ' ,
288+ 'uploads ' , 'scripts ' , '.idea ' , 'dist ' , 'log ' , 'img '
289+ ];
290+
291+ $ ignoreFiles = [
292+ 'composer.json ' , 'composer.lock ' , '.env ' ,
293+ '.gitignore ' , '.htaccess ' , 'phpunit.xml ' ,
294+ 'README.md ' , 'LICENSE '
295+ ];
296+
297+ $ ignoreExtensions = [
298+ 'md ' , 'json ' , 'yaml ' , 'yml ' , 'xml ' , 'ini ' , 'log ' , 'txt ' ,
299+ 'css ' , 'js ' , 'scss ' , 'less ' , 'html ' , 'htm ' , 'config '
300+ ];
301+
257302 $ dir = str_replace ("// " , "/ " , $ dir );
258303
259- if (file_exists ($ dir )) {
304+ if (file_exists ($ dir ) && is_dir ($ dir )) {
305+
260306 $ files = array_diff (scandir ($ dir , 1 ), array (". " , ".. " ));
261- foreach ($ files as $ value ) {
262- $ path = $ dir . DIRECTORY_SEPARATOR . $ value ; // If folder in folder
263- $ this ->loopSort ($ path , $ results );
307+
308+ foreach ($ files as $ file ) {
309+
310+ if ($ file == '. ' || $ file == '.. ' ) continue ;
311+
312+ $ fullPath = $ dir . '/ ' . $ file ;
313+ $ relativePath = str_replace (MBT_DOCUMENT_ROOT , '' , $ fullPath );
314+ $ pathInfo = pathinfo ($ fullPath );
315+
316+ $ skip = false ;
317+
318+ foreach ($ ignoreDirs as $ ignoreDir ) {
319+ if (strpos ($ relativePath , '/ ' . $ ignoreDir . '/ ' ) !== false ) {
320+ $ skip = true ;
321+ break ;
322+ }
323+ }
324+
325+ if (!$ skip && in_array ($ file , $ ignoreFiles )) {
326+ $ skip = true ;
327+ }
328+
329+ if (!$ skip && isset ($ pathInfo ['extension ' ]) && in_array ($ pathInfo ['extension ' ], $ ignoreExtensions )) {
330+ $ skip = true ;
331+ }
332+
333+ if (!$ skip ) {
334+
335+ $ path = $ dir . DIRECTORY_SEPARATOR . $ file ; // If folder in folder
336+ $ this ->loopSort ($ path , $ results );
337+ }
264338 }
265339 }
266340
0 commit comments