Skip to content

Commit

Permalink
General update
Browse files Browse the repository at this point in the history
- Update comments
- Add .gitignore
- Add more information
- Small code fixes
  • Loading branch information
prod3v3loper committed Apr 27, 2024
1 parent d4d69ec commit cf49c0c
Show file tree
Hide file tree
Showing 10 changed files with 202 additions and 175 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*.log
vendor
13 changes: 9 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
![Melabuai Logo](https://raw.githubusercontent.com/prod3v3loper/syntaxo/master/img/icon-MB.png "Melabuai Brand")

![Language](https://img.shields.io/github/languages/top/prod3v3loper/php-auto-autoloader.svg?style=flat "Language")
[![Codacy Badge](https://api.codacy.com/project/badge/Grade/90539baa57ba4ea1beb8451090f42547)](https://www.codacy.com/app/prod3v3loper/php-auto-autoloader?utm_source=github.com&utm_medium=referral&utm_content=prod3v3loper/php-auto-autoloader&utm_campaign=Badge_Grade)
[![Build Status](https://travis-ci.org/prod3v3loper/syntaxo.svg?branch=master "Build Status")](https://travis-ci.org/prod3v3loper/syntaxo "Build Status")
![Repo Size](https://img.shields.io/github/repo-size/prod3v3loper/php-auto-autoloader.svg?style=flat "Repo size")
![Code SIze](https://img.shields.io/github/languages/code-size/prod3v3loper/php-auto-autoloader.svg?style=flat "Code size")
Expand Down Expand Up @@ -57,15 +56,18 @@ This is the vendor autoloader invites our autoloader and now we do not need to s
`index.php`
```php
<?php
// Define the document root project folder
define('MBT_DOCUMENT_ROOT', __DIR__);

// Load the autoloader
require_once __DIR__ . '/vendor/autoload.php';
new \Aautoloder\Loader(array(MBT_DOCUMENT_ROOT));
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>MB tnado Ai - Autoload</title>
<title>Prod3v3loper - Autoload</title>
</head>
<body>
<?php
Expand Down Expand Up @@ -100,15 +102,18 @@ So we use it without a vendor and can start a direct call.
`index.php`
```php
<?php
// Define the document root project folder
define('MBT_DOCUMENT_ROOT', __DIR__);

// Load the autoloader
require_once './autoload/src/Loader.php';
new \Aautoloder\Loader(array(MBT_DOCUMENT_ROOT));
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>MB tnado Ai - Autoload</title>
<title>Prod3v3loper - Autoload</title>
</head>
<body>
<?php
Expand Down Expand Up @@ -263,6 +268,6 @@ possible.

# License

[MIT](https://github.com/prod3v3loper/php-auto-autoloader/blob/master/LICENSE) - [prod3v3loper](https://www.tnado.com/author/prod3v3loper/)
[MIT](https://github.com/prod3v3loper/php-auto-autoloader/blob/master/LICENSE) - [prod3v3loper](https://www.prod3v3loper.com/)

</div>
1 change: 0 additions & 1 deletion autoload/log/_2019_01_31.log

This file was deleted.

127 changes: 62 additions & 65 deletions autoload/src/Loader.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

declare(strict_types = 1);
declare(strict_types=1);

namespace Aautoloder;

Expand All @@ -13,88 +13,86 @@
*
* This autoloader class Loader have three methods inside that checks for classes
*
* @author Samet Tarim (prod3v3loper)
* @copyright (c) 2017, Samet Tarim
* @link https://www.tnado.com/
* @author prod3v3loper
* @copyright (c) 2024, prod3v3loper
* @link https://www.prod3v3loper.com/
* @package Melabuai
* @subpackage autoloader
* @since 1.0
* @subpackage Autoloader
* @version 1.1
* @since 1.1
* @see https://github.com/prod3v3loper/php-auto-autoloader
*/
class Loader extends LoaderHelper {

class Loader extends LoaderHelper
{
/**
* Directory for loop
* @var type
*
* @var array $dir
*/
private $dir = array();
private $dir = [];

/**
* File for require class
* @var type
*
* @var array $file
*/
private $file = '';
private $file = [];

/**
* Empty Array for all forced Files and folders
* @var type
*
* @var array $list
*/
private $list = array();
private $list = [];

/**
* Found state
* @var type
*
* @var boolean $found
*/
protected $found = false;

/**
* Instance - interface, class, extends classes, abstract classes, trait
* @var type
*
* @var string $instance
*/
protected $insatnce = '';
protected $instance = '';

/**
* Namespace
* @var type
*
* @var string $namespace
*/
protected $namespace = '';

/**
* Constructor
*/
public function __construct(array $dir) {

$this->dir = $dir; // Root path dir/folder to force recrusive
$this->action(); // Action
public function __construct(array $dir)
{
$this->dir = $dir;
$this->action();

if (!file_exists(MBT_CORE_AUTOLOAD_LOG_FOLDER)) {
mkdir(MBT_CORE_AUTOLOAD_LOG_FOLDER, 0755);
}
}

/**
* Action Handler
* We register our autoloader
*
* @see http://php.net/manual/de/function.spl-autoload-register.php
*/
protected function action() {

/**
* We register our autoloader
* @see http://php.net/manual/de/function.spl-autoload-register.php
*/
protected function action()
{
if (version_compare(PHP_VERSION, '5.1.2', '>=')) {
if (version_compare(PHP_VERSION, '5.3.0', '>=')) {
spl_autoload_register(array($this, '__autoload'), true, true);
} else {
spl_autoload_register(array($this, '__autoload'));
}
} else {
// Add alert
echo 'Ohhh NO Autoload exists, you use a PHP version under 5.1.2';
}

/**
* Control debug
*/
if (MBT_DEBUG_DISPLAY_AUTOLOAD == true) {
echo $this->logInfo();
}
Expand All @@ -104,10 +102,10 @@ protected function action() {
* This function is the Loader, the heart from autoloader
* Here we search with 3 different methods, the used classes, interfaces or even abstract classes
*
* @param type $name
* @param string $name
*/
protected function __autoload($name) {

protected function __autoload($name)
{
// Start Site load time needs core performance
$timeA = start_time();

Expand All @@ -120,7 +118,7 @@ protected function __autoload($name) {
if (file_exists($classFilePath) && is_dir($classFilePath) && !empty($this->splitInsatnce(true))) {

### This is the fastest way ###
// $this->debugInfo['MB_AUTOLOAD_INSTANCE'][] = 'FAST';
// $this->debugInfo['MB_AUTOLOAD_INSTANCE'][] = 'FAST';

/**
* If so, then we create the class file.
Expand All @@ -146,7 +144,7 @@ protected function __autoload($name) {
else {

### This method is slightly slower than the first, so 0.03 - 0.05 seconds ###
// $this->debugInfo['MB_AUTOLOAD_INSTANCE'][] = 'MIDDLE';
// $this->debugInfo['MB_AUTOLOAD_INSTANCE'][] = 'MIDDLE';

/**
* This function namspace as folder path and force only this path for class file.
Expand All @@ -167,7 +165,7 @@ protected function __autoload($name) {
} else {

### This method is the slowest, but found class anything where ###
// $this->debugInfo['MB_AUTOLOAD_INSTANCE'][] = 'SLOW';
// $this->debugInfo['MB_AUTOLOAD_INSTANCE'][] = 'SLOW';

/**
* This method is the slowest, because it scans all your folders.
Expand All @@ -194,29 +192,28 @@ protected function __autoload($name) {
$this->debugInfo[$reuri][$name] = $endTimeA;

file_put_contents(MBT_CORE_AUTOLOAD_LOG_LOGS, serialize($this->debugInfo), LOCK_EX);
// chmod(MBT_CORE_AUTOLOAD_LOG_LOGS, 0755);
// chmod(MBT_CORE_AUTOLOAD_LOG_LOGS, 0755);

/**
* Control debug
*/
if (MBT_DEBUG_DISPLAY_AUTOLOAD_SEARCH == true) {
echo $this->getDebug();
echo $this->getDebug();
}
}

/**
* Loop all files and read it to found the instance class
*
* @param type $filepath
* @param string $filepath
*/
protected function readFile($filepath) {

protected function readFile($filepath)
{
$arr = array();
$this->file = $this->getFile($filepath);
if (false != $this->file) {
if (false != $this->file && is_array($this->file)) {
foreach ($this->file as $lineNum => $line) {

// Get actually namespace
// $this->getNamespace($line, $arr);
// $this->getNamespace($line, $arr);

// Get actually class
$this->getClass($filepath, $line, $arr, $lineNum);
if ($lineNum > MBT_CORE_AUTOLOAD_READ_MAX_LINES || $this->found == true) {
Expand All @@ -230,10 +227,10 @@ protected function readFile($filepath) {
/**
* Get class file
*
* @return type
* @return boolean|array
*/
public function getFile($filepath) {

public function getFile($filepath)
{
$return = false;
if (file_exists($filepath) && is_file($filepath)) {
$return = file($filepath, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
Expand All @@ -244,12 +241,13 @@ public function getFile($filepath) {
/**
* Recursive all directorys in MBT_DOCUMENT_ROOT
*
* @param type $dir
* @param type $results
* @return type
* @param string $dir
* @param array $results
*
* @return array
*/
public function loopDirectory($dir = NULL, array &$results = array()) {

public function loopDirectory($dir = NULL, array &$results = array())
{
if ($dir == NULL) {
foreach ($this->dir as $direct) {
$dir = $direct;
Expand All @@ -272,10 +270,10 @@ public function loopDirectory($dir = NULL, array &$results = array()) {
/**
* Sort files and folders
*
* @param type $path
* @param string $path
*/
private function loopSort($path, array &$results = array()) {

private function loopSort($path, array &$results = array())
{
if (is_file($path)) {
$results['files'][] = $path;
}
Expand All @@ -285,9 +283,8 @@ private function loopSort($path, array &$results = array()) {
$this->loopDirectory($path, $results);
}
}

}

// Define web roots
define('MBT_SERVER_ROOT', str_replace(MBT_DOCUMENT_ROOT, '', str_replace(filter_input(INPUT_SERVER, 'DOCUMENT_ROOT'), '', str_replace("\\", "/", MBT_DOCUMENT_ROOT))));
define('MBT_HTTP_ROOT', get_protocol() . get_host() . MBT_SERVER_ROOT);
define('MBT_HTTP_ROOT', get_protocol() . get_host() . MBT_SERVER_ROOT);
Loading

0 comments on commit cf49c0c

Please sign in to comment.