From 7cdbd3e83f49d8f195e3b1369d758cf44a75c920 Mon Sep 17 00:00:00 2001 From: Robert Date: Fri, 30 Dec 2022 09:57:03 -0500 Subject: [PATCH] Auto load extras and arrays. --- README | 16 ++++ documents/folders.txt | 25 ++++--- src/bootstrap/auto_loader.php | 56 +++++++++++++- src/bootstrap/site_helper.php | 14 +++- src/classes/{ => extras}/bb_code_parser.php | 0 src/classes/{ => extras}/html_parser.php | 0 src/classes/{services => }/obsolete/.htaccess | 0 .../obsolete/http_socket_request.php | 0 .../sessions_interface.php | 0 src/compiler.php | 75 ++++++++++++++++++- 10 files changed, 167 insertions(+), 19 deletions(-) rename src/classes/{ => extras}/bb_code_parser.php (100%) rename src/classes/{ => extras}/html_parser.php (100%) rename src/classes/{services => }/obsolete/.htaccess (100%) rename src/classes/{services => }/obsolete/http_socket_request.php (100%) rename src/classes/{contracts => obsolete}/sessions_interface.php (100%) diff --git a/README b/README index b2b9e21..2b16958 100644 --- a/README +++ b/README @@ -24,6 +24,22 @@ chgrp www-data HTMLPurifier/DefinitionCache/Serializer cd /var/www/frames/tts_framework composer install ``` + +### Compile the framework!!! +``` +cd /var/www/frames/tts_framework/src +php compiler.php +``` + +### Install JavaScript Lib: +``` +cd /var/www/frames/ +git clone https://git.mysnippetsofcode.com/tts/tts_js.git +sudo npm install --global gulp-cli +npm install +gulp +``` + ### Install sample project: ``` diff --git a/documents/folders.txt b/documents/folders.txt index 231d7d1..496141c 100644 --- a/documents/folders.txt +++ b/documents/folders.txt @@ -3,7 +3,7 @@ tts_framework/src │   ├── auto_loader.php (PSR-4-autoloader) │   ├── common.php (custom string FNs, wipe data, and dump) │   ├── errors.php (Error Handler) -│   ├── filter.php (HTMLPurifier Bootstrap) +│   ├── html_purifier.php (HTMLPurifier Bootstrap) │   ├── requires.php (secure_includes, file/dir Filters/Validators) │   ├── safer_io.php (Sanitize Input, Validate data and Escape output) │   ├── site_helper.php (Set/Get Routes/Root Paths/Assets, etc...) @@ -21,11 +21,9 @@ tts_framework/src │   │   ├── shortn.php (short_street_suffix) │   │   └── zipcodes.php (5-digit Zip into State) │   ├── assets.php (Fetch CSS/JS/Image, and site redirects) -│   ├── bb_code_parser.php (Take BB code and display output as HTML) │   ├── console_app.php (Detects if running PHP from Command Line) │   ├── contracts -│   │   ├── http_request_options.php (Used by http_curl_request) -│   │   └── sessions_interface.php +│   │   └── http_request_options.php (Used by http_curl_request) │   ├── database │   │   ├── dummy_data.php (Populates a DB w/ Dummy Data you give it bu # Rows wanted) │   │   ├── model.php (Model used to save into DB) @@ -36,13 +34,18 @@ tts_framework/src │   ├── exceptions │   │   ├── Bool_Exception.php (Handles invalid Boolean types) │   │   └── DB_Exception.php (Handles DB Exceptions) +│   ├── extras +│ │   ├── bb_code_parser.php (Take BB code and display output as HTML) +│ │   └── html_parser.php (Escape HTML into BB code...to save into db) │   ├── html_document.php (HTML Class: set/get CSS/JS/Breadcrumbs/Title/Author/Header/Footer) -│   ├── html_parser.php (Escape HTML into BB code...to save into db) │   ├── html.php (HTML Select-Options, Table maker) │   ├── json.php (Outputs DB Records into JSON format) │   ├── loadall.php (Creates loadall.php files if do not exist for Services & Config-Files) │   ├── memory_usage.php (Displays PHP Memory Usage, when debug is set) │   ├── misc.php (create a short url from get_url, post_var, misc. filter FNs) +│ ├── obsolete +│ │ ├── http_socket_request.php (Failed to get Sockets working, use Guzzle, PHP HTTP client instead!) +│   │   └── sessions_interface.php │   ├── page_not_found.php (CLI or tts built in views/404 page not found error) │   ├── random_engine.php (random bytes/numbers/array shuffle) │   ├── router.php (router::get/post, etc... used to setup PHP routes) @@ -56,8 +59,6 @@ tts_framework/src │   │   │   └── http_curl_request.php (Does Curl requests) │   │   ├── liquid_templates.php (Sets up Liquid Templates Engine for Views) │   │   ├── log.php (Appends new Log to Project's logs folder) -│   │   ├── obsolete -│   │   │   └── http_socket_request.php (Failed to get Sockets working, use Guzzle, PHP HTTP client instead!) │   │   ├── paragon_crypto │   │   │   ├── crypto.php (Newer sodium_crypto) │   │   │   ├── password_storage.php (Hash and Verify Hashed passwords) @@ -81,13 +82,15 @@ tts_framework/src │   │   └── session_hijacking_functions.php (Prevent SESSION Hijacking and Fixation in PHP) │ ├── validator.php (validates HTML Forms) │   └── view.php (Loads view files from common folders you defined) +├── compiler.php (the PHP Compiler) +├── dist +│   ├── tts_arrays.php (Mocking Data arrays) +│   ├── tts_extras.php (bb code and html parsers) +│   └── tts.php (the Compiled framework, all in one-file!!) ├── main.inc.php (Bootstraps App, sets configure, registry, di, and name-spaces) └── views ├── 404_page.php (Default 404 Page Not Found Page/Image)    ├── dev_error.php (When NOT Live, show Exceptions/Errors) └── prod_errors.php (when Live, this: Sorry, we had an error... Page is used) -~70 files - -/src/compiler.php (the PHP Compiler) -/src/dist/tts.php (the Compiled framework, all in one-file!!) \ No newline at end of file +~22 directories, ~74 files \ No newline at end of file diff --git a/src/bootstrap/auto_loader.php b/src/bootstrap/auto_loader.php index b3826fe..2c6a4ca 100644 --- a/src/bootstrap/auto_loader.php +++ b/src/bootstrap/auto_loader.php @@ -72,12 +72,12 @@ class Psr4AutoloaderClass { * @return mixed The mapped file name on success, or boolean false on * failure. */ - public function load_class(string $class) { + public function load_class(string $class): false|string { /** * Semi-Fix for non-namespaced classes */ - if (! strrpos($class, '\\')) { - return ($this->load_mapped_file($class . '\\', $class)); + if (substr_count($class, '\\') < 2) { + return ($this->load_mapped_file($class, $class)); } $prefix = $class; while (false !== $pos = strrpos($prefix, '\\')) { @@ -92,6 +92,19 @@ class Psr4AutoloaderClass { return false; } + private function decode(false|string $var): array { + if ($var !== false) { + $decoded = base64_decode($var); + if ($decoded !== false) { + $a = json_decode($decoded, true); + if ($a !== false) { + return $a; + } + } + } + return []; + } + /** * Load the mapped file for a namespace prefix and relative class. * @@ -100,15 +113,50 @@ class Psr4AutoloaderClass { * @return mixed Boolean false if no mapped file can be loaded, or the * name of the mapped file that was loaded. */ - protected function load_mapped_file(string $prefix, string $relative_class) { + protected function load_mapped_file(string $prefix, string $relative_class): false | string { + if ($prefix === $relative_class) { + $parts = explode('\\', $prefix); + $last = array_pop($parts); + $parts = array(implode('\\', $parts), $last); + $prefix = $parts[0] ?? 'tts'; + $prefix = trim($prefix, '\\') . '\\'; + + $reversedParts = explode('\\', strrev($relative_class), 2); + $relative_class = strrev($reversedParts[0]); + } + if (isset($this->prefixes[$prefix]) === false) { return false; } + + $fw = rtrim(\bs_tts\site_helper::get_fw_dist(), "/") . "/"; foreach ($this->prefixes[$prefix] as $base_dir) { + + $bd = rtrim($base_dir, "/") . "/"; + if ($fw !== false && $prefix === 'tts\\' && $bd === $fw) { + $rc = str_replace('\\', '', $relative_class); + + $tts_arrays = $GLOBALS['tts_arrays'] ?? false; + $ta = $this->decode($tts_arrays); + if (count($ta) && in_array($rc, $ta)) { + if ($this->require_file($bd, "tts_arrays.php")) { + return "tts_arrays.php"; + } + } + $tts_extras = $GLOBALS['tts_extras'] ?? false; + $te = $this->decode($tts_extras); + if (count($te) && in_array($rc, $te)) { + if ($this->require_file($bd, "tts_extras.php")) { + return "tts_extras.php"; + } + } + } + $file = str_replace('\\', '/', $relative_class) . '.php'; if ($this->require_file($base_dir, $file)) { return $file; } + } return false; } diff --git a/src/bootstrap/site_helper.php b/src/bootstrap/site_helper.php index 2545f1b..6bf6789 100644 --- a/src/bootstrap/site_helper.php +++ b/src/bootstrap/site_helper.php @@ -15,6 +15,7 @@ final class site_helper { private static $ROOT; private static $ROUTE; private static $PRJ; + private static $FW_DIST; private static $REQUEST_URI; private static $REQUEST_METHOD; private static $USE_SECURE = true; @@ -80,6 +81,14 @@ final class site_helper { return self::$ROOT; } + public static function get_fw_dist(): string { + return self::$FW_DIST; + } + + public static function set_fw_dist(string $fw): void { + self::$FW_DIST = $fw; + } + public static function get_route(): string { return self::$ROUTE; } @@ -311,7 +320,10 @@ final class site_helper { public static function set_project_namespace() { $prj = rtrim(self::$ROOT, '/'); \main_tts\registry::get('loader')->add_namespace("prj", $prj); - + + $fw = rtrim(self::$FW_DIST, '/'); + \main_tts\registry::get('loader')->add_namespace("tts", $fw); + $up_one = dirname(self::$ROOT, 1); $project = self::$PRJ; diff --git a/src/classes/bb_code_parser.php b/src/classes/extras/bb_code_parser.php similarity index 100% rename from src/classes/bb_code_parser.php rename to src/classes/extras/bb_code_parser.php diff --git a/src/classes/html_parser.php b/src/classes/extras/html_parser.php similarity index 100% rename from src/classes/html_parser.php rename to src/classes/extras/html_parser.php diff --git a/src/classes/services/obsolete/.htaccess b/src/classes/obsolete/.htaccess similarity index 100% rename from src/classes/services/obsolete/.htaccess rename to src/classes/obsolete/.htaccess diff --git a/src/classes/services/obsolete/http_socket_request.php b/src/classes/obsolete/http_socket_request.php similarity index 100% rename from src/classes/services/obsolete/http_socket_request.php rename to src/classes/obsolete/http_socket_request.php diff --git a/src/classes/contracts/sessions_interface.php b/src/classes/obsolete/sessions_interface.php similarity index 100% rename from src/classes/contracts/sessions_interface.php rename to src/classes/obsolete/sessions_interface.php diff --git a/src/compiler.php b/src/compiler.php index 4e5fcd4..394ecf2 100644 --- a/src/compiler.php +++ b/src/compiler.php @@ -14,10 +14,12 @@ if ($debugging_AKA_non_production_mode) { $print_new_line = true; $show_filename_comments = true; $skip_mocking_data_arrays = false; + $skip_extras_folder = false; } else { $print_new_line = false; $show_filename_comments = false; $skip_mocking_data_arrays = true; + $skip_extras_folder = true; } $make_one_file = true; @@ -43,8 +45,10 @@ function single_file(): void { foreach($bootstap_files as $bs) { $core_files[] = $bs; } - rsearch($core_files, "classes", "/^.*\.(php)$/"); + $rsearch = rsearch($core_files, "classes", "/^.*\.(php)$/"); compile($core_files, "tts.php", true); // true = Append mode + get_classes($rsearch['arrays'], "tts_arrays"); + get_classes($rsearch['extras'], "tts_extras"); } function multi_files(): void { @@ -69,9 +73,56 @@ function multi_files(): void { foreach($bootstap_files as $bs) { $core_files[] = $bs; } - rsearch($core_files, "classes", "/^.*\.(php)$/"); + $rsearch = rsearch($core_files, "classes", "/^.*\.(php)$/"); compile($core_files, "tts_core.php", false, "namespace tts;"); + get_classes($rsearch['arrays'], "tts_arrays"); + get_classes($rsearch['extras'], "tts_extras"); } + +function grab_class_name(string $input): false | string { + $e = explode(" ", $input); + $class = $e[0] ?? false; + if ($class === false) { + return false; + } + $reversedParts = explode('/', strrev($class), 2); + $ret = strrev($reversedParts[0]); + if (! empty($ret)) { + return $ret; + } + return false; +} + +function get_classes(array $file_list, string $file_name): void { + $classes = []; + foreach($file_list as $file) { + $read = fopen($file, 'r'); + if ( $read === false || ! is_resource($read) ) { + continue; + } + while (($line = fgets($read)) !== false) { + if (!str_contains($line, "class ")) { + continue; + } + $ex = explode("class ", $line); + $class_part = $ex[1] ?? false; + if ($class_part === false) { + continue; + } + $class_name = grab_class_name($class_part); + if ($class_name === false) { + continue; + } + $classes[] = $class_name; + } + fclose($read); + } + $append = fopen(__DIR__ . "/dist/tts.php", "a"); + + fwrite($append, "\$$file_name = \"". base64_encode(json_encode($classes)) . "\";"); + fclose($append); +} + /* * Remove already included BS files, pop them...out of the array */ @@ -83,14 +134,28 @@ function pop_bs(array & $files, array $files_to_pop): void { } } -function rsearch(array & $file_list, string $folder, string $pattern): void { +function rsearch(array & $file_list, string $folder, string $pattern): array { $dir = new RecursiveDirectoryIterator($folder); $ite = new RecursiveIteratorIterator($dir); $files = new RegexIterator($ite, $pattern, RegexIterator::GET_MATCH); $arrays = []; + $extras = []; foreach($files as $file) { + if (str_contains($file[0], "/obsolete/")) { + continue; // Skip Obsolete stuff + } + if (( + $GLOBALS['skip_extras_folder'] || + $GLOBALS['make_one_file'] === false + ) && + str_contains($file[0], "/extras/") + ) { + $extras[] = $file[0]; + continue; + } + // If we are making Multiple files, skip now and later make new file for arrays if (( $GLOBALS['skip_mocking_data_arrays'] || @@ -103,9 +168,13 @@ function rsearch(array & $file_list, string $folder, string $pattern): void { } $file_list[] = $file[0]; } + if ($GLOBALS['skip_extras_folder'] || $GLOBALS['make_one_file'] === false) { + compile($extras, "tts_extras.php", false, "namespace tts;"); + } if ($GLOBALS['skip_mocking_data_arrays'] || $GLOBALS['make_one_file'] === false) { compile($arrays, "tts_arrays.php", false, "namespace tts;"); } + return ['extras' => $extras, 'arrays' => $arrays]; } function remove_comments(string & $str): void {