Skip to content

Commit a2844d5

Browse files
1.30
1 parent f322e11 commit a2844d5

File tree

6 files changed

+161
-120
lines changed

6 files changed

+161
-120
lines changed

.gitattributes

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
/examples export-ignore
2-
/vendor export-ignore
3-
/doc export-ignore
4-
/tests export-ignore
5-
/.gitattributes export-ignore
6-
/.gitignore export-ignore
7-
/.travis.yml export-ignore
8-
/phpunit.xml export-ignore
1+
/examples export-ignore
2+
/examplesexternal export-ignore
3+
/vendor export-ignore
4+
/doc export-ignore
5+
/tests export-ignore
6+
/.gitattributes export-ignore
7+
/.gitignore export-ignore
8+
/.travis.yml export-ignore
9+
/phpunit.xml export-ignore

AutoLoadOne.php

Lines changed: 54 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,12 @@
4040
*
4141
* @copyright Jorge Castro C. MIT License https://github.com/EFTEC/AutoLoadOne
4242
*
43-
* @version 1.29 2024-12-30
43+
* @version 1.30 2025-02-21
4444
* @noautoload
4545
*/
4646
class AutoLoadOne
4747
{
48-
public const VERSION = '1.29';
48+
public const VERSION = '1.30';
4949
public ?string $rooturl = '';
5050
public ?string $fileGen = '';
5151
public ?int $savefile = 1;
@@ -62,20 +62,18 @@ class AutoLoadOne
6262
public string $cli = '';
6363
public ?bool $logged = false;
6464
public string $current = '';
65-
public int $t1 = 0;
65+
public float $t1 = 0;
6666
public bool $debugMode = false;
6767
public int $statNumClass = 0;
6868
public int $statNumPHP = 0;
6969
public int $statConflict = 0;
7070
public int $statError = 0;
7171
public array $statNameSpaces = [];
72-
public int $statByteUsed = 1024;
73-
public int $statByteUsedCompressed = 1024;
72+
public float $statByteUsed = 1024;
73+
public float $statByteUsedCompressed = 1024;
7474
public string $fileConfig = 'autoloadone.json';
75-
7675
public string $extension = '.php';
77-
78-
private $baseGen;
76+
private string $baseGen = "";
7977

8078
/**
8179
* AutoLoadOne constructor.
@@ -138,7 +136,7 @@ public static function format($json, $unescapeUnicode, $unescapeSlashes): string
138136
$buffer = str_replace('\\/', '/', $buffer);
139137
}
140138
if ($unescapeUnicode && function_exists('mb_convert_encoding')) {
141-
$buffer = preg_replace_callback('/(\\\\+)u([0-9a-f]{4})/i', static function ($match) {
139+
$buffer = preg_replace_callback('/(\\\\+)u([0-9a-f]{4})/i', static function($match) {
142140
$l = strlen($match[1]);
143141
if ($l % 2) {
144142
$code = hexdec($match[2]);
@@ -400,7 +398,7 @@ private function loadParam(): bool
400398
public function addLog($txt, string $type = ''): void
401399
{
402400
if (PHP_SAPI === 'cli') {
403-
$txt = str_replace(array('<b>', '<i>', '</b>', '</i>'), array("\033[1m", "\033[4m", "\033[0m", "\033[0m"), $txt);
401+
$txt = str_replace(['<b>', '<i>', '</b>', '</i>'], ["\033[1m", "\033[4m", "\033[0m", "\033[0m"], $txt);
404402
switch ($type) {
405403
case 'error':
406404
case 'staterror':
@@ -465,13 +463,14 @@ private function cleanInputFolder($value): string
465463
// we add ,\n again.
466464
// we remove trailing \
467465
// we remove trailing /
468-
return str_replace(array("\r\n", ",\n", "\n", '\\,', '/,'), array("\n", "\n", ",\n", ',', ','), $value);
466+
return str_replace(["\r\n", ",\n", "\n", '\\,', '/,'], ["\n", "\n", ",\n", ',', ','], $value);
469467
}
470468

471469
public function process(): void
472470
{
473471
$this->rooturl = $this->fixSeparator($this->rooturl);
474472
$this->fileGen = $this->fixSeparator($this->fileGen);
473+
$this->externalPath = $this->fixSeparator($this->externalPath);
475474
if ($this->rooturl) {
476475
$this->baseGen = $this->dirNameLinux($this->fileGen . '/' . $this->getFileName());
477476
[$files, $json] = $this->listFolderFiles($this->rooturl);
@@ -490,9 +489,11 @@ public function process(): void
490489
$filesAbsolute[] = true;
491490
}
492491
}
492+
//die(1);
493493
$ns = [];
494494
$nsAlt = [];
495495
$pathAbsolute = [];
496+
$pathAbsoluteExt = [];
496497
$autoruns = [];
497498
$autorunsFromJson = [];
498499
$autorunsFirst = [];
@@ -589,14 +590,22 @@ public function process(): void
589590
if ($nsp === '') {
590591
$this->addLog("Adding Full map (empty namespace): <b>$altUrl -> $full</b> to class <i>$cs</i>", 'warning');
591592
$nsAlt[$altUrl] = $full;
592-
$pathAbsolute[$altUrl] = $filesAbsolute[$key];
593+
if ($this->externalPath !== "" && strpos($full, $this->externalPath) === 0) {
594+
$pathAbsoluteExt[$altUrl] = $filesAbsolute[$key];
595+
} else {
596+
$pathAbsolute[$altUrl] = $filesAbsolute[$key];
597+
}
593598
} elseif (isset($ns[$nsp])) {
594599
$mapped[] = $nsp . '\\' . $cs;
595600
$this->addLog("Reusing the folder: <b>$nsp -> $dir</b> to class <i>$cs</i>",
596601
'success');
597602
} else {
598603
$ns[$nsp] = $dir;
599-
$pathAbsolute[$nsp] = $filesAbsolute[$key];
604+
if ($this->externalPath !== "" && strpos($dir, $this->externalPath) === 0) {
605+
$pathAbsoluteExt[$nsp] = $filesAbsolute[$key];
606+
} else {
607+
$pathAbsolute[$nsp] = $filesAbsolute[$key];
608+
}
600609
$this->addLog("Adding Folder as namespace: <b>$nsp -> $dir</b> to class <i>$cs</i>", 'info');
601610
}
602611
}
@@ -614,7 +623,11 @@ public function process(): void
614623
} else {
615624
$this->addLog("Adding Full relation: <b>$altUrl -> $full</b> to class <i>$cs</i>", 'warning');
616625
$nsAlt[$altUrl] = $full;
617-
$pathAbsolute[$altUrl] = $filesAbsolute[$key];
626+
if ($this->externalPath !== "" && strpos($full, $this->externalPath) === 0) {
627+
$pathAbsoluteExt[$altUrl] = $filesAbsolute[$key];
628+
} else {
629+
$pathAbsolute[$altUrl] = $filesAbsolute[$key];
630+
}
618631
}
619632
}
620633
}
@@ -641,7 +654,7 @@ public function process(): void
641654
$autoruns = array_merge($autorunsFirst, $autoruns);
642655
$this->result =
643656
$this->genautoload($this->fileGen . '/' . $this->getFileName(), $ns, $nsAlt, $pathAbsolute,
644-
$autoruns);
657+
$pathAbsoluteExt, $autoruns);
645658
}
646659
if ($this->statNumPHP === 0) {
647660
$p = 100;
@@ -908,7 +921,7 @@ public function endswith($string, $test): bool
908921
return substr_compare($string, $test, $strlen - $testlen, $testlen) === 0;
909922
}
910923

911-
public function genautoload($file, $namespaces, $namespacesAlt, $pathAbsolute, $autoruns)
924+
public function genautoload($file, $namespaces, $namespacesAlt, $pathAbsolute, $pathAbsoluteExt, $autoruns)
912925
{
913926
$template = "<?php" . <<<'EOD'
914927
@@ -959,6 +972,9 @@ public function genautoload($file, $namespaces, $namespacesAlt, $pathAbsolute, $
959972
${{tempname}}__arrautoloadAbsolute = [
960973
{{includeabsolute}}
961974
];
975+
${{tempname}}__arrautoloadAbsoluteExt = [
976+
{{includeabsoluteext}}
977+
];
962978
963979
/**
964980
* @param $class_name
@@ -1012,6 +1028,8 @@ function {{tempname}}__loadIfExists($className,$filename, $key,$arrayName='')
10121028
$oldDir = getcwd(); // we copy the current url
10131029
chdir(__DIR__);
10141030
}
1031+
} elseif (isset($GLOBALS['{{tempname}}__arrautoloadAbsoluteExt'][$key])) {
1032+
$fullFile = {{tempname}}__replaceCurlyVariable($filename, $arrayName); // it's an absolute external path
10151033
} else {
10161034
$fullFile = __DIR__ . '/' . {{tempname}}__replaceCurlyVariable($filename, $arrayName); // it's relative to this path
10171035
}
@@ -1081,12 +1099,19 @@ function autoloadone_exception_handler($exception) {
10811099
$include = $this->createArrayPHP($namespaces);
10821100
$htmlCommonNameAbs = $this->createArrayPHP($commonNameAbs);
10831101
$includeAbsolute = '';
1102+
$includeAbsoluteExt = '';
10841103
foreach ($pathAbsolute as $k => $v) {
10851104
if ($v) {
10861105
$includeAbsolute .= "\t'$k' => true,\n";
10871106
}
10881107
}
1108+
foreach ($pathAbsoluteExt as $k => $v) {
1109+
if ($v) {
1110+
$includeAbsoluteExt .= "\t'$k' => true,\n";
1111+
}
1112+
}
10891113
$includeAbsolute = rtrim($includeAbsolute, ",\n");
1114+
$includeAbsoluteExt = rtrim($includeAbsoluteExt, ",\n");
10901115
$autorun = ''; //
10911116
$excludePathArr = str_replace(["\n", "\r"], '', $this->excludePath);
10921117
$excludePathArr = explode(',', $excludePathArr);
@@ -1103,27 +1128,29 @@ function autoloadone_exception_handler($exception) {
11031128
}
11041129
}
11051130
// 1024 is the memory used by code, *1.3 is an overhead, usually it's mess.
1106-
$this->statByteUsedCompressed = (strlen($include) + strlen($includeAbsolute) + strlen($custom)) * 1.3 + 1024;
1131+
$this->statByteUsedCompressed = (strlen($include) + strlen($includeAbsolute) + strlen($includeAbsoluteExt) + strlen($custom)) * 1.3 + 1024;
11071132
$this->statByteUsed =
11081133
(strlen($includeNotCompressed) + strlen($htmlCommonAbsolute) + strlen($htmlCommonNameAbs) +
1109-
strlen($includeAbsolute) + strlen($customNotCompressed)) * 1.3 + 1024;
1110-
$template = str_replace(array(
1134+
strlen($includeAbsolute) + strlen($includeAbsoluteExt) + strlen($customNotCompressed)) * 1.3 + 1024;
1135+
$template = str_replace([
11111136
'{{custom}}',
11121137
'{{include}}',
11131138
'{{customCommon}}',
11141139
'{{includeabsolute}}',
1140+
'{{includeabsoluteext}}',
11151141
'{{includeCommon}}',
11161142
'{{tempname}}',
11171143
'{{autorun}}',
11181144
'{{autorun_flag}}',
11191145
'{{version}}',
11201146
'{{extension}}',
11211147
'{{date}}'
1122-
), array(
1148+
], [
11231149
$custom,
11241150
$include,
11251151
$htmlCommonAbsolute,
11261152
$includeAbsolute,
1153+
$includeAbsoluteExt,
11271154
$htmlCommonNameAbs,
11281155
substr(uniqid('s'), 0, 6), // we don't need a long name.
11291156
$autorun,
@@ -1133,7 +1160,7 @@ function autoloadone_exception_handler($exception) {
11331160
$this::VERSION,
11341161
$this->extension,
11351162
date('Y/m/d H:i:s')
1136-
), $template);
1163+
], $template);
11371164
if ($this->savefile) {
11381165
$ok = @file_put_contents($file, $template);
11391166
if ($ok) {
@@ -1545,7 +1572,7 @@ public function render(): void
15451572
15461573
</html>
15471574
TEM1;
1548-
$web = str_replace(array(
1575+
$web = str_replace([
15491576
'{{rooturl}}',
15501577
'{{autorun_enable}}',
15511578
'{{fileGen}}',
@@ -1561,7 +1588,7 @@ public function render(): void
15611588
'{{logstat}}',
15621589
'{{version}}',
15631590
'{{result}}'
1564-
), array(
1591+
], [
15651592
$this->rooturl,
15661593
_AUTOLOAD_COMPOSERJSON ? '<em><b></b>_AUTOLOAD_COMPOSERJSON=true</b> Composer.json autorun is considered. PHP files that contain the comment <b>"@autorun"</b> are executed (even if they don\'t have a class)</em><br>
15671594
<em>PHP files that contain the comment <b>"@autorun first"</b> are executed with priority</em><br>'
@@ -1579,15 +1606,15 @@ public function render(): void
15791606
$this->logStat,
15801607
$this::VERSION,
15811608
$this->result
1582-
), $web);
1609+
], $web);
15831610
$this->cli = "php autoloadone.php -folder \"$this->rooturl\" -filegen \"$this->fileGen\" -save ";
15841611
$tmp = ($this->compression ? 'yes' : 'no');
15851612
$this->cli .= "-compression $tmp ";
1586-
$tmp = str_replace(array("\n", "\r"), '', $this->excludeNS);
1613+
$tmp = str_replace(["\n", "\r"], '', $this->excludeNS);
15871614
$this->cli .= "-excludens \"$tmp\" ";
1588-
$tmp = str_replace(array("\n", "\r"), '', $this->externalPath);
1615+
$tmp = str_replace(["\n", "\r"], '', $this->externalPath);
15891616
$this->cli .= "-externalpath \"$tmp\" ";
1590-
$tmp = str_replace(array("\n", "\r"), '', $this->excludePath);
1617+
$tmp = str_replace(["\n", "\r"], '', $this->excludePath);
15911618
$this->cli .= "-excludepath \"$tmp\"";
15921619
$web = str_replace('{{cli}}', $this->cli, $web);
15931620
$t2 = microtime(true);

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -614,6 +614,9 @@ reads the composer.json files once.
614614

615615

616616
## Version
617+
* 1.30 2025-02-21
618+
* Fixed external path.
619+
* Fixed a warning with a float value.
617620
* 1.29 2024-12-30
618621
* update dependencies.
619622
* cleaned the code

examples/runfirst.php

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,29 @@
1-
<?php
2-
// This code will generate the file autoload.php
3-
4-
use eftec\AutoLoadOne\AutoLoadOne;
5-
6-
@ini_set('max_execution_time', 300);
7-
8-
9-
10-
11-
define("_AUTOLOAD_SELFRUN",false);
12-
define("_AUTOLOAD_ONLYCLI",false);
13-
define("_AUTOLOAD_ENTER",false);
14-
define("_AUTOLOAD_COMPOSERJSON",true);
15-
echo "<h1>Test</h1>It's an interactive test.";
16-
echo "It will generate the file autoload.php of this folder<br>";
17-
echo "And, when you are ready, you could test the result on <a href='runtest.php'>runtest.php</a><br>";
18-
echo "The user and password is autoloadone/autoloadone<hr>";
19-
include "../AutoLoadOne.php";
20-
$auto=new AutoLoadOne();
21-
$auto->extension='.php'; // it's not required. By default it's .php
22-
$auto->rooturl=__DIR__; // this default value is optional, it's only for the example
23-
$auto->fileGen=__DIR__; // this default value is optional, it's only for the example
24-
$auto->savefile=1; // this default value is optional, it's only for the example
25-
$auto->init();
26-
$auto->process();
27-
$auto->render();
1+
<?php
2+
// This code will generate the file autoload.php
3+
4+
use eftec\AutoLoadOne\AutoLoadOne;
5+
6+
@ini_set('max_execution_time', 300);
7+
8+
9+
10+
11+
define("_AUTOLOAD_SELFRUN",false);
12+
define("_AUTOLOAD_ONLYCLI",false);
13+
define("_AUTOLOAD_ENTER",false);
14+
define("_AUTOLOAD_COMPOSERJSON",true);
15+
echo "<h1>Test</h1>It's an interactive test.";
16+
echo "It will generate the file autoload.php of this folder<br>";
17+
echo "And, when you are ready, you could test the result on <a href='runtest.php'>runtest.php</a><br>";
18+
echo "The user and password is autoloadone/autoloadone<hr>";
19+
include "../AutoLoadOne.php";
20+
$auto=new AutoLoadOne();
21+
$auto->extension='.php'; // it's not required. By default it's .php
22+
$auto->rooturl=__DIR__; // this default value is optional, it's only for the example
23+
$auto->fileGen=__DIR__; // this default value is optional, it's only for the example
24+
$auto->savefile=1; // this default value is optional, it's only for the example
25+
$extpath=str_replace("examples","examplesexternal",__DIR__);
26+
$auto->externalPath=$extpath;
27+
$auto->init();
28+
$auto->process();
29+
$auto->render();

0 commit comments

Comments
 (0)