* @copyright Copyright (c) 2022, Robert Strutts. * @license https://mit-license.org/ */ $print_new_line = false; $show_filename_comments = false; $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/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 ") ) { continue; } if ($file === "main.inc.php" && ( str_contains($line, "require_once") || str_contains($line, "add_namespace(") )) { continue; } $search = ['bs_tts', 'main_tts', '\\tts\\enum\\', '\\tts\\contacts\\', '\\tts\\traits\\database\\', '\\tts\\traits\\security\\', '\\tts\\services\\sessions\\']; $replace = ['tts', 'tts', '\\tts\\', '\\tts\\', '\\tts\\', '\\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); } echo "Compiled tts_framework into folder: dist";