13
13
14
14
//*************************************************************
15
15
use Exception ;
16
- use RuntimeException ;
16
+
17
17
18
18
if (!defined ('_AUTOLOAD_USER ' )) {
19
19
define ('_AUTOLOAD_USER ' , 'autoloadone ' );
45
45
*
46
46
* @copyright Jorge Castro C. MIT License https://github.com/EFTEC/AutoLoadOne
47
47
*
48
- * @version 1.21 2021-02-15
48
+ * @version 1.22 2021-02-26
49
49
* @noautoload
50
50
*/
51
51
class AutoLoadOne
52
52
{
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
+
57
55
58
56
public $ rooturl = '' ;
59
57
public $ fileGen = '' ;
@@ -93,10 +91,9 @@ public function __construct() {
93
91
$ this ->fileGen = '. ' ; //getcwd(); // dirname($_SERVER['SCRIPT_FILENAME']);
94
92
$ this ->rooturl = '. ' ; //getcwd(); // dirname($_SERVER['SCRIPT_FILENAME']);
95
93
$ this ->t1 = microtime (true );
96
- $ tmpArr =explode ('\\ ' ,$ _SERVER ['SCRIPT_FILENAME ' ]);
94
+ $ tmpArr =explode ('/ ' ,$ _SERVER ['SCRIPT_FILENAME ' ]); // it always returns with linux separators.
97
95
$ 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 );
100
97
}
101
98
102
99
private function getAllParametersCli () {
@@ -194,57 +191,8 @@ private function initSapi() {
194
191
echo "------------------------------------------------------------------ \n" ;
195
192
}
196
193
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
- }
212
194
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
- }
228
195
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
- }
248
196
249
197
public static function format ($ json , $ unescapeUnicode , $ unescapeSlashes ) {
250
198
$ result = '' ;
@@ -341,28 +289,35 @@ private function saveParam() {
341
289
return false ;
342
290
}
343
291
$ 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 ;
346
296
$ param ['savefile ' ] = $ this ->savefile ;
347
297
$ param ['compression ' ] = $ this ->compression ;
348
298
$ param ['savefileName ' ] = $ this ->savefileName ;
349
299
$ param ['excludeNS ' ] = $ this ->excludeNS ;
350
300
$ param ['excludePath ' ] = $ this ->excludePath ;
351
301
$ 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 );
366
321
}
367
322
368
323
/**
@@ -372,19 +327,43 @@ private function loadParam() {
372
327
if (!_AUTOLOAD_SAVEPARAM ) {
373
328
return false ;
374
329
}
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 ) {
377
357
return false ;
378
358
}
379
- $ param = json_decode ($ txt , true );
380
- $ this ->fileGen = @$ param ['local ' ]['fileGen ' ];
359
+ $ this ->fileGen = @$ param ['fileGen ' ];
381
360
$ 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 ' ];
388
367
return true ;
389
368
}
390
369
@@ -410,8 +389,11 @@ private function initWeb() {
410
389
if (!$ this ->button ) {
411
390
$ loadOk = $ this ->loadParam ();
412
391
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 .
414
393
'</b>. It is not obligatory ' , 'warning ' );
394
+ } else {
395
+ $ this ->addLog ('Configuration loaded <b> ' . $ this ->savefileName .
396
+ '</b>. ' , 'info ' );
415
397
}
416
398
} else {
417
399
$ this ->debugMode = isset ($ _GET ['debug ' ]);
@@ -431,11 +413,6 @@ private function initWeb() {
431
413
$ this ->savefileName = (@$ _POST ['savefileName ' ]) ?: $ this ->savefileName ;
432
414
$ this ->stop = @$ _POST ['stop ' ];
433
415
$ 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
- }
439
416
}
440
417
if ($ this ->button === 'logout ' ) {
441
418
@session_destroy ();
@@ -531,7 +508,9 @@ public function genautoload($file, $namespaces, $namespacesAlt, $pathAbsolute, $
531
508
* @noinspection PhpMissingParamTypeInspection
532
509
* @noinspection ClassConstantCanBeUsedInspection
533
510
*/
534
-
511
+ /* -- CONFIG START HERE --
512
+ -- CONFIG END HERE -- */
513
+
535
514
/**
536
515
* This class is used for autocomplete.
537
516
* Class _AUTOLOAD_
@@ -712,12 +691,22 @@ function {{tempname}}__replaceCurlyVariable($string,$arrayName) {
712
691
$ ok = @file_put_contents ($ file , $ template );
713
692
if ($ ok ) {
714
693
$ 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
+
715
701
} else {
716
702
$ this ->addLog ("Unable to write file <b> $ file</b>. Check the folder and permissions. You could write it manually. " ,
717
703
'error ' );
718
704
$ this ->statError ++;
719
705
}
720
706
$ this ->addLog (' ' );
707
+
708
+
709
+
721
710
}
722
711
723
712
return $ template ;
@@ -848,10 +837,6 @@ public function parsePHPFile($filename, &$runMe) {
848
837
}
849
838
$ nameSpace = '' ;
850
839
$ className = '' ;
851
- /*echo "<pre>";
852
- var_dump($tokens);
853
- echo "</pre>";
854
- */
855
840
foreach ($ tokens as $ p => $ token ) {
856
841
if (is_array ($ token ) && $ token [0 ] == T_NAMESPACE ) {
857
842
// We found a namespace
@@ -947,6 +932,10 @@ public function dirNameLinux($url, $ifFullUrl = true) {
947
932
return $ dir ;
948
933
}
949
934
935
+ /**
936
+ * @param mixed $txt The message to show
937
+ * @param string $type=['error','warning','info','success','stat','statinfo','staterror'][$i]
938
+ */
950
939
public function addLog ($ txt , $ type = '' ) {
951
940
if (PHP_SAPI === 'cli ' ) {
952
941
echo "\t" . $ txt . "\n" ;
@@ -1038,9 +1027,7 @@ public function process() {
1038
1027
//echo "running $f<br>";
1039
1028
$ f = $ this ->fixSeparator ($ f );
1040
1029
$ dirOriginal = $ this ->dirNameLinux ($ f );
1041
-
1042
1030
$ jsonE =$ this ->parseJSONFile ($ dirOriginal );
1043
- //var_dump($jsonE);
1044
1031
foreach ($ jsonE as $ item ) {
1045
1032
if (!$ jsonAbsolute [$ key ]) {
1046
1033
$ dir = $ this ->genPath ($ dirOriginal ); //folder/subfolder/f1
@@ -1051,8 +1038,6 @@ public function process() {
1051
1038
}
1052
1039
$ autoruns [] = $ full ;
1053
1040
$ autorunsFromJson []=$ full ;
1054
- //var_dump($this->baseGen);
1055
- //echo "<br>adding autorun $item , $dirOriginal , $dir , $full<br>";
1056
1041
}
1057
1042
}
1058
1043
foreach ($ files as $ key => $ f ) {
@@ -1069,12 +1054,8 @@ public function process() {
1069
1054
$ full = $ f ; //D:/Dropbox/www/currentproject/AutoLoadOne/examples/folder/NaturalClass.php
1070
1055
}
1071
1056
$ urlFull = $ this ->dirNameLinux ($ full ); ///folder/subfolder/f1
1072
- $ tmpArr =explode ('\\ ' ,$ f ); //F1.php
1057
+ $ tmpArr =explode ('/ ' ,$ f ); //F1.php
1073
1058
$ 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
-
1078
1059
if ($ runMe != '' ) {
1079
1060
switch ($ runMe ) {
1080
1061
case '@autorun first ' :
0 commit comments