2
2
3
3
use Illuminate \Contracts \Container \Container ;
4
4
use AgelxNash \Modx \Evo \Database \Exceptions \ConnectException ;
5
+ use Symfony \Component \Console \Exception \CommandNotFoundException ;
6
+ use Symfony \Component \Console \Exception \InvalidArgumentException ;
7
+ use Symfony \Component \Console \Exception \InvalidOptionException ;
8
+ use Symfony \Component \Console \Exception \RuntimeException ;
5
9
use Symfony \Component \ErrorHandler \Error \FatalError ;
6
10
use Symfony \Component \ErrorHandler \Error \FatalError as FatalErrorException ;
7
11
use EvolutionCMS \Providers \TracyServiceProvider ;
@@ -22,16 +26,12 @@ public function __construct(Container $container)
22
26
$ this ->container = $ container ;
23
27
$ this ->container ->register (TracyServiceProvider::class);
24
28
if (!$ this ->container ['config ' ]->get ('tracy.active ' )) {
25
- $ this ->registerHanlders ();
29
+ $ this ->registerHandlers ();
26
30
}
27
31
}
28
32
29
- protected function registerHanlders ()
33
+ protected function registerHandlers ()
30
34
{
31
- if (!defined ('MODX_CLI ' ) || MODX_CLI ) {
32
- return ;
33
- }
34
-
35
35
register_shutdown_function ([$ this , 'handleShutdown ' ]);
36
36
set_exception_handler ([$ this , 'handleException ' ]);
37
37
set_error_handler ([$ this , 'phpError ' ]);
@@ -175,8 +175,6 @@ public function messageQuit(
175
175
176
176
$ table = array ();
177
177
178
- $ version = isset ($ GLOBALS ['modx_version ' ]) ? $ GLOBALS ['modx_version ' ] : '' ;
179
- $ release_date = isset ($ GLOBALS ['release_date ' ]) ? $ GLOBALS ['release_date ' ] : '' ;
180
178
if (isset ($ _SERVER ['HTTP_HOST ' ])) {
181
179
$ request_uri = "http:// " . $ _SERVER ['HTTP_HOST ' ] . ($ _SERVER ["SERVER_PORT " ] == 80 ? "" : (": " . $ _SERVER ["SERVER_PORT " ])) . $ _SERVER ['REQUEST_URI ' ];
182
180
$ request_uri = $ this ->container ->getPhpCompat ()->htmlspecialchars ($ request_uri , ENT_QUOTES ,
@@ -317,7 +315,13 @@ public function messageQuit(
317
315
if (!empty ($ php_errormsg ) && isset ($ php_errormsg ['message ' ])) {
318
316
$ str = '<b> ' . $ php_errormsg ['message ' ] . '</b><br /> ' . PHP_EOL . $ str ;
319
317
}
320
- $ str .= $ this ->getBacktrace (empty ($ backtrace ) ? debug_backtrace () : $ backtrace );
318
+
319
+ if (empty ($ backtrace )) {
320
+ $ backtrace = debug_backtrace ();
321
+ }
322
+ $ backtrace = $ this ->prepareBacktrace ($ backtrace );
323
+ $ str .= $ this ->getBacktrace ($ backtrace );
324
+
321
325
// Log error
322
326
if (!empty ($ this ->container ->currentSnippet )) {
323
327
$ source = 'Snippet - ' . $ this ->container ->currentSnippet ;
@@ -367,7 +371,47 @@ public function messageQuit(
367
371
}
368
372
369
373
// Display error
370
- if ($ this ->shouldDisplay ()) {
374
+ if (is_cli ()) {
375
+ echo $ msg , "\n\n" ;
376
+
377
+ if (!empty ($ query )) {
378
+ echo 'SQL: ' , $ query , "\n" ;
379
+ }
380
+
381
+ if (!empty ($ nr ) || !empty ($ file )) {
382
+ if ($ text != '' ) {
383
+ echo 'Error: ' , $ text , "\n" ;
384
+ }
385
+ if ($ output != '' ) {
386
+ echo $ output , "\n" ;
387
+ }
388
+ if ($ nr !== '' ) {
389
+ echo 'ErrorType[num]: ' , $ errortype [$ nr ] . "[ $ nr] " , "\n" ;
390
+ }
391
+ if ($ file ) {
392
+ echo 'File: ' , $ file , "\n" ;
393
+ }
394
+ if ($ line ) {
395
+ echo 'Line: ' , $ line , "\n" ;
396
+ }
397
+ }
398
+
399
+ if ($ source != '' ) {
400
+ echo 'Source: ' , $ source , "\n" ;
401
+ }
402
+
403
+ if (!empty ($ this ->currentSnippet )) {
404
+ echo 'Current Snippet: ' , $ this ->currentSnippet , "\n" ;
405
+ }
406
+
407
+ if (!empty ($ this ->event ->activePlugin )) {
408
+ echo 'Current Plugin: ' , $ this ->event ->activePlugin . '( ' . $ this ->event ->name . ') ' , "\n" ;
409
+ }
410
+
411
+ echo "\n" , $ this ->getConsoleBacktrace ($ backtrace );
412
+ } else if ($ this ->shouldDisplay ()) {
413
+ $ version = isset ($ GLOBALS ['modx_version ' ]) ? $ GLOBALS ['modx_version ' ] : '' ;
414
+ $ release_date = isset ($ GLOBALS ['release_date ' ]) ? $ GLOBALS ['release_date ' ] : '' ;
371
415
372
416
echo '<!DOCTYPE html><html><head><title>Evolution CMS Content Manager ' . $ version . ' » ' . $ release_date . '</title>
373
417
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
@@ -376,7 +420,6 @@ public function messageQuit(
376
420
<style type="text/css">body { padding:10px; } td {font:inherit;}</style>
377
421
</head><body>
378
422
' . $ str . '</body></html> ' ;
379
-
380
423
} else {
381
424
if (file_exists (EVO_CORE_PATH . 'custom/error_page.html ' )) {
382
425
echo file_get_contents (EVO_CORE_PATH . 'custom/error_page.html ' );
@@ -394,18 +437,11 @@ protected function shouldDisplay()
394
437
return isset ($ _SESSION ['mgrValidated ' ]) || $ this ->container ['config ' ]->get ('app.debug ' );
395
438
}
396
439
397
- /**
398
- * @param $backtrace
399
- * @return string
400
- */
401
- public function getBacktrace ($ backtrace )
440
+ protected function prepareBacktrace ($ backtrace )
402
441
{
403
- $ MakeTable = $ this ->container ->getService ('makeTable ' );
404
- $ MakeTable ->setTableClass ('grid ' );
405
- $ MakeTable ->setRowRegularClass ('gridItem ' );
406
- $ MakeTable ->setRowAlternateClass ('gridAltItem ' );
407
- $ table = array ();
442
+ $ result = [];
408
443
$ backtrace = array_reverse ($ backtrace );
444
+
409
445
foreach ($ backtrace as $ key => $ val ) {
410
446
$ key ++;
411
447
if (substr ($ val ['function ' ], 0 , 11 ) === 'messageQuit ' ) {
@@ -475,16 +511,55 @@ public function getBacktrace($backtrace)
475
511
476
512
return $ out ;
477
513
}, $ args );
478
- $ line = array (
479
- "<strong> " . $ functionName . "</strong>( " . $ args . ") " ,
480
- $ path . " on line " . $ val ['line ' ]
481
- );
482
- $ table [] = array (implode ("<br /> " , $ line ));
514
+
515
+ $ result [] = [
516
+ 'func ' => $ functionName ,
517
+ 'args ' => $ args ,
518
+ 'path ' => $ path ,
519
+ 'line ' => $ val ['line ' ],
520
+ ];
521
+ }
522
+
523
+ return $ result ;
524
+ }
525
+
526
+ /**
527
+ * @param $backtrace
528
+ * @return string
529
+ */
530
+ public function getBacktrace ($ backtrace )
531
+ {
532
+ $ MakeTable = $ this ->container ->getService ('makeTable ' );
533
+ $ MakeTable ->setTableClass ('grid ' );
534
+ $ MakeTable ->setRowRegularClass ('gridItem ' );
535
+ $ MakeTable ->setRowAlternateClass ('gridAltItem ' );
536
+ $ table = array ();
537
+
538
+ foreach ($ backtrace as $ line ) {
539
+ $ table [] = array (implode ("<br /> " , [
540
+ "<strong> " . $ line ['func ' ] . "</strong>( " . $ line ['args ' ] . ") " ,
541
+ $ line ['path ' ] . " on line " . $ line ['line ' ],
542
+ ]));
483
543
}
484
544
485
545
return $ MakeTable ->create ($ table , array ('Backtrace ' ));
486
546
}
487
547
548
+ /**
549
+ * @param $backtrace
550
+ * @return string
551
+ */
552
+ public function getConsoleBacktrace ($ backtrace )
553
+ {
554
+ $ result = '' ;
555
+
556
+ foreach ($ backtrace as $ i => $ line ) {
557
+ $ result .= '# ' . ($ i + 1 ) . '. ' . $ line ['func ' ] . '( ' . $ line ['args ' ] . '), ' . $ line ['path ' ] . ' on line ' . $ line ['line ' ] . "\n" ;
558
+ }
559
+
560
+ return $ result ;
561
+ }
562
+
488
563
/**
489
564
* Determine if the exception should be reported.
490
565
*
@@ -502,14 +577,23 @@ public function shouldReport(\Throwable $exception)
502
577
*/
503
578
public function handleException (\Throwable $ exception )
504
579
{
505
-
506
580
if (
507
581
$ exception instanceof ConnectException ||
508
582
($ exception instanceof \PDOException && $ exception ->getCode () === 1045 )
509
583
) {
510
584
$ this ->container ->getDatabase ()->disconnect ();
511
585
}
512
586
587
+ if (is_cli () && (
588
+ $ exception instanceof RuntimeException ||
589
+ $ exception instanceof InvalidArgumentException ||
590
+ $ exception instanceof InvalidOptionException ||
591
+ $ exception instanceof CommandNotFoundException
592
+ )) {
593
+ echo $ exception ->getMessage ();
594
+ exit ;
595
+ }
596
+
513
597
$ this ->messageQuit (
514
598
$ exception ->getMessage (),
515
599
'' ,
0 commit comments