Skip to content

Commit cf8acc1

Browse files
1.27
1 parent c44288b commit cf8acc1

File tree

4 files changed

+89
-69
lines changed

4 files changed

+89
-69
lines changed

AutoLoadOne.php

Lines changed: 60 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
<?php /** @noinspection TypeUnsafeArraySearchInspection */
2-
/** @noinspection SuspiciousAssignmentsInspection */
3-
/** @noinspection TypeUnsafeComparisonInspection */
1+
<?php
42
/** @noinspection NotOptimalIfConditionsInspection */
53
/** @noinspection NonSecureUniqidUsageInspection */
64
/** @noinspection SubStrUsedAsStrPosInspection */
@@ -45,12 +43,12 @@
4543
*
4644
* @copyright Jorge Castro C. MIT License https://github.com/EFTEC/AutoLoadOne
4745
*
48-
* @version 1.26 2022-02-21
46+
* @version 1.27 2022-08-27
4947
* @noautoload
5048
*/
5149
class AutoLoadOne
5250
{
53-
public const VERSION = '1.26';
51+
public const VERSION = '1.27';
5452

5553

5654
public $rooturl = '';
@@ -248,7 +246,7 @@ private function initSapi(): void
248246
echo "------------------------------------------------------------------\n";
249247
} else {
250248
$this->getAllParametersCli();
251-
$this->fileGen = ($this->fileGen == '') ? '.' : $this->fileGen; //getcwd()
249+
$this->fileGen = ($this->fileGen === '') ? '.' : $this->fileGen; //getcwd()
252250
$this->button = 1;
253251
}
254252
if ($this->current) {
@@ -328,7 +326,7 @@ private function initWeb(): void
328326
if (!$this->logged) {
329327
$user = @$_POST['user'];
330328
$password = @$_POST['password'];
331-
if (($user == _AUTOLOAD_USER && $password == _AUTOLOAD_PASSWORD) || _AUTOLOAD_ENTER) {
329+
if (($user === _AUTOLOAD_USER && $password === _AUTOLOAD_PASSWORD) || _AUTOLOAD_ENTER) {
332330
$_SESSION['log'] = '1';
333331
$this->logged = 1;
334332
} else {
@@ -556,6 +554,7 @@ public function process(): void
556554
$autorunsFromJson[] = $full;
557555
}
558556
}
557+
$mapped=[];
559558
foreach ($files as $key => $f) {
560559
$f = $this->fixSeparator($f);
561560
$runMe = '';
@@ -572,7 +571,7 @@ public function process(): void
572571
$urlFull = $this->dirNameLinux($full); ///folder/subfolder/f1
573572
$tmpArr = explode('/', $f); //F1.php
574573
$basefile = end($tmpArr); // the config name shares the same name as the php but with extension .json
575-
if ($runMe != '') {
574+
if ($runMe !== '') {
576575
switch ($runMe) {
577576
case '@autorun first':
578577
$autorunsFirst[] = $full;
@@ -588,23 +587,24 @@ public function process(): void
588587
break;
589588
}
590589
}
590+
591591
foreach ($pArr as $p) {
592592
$nsp = $p['namespace'];
593593
$cs = $p['classname'];
594594
$this->statNameSpaces[$nsp] = 1;
595595
$this->statNumPHP++;
596-
if ($cs != '') {
596+
if ($cs !== '') {
597597
$this->statNumClass++;
598598
}
599599

600-
$altUrl = ($nsp != '') ? $nsp . '\\' . $cs : $cs; // namespace
600+
$altUrl = ($nsp !== '') ? $nsp . '\\' . $cs : $cs; // namespace
601601

602-
if ($nsp != '' || $cs != '') {
603-
if ((!isset($ns[$nsp]) || $ns[$nsp] == $dir) && $basefile == $cs . $this->extension) {
602+
if ($nsp !== '' || $cs !== '') {
603+
if ((!isset($ns[$nsp]) || $ns[$nsp] === $dir) && $basefile === $cs . $this->extension) {
604604
// namespace doesn't exist and the class is equals to the name
605605
// adding as a folder
606606
$exclude = false;
607-
if ($nsp != '' && in_array($nsp, $excludeNSArr)) {
607+
if ($nsp !== '' && in_array($nsp, $excludeNSArr, true)) {
608608
//if ($this->inExclusion($nsp, $this->excludeNSArr) && $nsp!="") {
609609
$this->addLog("Ignoring namespace (path specified in <b>Excluded NameSpace</b>): <b>$altUrl -> $full</b>",
610610
'warning');
@@ -622,11 +622,12 @@ public function process(): void
622622
}
623623

624624
if (!$exclude) {
625-
if ($nsp == '') {
625+
if ($nsp === '') {
626626
$this->addLog("Adding Full map (empty namespace): <b>$altUrl -> $full</b> to class <i>$cs</i>", 'warning');
627627
$nsAlt[$altUrl] = $full;
628628
$pathAbsolute[$altUrl] = $filesAbsolute[$key];
629629
} elseif (isset($ns[$nsp])) {
630+
$mapped[]=$nsp.'\\'.$cs;
630631
$this->addLog("Reusing the folder: <b>$nsp -> $dir</b> to class <i>$cs</i>",
631632
'success');
632633
} else {
@@ -642,15 +643,19 @@ public function process(): void
642643
if ($this->stop) {
643644
die(1);
644645
}
645-
} elseif ((!in_array($altUrl, $excludeNSArr) || $nsp == '') &&
646+
} elseif ((!in_array($altUrl, $excludeNSArr, true) || $nsp === '') &&
646647
!$this->inExclusion($urlFull, $excludePathArr)) {
647-
$this->addLog("Adding Full relation: <b>$altUrl -> $full</b> to class <i>$cs</i>", 'warning');
648-
$nsAlt[$altUrl] = $full;
649-
$pathAbsolute[$altUrl] = $filesAbsolute[$key];
648+
if(in_array($altUrl, $mapped, true)) {
649+
$this->addLog("Not Added Full relation: <b>$altUrl -> $full</b> to class <i>$cs</i> (already added)", 'warning');
650+
} else {
651+
$this->addLog("Adding Full relation: <b>$altUrl -> $full</b> to class <i>$cs</i>", 'warning');
652+
$nsAlt[$altUrl] = $full;
653+
$pathAbsolute[$altUrl] = $filesAbsolute[$key];
654+
}
650655
}
651656
}
652657
}
653-
if (count($pArr) == 0) {
658+
if (count($pArr) === 0) {
654659
$this->statNumPHP++;
655660
if ($runMe === '@noautoload') {
656661
$this->addLog("Ignoring <b>$full</b> Reason: <b>@noautoload</b> found", 'warning');
@@ -663,7 +668,7 @@ public function process(): void
663668
$this->addLog("Adding file <b>$auto</b> Reason: <b>@autoload first</b> found", 'warning');
664669
}
665670
foreach ($autoruns as $auto) {
666-
if (in_array($auto, $autorunsFromJson)) {
671+
if (in_array($auto, $autorunsFromJson, true)) {
667672
$this->addLog("Adding file <b>$auto</b> Reason: <b>composer.json</b> found", 'warning');
668673
} else {
669674
$this->addLog("Adding file <b>$auto</b> Reason: <b>@autoload</b> found", 'warning');
@@ -751,7 +756,7 @@ public function listFolderFilesAlt($dir, &$list, &$json): array
751756
if ($ff === 'composer.json') {
752757
$json[] = $list[] = $dir . '/' . $ff;
753758
}
754-
if ((strlen($ff) >= 5) && substr($ff, -4) == $this->extension) {
759+
if ((strlen($ff) >= 5) && substr($ff, -4) === $this->extension) {
755760
// PHP_OS_FAMILY=='Windows'
756761
$list[] = $dir . '/' . $ff;
757762
}
@@ -800,15 +805,15 @@ public function genPath($path)
800805
$path = $this->fixSeparator($path);
801806

802807

803-
if (strpos($path, $this->baseGen) == 0) {
808+
if (strpos($path, $this->baseGen) === 0) {
804809
$min1 = strrpos($path, '/');
805810
$min2 = strrpos($this->baseGen . '/', '/');
806811
//$min=min(strlen($path),strlen($this->baseGen));
807812
$min = min($min1, $min2);
808813
$baseCommon = $min;
809814

810815
for ($i = 0; $i < $min; $i++) {
811-
if (substr($path, 0, $i) != substr($this->baseGen, 0, $i)) {
816+
if (substr($path, 0, $i) !== substr($this->baseGen, 0, $i)) {
812817
$baseCommon = $i - 2;
813818
break;
814819
}
@@ -851,7 +856,7 @@ public function parsePHPFile($filename, string &$runMe): array
851856
die(1);
852857
}
853858
foreach ($tokens as $token) {
854-
if (is_array($token) && ($token[0] == T_COMMENT || $token[0] == T_DOC_COMMENT)) {
859+
if (is_array($token) && ($token[0] === T_COMMENT || $token[0] === T_DOC_COMMENT)) {
855860
if (strpos($token[1], '@noautoload') !== false) {
856861
$runMe = '@noautoload';
857862

@@ -871,7 +876,7 @@ public function parsePHPFile($filename, string &$runMe): array
871876
$nameSpace = '';
872877
$className = '';
873878
foreach ($tokens as $p => $token) {
874-
if (is_array($token) && $token[0] == T_NAMESPACE) {
879+
if (is_array($token) && $token[0] === T_NAMESPACE) {
875880
// We found a namespace
876881
$ns = '';
877882
for ($i = $p + 2; $i < $p + 30; $i++) {
@@ -887,10 +892,10 @@ public function parsePHPFile($filename, string &$runMe): array
887892

888893
$isClass = false;
889894
// A class is defined by a T_CLASS + a space + name of the class.
890-
if (is_array($token) && ($token[0] == T_CLASS || $token[0] == T_INTERFACE || $token[0] == T_TRAIT) &&
891-
is_array($tokens[$p + 1]) && $tokens[$p + 1][0] == T_WHITESPACE) {
895+
if (is_array($token) && ($token[0] === T_CLASS || $token[0] === T_INTERFACE || $token[0] === T_TRAIT) &&
896+
is_array($tokens[$p + 1]) && $tokens[$p + 1][0] === T_WHITESPACE) {
892897
$isClass = true;
893-
if (is_array($tokens[$p - 1]) && $tokens[$p - 1][0] == T_PAAMAYIM_NEKUDOTAYIM &&
898+
if (is_array($tokens[$p - 1]) && $tokens[$p - 1][0] === T_PAAMAYIM_NEKUDOTAYIM &&
894899
$tokens[$p - 1][1] === '::') {
895900
// /namespace/Nameclass:class <-- we skip this case.
896901
$isClass = false;
@@ -902,7 +907,7 @@ public function parsePHPFile($filename, string &$runMe): array
902907
// encontramos una clase
903908
$min = min($p + 30, count($tokens) - 1);
904909
for ($i = $p + 2; $i < $min; $i++) {
905-
if (is_array($tokens[$i]) && $tokens[$i][0] == T_STRING) {
910+
if (is_array($tokens[$i]) && $tokens[$i][0] === T_STRING) {
906911
$className = $tokens[$i][1];
907912
break;
908913
}
@@ -922,7 +927,7 @@ public function parsePHPFile($filename, string &$runMe): array
922927
private function inExclusion(string $path, array $exclusions): bool
923928
{
924929
foreach ($exclusions as $ex) {
925-
if ($ex != '') {
930+
if ($ex !== '') {
926931
if ($ex[strlen($ex) - 1] === '*') {
927932
$bool = $this->startwith($path, substr($ex, 0, -1));
928933
if ($bool) {
@@ -935,7 +940,7 @@ private function inExclusion(string $path, array $exclusions): bool
935940
return true;
936941
}
937942
}
938-
if ((strpos($ex, '*') === false) && $path == $ex) {
943+
if ((strpos($ex, '*') === false) && $path === $ex) {
939944
return true;
940945
}
941946
}
@@ -966,7 +971,9 @@ public function genautoload($file, $namespaces, $namespacesAlt, $pathAbsolute, $
966971

967972
$template = "<?php" . <<<'EOD'
968973
969-
/** @noinspection PhpUnhandledExceptionInspection
974+
/**
975+
* @noinspection PhpRedundantVariableDocTypeInspection
976+
* @noinspection PhpUnhandledExceptionInspection
970977
* @noinspection PhpMissingParamTypeInspection
971978
* @noinspection ClassConstantCanBeUsedInspection
972979
*/
@@ -983,25 +990,33 @@ public function genautoload($file, $namespaces, $namespacesAlt, $pathAbsolute, $
983990
/** @var bool $autoloadone__debug if true then in case of error, it shows more information about the source of it */
984991
$autoloadone__debug = true;
985992
986-
/* @var string[] Where $_arrautoloadCustom['namespace\Class']='folder\file.php' */
993+
/**
994+
* @var string[] ${{tempname}}__arrautoloadCustom It stores the map of definitions full=>filename.<br>
995+
* example: ['namespace\Class']='folder\file.php'
996+
*/
987997
${{tempname}}__arrautoloadCustom = [
988998
{{custom}}
989999
];
9901000
${{tempname}}__arrautoloadCustomCommon = [
9911001
{{customCommon}}
9921002
];
9931003
994-
/* @var string[] Where $_arrautoload['namespace']='folder' */
1004+
/* @var string[] ${{tempname}}__arrautoload It stores the map of definitions as namespace=>folder
1005+
* Example: ['namespace']='folder'
1006+
*/
9951007
${{tempname}}__arrautoload = [
9961008
{{include}}
9971009
];
9981010
${{tempname}}__arrautoloadCommon = [
9991011
{{includeCommon}}
10001012
];
10011013
1002-
/* @var boolean[] Where $_arrautoload['namespace' or 'namespace\Class']=true if it's absolute (it uses the full path) */
1014+
/**
1015+
* @var boolean[] ${{tempname}}__arrautoloadAbsolute It stores the map absolutely<br>
1016+
* Example: $['namespace' or 'namespace\Class']=true if it's absolute (it uses the full path)
1017+
*/
10031018
${{tempname}}__arrautoloadAbsolute = [
1004-
{{includeabsolute}}
1019+
{{includeabsolute}}
10051020
];
10061021
10071022
/**
@@ -1010,7 +1025,7 @@ public function genautoload($file, $namespaces, $namespacesAlt, $pathAbsolute, $
10101025
*/
10111026
function {{tempname}}__auto($class_name)
10121027
{
1013-
// its called only if the class is not loaded.
1028+
// it's called only if the class is not loaded.
10141029
set_exception_handler('autoloadone_exception_handler');
10151030
$p=strrpos($class_name,'\\');
10161031
if($p!==false) {
@@ -1037,29 +1052,28 @@ function {{tempname}}__auto($class_name)
10371052
if(!class_exists($class_name)) {
10381053
throw new RuntimeException("AutoLoadOne Error: No file found for class [$class_name]");
10391054
}
1040-
restore_exception_handler();
1055+
restore_exception_handler();
10411056
}
10421057
10431058
/**
1044-
* We load the file.
1059+
* We load the file.
10451060
* @param string $className the name of the class
10461061
* @param string $filename the filename to load
10471062
* @param string $key key of the class it could be the full class name or only the namespace
1048-
* @param string $arrayName [optional] it's the name of the arrayname used to replaced |n| values.
1063+
* @param string $arrayName [optional] it's the name of the arrayname used to replaced |n| values.
10491064
* @throws Exception
10501065
*/
10511066
function {{tempname}}__loadIfExists($className,$filename, $key,$arrayName='')
10521067
{
10531068
if (isset($GLOBALS['{{tempname}}__arrautoloadAbsolute'][$key])) {
1054-
$fullFile = $filename; // its an absolute path
1069+
$fullFile = $filename; // it's an absolute path
10551070
if (strpos($fullFile, '../') === 0) { // Or maybe, not, it's a remote-relative path.
10561071
$oldDir = getcwd(); // we copy the current url
10571072
chdir(__DIR__);
10581073
}
10591074
} else {
1060-
$fullFile = __DIR__ . '/' . {{tempname}}__replaceCurlyVariable($filename, $arrayName); // its relative to this path
1075+
$fullFile = __DIR__ . '/' . {{tempname}}__replaceCurlyVariable($filename, $arrayName); // it's relative to this path
10611076
}
1062-
/** @noinspection PhpIncludeInspection */
10631077
if ((@include $fullFile) === false) {
10641078
if ($GLOBALS['autoloadone__debug']) {
10651079
throw new RuntimeException("AutoLoadOne Error: Loading file [$fullFile] for class [$className]");
@@ -1069,7 +1083,7 @@ function {{tempname}}__loadIfExists($className,$filename, $key,$arrayName='')
10691083
if (isset($oldDir)) {
10701084
chdir($oldDir);
10711085
}
1072-
}
1086+
}
10731087
function {{tempname}}__replaceCurlyVariable($string,$arrayName) {
10741088
if(strpos($string,'|')===false) {
10751089
return $string;
@@ -1095,7 +1109,7 @@ function autoloadone_exception_handler($exception) {
10951109
} else {
10961110
$r .= '(nofile)' . '[' . @$error['line'] . ']' . ' function:' . @$error['function'] . '('
10971111
.( is_array(@$error['args']) ? @implode(',', @$error['args']) . ')' :@$error['args'])
1098-
. "\n";
1112+
. "\n";
10991113
}
11001114
}
11011115
}
@@ -1178,7 +1192,7 @@ function autoloadone_exception_handler($exception) {
11781192
$htmlCommonAbsolute,
11791193
$includeAbsolute,
11801194
$htmlCommonNameAbs,
1181-
uniqid('s'),
1195+
substr(uniqid('s'),0,6), // we don't need a long name.
11821196
$autorun,
11831197
_AUTOLOAD_COMPOSERJSON ?
11841198
'Autorun: this file was created using _AUTOLOAD_COMPOSERJSON=true'

README.md

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# PHP Auto Include AutoLoadOne Generator
22

3-
AutoloadOne is a program that generates an autoload class (Auto Include) for PHP that is project specific. This class is useful to use classes on code without calling each "include" manually. It is a direct alternative to Composer's autoload, but it works differently. Composer's autoload, scans the folders during runtime, while this library only scans the folder once and it stores the result.
3+
AutoloadOne is a program that generates an autoload class (Auto Include) for PHP that is project specific. This class is useful to use classes on code without calling each "include" manually. It is a direct alternative to Composer's autoload, but it works differently.
4+
Composer's autoload, scans the folders during runtime, while this library only scans the folder once, and it stores the result.
45

56
[![Packagist](https://img.shields.io/packagist/v/eftec/autoloadone.svg)](https://packagist.org/packages/eftec/autoloadone)
67
[![Total Downloads](https://poser.pugx.org/eftec/autoloadone/downloads)](https://packagist.org/packages/eftec/autoloadone)
@@ -15,7 +16,7 @@ AutoloadOne is a program that generates an autoload class (Auto Include) for PHP
1516
Contrary to other alternatives, it supports the easiest way to autoload classes using PHP without sacrifice performance.
1617
How it works?. AutoLoadOne pre-calculates every class of a project and generates a single autoload.php (or the name
1718
indicated) file that it's ready to use. You don't need a specific folder, structure or rule to use it. Just generate
18-
the autoload **class**, **include** and you are ready to load any class (even classes without a namespace, classes in the
19+
the autoload **class**, **include,** and you are ready to load any class (even classes without a namespace, classes in the
1920
namespace in different folders, multiple classes defined in a single file...).
2021

2122
AutoLoadOne is a replacement to Composer's Autoload, rendering obsolete the use of psr-0 or psr-4.
@@ -391,9 +392,9 @@ However, some classes are not required to be loaded by the project (for example
391392
For example, excluding PHPUnit and Mockery reduces the use to 206mb (1000 users) or 2gb (10k users) but **we could optimize it even further.**
392393

393394
| Concurrent Users | Composer's autoload (Optimized) | AutoLoadOne | AutoLoadOne Optimized | AutoLoadOne Optimized and compressed |
394-
|------------------|---------------------------------|-------------|-----------------------|-----------------------|
395-
| 1000 | 609mb | 301mb | 206mb | 130mb |
396-
| 10000 | 6gb | 3gb | 2gb |1gb |
395+
|------------------|---------------------------------|-------------|-----------------------|--------------------------------------|
396+
| 1000 | 609mb | 301mb | 206mb | 130mb |
397+
| 10000 | 6gb | 3gb | 2gb | 1gb |
397398

398399

399400
### Lookup usage?
@@ -570,6 +571,8 @@ reads the composer.json files once.
570571

571572
## Version
572573

574+
* 1.27 2022-08-27
575+
* [fix] fixed a problem with double mapped.
573576
* 1.26 2022-02-21
574577
* [fix] compatibility with PHP 8.1.
575578
* Raising the compatibility with php 7.1.5 and higher. If you want to use an old version, then you can use 1.25.1

0 commit comments

Comments
 (0)