Skip to content

Commit

Permalink
Helpers, Migration and Dummy Update
Browse files Browse the repository at this point in the history
  • Loading branch information
Fredrick Peter committed May 7, 2023
1 parent e5afeca commit 384ba9c
Show file tree
Hide file tree
Showing 8 changed files with 1,413 additions and 9 deletions.
1 change: 0 additions & 1 deletion src/Capsule/AppManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace builder\Database\Capsule;

use Exception;
use builder\Database\Constants;


Expand Down
2 changes: 0 additions & 2 deletions src/Capsule/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
namespace builder\Database\Capsule;

use Exception;
use builder\Database\DB;
use builder\Database\Constants;
use builder\Database\Query\Builder;


class Manager extends Constants{
Expand Down
18 changes: 17 additions & 1 deletion src/Dummy/dummyInit.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,20 @@
$db->configurePagination([
'allow' => false,
'view' => 'simple',
]);
]);


/*
|--------------------------------------------------------------------------
| ERROR HANDLER WHEN APP_DEBUG is set to FALSE
| Disable error on server and log to a file
| path => \storage\logs\orm.log
|
| To enable error logs ?
| Set the below information, As the custom logger only works on production environment
| APP_ENV=local
| APP_DEBUG = true
|
|--------------------------------------------------------------------------
*/
$dotEnv['self']::error_logger();
1 change: 0 additions & 1 deletion src/Pagination/Traits/PaginateTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace builder\Database\Pagination\Traits;

use Throwable;
use yidas\data\Pagination;
use builder\Database\Capsule\Manager;
use builder\Database\Pagination\OrmPagination;
Expand Down
1 change: 0 additions & 1 deletion src/Schema/Insertion.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace builder\Database\Schema;

use stdClass;
use PDOException;
use builder\Database\Query\Builder;
use builder\Database\Traits\InsertionTrait;
Expand Down
97 changes: 96 additions & 1 deletion src/Schema/OrmDotEnv.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
use Exception;
use Dotenv\Dotenv;
use builder\Database\Constants;
use builder\Database\Traits\ServerTrait;
use builder\Database\Capsule\Manager;
use builder\Database\Capsule\AppManager;
use builder\Database\Traits\ServerTrait;
use builder\Database\Traits\ReusableTrait;

class OrmDotEnv extends Constants{
Expand Down Expand Up @@ -129,6 +130,86 @@ static public function createOrIgnore()
}
}

/**
* Turn off error reporting and log errors to a file
*
* @param string $logFile The name of the file to log errors to
*
* @return void
*/
static public function error_logger()
{
// Directory path
$dir = self::$path . "storage/logs/";

// create custom file name
$filename = "{$dir}orm.log";

// if \storage folder not found
if(!is_dir(self::$path. "storage")){
@mkdir(self::$path. "storage", 0777);
}

// if \storage\logs\ folder not found
if(!is_dir($dir)){
@mkdir($dir, 0777);
}

// If the log file doesn't exist, create it
if(!file_exists($filename)) {
touch($filename);
chmod($filename, 0777);
}

// Determine the log message format
$log_format = "[%s] %s in %s on line %d\n";

$append = true;
$max_size = 1024*1024;

// Define the error level mapping
$error_levels = array(
E_ERROR => 'Fatal Error',
E_USER_ERROR => 'User Error',
E_PARSE => 'Parse Error',
E_WARNING => 'Warning',
E_USER_WARNING => 'User Warning',
E_NOTICE => 'Notice',
E_USER_NOTICE => 'User Notice',
E_STRICT => 'Strict Standards',
E_DEPRECATED => 'Deprecated',
E_USER_DEPRECATED => 'User Deprecated',
);

// if true
if(self::is_production()){
// Define the error handler function
$error_handler = function($errno, $errstr, $errfile, $errline) use ($filename, $append, $max_size, $log_format, $error_levels) {
// Construct the log message
$error_level = isset($error_levels[$errno]) ? $error_levels[$errno] : 'Unknown Error';
$log_message = sprintf($log_format, date('Y-m-d H:i:s'), $error_level . ': ' . $errstr, $errfile, $errline);

// Write the log message to the file
if ($append && file_exists($filename)) {
$current_size = filesize($filename);
if ($current_size > $max_size) {
file_put_contents($filename, "{$log_message}");
} else {
file_put_contents($filename, "{$log_message}", FILE_APPEND);
}
} else {
file_put_contents($filename, $log_message);
}

// Let PHP handle the error in the normal way
return false;
};

// Set the error handler function
set_error_handler($error_handler);
}
}

/**
* Update Environment path .env file
* @param string $key \Environment key you want to update
Expand Down Expand Up @@ -185,6 +266,20 @@ static public function updateENV(?string $key = null, string|bool $value = null,
return false;
}

/**
* Determines if the application is running in production environment.
*
* @return bool Returns true if the application is running in production environment, false otherwise.
*/
static private function is_production() {
// check using default setting
if(Manager::setEnvBool(app_config('APP_DEBUG')) && app_config('APP_ENV') == 'local'){
return true;
}

return !empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' && $_SERVER['SERVER_ADDR'] !== '127.0.0.1';
}

/**
* Check if environment key is set
* @param string $key
Expand Down
14 changes: 12 additions & 2 deletions src/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,12 +161,22 @@ function base_dir()
if (! function_exists('app_config')) {
/**
* Get App Configuration
* @param string $key
*
* @return mixed
*/
function app_config()
function app_config(?string $key = null)
{
return db_exec()->AppConfig();
// get Config data from ENV file
$AppConfig = db_exec()->AppConfig();

// Convert all keys to lowercase
$AppConfig = array_change_key_case($AppConfig, CASE_UPPER);

// convert to upper-case
$key = strtoupper(trim((string) $key));

return $AppConfig[$key] ?? $AppConfig;
}
}

Expand Down
Loading

0 comments on commit 384ba9c

Please sign in to comment.