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 */
4747class 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 =
0 commit comments