diff --git a/documents/folders.txt b/documents/folders.txt index d92ceaa..93fec1b 100644 --- a/documents/folders.txt +++ b/documents/folders.txt @@ -28,8 +28,7 @@ tts_framework/src │   │   └── sessions_interface.php │   ├── database │   │   ├── dummy_data.php (Populates a DB w/ Dummy Data you give it bu # Rows wanted) -│   │   ├── help_load.php (fetch_lazy Generator) -│   │   ├── help_save.php (Model used to save into DB) +│   │   ├── model.php (Model used to save into DB) │   │   ├── paginate.php (PDO DB query to create links) │   ├── enum │   │   ├── app_environment.php (fetch if is_production environment) @@ -88,4 +87,4 @@ tts_framework/src    ├── dev_error.php (When NOT Live, show Exceptions/Errors) └── prod_errors.php (when Live, this: Sorry, we had an error... Page is used) -~72 files +~70 files diff --git a/src/classes/tag_matches.php b/src/classes/tag_matches.php index b1ad9ee..1481d5b 100644 --- a/src/classes/tag_matches.php +++ b/src/classes/tag_matches.php @@ -16,7 +16,7 @@ const tags_to_check = array('div', 'span', 'form', 'i*', 'a*', 'h1', 'p*'); /** * Function checks tags to make sure they match. - * Used by app.php + * Used by view.php * @param string $page * @return array [output, alert] */ @@ -36,7 +36,7 @@ public static function check_tags(string $page): array { $tag_name = str_replace('*', '', $tag_name); $otag = "<{$tag_name}>"; // Open Tag $open = substr_count($l_page, $otag); // Count open tags in page - $otag = "<{$tag_name} "; // Open Tag with space! + $otag = "<{$tag_name} "; /* Open Tag with space */ $open += substr_count($l_page, $otag); // Count open tags in page } else { $otag = "<{$tag_name}"; // Open Tag diff --git a/src/classes/view.php b/src/classes/view.php index 4ad1b07..a1ef1fe 100644 --- a/src/classes/view.php +++ b/src/classes/view.php @@ -12,10 +12,6 @@ declare(strict_types=1); /** * @todo Add Template Service here instead of hardcoded * Ref to Liquid. - * @todo Add Support for project preference over tts views - * On Errors like 404 and templates for Errors... - * - * So, Try Project First, then as Fallback use TTS Framework */ namespace tts; @@ -35,9 +31,6 @@ final class view { $this->project_dir = \bs_tts\site_helper::get_project(); } - /** - * @todo Ignore render path tts, should go prj/,...,then tts - */ private function get_file(string $view_file, string $default): string { $file_ext = \bs_tts\common::get_string_right($view_file, 4); if (! \bs_tts\common::is_string_found($file_ext, '.')) { diff --git a/src/compiler.php b/src/compiler.php new file mode 100644 index 0000000..7162fed --- /dev/null +++ b/src/compiler.php @@ -0,0 +1,211 @@ + + * @copyright Copyright (c) 2022, Robert Strutts. + * @license https://mit-license.org/ + */ +$print_new_line = true; +$show_filename_comments = true; +$make_one_file = true; + +if ($make_one_file) { + single_file(); +} else { + multi_files(); +} + +function single_file(): void { + $bootstap_files = glob("bootstrap/*.php"); + + $loader_files = []; + $loader_files[] = "bootstrap/auto_loader.php"; + $loader_files[] = "bootstrap/errors.php"; + $loader_files[] = "bootstrap/html_purifier.php"; + $loader_files[] = "bootstrap/site_helper.php"; + compile($loader_files, "tts.php", false, "namespace tts;"); + pop_bs($bootstap_files, $loader_files); + unset($loader_files); + + $core_files = []; + $core_files[] = "main.inc.php"; + foreach($bootstap_files as $bs) { + $core_files[] = $bs; + } + rsearch($core_files, "classes", "/^.*\.(php)$/"); + compile($core_files, "tts.php", true); // true = Append mode +} + +function multi_files(): void { + $bootstap_files = glob("bootstrap/*.php"); + + $main_files = []; + $main_files[] = "bootstrap/site_helper.php"; + compile($main_files, "tts.php", false ,"namespace tts;"); + pop_bs($bootstap_files, $main_files); + unset($main_files); + + $loader_files = []; + $loader_files[] = "bootstrap/auto_loader.php"; + $loader_files[] = "bootstrap/errors.php"; + $loader_files[] = "bootstrap/html_purifier.php"; + compile($loader_files, "tts_loader.php", false, null, "require_once \"tts_core.php\";"); + pop_bs($bootstap_files, $loader_files); + unset($loader_files); + + $core_files = []; + $core_files[] = "main.inc.php"; + foreach($bootstap_files as $bs) { + $core_files[] = $bs; + } + rsearch($core_files, "classes", "/^.*\.(php)$/"); + compile($core_files, "tts_core.php", false, "namespace tts;"); +} +/* + * Remove already included BS files, pop them...out of the array + */ +function pop_bs(array & $files, array $files_to_pop): void { + foreach($files as $index => $file) { + if (in_array($file, $files_to_pop)) { + unset($files[$index]); + } + } +} + +function rsearch(array & $file_list, string $folder, string $pattern): void { + $dir = new RecursiveDirectoryIterator($folder); + $ite = new RecursiveIteratorIterator($dir); + $files = new RegexIterator($ite, $pattern, RegexIterator::GET_MATCH); + + $arrays = []; + + foreach($files as $file) { + // If we are making Multiple files, skip now and later make new file for arrays + if ( + $GLOBALS['make_one_file'] === false && + str_contains($file[0], "/arrays/") + ) { + $arrays[] = $file[0]; + continue; // I do not want Dummy Array Data as it is kinda big in the core file + } + $file_list[] = $file[0]; + } + if ($GLOBALS['make_one_file'] === false) { + compile($arrays, "tts_arrays.php", "namespace tts;"); + } +} + +function remove_comments(string & $str): void { + foreach (token_get_all($str) as $token ) { + if ($token[0] != T_COMMENT && + $token[0] != \T_DOC_COMMENT + ) { + continue; // Do nothing on NON-Comments! + } + $str = str_replace($token[1], '', $str); // Replace comment + } +} + +function is_folder(string $dir): bool { + if (!file_exists($dir)) { + echo "Compiler Path $dir Does not Exists!"; + exit(1); + } + if (!is_writable($dir)) { + echo "Unable to write to Compiler Path $dir !"; + exit(2); + } + return true; +} + +function compile( + array $files, + string $output_filename, + bool $append = false, + ?string $prefix = "", + ?string $postfix = "" +): void { + $root_path = __DIR__ . '/'; + is_folder($root_path); + + $dist = $root_path . 'dist'; + if (!is_dir($dist)) { + mkdir($dist); + } + is_folder($dist); + $dist .= '/'; + + $tts_file = $dist . $output_filename; + $mode = ($append) ? "a" : "w"; + $fl_handle = fopen($tts_file, $mode); + if ($fl_handle === false) { + echo "Compiler error, unable to write to file $tts_file"; + exit(3); + } + if (! $append) { + fwrite($fl_handle, " \r\n \t * @copyright Copyright (c) 2022, Robert Strutts. \r\n \t * @license MIT \r\n \t */ \r\n"); + } + if ($prefix !== null && ! empty($prefix)) { + fwrite($fl_handle, $prefix); + } + foreach ($files as $file) { + $file_with_path = $root_path . $file; + + $content = file_get_contents($file_with_path); + if ($content === false) { + continue; + } + + remove_comments($content); + + $a = explode(PHP_EOL, $content); + unset($content); + + $is_php_start_block = true; + foreach ($a as $line) { + if ($is_php_start_block) { + $is_php_start_block = false; + if ($GLOBALS['show_filename_comments']) { + $line = PHP_EOL . "/* Contents of : {$file} */"; + fwrite($fl_handle, $line); + } + continue; + } + + if ($GLOBALS['print_new_line']) { + $line .= PHP_EOL; + } else { + $line = trim($line); + } + + if ( + (str_contains($line, "declare(") && + ! str_contains($line, "fwrite(") && + ! str_contains($line, "str_contains(") + ) || + str_contains($line, "namespace ") || + str_contains($line, "use ") + ) { + continue; + } + if ($file === "main.inc.php" && ( + str_contains($line, "require_once") || + str_contains($line, "add_namespace(") + )) { + continue; + } + $search = ['bs_tts', 'main_tts']; + $replace = ['tts', 'tts']; + $out = str_replace($search, $replace, $line); + fwrite($fl_handle, $out); + } + } + if ($postfix !== null && ! empty($postfix)) { + fwrite($fl_handle, $postfix); + } + fclose($fl_handle); + chmod($tts_file, 0664); +}