1919
2020namespace  phpMyFAQ ;
2121
22+ use  Monolog \Handler \StreamHandler ;
23+ use  Monolog \Logger ;
2224use  Symfony \Component \Dotenv \Dotenv ;
25+ use  Symfony \Component \ErrorHandler \Debug ;
26+ use  Symfony \Component \ErrorHandler \ErrorHandler ;
2327
2428class  Environment
2529{
2630    private  static  bool  $ debugMode  = false ;
2731
28-     private  static  int  $ debugLevel  = 0 ;
29- 
3032    private  static  bool  $ debugLogQueries  = false ;
3133
3234    private  static  bool  $ initialized  = false ;
@@ -56,20 +58,9 @@ public static function enableTestMode(): void
5658        self ::$ testMode  = true ;
5759    }
5860
59-     public  static  function  reset (): void 
60-     {
61-         self ::$ debugMode  = false ;
62-         self ::$ debugLevel  = 0 ;
63-         self ::$ debugLogQueries  = false ;
64-         self ::$ initialized  = false ;
65-         self ::$ environment  = 'production ' ;
66-         self ::$ testMode  = false ;
67-         self ::$ dotenv  = null ;
68-     }
69- 
7061    private  static  function  loadEnvironment (): void 
7162    {
72-         $ envPath  = dirname (__DIR__ , 2 );
63+         $ envPath  = dirname (__DIR__ , levels:  2 );
7364        $ envFile  = $ envPath  . '/.env ' ;
7465
7566        if  (file_exists ($ envFile )) {
@@ -80,37 +71,43 @@ private static function loadEnvironment(): void
8071
8172    private  static  function  setupDebugMode (): void 
8273    {
74+         if  (self ::$ testMode ) {
75+             return ;
76+         }
77+ 
8378        self ::$ debugMode  = filter_var ($ _ENV  ['DEBUG ' ] ?? false , FILTER_VALIDATE_BOOLEAN );
84-         self ::$ debugLevel  = (int ) ($ _ENV  ['DEBUG_LEVEL ' ] ?? 0 );
8579        self ::$ debugLogQueries  = filter_var ($ _ENV  ['DEBUG_LOG_QUERIES ' ] ?? false , FILTER_VALIDATE_BOOLEAN );
8680        self ::$ environment  = $ _ENV  ['APP_ENV ' ] ?? 'production ' ;
8781
88-         // Legacy support 
89-         if  (!defined ('DEBUG ' )) {
90-             define ('DEBUG ' , self ::$ debugMode );
91-         }
82+         error_reporting (self ::$ debugMode  ? E_ALL  : E_ERROR  | E_WARNING  | E_PARSE );
9283
93-         // Error reporting based on debug mode 
9484        if  (self ::$ debugMode ) {
95-             ini_set ('display_errors ' , '1 ' );
96-             ini_set ('display_startup_errors ' , '1 ' );
97-             error_reporting (E_ALL );
98-         } else  {
99-             ini_set ('display_errors ' , '0 ' );
100-             error_reporting (E_ERROR  | E_WARNING  | E_PARSE );
85+             Debug::enable ();
86+             ErrorHandler::register ();
87+             return ;
10188        }
89+ 
90+         $ handler  = ErrorHandler::register ();
91+ 
92+         $ logger  = new  Logger (name: 'phpmyfaq ' );
93+         $ logTarget  = $ _ENV  ['ERROR_LOG ' ] ?? 'php://stderr ' ;
94+         $ logger ->pushHandler (new  StreamHandler ($ logTarget , Logger::WARNING ));
95+ 
96+         $ levelsMap  = [
97+             E_DEPRECATED  => null ,
98+             E_USER_DEPRECATED  => null ,
99+         ];
100+         $ handler ->setDefaultLogger ($ logger , $ levelsMap );
101+ 
102+         $ handler ->screamAt (E_ALL  & ~E_DEPRECATED  & ~E_USER_DEPRECATED );
103+         $ handler ->scopeAt (E_ALL  & ~E_DEPRECATED  & ~E_USER_DEPRECATED );
102104    }
103105
104106    public  static  function  isDebugMode (): bool 
105107    {
106108        return  self ::$ debugMode ;
107109    }
108110
109-     public  static  function  getDebugLevel (): int 
110-     {
111-         return  self ::$ debugLevel ;
112-     }
113- 
114111    public  static  function  shouldLogQueries (): bool 
115112    {
116113        return  self ::$ debugLogQueries ;
0 commit comments