Skip to content

Commit 581f96a

Browse files
committed
1.19
1 parent 20a3bfa commit 581f96a

File tree

6 files changed

+143
-37
lines changed

6 files changed

+143
-37
lines changed

AutoLoadOne.php

Lines changed: 73 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,12 @@
4141
*
4242
* @copyright Jorge Castro C. MIT License https://github.com/EFTEC/AutoLoadOne
4343
*
44-
* @version 1.18 2020-04-23
44+
* @version 1.19 2020-05-26
4545
* @noautoload
4646
*/
4747
class AutoLoadOne
4848
{
49-
const VERSION = '1.18';
49+
const VERSION = '1.19';
5050
const JSON_UNESCAPED_SLASHES = 64;
5151
const JSON_PRETTY_PRINT = 128;
5252
const JSON_UNESCAPED_UNICODE = 256;
@@ -712,9 +712,9 @@ private function createArrayPHP($array) {
712712

713713
public function listFolderFiles($dir) {
714714
$arr = [];
715-
$this->listFolderFilesAlt($dir, $arr);
716-
717-
return $arr;
715+
$json=[];
716+
$this->listFolderFilesAlt($dir, $arr,$json);
717+
return [$arr,$json];
718718
}
719719

720720
private function fixRelative($path) {
@@ -725,7 +725,7 @@ private function fixRelative($path) {
725725
return $path;
726726
}
727727

728-
public function listFolderFilesAlt($dir, &$list) {
728+
public function listFolderFilesAlt($dir, &$list,&$json) {
729729
if ($dir === '') {
730730
return [];
731731
}
@@ -739,18 +739,43 @@ public function listFolderFilesAlt($dir, &$list) {
739739
}
740740
foreach ($ffs as $ff) {
741741
if ($ff !== '.' && $ff !== '..') {
742+
if($ff==='composer.json') {
743+
$json[] = $list[] = $dir . '/' . $ff;
744+
}
742745
if ((strlen($ff) >= 5) && substr($ff, -4) == $this->extension) {
743746
// PHP_OS_FAMILY=='Windows'
744747
$list[] = $dir . '/' . $ff;
745748
}
746749
if (is_dir($dir . '/' . $ff)) {
747-
$this->listFolderFilesAlt($dir . '/' . $ff, $list);
750+
$this->listFolderFilesAlt($dir . '/' . $ff, $list,$json);
748751
}
749752
}
750753
}
751754

752755
return $list;
753756
}
757+
public function parseJSONFile($filename) {
758+
try {
759+
$filenameFixed=$this->fixRelative($filename).'/composer.json';
760+
if (is_file($filenameFixed)) {
761+
762+
$content = file_get_contents($filenameFixed);
763+
} else {
764+
return [];
765+
}
766+
if ($this->debugMode) {
767+
echo $filename . ' trying token...<br>';
768+
}
769+
$tokens = json_decode($content,true);
770+
} catch (Exception $ex) {
771+
echo "Error in $filename\n";
772+
die(1);
773+
}
774+
if(isset($tokens['autoload']['files'])) {
775+
return $tokens['autoload']['files'];
776+
}
777+
return [];
778+
}
754779

755780
/**
756781
* @param $filename
@@ -861,11 +886,6 @@ public function genPath($path) {
861886
break;
862887
}
863888
}
864-
/*if (substr($path,1,2)==':/') {
865-
// windows style c:/somefolder
866-
$baseCommon=0;
867-
}
868-
*/
869889
// moving down the relative path (/../../)
870890
$c = substr_count(substr($this->baseGen, $baseCommon), '/');
871891
$r = str_repeat('/..', $c);
@@ -952,22 +972,34 @@ public function process() {
952972
$this->fileGen = $this->fixSeparator($this->fileGen);
953973
if ($this->rooturl) {
954974
$this->baseGen = $this->dirNameLinux($this->fileGen . '/' . $this->getFileName());
955-
$files = $this->listFolderFiles($this->rooturl);
975+
$filesjson = $this->listFolderFiles($this->rooturl);
976+
$files=$filesjson[0];
977+
/** @var array $json Example['dir/composer.json','folder/composer.json'] */
978+
$json=$filesjson[1];
956979
$filesAbsolute = array_fill(0, count($files), false);
980+
$jsonAbsolute = array_fill(0, count($json), false);
957981

958982
$extPathArr = explode(',', $this->externalPath);
959983
foreach ($extPathArr as $ep) {
960984
$ep = $this->dirNameLinux($ep, false);
961-
$files2 = $this->listFolderFiles($ep);
985+
$filesjson2 = $this->listFolderFiles($ep);
986+
$files2=$filesjson2[0];
987+
$json2=$filesjson2[1];
988+
foreach ($json2 as $newJson) {
989+
$json[]=$newJson;
990+
$jsonAbsolute[]=true;
991+
}
962992
foreach ($files2 as $newFile) {
963993
$files[] = $newFile;
964994
$filesAbsolute[] = true;
965995
}
966996
}
997+
967998
$ns = [];
968999
$nsAlt = [];
9691000
$pathAbsolute = [];
9701001
$autoruns = [];
1002+
$autorunsFromJson=[];
9711003
$autorunsFirst = [];
9721004
$excludeNSArr = str_replace(["\n", "\r", ' '], '', $this->excludeNS);
9731005
$excludeNSArr = explode(',', $excludeNSArr);
@@ -980,6 +1012,27 @@ public function process() {
9801012

9811013
$this->result = '';
9821014
if ($this->button) {
1015+
foreach($json as $key=>$f) {
1016+
//echo "running $f<br>";
1017+
$f = $this->fixSeparator($f);
1018+
$dirOriginal = $this->dirNameLinux($f);
1019+
1020+
$jsonE=$this->parseJSONFile($dirOriginal);
1021+
//var_dump($jsonE);
1022+
foreach($jsonE as $item) {
1023+
if (!$jsonAbsolute[$key]) {
1024+
$dir = $this->genPath($dirOriginal); //folder/subfolder/f1
1025+
$full = $dir.'/'.$item; ///folder/subfolder/f1/F1.php
1026+
} else {
1027+
$dir = $dirOriginal; //D:/Dropbox/www/currentproject/AutoLoadOne/examples/folder
1028+
$full = $dirOriginal.'/'.$item; //D:/Dropbox/www/currentproject/AutoLoadOne/examples/folder/NaturalClass.php
1029+
}
1030+
$autoruns[] = $full;
1031+
$autorunsFromJson[]=$full;
1032+
//var_dump($this->baseGen);
1033+
//echo "<br>adding autorun $item , $dirOriginal , $dir , $full<br>";
1034+
}
1035+
}
9831036
foreach ($files as $key => $f) {
9841037
$f = $this->fixSeparator($f);
9851038
$runMe = '';
@@ -1089,7 +1142,12 @@ public function process() {
10891142
$this->addLog("Adding file <b>$auto</b> Reason: <b>@autoload first</b> found");
10901143
}
10911144
foreach ($autoruns as $auto) {
1092-
$this->addLog("Adding file <b>$auto</b> Reason: <b>@autoload</b> found");
1145+
if(in_array($auto,$autorunsFromJson)) {
1146+
$this->addLog("Adding file <b>$auto</b> Reason: <b>composer.json</b> found");
1147+
} else {
1148+
$this->addLog("Adding file <b>$auto</b> Reason: <b>@autoload</b> found");
1149+
}
1150+
10931151
}
10941152
$autoruns = array_merge($autorunsFirst, $autoruns);
10951153
$this->result =

README.md

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -509,10 +509,46 @@ Deny from all
509509
</Files>
510510
```
511511

512-
* Or you could restrict the access to PHP and it's the behaviour by default on Linux (it runs under Apache's account, most of the time as user NOBODY)
512+
* Or you could restrict the access to PHP and it's the behaviour by default on Linux (it runs under Apache's account
513+
, most of the time as user NOBODY)
514+
515+
516+
## Composer.json
517+
518+
Commonly, this library does not need **composer.json**, and it actively ignores its configuration (because it reads the PHP
519+
source code directly). However, since 1.19 it reads part of the information of composer.json (for compatibility
520+
purpose). For example, the library **guzzlehttp** uses this feature.
521+
522+
Let's say the next file:
523+
524+
composer.json
525+
526+
```json
527+
{
528+
"autoload": {
529+
"psr-4": {
530+
"blahblahblah": "some folder blahblahblah/"
531+
},
532+
"files": ["src/functions_include.php"]
533+
}
534+
}
535+
```
536+
537+
AutoLoadOne includes the file (or files) declared in composer.json and declared in 'files'. This file or files is
538+
added to our autoload.php as follows:
539+
540+
```php
541+
@include __DIR__.'/folder/jsontest/src/functions_include.php';
542+
```
543+
Since it is done once (when autoload.php is generated), then it does not affect the performance.
544+
545+
546+
513547

514548
## Version
515549

550+
* 1.19 2020-06-05
551+
* Added composer.json 'autoload'/'files'
516552
* 1.18 2020-04-23
517553
* Some cleaning.
518554
* The generated file is alsoc cleaned and optimized.

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "eftec/autoloadone",
33
"description": "AutoloadOne is a program that generates an autoload class for PHP.",
44
"type": "software",
5-
"version": "1.18",
5+
"version": "1.19",
66
"keywords": ["php","autoload"],
77
"homepage": "https://github.com/EFTEC/AutoLoadOne",
88
"minimum-stability": "beta",

examples/autoload.php

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
* This class is used for autocomplete.
44
* Class _AUTOLOAD_
55
* @noautoload it avoids to index this class
6-
* @generated by AutoLoadOne 1.17 generated 2020/04/23 05:26:20
6+
* @generated by AutoLoadOne 1.18 generated 2020/06/05 09:59:32
77
* @copyright Copyright Jorge Castro C - MIT License. https://github.com/EFTEC/AutoLoadOne
88
*/
9-
$s5ea207fc4c063__debug = true;
9+
$s5eda4fc45cee8__debug = true;
1010

1111
/* @var string[] Where $_arrautoloadCustom['namespace\Class']='folder\file.php' */
12-
$s5ea207fc4c063__arrautoloadCustom = [
12+
$s5eda4fc45cee8__arrautoloadCustom = [
1313
'ClassFolder1' => '|1|1.php',
1414
'ClassFolder2' => '|1|2.php',
1515
'ClassFolder3' => '|1|3.php',
@@ -21,15 +21,15 @@
2121
'ClassWithoutNameSpace' => '|4|alt/ClassWithoutNameSpace.php',
2222
'nsfolder\subnamespace\CustomClass' => '|4|alt/CustomClass.php'
2323
];
24-
$s5ea207fc4c063__arrautoloadCustomCommon = [
24+
$s5eda4fc45cee8__arrautoloadCustomCommon = [
2525
1 => '/folder/folder1234567890123456789/ClassFolder',
2626
2 => '/folder/multiplenamespace.php',
2727
3 => '/folder/multiplenamespace',
2828
4 => '/folder/subfolder'
2929
];
3030

3131
/* @var string[] Where $_arrautoload['namespace']='folder' */
32-
$s5ea207fc4c063__arrautoload = [
32+
$s5eda4fc45cee8__arrautoload = [
3333
'nsfolder' => '/folder',
3434
'specialnamespace' => '/folder',
3535
'folder_ns\subnamespace\ClassFolder1' => '|1|',
@@ -43,36 +43,36 @@
4343
'nsfolder\subnamespace\f5' => '|2|/f5',
4444
'sub\sub\sub' => '|2|/subsubfolder'
4545
];
46-
$s5ea207fc4c063__arrautoloadCommon = [
46+
$s5eda4fc45cee8__arrautoloadCommon = [
4747
1 => '/folder/folder_ns_1234567890123456789',
4848
2 => '/folder/subfolder'
4949
];
5050

5151
/* @var boolean[] Where $_arrautoload['namespace' or 'namespace\Class']=true if it's absolute (it uses the full path) */
52-
$s5ea207fc4c063__arrautoloadAbsolute = [
52+
$s5eda4fc45cee8__arrautoloadAbsolute = [
5353

5454
];
5555

5656
/**
5757
* @param $class_name
5858
* @throws Exception
5959
*/
60-
function s5ea207fc4c063__auto($class_name)
60+
function s5eda4fc45cee8__auto($class_name)
6161
{
6262
// its called only if the class is not loaded.
6363
$ns = dirname($class_name); // without trailing
6464
$ns = ($ns === '.') ? '' : $ns;
6565
$cls = basename($class_name);
6666
// special cases
67-
if (isset($GLOBALS['s5ea207fc4c063__arrautoloadCustom'][$class_name])) {
68-
s5ea207fc4c063__loadIfExists($GLOBALS['s5ea207fc4c063__arrautoloadCustom'][$class_name]
69-
, $class_name,'s5ea207fc4c063__arrautoloadCustomCommon');
67+
if (isset($GLOBALS['s5eda4fc45cee8__arrautoloadCustom'][$class_name])) {
68+
s5eda4fc45cee8__loadIfExists($GLOBALS['s5eda4fc45cee8__arrautoloadCustom'][$class_name]
69+
, $class_name,'s5eda4fc45cee8__arrautoloadCustomCommon');
7070
return;
7171
}
7272
// normal (folder) cases
73-
if (isset($GLOBALS['s5ea207fc4c063__arrautoload'][$ns])) {
74-
s5ea207fc4c063__loadIfExists($GLOBALS['s5ea207fc4c063__arrautoload'][$ns] . '/' . $cls . '.php'
75-
, $ns,'s5ea207fc4c063__arrautoloadCommon');
73+
if (isset($GLOBALS['s5eda4fc45cee8__arrautoload'][$ns])) {
74+
s5eda4fc45cee8__loadIfExists($GLOBALS['s5eda4fc45cee8__arrautoload'][$ns] . '/' . $cls . '.php'
75+
, $ns,'s5eda4fc45cee8__arrautoloadCommon');
7676
return;
7777
}
7878
}
@@ -84,20 +84,20 @@ function s5ea207fc4c063__auto($class_name)
8484
* @param string $arrayName [optional] it's the name of the arrayname used to replaced |n| values.
8585
* @throws Exception
8686
*/
87-
function s5ea207fc4c063__loadIfExists($filename, $key,$arrayName='')
87+
function s5eda4fc45cee8__loadIfExists($filename, $key,$arrayName='')
8888
{
89-
if (isset($GLOBALS['s5ea207fc4c063__arrautoloadAbsolute'][$key])) {
89+
if (isset($GLOBALS['s5eda4fc45cee8__arrautoloadAbsolute'][$key])) {
9090
$fullFile = $filename; // its an absolute path
9191
if (strpos($fullFile, '../') === 0) { // Or maybe, not, it's a remote-relative path.
9292
$oldDir = getcwd(); // we copy the current url
9393
chdir(__DIR__);
9494
}
9595
} else {
96-
$fullFile = __DIR__ . '/' . s5ea207fc4c063__replaceCurlyVariable($filename, $arrayName); // its relative to this path
96+
$fullFile = __DIR__ . '/' . s5eda4fc45cee8__replaceCurlyVariable($filename, $arrayName); // its relative to this path
9797
}
9898
/** @noinspection PhpIncludeInspection */
9999
if ((@include $fullFile) === false) {
100-
if ($GLOBALS['s5ea207fc4c063__debug']) {
100+
if ($GLOBALS['s5eda4fc45cee8__debug']) {
101101
throw new RuntimeException('AutoLoadOne Error: Loading file [' . __DIR__ . '/' . $filename . '] for class [' . basename($filename) .
102102
']');
103103
}
@@ -107,7 +107,7 @@ function s5ea207fc4c063__loadIfExists($filename, $key,$arrayName='')
107107
chdir($oldDir);
108108
}
109109
}
110-
function s5ea207fc4c063__replaceCurlyVariable($string,$arrayName) {
110+
function s5eda4fc45cee8__replaceCurlyVariable($string,$arrayName) {
111111
if(strpos($string,'|')===false) {
112112
return $string;
113113
} // nothing to replace.
@@ -119,10 +119,11 @@ function s5ea207fc4c063__replaceCurlyVariable($string,$arrayName) {
119119
}
120120

121121
spl_autoload_register(static function ($class_name) {
122-
s5ea207fc4c063__auto($class_name);
122+
s5eda4fc45cee8__auto($class_name);
123123
});
124124
// autorun
125125
@include __DIR__.'/folder/runme.php';
126+
@include __DIR__.'/folder/jsontest/src/functions_include.php';
126127
@include __DIR__.'/folder/RunMeClass.php';
127128
@include __DIR__.'/folder/runmelast.php';
128129

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"autoload": {
3+
"psr-4": {
4+
"blahblahblah": "some folder blahblahblah/"
5+
},
6+
"files": ["src/functions_include.php"]
7+
}
8+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?php
2+
3+
echo "include me!<br>";

0 commit comments

Comments
 (0)