1313
1414//*************************************************************
1515use Exception ;
16- use RuntimeException ;
16+
1717
1818if (!defined ('_AUTOLOAD_USER ' )) {
1919 define ('_AUTOLOAD_USER ' , 'autoloadone ' );
4545 *
4646 * @copyright Jorge Castro C. MIT License https://github.com/EFTEC/AutoLoadOne
4747 *
48- * @version 1.21 2021-02-15
48+ * @version 1.22 2021-02-26
4949 * @noautoload
5050 */
5151class AutoLoadOne
5252{
53- const VERSION = '1.21 ' ;
54- const JSON_UNESCAPED_SLASHES = 64 ;
55- const JSON_PRETTY_PRINT = 128 ;
56- const JSON_UNESCAPED_UNICODE = 256 ;
53+ const VERSION = '1.22 ' ;
54+
5755
5856 public $ rooturl = '' ;
5957 public $ fileGen = '' ;
@@ -93,10 +91,9 @@ public function __construct() {
9391 $ this ->fileGen = '. ' ; //getcwd(); // dirname($_SERVER['SCRIPT_FILENAME']);
9492 $ this ->rooturl = '. ' ; //getcwd(); // dirname($_SERVER['SCRIPT_FILENAME']);
9593 $ this ->t1 = microtime (true );
96- $ tmpArr =explode ('\\ ' ,$ _SERVER ['SCRIPT_FILENAME ' ]);
94+ $ tmpArr =explode ('/ ' ,$ _SERVER ['SCRIPT_FILENAME ' ]); // it always returns with linux separators.
9795 $ this ->fileConfig =end ($ tmpArr ); // the config name shares the same name than the php but with extension .json
98- $ this ->fileConfig = getcwd () . '/ ' . str_replace ($ this ->extension , '.json ' , $ this ->fileConfig );
99- //var_dump($this->fileConfig);
96+ $ this ->fileConfig =$ this ->dirNameLinux (getcwd ()) . '/ ' . str_replace ($ this ->extension , '.json ' , $ this ->fileConfig );
10097 }
10198
10299 private function getAllParametersCli () {
@@ -194,57 +191,8 @@ private function initSapi() {
194191 echo "------------------------------------------------------------------ \n" ;
195192 }
196193
197- public static function encode ($ data , $ options = 448 ) {
198- if (PHP_VERSION_ID >= 50400 ) {
199- $ json = json_encode ($ data , $ options );
200- if (false === $ json ) {
201- self ::throwEncodeError (json_last_error ());
202- }
203-
204- if (PHP_VERSION_ID < 50428 || (PHP_VERSION_ID >= 50500 && PHP_VERSION_ID < 50512 ) ||
205- (defined ('JSON_C_VERSION ' ) && version_compare (phpversion ('json ' ), '1.3.6 ' , '< ' ))) {
206- $ json = preg_replace ('/\[\s+]/ ' , '[] ' , $ json );
207- $ json = preg_replace ('/{\s+}/ ' , '{} ' , $ json );
208- }
209-
210- return $ json ;
211- }
212194
213- $ json = json_encode ($ data );
214- if (false === $ json ) {
215- self ::throwEncodeError (json_last_error ());
216- }
217-
218- $ prettyPrint = (bool )($ options & self ::JSON_PRETTY_PRINT );
219- $ unescapeUnicode = (bool )($ options & self ::JSON_UNESCAPED_UNICODE );
220- $ unescapeSlashes = (bool )($ options & self ::JSON_UNESCAPED_SLASHES );
221-
222- if (!$ prettyPrint && !$ unescapeUnicode && !$ unescapeSlashes ) {
223- return $ json ;
224- }
225-
226- return self ::format ($ json , $ unescapeUnicode , $ unescapeSlashes );
227- }
228195
229- private static function throwEncodeError ($ code ) {
230- switch ($ code ) {
231- case JSON_ERROR_DEPTH :
232- $ msg = 'Maximum stack depth exceeded ' ;
233- break ;
234- case JSON_ERROR_STATE_MISMATCH :
235- $ msg = 'Underflow or the modes mismatch ' ;
236- break ;
237- case JSON_ERROR_CTRL_CHAR :
238- $ msg = 'Unexpected control character found ' ;
239- break ;
240- case JSON_ERROR_UTF8 :
241- $ msg = 'Malformed UTF-8 characters, possibly incorrectly encoded ' ;
242- break ;
243- default :
244- $ msg = 'Unknown error ' ;
245- }
246- throw new RuntimeException ('JSON encoding failed: ' . $ msg );
247- }
248196
249197 public static function format ($ json , $ unescapeUnicode , $ unescapeSlashes ) {
250198 $ result = '' ;
@@ -341,28 +289,35 @@ private function saveParam() {
341289 return false ;
342290 }
343291 $ param = [];
344- $ param ['rooturl ' ] = $ this ->rooturl ;
345- $ param ['fileGen ' ] = $ this ->fileGen ;
292+ $ currentPath =$ this ->dirNameLinux (getcwd (),false );
293+
294+ $ param ['rooturl ' ] = $ this ->rooturl ===$ currentPath ?'. ' :$ this ->rooturl ;
295+ $ param ['fileGen ' ] = $ this ->fileGen ===$ currentPath ?'. ' :$ this ->fileGen ;
346296 $ param ['savefile ' ] = $ this ->savefile ;
347297 $ param ['compression ' ] = $ this ->compression ;
348298 $ param ['savefileName ' ] = $ this ->savefileName ;
349299 $ param ['excludeNS ' ] = $ this ->excludeNS ;
350300 $ param ['excludePath ' ] = $ this ->excludePath ;
351301 $ param ['externalPath ' ] = $ this ->externalPath ;
352- $ remote = [];
353- $ remote ['rooturl ' ] = '' ;
354- $ remote ['destination ' ] = $ this ->fileGen ;
355- $ remote ['name ' ] = '' ;
356- $ remoteint = '1 ' ;
357- $ generatedvia = 'AutoloadOne ' ;
358- $ date = date ('Y/m/d h:i ' );
359-
360- return @file_put_contents ($ this ->fileConfig , self ::encode ([
361- 'application ' => $ generatedvia ,
362- 'generated ' => $ date ,
363- 'local ' => $ param ,
364- 'remote ' => [$ remoteint => $ remote ]
365- ]));
302+
303+ $ fullPHP = @file_get_contents ($ this ->savefileName );
304+ if ($ fullPHP === false ) {
305+ return false ;
306+ }
307+ $ a1 =strpos ($ fullPHP ,'/* -- CONFIG START HERE -- ' );
308+ if ($ a1 ===false ) {
309+ return false ;
310+ }
311+ $ a1 +=strlen ('/* -- CONFIG START HERE -- ' );
312+ $ a2 =strpos ($ fullPHP ,"\n-- CONFIG END HERE -- */ " ,$ a1 );
313+ if ($ a2 ===false ) {
314+ return false ;
315+ }
316+ $ txt =substr ($ fullPHP ,0 ,$ a1 )."\n"
317+ .json_encode ($ param ,JSON_PRETTY_PRINT +JSON_UNESCAPED_SLASHES +JSON_UNESCAPED_UNICODE ).
318+ substr ($ fullPHP ,$ a2 );
319+
320+ return @file_put_contents ($ this ->savefileName ,$ txt );
366321 }
367322
368323 /**
@@ -372,19 +327,43 @@ private function loadParam() {
372327 if (!_AUTOLOAD_SAVEPARAM ) {
373328 return false ;
374329 }
375- $ txt = @file_get_contents ($ this ->fileConfig );
376- if ($ txt === false ) {
330+ $ fullPHP = @file_get_contents ($ this ->savefileName );
331+ if ($ fullPHP === false ) {
332+ $ fullPHP ='' ;
333+ }
334+ $ a1 =strpos ($ fullPHP ,'/* -- CONFIG START HERE -- ' );
335+ if ($ a1 ===false ) {
336+ // we try the old method (json file if exists)
337+
338+ $ oldMethod =@file_get_contents ($ this ->fileConfig );
339+ if (!$ oldMethod ) {
340+ return false ;
341+ }
342+ $ this ->addLog ('Reading the configuration using the old method ' .$ this ->fileConfig .' (you could delete this file) ' ,'error ' );
343+ $ param = json_decode ($ oldMethod , true );
344+ $ param =isset ($ param ['local ' ])?$ param ['local ' ]:null ;
345+ } else {
346+ $ a1 +=strlen ('/* -- CONFIG START HERE -- ' );
347+ $ a2 =strpos ($ fullPHP ,'-- CONFIG END HERE -- ' ,$ a1 );
348+ if ($ a2 ===false ) {
349+ return false ;
350+ }
351+ $ txt =trim (substr ($ fullPHP ,$ a1 ,$ a2 -$ a1 ));
352+ $ param = json_decode ($ txt , true );
353+ }
354+
355+
356+ if ($ param ===null ) {
377357 return false ;
378358 }
379- $ param = json_decode ($ txt , true );
380- $ this ->fileGen = @$ param ['local ' ]['fileGen ' ];
359+ $ this ->fileGen = @$ param ['fileGen ' ];
381360 $ this ->fileGen = ($ this ->fileGen === '. ' ) ? $ this ->rooturl : $ this ->fileGen ;
382- $ this ->savefile = @$ param ['local ' ][ ' savefile ' ];
383- $ this ->compression = @$ param ['local ' ][ ' compression ' ];
384- $ this ->savefileName = @$ param ['local ' ][ ' savefileName ' ];
385- $ this ->excludeNS = @$ param ['local ' ][ ' excludeNS ' ];
386- $ this ->excludePath = @$ param ['local ' ][ ' excludePath ' ];
387- $ this ->externalPath = @$ param ['local ' ][ ' externalPath ' ];
361+ $ this ->savefile = @$ param ['savefile ' ];
362+ $ this ->compression = @$ param ['compression ' ];
363+ $ this ->savefileName = @$ param ['savefileName ' ];
364+ $ this ->excludeNS = @$ param ['excludeNS ' ];
365+ $ this ->excludePath = @$ param ['excludePath ' ];
366+ $ this ->externalPath = @$ param ['externalPath ' ];
388367 return true ;
389368 }
390369
@@ -410,8 +389,11 @@ private function initWeb() {
410389 if (!$ this ->button ) {
411390 $ loadOk = $ this ->loadParam ();
412391 if ($ loadOk === false ) {
413- $ this ->addLog ('Unable to load configuration file <b> ' . $ this ->fileConfig .
392+ $ this ->addLog ('Unable to load configuration file <b> ' . $ this ->savefileName .
414393 '</b>. It is not obligatory ' , 'warning ' );
394+ } else {
395+ $ this ->addLog ('Configuration loaded <b> ' . $ this ->savefileName .
396+ '</b>. ' , 'info ' );
415397 }
416398 } else {
417399 $ this ->debugMode = isset ($ _GET ['debug ' ]);
@@ -431,11 +413,6 @@ private function initWeb() {
431413 $ this ->savefileName = (@$ _POST ['savefileName ' ]) ?: $ this ->savefileName ;
432414 $ this ->stop = @$ _POST ['stop ' ];
433415 $ this ->compression = @$ _POST ['compression ' ];
434- $ ok = $ this ->saveParam ();
435- if ($ ok === false ) {
436- $ this ->addLog ('Unable to save configuration file <b> ' . $ this ->fileConfig .
437- '</b>. It is not obligatory. ' , 'warning ' );
438- }
439416 }
440417 if ($ this ->button === 'logout ' ) {
441418 @session_destroy ();
@@ -531,7 +508,9 @@ public function genautoload($file, $namespaces, $namespacesAlt, $pathAbsolute, $
531508 * @noinspection PhpMissingParamTypeInspection
532509 * @noinspection ClassConstantCanBeUsedInspection
533510 */
534-
511+ /* -- CONFIG START HERE --
512+ -- CONFIG END HERE -- */
513+
535514/**
536515 * This class is used for autocomplete.
537516 * Class _AUTOLOAD_
@@ -712,12 +691,22 @@ function {{tempname}}__replaceCurlyVariable($string,$arrayName) {
712691 $ ok = @file_put_contents ($ file , $ template );
713692 if ($ ok ) {
714693 $ this ->addLog ("File <b> $ file</b> generated " , 'info ' );
694+
695+ $ ok = $ this ->saveParam ();
696+ if ($ ok === false ) {
697+ $ this ->addLog ('Unable to save configuration file <b> ' . $ file .
698+ '</b>. It is not obligatory. ' , 'warning ' );
699+ }
700+
715701 } else {
716702 $ this ->addLog ("Unable to write file <b> $ file</b>. Check the folder and permissions. You could write it manually. " ,
717703 'error ' );
718704 $ this ->statError ++;
719705 }
720706 $ this ->addLog (' ' );
707+
708+
709+
721710 }
722711
723712 return $ template ;
@@ -848,10 +837,6 @@ public function parsePHPFile($filename, &$runMe) {
848837 }
849838 $ nameSpace = '' ;
850839 $ className = '' ;
851- /*echo "<pre>";
852- var_dump($tokens);
853- echo "</pre>";
854- */
855840 foreach ($ tokens as $ p => $ token ) {
856841 if (is_array ($ token ) && $ token [0 ] == T_NAMESPACE ) {
857842 // We found a namespace
@@ -947,6 +932,10 @@ public function dirNameLinux($url, $ifFullUrl = true) {
947932 return $ dir ;
948933 }
949934
935+ /**
936+ * @param mixed $txt The message to show
937+ * @param string $type=['error','warning','info','success','stat','statinfo','staterror'][$i]
938+ */
950939 public function addLog ($ txt , $ type = '' ) {
951940 if (PHP_SAPI === 'cli ' ) {
952941 echo "\t" . $ txt . "\n" ;
@@ -1038,9 +1027,7 @@ public function process() {
10381027 //echo "running $f<br>";
10391028 $ f = $ this ->fixSeparator ($ f );
10401029 $ dirOriginal = $ this ->dirNameLinux ($ f );
1041-
10421030 $ jsonE =$ this ->parseJSONFile ($ dirOriginal );
1043- //var_dump($jsonE);
10441031 foreach ($ jsonE as $ item ) {
10451032 if (!$ jsonAbsolute [$ key ]) {
10461033 $ dir = $ this ->genPath ($ dirOriginal ); //folder/subfolder/f1
@@ -1051,8 +1038,6 @@ public function process() {
10511038 }
10521039 $ autoruns [] = $ full ;
10531040 $ autorunsFromJson []=$ full ;
1054- //var_dump($this->baseGen);
1055- //echo "<br>adding autorun $item , $dirOriginal , $dir , $full<br>";
10561041 }
10571042 }
10581043 foreach ($ files as $ key => $ f ) {
@@ -1069,12 +1054,8 @@ public function process() {
10691054 $ full = $ f ; //D:/Dropbox/www/currentproject/AutoLoadOne/examples/folder/NaturalClass.php
10701055 }
10711056 $ urlFull = $ this ->dirNameLinux ($ full ); ///folder/subfolder/f1
1072- $ tmpArr =explode ('\\ ' ,$ f ); //F1.php
1057+ $ tmpArr =explode ('/ ' ,$ f ); //F1.php
10731058 $ basefile =end ($ tmpArr ); // the config name shares the same name than the php but with extension .json
1074-
1075-
1076- // echo "$dir $full $urlFull $basefile<br>";
1077-
10781059 if ($ runMe != '' ) {
10791060 switch ($ runMe ) {
10801061 case '@autorun first ' :
0 commit comments