Skip to content

Commit 1eb055c

Browse files
committed
Major Update
- Add phpunit 8 - Add travis ci php 8.1 and vendor e.g. self test with - Add image - Fix composer json and test class - Update readme and .gitignore - Fsat up hole code
1 parent cf49c0c commit 1eb055c

File tree

15 files changed

+239
-113
lines changed

15 files changed

+239
-113
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
*.log
2-
vendor
2+
vendor
3+
*.lock

.travis.yml

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,17 @@ cache:
77
php:
88
- 7.2
99
- 7.3
10+
- 8.1
1011

11-
install:
12-
- composer install
13-
14-
script: phpunit --exclude-group FITS --bootstrap vendor/autoload.php tests/LoaderTest
12+
before_script:
13+
- if [[ "$USE_VENDOR_AUTOLOAD" == "true" ]]; then composer install; fi
14+
15+
script:
16+
- if [[ "$USE_VENDOR_AUTOLOAD" == "true" ]]; then phpunit --bootstrap vendor/autoload.php tests/; else phpunit --bootstrap autoload/src/Loader.php tests/; fi
17+
18+
env:
19+
- USE_VENDOR_AUTOLOAD=true
20+
- USE_VENDOR_AUTOLOAD=false
21+
22+
matrix:
23+
fast_finish: true

README.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<div align="center">
22

3-
![Melabuai Logo](https://raw.githubusercontent.com/prod3v3loper/syntaxo/master/img/icon-MB.png "Melabuai Brand")
3+
![Melabuai Logo](https://raw.githubusercontent.com/prod3v3loper/php-auto-autoloader/master/img/icon-MB.png "Melabuai Brand")
44

55
![Language](https://img.shields.io/github/languages/top/prod3v3loper/php-auto-autoloader.svg?style=flat "Language")
6-
[![Build Status](https://travis-ci.org/prod3v3loper/syntaxo.svg?branch=master "Build Status")](https://travis-ci.org/prod3v3loper/syntaxo "Build Status")
6+
[![Build Status](https://travis-ci.org/prod3v3loper/php-auto-autoloader.svg?branch=master "Build Status")](https://travis-ci.org/prod3v3loper/php-auto-autoloader "Build Status")
77
![Repo Size](https://img.shields.io/github/repo-size/prod3v3loper/php-auto-autoloader.svg?style=flat "Repo size")
88
![Code SIze](https://img.shields.io/github/languages/code-size/prod3v3loper/php-auto-autoloader.svg?style=flat "Code size")
99
![Github Release date](https://img.shields.io/github/release/prod3v3loper/php-auto-autoloader.svg?style=flat "Github Release date")
@@ -50,6 +50,10 @@ Now run the composer install command with php
5050
```
5151
php composer.phar install
5252
```
53+
e.g.
54+
```
55+
composer install
56+
```
5357

5458
This is the vendor autoloader invites our autoloader and now we do not need to specify any more class and can load all our classes.
5559

@@ -190,9 +194,9 @@ The autoloader finds everything yourself you do not have to do anything except w
190194

191195
Method | Namespace (Instance) | Path | Load Time
192196
------------ | ------------ | ------------- | -------------
193-
1 | testclasses\classOne | php-auto-autoloader/testclasses/classOne.php | 0.001 sec.
197+
1 | testclasses\classOne | php-auto-autoloader/testclasses/classOne.php | 0.0 sec.
194198
2 | testclasses\classes\classTwo.php | php-auto-autoloader/testclasses/classes/classTwo.php | 0.002 sec.
195-
3 | name_space\namespace2\three_class | php-auto-autoloader/testclasses/classThree.php | 0.021 sec.
199+
3 | name_space\namespace2\three_class | php-auto-autoloader/testclasses/classThree.php | 0.011 sec.
196200

197201
## METHOD 1:
198202

autoload/src/Loader.php

Lines changed: 110 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
*/
2525
class 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

Comments
 (0)