From dbfa82ef817a045ed43bb28de0539ded358007b6 Mon Sep 17 00:00:00 2001 From: Robert Date: Fri, 29 Aug 2025 15:14:06 -0400 Subject: [PATCH] count files... --- src/bootstrap/auto_loader.php | 12 ++++++---- src/bootstrap/errors.php | 2 +- src/bootstrap/main.php | 6 ++--- src/bootstrap/requires.php | 8 +++++++ src/bootstrap/site_helper.php | 18 ++++++++++++++- src/classes/http/response.php | 18 +++++++++++++-- src/classes/uuids/base62.php | 37 ++++++++++++++++++++++++++++++ src/classes/{ => uuids}/uuidv7.php | 2 +- 8 files changed, 91 insertions(+), 12 deletions(-) create mode 100644 src/classes/uuids/base62.php rename src/classes/{ => uuids}/uuidv7.php (99%) diff --git a/src/bootstrap/auto_loader.php b/src/bootstrap/auto_loader.php index 607d01b..0d42a5e 100644 --- a/src/bootstrap/auto_loader.php +++ b/src/bootstrap/auto_loader.php @@ -130,10 +130,14 @@ class Psr4AutoloaderClass { private function require_file(string $path, string $file): bool { $safer_file = requires::safer_file_exists($file, $path); if ($safer_file !== false) { - if (! isset($this->loaded_files[$safer_file])) { - require $safer_file; - $this->loaded_files[$safer_file] = true; - } + if (defined('CountFiles') && CountFiles) { + if (! isset($this->loaded_files[$safer_file])) { + require $safer_file; + $this->loaded_files[$safer_file] = true; + } + } else { + require_once $safer_file; + } return true; } return false; diff --git a/src/bootstrap/errors.php b/src/bootstrap/errors.php index 97aa2a2..c81a7ba 100644 --- a/src/bootstrap/errors.php +++ b/src/bootstrap/errors.php @@ -332,7 +332,7 @@ function fallback_requires(string $file, bool $fw = false, $local = null): bool $view = CodeHydrater_FRAMEWORK . $file; } if (file_exists($view)) { - include $view; + site_helper::load_file($view); return true; } } diff --git a/src/bootstrap/main.php b/src/bootstrap/main.php index 9311961..a033bd1 100644 --- a/src/bootstrap/main.php +++ b/src/bootstrap/main.php @@ -12,7 +12,7 @@ namespace CodeHydrater\bootstrap; $mem_baseline = memory_get_usage(); -require_once CodeHydrater_FRAMEWORK . 'bootstrap/errors.php'; +site_helper::load_file(CodeHydrater_FRAMEWORK . 'bootstrap/errors.php'); final class views { public static function ob_start(): void { @@ -261,8 +261,8 @@ final class di { // Initialize our Dependency Injector registry::set('di', new di()); -require_once CodeHydrater_FRAMEWORK . 'bootstrap/requires.php'; -require_once CodeHydrater_FRAMEWORK . 'bootstrap/auto_loader.php'; +site_helper::load_file(CodeHydrater_FRAMEWORK . 'bootstrap/requires.php'); +site_helper::load_file(CodeHydrater_FRAMEWORK . 'bootstrap/auto_loader.php'); registry::set('loader', new Psr4AutoloaderClass); registry::get('loader')->register(); diff --git a/src/bootstrap/requires.php b/src/bootstrap/requires.php index 13810b4..32ab7ec 100644 --- a/src/bootstrap/requires.php +++ b/src/bootstrap/requires.php @@ -18,6 +18,11 @@ enum UseDir: string { } final class requires { + private static $loaded_files = []; + + public static function get_loaded_files(): array { + return self::$loaded_files; + } public static function is_valid_file(string $filename): bool { if (is_string($filename) && strlen($filename) < 64) { @@ -174,6 +179,9 @@ final class requires { extract($args, EXTR_PREFIX_SAME, "dup"); } + if (defined('CountFiles') && CountFiles) { + self::$loaded_files[] = $versioned_file; + } if ($return_contents) { $script_output = (string) ob_get_clean(); self::ob_starter(); diff --git a/src/bootstrap/site_helper.php b/src/bootstrap/site_helper.php index 9f00554..e072621 100644 --- a/src/bootstrap/site_helper.php +++ b/src/bootstrap/site_helper.php @@ -26,7 +26,23 @@ final class site_helper { private static $local_site_domains = ['localhost']; private static $Private_IPs_allowed = ['127.0.0.1', '::1']; private static $Public_IPs_allowed = []; - + private static $loaded_files = []; + + /** + * Don't USE THIS method, instead use requires::secure_include + * It validates that the file is not dangerous + */ + public static function load_file(string $file): void { + if (defined('CountFiles') && CountFiles) { + self::$loaded_files[] = $file; + } + require_once $file; + } + + public static function get_loaded_files(): array { + return self::$loaded_files; + } + public static function set_local_site_domains(string|array $domain_name): void { if (is_array($domain_name)) { foreach($domain_name as $domain) { diff --git a/src/classes/http/response.php b/src/classes/http/response.php index 0a282de..d3dd1a0 100644 --- a/src/classes/http/response.php +++ b/src/classes/http/response.php @@ -12,6 +12,8 @@ namespace CodeHydrater\http; class response { use \CodeHydrater\traits\Macroable; + + private $json = false; public function __construct( protected string $content = '', @@ -25,10 +27,22 @@ class response foreach ($this->headers as $name => $value) { header("$name: $value"); } - - echo $this->content; + + if ($this->status_code > 499) { + throw new \Exception($this->content); + } elseif ($this->json) { + echo $this->json; + } else { + echo $this->content; + } } + public function set_json(mixed $input): self { + $this->headers['Content-Type'] = 'application/json; charset=utf-8'; + $this->json = json_encode($input, JSON_PRETTY_PRINT); + return $this; + } + public function get_content(): string { return $this->content; } diff --git a/src/classes/uuids/base62.php b/src/classes/uuids/base62.php new file mode 100644 index 0000000..216c2ec --- /dev/null +++ b/src/classes/uuids/base62.php @@ -0,0 +1,37 @@ + + * @copyright (c) 2025, Robert Strutts + * @license MIT + */ +namespace CodeHydrater\uuids; + +const BASE62 = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; + +class base62 { + + public static function encode_id(int $num): string { + if ($num === 0) return BASE62[0]; + $base = strlen(BASE62); + $encoded = ''; + while ($num > 0) { + $encoded = BASE62[$num % $base] . $encoded; + $num = intdiv($num, $base); + } + return $encoded; + } + + public static function decode_id(string $str): int { + $base = strlen(BASE62); + $len = strlen($str); + $num = 0; + for ($i = 0; $i < $len; $i++) { + $num = $num * $base + strpos(BASE62, $str[$i]); + } + return $num; + } + +} diff --git a/src/classes/uuidv7.php b/src/classes/uuids/uuidv7.php similarity index 99% rename from src/classes/uuidv7.php rename to src/classes/uuids/uuidv7.php index 8806556..e0b284e 100644 --- a/src/classes/uuidv7.php +++ b/src/classes/uuids/uuidv7.php @@ -7,7 +7,7 @@ declare(strict_types = 1); * @copyright (c) 2025, Robert Strutts * @license MIT */ -namespace CodeHydrater; +namespace CodeHydrater\uuids; /** * Description of uuidv7