Skip to content

Commit

Permalink
better error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
RichardTMiles committed Jul 19, 2024
1 parent 9a6c0c6 commit a84185b
Show file tree
Hide file tree
Showing 6 changed files with 260 additions and 207 deletions.
130 changes: 69 additions & 61 deletions carbonphp/CarbonPHP.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use CarbonPHP\Abstracts\ColorCode;
use CarbonPHP\Abstracts\Files;
use CarbonPHP\Abstracts\Htaccess;
use CarbonPHP\Abstracts\Serialized;
use CarbonPHP\Enums\ThrowableReportDisplay;
use CarbonPHP\Error\PrivateAlert;
Expand Down Expand Up @@ -40,7 +39,7 @@ class CarbonPHP
{

// folder locations
public const CARBON_ROOT = __DIR__ . DIRECTORY_SEPARATOR;
public const string CARBON_ROOT = __DIR__ . DIRECTORY_SEPARATOR;
public static bool $carbon_is_root = false;
public static string $public_carbon_root = '/'; // uri
public static string $app_root;
Expand All @@ -57,74 +56,74 @@ class CarbonPHP
public static bool $setupComplete = false;

// CarbonPHP Configuration Keys
public const DATABASE = 'DATABASE';
public const DB_HOST = 'DB_HOST';
public const DB_READER = 'DB_READER';
public const DB_PORT = 'DB_PORT';
public const DB_NAME = 'DB_NAME';
public const DB_USER = 'DB_USER';
public const DB_PASS = 'DB_PASS';
public const REBUILD = 'REBUILD';
public const REBUILD_WITH_CARBON_TABLES = 'REBUILD_WITH_CARBON_TABLES';
public const string DATABASE = 'DATABASE';
public const string DB_HOST = 'DB_HOST';
public const string DB_READER = 'DB_READER';
public const string DB_PORT = 'DB_PORT';
public const string DB_NAME = 'DB_NAME';
public const string DB_USER = 'DB_USER';
public const string DB_PASS = 'DB_PASS';
public const string REBUILD = 'REBUILD';
public const string REBUILD_WITH_CARBON_TABLES = 'REBUILD_WITH_CARBON_TABLES';

// todo - transfer all cli options to the config
public const REST = 'REST';
public const NAMESPACE = 'NAMESPACE';
public const TABLE_PREFIX = 'TABLE_PREFIX';
public const VALIDATE_EXTERNAL_REQUESTS_GENERATED_SQL = 'VALIDATE_EXTERNAL_REQUESTS_GENERATED_SQL';
public const string REST = 'REST';
public const string NAMESPACE = 'NAMESPACE';
public const string TABLE_PREFIX = 'TABLE_PREFIX';
public const string VALIDATE_EXTERNAL_REQUESTS_GENERATED_SQL = 'VALIDATE_EXTERNAL_REQUESTS_GENERATED_SQL';

// Site Config
public const SITE = 'SITE';
public const QUERY_WITH_DATABASE_NAME = 'QUERY_WITH_DATABASE_NAME';
public const URL = 'URL';
public const ROOT = 'ROOT';
public const CACHE_CONTROL = 'CACHE_CONTROL';
public const CONFIG = 'CONFIG';
public const TIMEZONE = 'TIMEZONE';
public const TITLE = 'TITLE';
public const VERSION = 'VERSION';
public const SEND_EMAIL = 'SEND_EMAIL';
public const REPLY_EMAIL = 'REPLY_EMAIL';
public const HTTP = 'HTTP';
public const IP_TEST = 'IP_TEST';
public const PROGRAMS = 'PROGRAMS';
public const PROGRAM_DIRECTORIES = 'PROGRAM_DIRECTORIES';
public const string SITE = 'SITE';
public const string QUERY_WITH_DATABASE_NAME = 'QUERY_WITH_DATABASE_NAME';
public const string URL = 'URL';
public const string ROOT = 'ROOT';
public const string CACHE_CONTROL = 'CACHE_CONTROL';
public const string CONFIG = 'CONFIG';
public const string TIMEZONE = 'TIMEZONE';
public const string TITLE = 'TITLE';
public const string VERSION = 'VERSION';
public const string SEND_EMAIL = 'SEND_EMAIL';
public const string REPLY_EMAIL = 'REPLY_EMAIL';
public const string HTTP = 'HTTP';
public const string IP_TEST = 'IP_TEST';
public const string PROGRAMS = 'PROGRAMS';
public const string PROGRAM_DIRECTORIES = 'PROGRAM_DIRECTORIES';

// Session Mgmt
public const SESSION = 'SESSION';
public const REMOTE = 'REMOTE';
public const SERIALIZE = 'SERIALIZE'; // accepts an array of values todo - doc
public const CALLBACK = 'CALLBACK';
public const PATH = 'PATH';
public const string SESSION = 'SESSION';
public const string REMOTE = 'REMOTE';
public const string SERIALIZE = 'SERIALIZE'; // accepts an array of values todo - doc
public const string CALLBACK = 'CALLBACK';
public const string PATH = 'PATH';

// Web Socket (Secure* see carbonphp.com)
public const SOCKET = 'SOCKET';
public const WEB_SOCKET = 'WEB_SOCKET';
public const PORT = 'PORT';
public const DEV = 'DEV';
public const SSL = 'SSL';
public const KEY = 'KEY';
public const CERT = 'CERT';
public const string SOCKET = 'SOCKET';
public const string WEB_SOCKET = 'WEB_SOCKET';
public const string PORT = 'PORT';
public const string DEV = 'DEV';
public const string SSL = 'SSL';
public const string KEY = 'KEY';
public const string CERT = 'CERT';

// Error Catcher
public const ERROR = 'ERROR';
public const DISPLAY = 'DISPLAY';
public const LOCATION = 'LOCATION';
public const LEVEL = 'LEVEL';
public const STORE = 'STORE';
public const SHOW = 'SHOW';
public const FULL = 'FULL';
public const string ERROR = 'ERROR';
public const string DISPLAY = 'DISPLAY';
public const string LOCATION = 'LOCATION';
public const string LEVEL = 'LEVEL';
public const string STORE = 'STORE';
public const string SHOW = 'SHOW';
public const string FULL = 'FULL';

// Default View info for Mvc
public const VIEW = 'VIEW';
public const WRAPPER = 'WRAPPER';
public const MINIFY = 'MINIFY';
public const CSS = 'CSS';
public const JS = 'JS';
public const OUT = 'OUT';
public const string VIEW = 'VIEW';
public const string WRAPPER = 'WRAPPER';
public const string MINIFY = 'MINIFY';
public const string CSS = 'CSS';
public const string JS = 'JS';
public const string OUT = 'OUT';

// Auto load is deprecated
public const AUTOLOAD = 'AUTOLOAD';
public const string AUTOLOAD = 'AUTOLOAD';

// Application invocation method
public static bool $is_running_production = false;
Expand Down Expand Up @@ -279,7 +278,16 @@ public static function run(Application|string|null $application = null): bool

}

WebSocket::handleSingleUserConnections();
// todo - if not apache it's possible to be handled???
if (PHP_SAPI !== 'cli'
&& function_exists('apache_connection_stream')
&& str_contains($_SERVER['HTTP_CONNECTION'] ?? '', 'Upgrade')
&& str_contains($_SERVER['HTTP_UPGRADE'] ?? '', 'websocket')) {

// Here you can handle the WebSocket upgrade logic
WebSocket::handleSingleUserConnections();

}

return self::startApplication() !== false; // startApplication can return null which is not allowed here

Expand Down Expand Up @@ -448,7 +456,7 @@ private static function &parseConfiguration(iConfig|array|string $configuration)
* @link https://www.php.net/manual/en/wrappers.php.php
* @return void
*/
public static function protocolWrapper() : void
public static function protocolWrapper(): void
{

if (!defined('STDOUT')) {
Expand Down Expand Up @@ -577,8 +585,6 @@ public static function make(iConfig|array|string|null $configuration = null, str

}

Htaccess::generalConfigurations();

#################### Define your own server root
self::$app_root ??= self::CARBON_ROOT;

Expand Down Expand Up @@ -754,7 +760,7 @@ public static function make(iConfig|array|string|null $configuration = null, str
# try to json decode. Json payloads ar sent to the input stream
$_POST = json_decode($getAnyJsonPayloads, true, 512, JSON_THROW_ON_ERROR);

}elseif (is_string($getAnyJsonPayloads) && !empty($getAnyJsonPayloads)) {
} elseif (is_string($getAnyJsonPayloads) && !empty($getAnyJsonPayloads)) {

$_POST = ['stdin' => $getAnyJsonPayloads];

Expand Down Expand Up @@ -829,7 +835,9 @@ public static function make(iConfig|array|string|null $configuration = null, str


if (!self::$cli && is_array($config[self::SESSION][self::SERIALIZE] ?? false)) {

forward_static_call_array([Serialized::class, 'start'], $config[self::SESSION][self::SERIALIZE]); // Pull theses from session, and store on shutdown

}

self::$setupComplete = true;
Expand Down
1 change: 0 additions & 1 deletion carbonphp/View.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace CarbonPHP;

use CarbonPHP\Error\PrivateAlert;
use CarbonPHP\Error\PublicAlert;
use CarbonPHP\Extras\MustacheEngine;

/**
Expand Down
94 changes: 47 additions & 47 deletions carbonphp/interfaces/iColorCode.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,59 +6,59 @@

interface iColorCode
{
public const BOLD = 'bold';
public const DARK = 'dark';
public const ITALIC = 'italic';
public const UNDERLINE = 'underline';
public const BLINK = 'blink';
public const REVERSE = 'reverse';
public const CONCEALED = 'concealed';
public const BLACK = 'black';
public const RED = 'red';
public const GREEN = 'green';
public const YELLOW = 'yellow';
public const BLUE = 'blue';
public const MAGENTA = 'magenta';
public const CYAN = 'cyan';
public const WHITE = 'white';
public const BACKGROUND_BLACK = 'background_black';
public const BACKGROUND_RED = 'background_red';
public const BACKGROUND_GREEN = 'background_green';
public const BACKGROUND_YELLOW = 'background_yellow';
public const BACKGROUND_BLUE = 'background_blue';
public const BACKGROUND_MAGENTA = 'background_magenta';
public const BACKGROUND_CYAN = 'background_cyan';
public const BACKGROUND_WHITE = 'background_white';
public const string BOLD = 'bold';
public const string DARK = 'dark';
public const string ITALIC = 'italic';
public const string UNDERLINE = 'underline';
public const string BLINK = 'blink';
public const string REVERSE = 'reverse';
public const string CONCEALED = 'concealed';
public const string BLACK = 'black';
public const string RED = 'red';
public const string GREEN = 'green';
public const string YELLOW = 'yellow';
public const string BLUE = 'blue';
public const string MAGENTA = 'magenta';
public const string CYAN = 'cyan';
public const string WHITE = 'white';
public const string BACKGROUND_BLACK = 'background_black';
public const string BACKGROUND_RED = 'background_red';
public const string BACKGROUND_GREEN = 'background_green';
public const string BACKGROUND_YELLOW = 'background_yellow';
public const string BACKGROUND_BLUE = 'background_blue';
public const string BACKGROUND_MAGENTA = 'background_magenta';
public const string BACKGROUND_CYAN = 'background_cyan';
public const string BACKGROUND_WHITE = 'background_white';


public const PRINTF_ANSI_COLOR = [
public const array PRINTF_ANSI_COLOR = [
// styles
// italic and blink may not work depending of your terminal
iColorCode::BOLD => "\033[1m%s\033[0m",
iColorCode::DARK => "\033[2m%s\033[0m",
iColorCode::ITALIC => "\033[3m%s\033[0m",
iColorCode::UNDERLINE => "\033[4m%s\033[0m",
iColorCode::BLINK => "\033[5m%s\033[0m",
iColorCode::REVERSE => "\033[7m%s\033[0m",
iColorCode::CONCEALED => "\033[8m%s\033[0m",
self::BOLD => "\033[1m%s\033[0m",
self::DARK => "\033[2m%s\033[0m",
self::ITALIC => "\033[3m%s\033[0m",
self::UNDERLINE => "\033[4m%s\033[0m",
self::BLINK => "\033[5m%s\033[0m",
self::REVERSE => "\033[7m%s\033[0m",
self::CONCEALED => "\033[8m%s\033[0m",
// foreground colors
iColorCode::BLACK => "\033[30m%s\033[0m",
iColorCode::RED => "\033[31m%s\033[0m",
iColorCode::GREEN => "\033[32m%s\033[0m",
iColorCode::YELLOW => "\033[33m%s\033[0m",
iColorCode::BLUE => "\033[34m%s\033[0m",
iColorCode::MAGENTA => "\033[35m%s\033[0m",
iColorCode::CYAN => "\033[36m%s\033[0m",
iColorCode::WHITE => "\033[37m%s\033[0m",
self::BLACK => "\033[30m%s\033[0m",
self::RED => "\033[31m%s\033[0m",
self::GREEN => "\033[32m%s\033[0m",
self::YELLOW => "\033[33m%s\033[0m",
self::BLUE => "\033[34m%s\033[0m",
self::MAGENTA => "\033[35m%s\033[0m",
self::CYAN => "\033[36m%s\033[0m",
self::WHITE => "\033[37m%s\033[0m",
// background colors
iColorCode::BACKGROUND_BLACK => "\033[40m%s\033[0m",
iColorCode::BACKGROUND_RED => "\033[41m%s\033[0m",
iColorCode::BACKGROUND_GREEN => "\033[42m%s\033[0m",
iColorCode::BACKGROUND_YELLOW => "\033[43m%s\033[0m",
iColorCode::BACKGROUND_BLUE => "\033[44m%s\033[0m",
iColorCode::BACKGROUND_MAGENTA => "\033[45m%s\033[0m",
iColorCode::BACKGROUND_CYAN => "\033[46m%s\033[0m",
iColorCode::BACKGROUND_WHITE => "\033[47m%s\033[0m",
self::BACKGROUND_BLACK => "\033[40m%s\033[0m",
self::BACKGROUND_RED => "\033[41m%s\033[0m",
self::BACKGROUND_GREEN => "\033[42m%s\033[0m",
self::BACKGROUND_YELLOW => "\033[43m%s\033[0m",
self::BACKGROUND_BLUE => "\033[44m%s\033[0m",
self::BACKGROUND_MAGENTA => "\033[45m%s\033[0m",
self::BACKGROUND_CYAN => "\033[46m%s\033[0m",
self::BACKGROUND_WHITE => "\033[47m%s\033[0m",
];

/**
Expand Down
Loading

0 comments on commit a84185b

Please sign in to comment.