From a533a91190d6e900d393aa1eec22405b38de2274 Mon Sep 17 00:00:00 2001 From: Robert Date: Fri, 30 Dec 2022 20:17:15 -0500 Subject: [PATCH] Made extras and arrays as fallback autoloading. --- documents/folders.txt | 2 +- src/bootstrap/auto_loader.php | 91 +++++++++++++++++++---------------- src/classes/loadall.php | 8 ++- src/compiler.php | 2 +- 4 files changed, 58 insertions(+), 45 deletions(-) diff --git a/documents/folders.txt b/documents/folders.txt index 496141c..4b74832 100644 --- a/documents/folders.txt +++ b/documents/folders.txt @@ -43,7 +43,7 @@ tts_framework/src │   ├── 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 +│ ├── obsolete (Anything in here, will be SKIPPED by my Compiler) │ │ ├── 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) diff --git a/src/bootstrap/auto_loader.php b/src/bootstrap/auto_loader.php index 2c6a4ca..4caf8df 100644 --- a/src/bootstrap/auto_loader.php +++ b/src/bootstrap/auto_loader.php @@ -41,7 +41,7 @@ class Psr4AutoloaderClass { return $this->loaded_files; } - /** + /** * Adds a base directory for a namespace prefix. * * @param string $prefix The namespace prefix. @@ -73,11 +73,11 @@ class Psr4AutoloaderClass { * failure. */ public function load_class(string $class): false|string { - /** - * Semi-Fix for non-namespaced classes - */ - if (substr_count($class, '\\') < 2) { - return ($this->load_mapped_file($class, $class)); + if (! strrpos($class, '\\')) { + $ret = ($this->load_mapped_file($class . '\\', $class)); + if ($ret !== false) { + return $ret; + } } $prefix = $class; while (false !== $pos = strrpos($prefix, '\\')) { @@ -89,7 +89,7 @@ class Psr4AutoloaderClass { } $prefix = rtrim($prefix, '\\'); } - return false; + return ($this->loader($class, $class)); } private function decode(false|string $var): array { @@ -105,44 +105,27 @@ class Psr4AutoloaderClass { return []; } - /** - * Load the mapped file for a namespace prefix and relative class. - * - * @param string $prefix The namespace prefix. - * @param string $relative_class The relative class name. - * @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): 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, '\\') . '\\'; + protected function loader(string $prefix, string $relative_class): false | string { + $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]); - } - + $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)) { @@ -150,17 +133,41 @@ class Psr4AutoloaderClass { return "tts_extras.php"; } } - } - - $file = str_replace('\\', '/', $relative_class) . '.php'; - if ($this->require_file($base_dir, $file)) { - return $file; - } - + + $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"; + } + } + } } return false; } + + /** + * Load the mapped file for a namespace prefix and relative class. + * + * @param string $prefix The namespace prefix. + * @param string $relative_class The relative class name. + * @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): false | string { + if (isset($this->prefixes[$prefix]) === false) { + return false; + } + foreach ($this->prefixes[$prefix] as $base_dir) { + $file = str_replace('\\', '/', $relative_class) . '.php'; + if ($this->require_file($base_dir, $file)) { + return $file; + } + } + return false; + } + /** * If a file exists, require it from the file system. * diff --git a/src/classes/loadall.php b/src/classes/loadall.php index 4b01d14..a0198de 100644 --- a/src/classes/loadall.php +++ b/src/classes/loadall.php @@ -29,13 +29,19 @@ final class loadall { if ($fl_handle === false) { die("System Loader Error"); } - + $config_path = $root_path . ltrim($path, "/"); if ($cdir = scandir($config_path)) { fwrite($fl_handle, " $file) { $file = trim($file); if ($file === '..' || $file === '.' || $file === 'loadall.php') { diff --git a/src/compiler.php b/src/compiler.php index 394ecf2..24d3763 100644 --- a/src/compiler.php +++ b/src/compiler.php @@ -179,7 +179,7 @@ function rsearch(array & $file_list, string $folder, string $pattern): array { function remove_comments(string & $str): void { foreach (token_get_all($str) as $token ) { - if ($token[0] != T_COMMENT && + if ($token[0] != \T_COMMENT && $token[0] != \T_DOC_COMMENT ) { continue; // Do nothing on NON-Comments!