Skip to content

Commit 384ba9c

Browse files
author
Fredrick Peter
committed
Helpers, Migration and Dummy Update
1 parent e5afeca commit 384ba9c

File tree

8 files changed

+1413
-9
lines changed

8 files changed

+1413
-9
lines changed

src/Capsule/AppManager.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
namespace builder\Database\Capsule;
66

7-
use Exception;
87
use builder\Database\Constants;
98

109

src/Capsule/Manager.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@
55
namespace builder\Database\Capsule;
66

77
use Exception;
8-
use builder\Database\DB;
98
use builder\Database\Constants;
10-
use builder\Database\Query\Builder;
119

1210

1311
class Manager extends Constants{

src/Dummy/dummyInit.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,20 @@
7979
$db->configurePagination([
8080
'allow' => false,
8181
'view' => 'simple',
82-
]);
82+
]);
83+
84+
85+
/*
86+
|--------------------------------------------------------------------------
87+
| ERROR HANDLER WHEN APP_DEBUG is set to FALSE
88+
| Disable error on server and log to a file
89+
| path => \storage\logs\orm.log
90+
|
91+
| To enable error logs ?
92+
| Set the below information, As the custom logger only works on production environment
93+
| APP_ENV=local
94+
| APP_DEBUG = true
95+
|
96+
|--------------------------------------------------------------------------
97+
*/
98+
$dotEnv['self']::error_logger();

src/Pagination/Traits/PaginateTrait.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
namespace builder\Database\Pagination\Traits;
66

7-
use Throwable;
87
use yidas\data\Pagination;
98
use builder\Database\Capsule\Manager;
109
use builder\Database\Pagination\OrmPagination;

src/Schema/Insertion.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
namespace builder\Database\Schema;
66

7-
use stdClass;
87
use PDOException;
98
use builder\Database\Query\Builder;
109
use builder\Database\Traits\InsertionTrait;

src/Schema/OrmDotEnv.php

Lines changed: 96 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@
77
use Exception;
88
use Dotenv\Dotenv;
99
use builder\Database\Constants;
10-
use builder\Database\Traits\ServerTrait;
10+
use builder\Database\Capsule\Manager;
1111
use builder\Database\Capsule\AppManager;
12+
use builder\Database\Traits\ServerTrait;
1213
use builder\Database\Traits\ReusableTrait;
1314

1415
class OrmDotEnv extends Constants{
@@ -129,6 +130,86 @@ static public function createOrIgnore()
129130
}
130131
}
131132

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+
132213
/**
133214
* Update Environment path .env file
134215
* @param string $key \Environment key you want to update
@@ -185,6 +266,20 @@ static public function updateENV(?string $key = null, string|bool $value = null,
185266
return false;
186267
}
187268

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+
188283
/**
189284
* Check if environment key is set
190285
* @param string $key

src/helpers.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,12 +161,22 @@ function base_dir()
161161
if (! function_exists('app_config')) {
162162
/**
163163
* Get App Configuration
164+
* @param string $key
164165
*
165166
* @return mixed
166167
*/
167-
function app_config()
168+
function app_config(?string $key = null)
168169
{
169-
return db_exec()->AppConfig();
170+
// get Config data from ENV file
171+
$AppConfig = db_exec()->AppConfig();
172+
173+
// Convert all keys to lowercase
174+
$AppConfig = array_change_key_case($AppConfig, CASE_UPPER);
175+
176+
// convert to upper-case
177+
$key = strtoupper(trim((string) $key));
178+
179+
return $AppConfig[$key] ?? $AppConfig;
170180
}
171181
}
172182

0 commit comments

Comments
 (0)