Made extras and arrays as fallback autoloading.

main
Robert 3 years ago
parent 7cdbd3e83f
commit a533a91190
  1. 2
      documents/folders.txt
  2. 91
      src/bootstrap/auto_loader.php
  3. 8
      src/classes/loadall.php
  4. 2
      src/compiler.php

@ -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)

@ -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.
*

@ -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, "<?php \r\n declare(strict_types=1);\r\n");
fwrite($fl_handle, "/* \r\n");
fwrite($fl_handle, "* This file is Auto-Generated, Do NOT Modify!!! Please Delete this file to update configuration! \r\n");
fwrite($fl_handle, "*/ \r\n");
if (str_contains($path, "configs/")) {
fwrite($fl_handle, "use \\tts\\configure as Config; \r\n");
}
if (str_contains($path, "services/")) {
fwrite($fl_handle, "use \\tts\\registry as Reg; \r\n");
}
foreach($cdir as $key => $file) {
$file = trim($file);
if ($file === '..' || $file === '.' || $file === 'loadall.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!

Loading…
Cancel
Save