main
Robert 2 months ago
parent 4a70f8b183
commit c396101ccc
  1. 2
      src/bootstrap/main.php
  2. 2
      src/bootstrap/safer_io.php
  3. 6
      src/classes/attributes/validators/my_validator.php
  4. 12
      src/classes/common.php
  5. 166
      src/classes/html.php
  6. 29
      src/classes/memory_usage.php
  7. 9
      src/classes/traits/form_validator.php
  8. 2
      src/classes/view.php

@ -10,7 +10,7 @@ declare(strict_types=1);
namespace CodeHydrater\bootstrap;
$mem_baseline = memory_get_usage();
define("Memory_Baseline", memory_get_usage());
site_helper::load_file(CodeHydrater_FRAMEWORK . 'bootstrap/errors.php');

@ -72,6 +72,8 @@ final class use_iol {
}
}
$root_folder = str_replace('/', "\\", $root_folder);
$class_name = "\\Project\\inputs\\{$root_folder}\\{$file}_in";
$input = $class_name::$method($app);

@ -214,16 +214,12 @@ $handlers = [
continue;
}
$message = $handlers[$class](
$handlers[$class](
$name,
$value,
$attribute->newInstance(),
$messages
);
if ($message) {
$this->errors[$name][] = $message;
}
}
}
}

@ -81,6 +81,14 @@ final class common {
public static function get_count($i): int {
return (is_array($i) || is_object($i)) ? count($i) : 0;
}
public static function str_replace_first(string $search, string $replace, string $subject): string {
$pos = strpos($subject, $search);
if ($pos !== false) {
return substr_replace($subject, $replace, $pos, strlen($search));
}
return $subject;
}
// Begin Strings Functions here::
public static function string_position(string $string, string $needle, int $offset = 0, $encoding = null) {
@ -222,10 +230,10 @@ final class common {
if (\CodeHydrater\bootstrap\configure::get('security', 'show_dumps') !== true) {
return;
}
if (!is_object($var)) {
// if (!is_object($var)) {
var_dump($var);
echo '<br>';
}
// }
if ($var === false) {
echo 'It is FALSE!';

@ -11,6 +11,14 @@ declare(strict_types=1);
namespace CodeHydrater;
final class html {
const ECHO = true;
public static function do_echo(?string $input, bool $echo = self::ECHO): ?string {
if ($echo) {
echo $input;
return null;
}
return $input;
}
/**
* Purpose: To output all HTML select (drop down) option values.
@ -53,6 +61,25 @@ final class html {
return $values;
}
private static function make_headers_on_table(array $header_fields, array $options = []) {
$echo = $options['echo'] ?? self::ECHO;
$nl = PHP_EOL;
$default_table_setup = "border='1' cellpadding='1' cellspacing='1'";
$table = $options['table_setup'] ?? $default_table_setup;
$escape = $options['escape'] ?? true;
$th = $options['th'] ?? "";
$th_class = (! empty($th)) ? " class=\"$th\" " : "";
$ret = self::do_echo("{$nl}<table {$table}>{$nl}", $echo);
$ret .= self::do_echo("\t<tr>{$nl}", $echo);
foreach($header_fields as $header_field) {
$field = ($escape) ? \CodeHydrater\bootstrap\safer_io::h($header_field) : $header_field;
$ret .= self::do_echo("\t\t<th{$th_class}>{$field}</th>{$nl}", $echo);
}
$ret .= self::do_echo("\t</tr>{$nl}", $echo);
return $ret;
}
/**
* Used by Memory_Usage script.
* Displays a table from input arrays -- fields
@ -64,70 +91,118 @@ final class html {
public static function show_table(
array $header_fields,
array $fields,
bool $escape = true
): void {
$nl = PHP_EOL;
echo "{$nl}<table border='1' cellpadding='1' cellspacing='1'>{$nl}";
echo "\t<tr>{$nl}";
foreach($header_fields as $header_field) {
$field = ($escape) ? \CodeHydrater\bootstrap\safer_io::h($header_field) : $header_field;
echo "\t\t<th>{$field}</th>{$nl}";
}
echo "\t</tr>{$nl}";
array $options = []
): ?string {
$echo = $options['echo'] ?? self::ECHO;
$nl = PHP_EOL;
$ret = self::make_headers_on_table($header_fields, $options);
$escape = $options['escape'] ?? true;
foreach($fields as $field) {
echo "\t<tr>{$nl}";
$ret .= self::do_echo("\t<tr>{$nl}", $echo);
foreach($field as $td) {
$cell = ($escape) ? \CodeHydrater\bootstrap\safer_io::h($td) : $td;
echo "\t\t<td>{$cell}</td>{$nl}";
$ret .= self::do_echo("\t\t<td>{$cell}</td>{$nl}", $echo);
}
echo "\t</tr>{$nl}";
$ret .= self::do_echo("\t</tr>{$nl}", $echo);
}
echo "</table>{$nl}";
$ret .= self::do_echo("</table>{$nl}", $echo);
return $ret;
}
/**
* Generators use Memory in an Effient way!!!! So use this.
* @param array $header_fields
* @param array $db_field_names
* @param \Iterator $records
* @param bool $escape
* @return void
*/
public static function show_table_from_generator(
array $header_fields,
\Iterator $records,
bool $escape = true
): void {
$nl = PHP_EOL;
echo "{$nl}<table border='1' cellpadding='1' cellspacing='1'>{$nl}";
echo "\t<tr>{$nl}";
foreach($header_fields as $header_field) {
$field = ($escape) ? \bs_tts\safer_io::h($header_field) : $header_field;
echo "\t\t<th>{$field}</th>{$nl}";
}
echo "\t</tr>{$nl}";
foreach($records as $record) {
echo "\t<tr>{$nl}";
private static function make_cell_table_helper_for_IO(\Iterator $record, array $options = []): array {
$echo = $options['echo'] ?? self::ECHO;
$escape = $options['escape'] ?? true;
$cell_class = $options['cell_class'] ?? "";
$row_class = $options['row_class'] ?? "";
$cell_class_tag = (! empty($cell_class)) ? " class=\"$cell_class\" " : "";
$row_class_tag = (! empty($row_class)) ? " class=\"$row_class\" " : "";
$nl = PHP_EOL;
$ret = self::do_echo("\t<tr{$row_class_tag}>{$nl}", $echo);
$errors = [];
foreach($record as $html) {
$key = $html['name'] ?? "";
$value = $html['html'];
if (\CodeHydrater\common::get_count($html['errors'])) {
$errors[$key] = $html['errors'][$key];
}
$td = $value ?? "";
if (is_string($td)) {
$cell = ($escape) ? \CodeHydrater\bootstrap\safer_io::h($td) : $td;
} else {
$cell = (string) $td;
}
$ret .= self::do_echo("\t\t<td{$cell_class_tag}>{$cell}</td>{$nl}", $echo);
}
$ret .= self::do_echo("\t</tr>{$nl}", $echo);
return ['errors' => $errors, 'data' => $ret];
}
private static function make_cell_table_helper(array $record, array $options = []): ?string {
$echo = $options['echo'] ?? self::ECHO;
$escape = $options['escape'] ?? true;
$cell_class = $options['cell_class'] ?? "";
$row_class = $options['row_class'] ?? "";
$cell_class_tag = (! empty($cell_class)) ? " class=\"$cell_class\" " : "";
$row_class_tag = (! empty($row_class)) ? " class=\"$row_class\" " : "";
$nl = PHP_EOL;
$ret = self::do_echo("\t<tr{$row_class_tag}>{$nl}", $echo);
foreach($record as $key => $value) {
if (! is_string($key)) {
continue; // Remove Duplicate records
}
$td = $value ?? "";
if (is_string($td)) {
$cell = ($escape) ? \bs_tts\safer_io::h($td) : $td;
$cell = ($escape) ? \CodeHydrater\bootstrap\safer_io::h($td) : $td;
} else {
$cell = (string) $td;
}
echo "\t\t<td>{$cell}</td>{$nl}";
$ret .= self::do_echo("\t\t<td{$cell_class_tag}>{$cell}</td>{$nl}", $echo);
}
echo "\t</tr>{$nl}";
$ret .= self::do_echo("\t</tr>{$nl}", $echo);
return $ret;
}
/**
* Generators use Memory in an Effient way!!!! So use this.
* @param array $header_fields
* @param array $db_field_names
* @param $records, note: \Iterator not always can be used here
* @param bool $escape
* @return void
*/
public static function show_table_from(
array $header_fields,
$records,
array $options = [],
) {
$echo = $options['echo'] ?? self::ECHO;
$ret = self::make_headers_on_table($header_fields, $options);
switch($options['helper']) {
case "single-row":
$ret .= self::make_cell_table_helper($records, $options);
break;
case "io":
$ret = self::make_cell_table_helper_for_IO($records, $options);
$ret['data'] .= self::do_echo($ret['data'] . "</table>" . PHP_EOL);
return $ret;
default:
foreach($records as $record) {
self::make_cell_table_helper($record, $options);
}
}
echo "</table>{$nl}";
$ret .= self::do_echo("</table>" . PHP_EOL, $echo);
return $ret;
}
public static function show_errors(?array $errors = []): void {
public static function show_errors(
?array $errors = [],
array $options = []
): ?string {
$echo = $options['echo'] ?? self::ECHO;
$ret = "";
if ($errors) {
$message = "Please correct the following errors: ";
$i = 0;
@ -135,7 +210,8 @@ final class html {
$i++;
$message .= "<br/>{$i}) " . $error;
}
echo $message;
$ret .= self::do_echo($message, $echo);
}
return $ret;
}
}

@ -18,6 +18,9 @@ final class memory_usage {
* @retval string of unit size for bytes
*/
public static function convert_bytes($size) {
if ($size === null) {
return 0;
}
$unit=array('b','kb','mb','gb','tb','pb');
return @round($size/pow(1024,($i=floor(log($size,1024)))),2).' '.$unit[$i];
}
@ -27,7 +30,11 @@ final class memory_usage {
* @param bool $echo (default of true) - true: displays, false: returns data
*/
public static function get_memory_stats($echo = true) {
global $mem_baseline;
if (defined("Memory_Baseline")) {
$mem_baseline = Memory_Baseline;
} else {
$mem_baseline = 0;
}
$check = common::get_bool(misc::request_var('debug'));
@ -44,27 +51,17 @@ final class memory_usage {
$diff_peak = $peak - $mem_baseline;
$s_diff_peak = self::convert_bytes($diff_peak);
if (! $echo) {
$a_fields['start-up'] = $s_startup_mem;
$a_fields['currently'] = $s_current;
$a_fields['total-diff'] = $s_diff;
$a_fields['peak'] = $s_peak;
$a_fields['total-diff-peak'] = $s_diff_peak;
} else {
$a_headers = array('Memory Item', 'Size');
$a_fields[] = array('On-StartUp', $s_startup_mem);
$a_fields[] = array('Currently', $s_current);
$a_fields[] = array('<b>Total Diff</b>', "<b>{$s_diff}</b>");
$a_fields[] = array('&nbsp;', '&nbsp;');
$a_fields[] = array('PEAK', $s_peak);
$a_fields[] = array('<i>Total Diff PEAK</i>', "<i>{$s_diff_peak}</i>");
}
if ($echo === true) {
html::show_table($a_headers, $a_fields, false);
} else {
return $a_fields;
}
$a_fields[] = array('<i>Total Diff PEAK</i>', "<i>{$s_diff_peak}</i>");
$options['escape'] = false;
$options['echo'] = $echo;
return html::show_table($a_headers, $a_fields, $options);
}
}

@ -53,9 +53,12 @@ trait form_validator {
}
private static function is_required(array $data, string $field): bool {
$r = self::check_if_null_or_false($data, $field);
if (is_bool($r)) return $r;
// Don't use check_if_null_or_false here, it should always fail on false or null!
$r = $data[$field] ?? null;
if ($r === null || $r === false) {
return false; // It's a required field, so fail
}
if (common::get_count($r)) {
return false; // Should not be an array here
}

@ -122,7 +122,7 @@ final class view {
* @param string $render_page
* @return bool was found
*/
public function set_template(string $render_page): bool {
public function set_php_template(string $render_page): bool {
if (! empty($render_page)) {
$render_page = str_replace('.php', '', $render_page);
$templ = "/templates/{$render_page}";

Loading…
Cancel
Save