|
7 | 7 | use Exception; |
8 | 8 | use Dotenv\Dotenv; |
9 | 9 | use builder\Database\Constants; |
10 | | -use builder\Database\Traits\ServerTrait; |
| 10 | +use builder\Database\Capsule\Manager; |
11 | 11 | use builder\Database\Capsule\AppManager; |
| 12 | +use builder\Database\Traits\ServerTrait; |
12 | 13 | use builder\Database\Traits\ReusableTrait; |
13 | 14 |
|
14 | 15 | class OrmDotEnv extends Constants{ |
@@ -129,6 +130,86 @@ static public function createOrIgnore() |
129 | 130 | } |
130 | 131 | } |
131 | 132 |
|
| 133 | + /** |
| 134 | + * Turn off error reporting and log errors to a file |
| 135 | + * |
| 136 | + * @param string $logFile The name of the file to log errors to |
| 137 | + * |
| 138 | + * @return void |
| 139 | + */ |
| 140 | + static public function error_logger() |
| 141 | + { |
| 142 | + // Directory path |
| 143 | + $dir = self::$path . "storage/logs/"; |
| 144 | + |
| 145 | + // create custom file name |
| 146 | + $filename = "{$dir}orm.log"; |
| 147 | + |
| 148 | + // if \storage folder not found |
| 149 | + if(!is_dir(self::$path. "storage")){ |
| 150 | + @mkdir(self::$path. "storage", 0777); |
| 151 | + } |
| 152 | + |
| 153 | + // if \storage\logs\ folder not found |
| 154 | + if(!is_dir($dir)){ |
| 155 | + @mkdir($dir, 0777); |
| 156 | + } |
| 157 | + |
| 158 | + // If the log file doesn't exist, create it |
| 159 | + if(!file_exists($filename)) { |
| 160 | + touch($filename); |
| 161 | + chmod($filename, 0777); |
| 162 | + } |
| 163 | + |
| 164 | + // Determine the log message format |
| 165 | + $log_format = "[%s] %s in %s on line %d\n"; |
| 166 | + |
| 167 | + $append = true; |
| 168 | + $max_size = 1024*1024; |
| 169 | + |
| 170 | + // Define the error level mapping |
| 171 | + $error_levels = array( |
| 172 | + E_ERROR => 'Fatal Error', |
| 173 | + E_USER_ERROR => 'User Error', |
| 174 | + E_PARSE => 'Parse Error', |
| 175 | + E_WARNING => 'Warning', |
| 176 | + E_USER_WARNING => 'User Warning', |
| 177 | + E_NOTICE => 'Notice', |
| 178 | + E_USER_NOTICE => 'User Notice', |
| 179 | + E_STRICT => 'Strict Standards', |
| 180 | + E_DEPRECATED => 'Deprecated', |
| 181 | + E_USER_DEPRECATED => 'User Deprecated', |
| 182 | + ); |
| 183 | + |
| 184 | + // if true |
| 185 | + if(self::is_production()){ |
| 186 | + // Define the error handler function |
| 187 | + $error_handler = function($errno, $errstr, $errfile, $errline) use ($filename, $append, $max_size, $log_format, $error_levels) { |
| 188 | + // Construct the log message |
| 189 | + $error_level = isset($error_levels[$errno]) ? $error_levels[$errno] : 'Unknown Error'; |
| 190 | + $log_message = sprintf($log_format, date('Y-m-d H:i:s'), $error_level . ': ' . $errstr, $errfile, $errline); |
| 191 | + |
| 192 | + // Write the log message to the file |
| 193 | + if ($append && file_exists($filename)) { |
| 194 | + $current_size = filesize($filename); |
| 195 | + if ($current_size > $max_size) { |
| 196 | + file_put_contents($filename, "{$log_message}"); |
| 197 | + } else { |
| 198 | + file_put_contents($filename, "{$log_message}", FILE_APPEND); |
| 199 | + } |
| 200 | + } else { |
| 201 | + file_put_contents($filename, $log_message); |
| 202 | + } |
| 203 | + |
| 204 | + // Let PHP handle the error in the normal way |
| 205 | + return false; |
| 206 | + }; |
| 207 | + |
| 208 | + // Set the error handler function |
| 209 | + set_error_handler($error_handler); |
| 210 | + } |
| 211 | + } |
| 212 | + |
132 | 213 | /** |
133 | 214 | * Update Environment path .env file |
134 | 215 | * @param string $key \Environment key you want to update |
@@ -185,6 +266,20 @@ static public function updateENV(?string $key = null, string|bool $value = null, |
185 | 266 | return false; |
186 | 267 | } |
187 | 268 |
|
| 269 | + /** |
| 270 | + * Determines if the application is running in production environment. |
| 271 | + * |
| 272 | + * @return bool Returns true if the application is running in production environment, false otherwise. |
| 273 | + */ |
| 274 | + static private function is_production() { |
| 275 | + // check using default setting |
| 276 | + if(Manager::setEnvBool(app_config('APP_DEBUG')) && app_config('APP_ENV') == 'local'){ |
| 277 | + return true; |
| 278 | + } |
| 279 | + |
| 280 | + return !empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' && $_SERVER['SERVER_ADDR'] !== '127.0.0.1'; |
| 281 | + } |
| 282 | + |
188 | 283 | /** |
189 | 284 | * Check if environment key is set |
190 | 285 | * @param string $key |
|
0 commit comments