Reduced memory footprint by removing duplicate data in arrays.

main
Robert 3 years ago
parent 98f34ddd42
commit 2721f8ae64
  1. 2
      src/mockup/go_text_templates/ctrl.txt
  2. 18
      src/mockup/go_text_templates/inputs.txt
  3. 7
      src/mockup/go_text_templates/models.txt
  4. 35
      src/mockup/go_text_templates/outputs.txt
  5. 4
      src/mockup/go_text_templates/views.txt
  6. 8
      src/mockup/inputs/app/home_in.php
  7. 4
      src/mockup/models/app/home_model.php
  8. 33
      src/mockup/outputs/app/home_out.php
  9. 1
      src/mockup/views/default/app/name_demo_post.php

@ -1,5 +1,7 @@
<?php <?php
declare(strict_types=1);
namespace prj\{{.Root}}\controllers\{{.Subfolder}}; namespace prj\{{.Root}}\controllers\{{.Subfolder}};
class {{.File}}_ctrl { class {{.File}}_ctrl {

@ -1,16 +1,19 @@
<?php <?php
declare(strict_types=1);
namespace prj\{{.Root}}\inputs\{{.Subfolder}}; namespace prj\{{.Root}}\inputs\{{.Subfolder}};
use \tts\safer_io as SafeIO;
use \tts\INPUTS; use \tts\INPUTS;
use \tts\FIELD_FILTER; use \tts\FIELD_FILTER;
use \tts\HTML_FLAG; use \tts\HTML_FLAG;
use \tts\DB_FILTER;
class {{.File}}_in { class {{.File}}_in {
public static function {{.Method}}(): array { public static function {{.Method}}(): array {
$safer_data = SafeIO::sanitize([ \tts\safer_io::init_json(); // Staticly set JSON data if any
return [
'first_name' => 'first_name' =>
[ [
'input' => INPUTS::post, 'input' => INPUTS::post,
@ -18,7 +21,9 @@ class {{.File}}_in {
'html' => HTML_FLAG::escape, 'html' => HTML_FLAG::escape,
'rule' => 'required|max: 75', // Others: email, valid_email_domain, 'rule' => 'required|max: 75', // Others: email, valid_email_domain,
// min, max, between, same, secure (password), alphanumeric // min, max, between, same, secure (password), alphanumeric
'message' => ['required' => 'Must fill out first_name!'] 'message' => ['required' => 'Must fill out first_name!'],
'db' => DB_FILTER::OFF, // Should be used on Login Data only
'skip_db' => false // Should not save?
], ],
'last_name' => 'last_name' =>
[ [
@ -26,11 +31,10 @@ class {{.File}}_in {
'field' => FIELD_FILTER::raw_string, 'field' => FIELD_FILTER::raw_string,
'html' => HTML_FLAG::escape, 'html' => HTML_FLAG::escape,
'rule' => 'required|max: 75', 'rule' => 'required|max: 75',
'message' => ['required' => 'Must fill out last_name!'] 'message' => ['required' => 'Must fill out last_name!'],
'db' => DB_FILTER::OFF // Should be used on Login Data only
], ],
]); ];
return $safer_data; // Makes an Array of: fields, html, and errrors
} }
} }

@ -1,10 +1,13 @@
<?php <?php
declare(strict_types=1);
namespace prj\{{.Root}}\models\{{.Subfolder}}; namespace prj\{{.Root}}\models\{{.Subfolder}};
class {{.File}}_model { class {{.File}}_model {
private $dbh; private $dbh;
private $demo_table = "test_names"; private $demo_table = "test_names";
private $db_helper;
public function __construct($dbh) { public function __construct($dbh) {
$this->dbh = $dbh; $this->dbh = $dbh;
@ -42,7 +45,7 @@ class {{.File}}_model {
} }
public function save_{{.Method}}(array $input): bool { public function save_{{.Method}}(array $input): bool {
$set = $this->db_helper->set_members_by_array($input); $set = $this->db_helper->set_members_by_generator($input);
if ($set === false) { if ($set === false) {
return false; // Found Validation issues in input return false; // Found Validation issues in input
} }
@ -64,7 +67,7 @@ class {{.File}}_model {
try { try {
$sql = "SELECT * FROM {$this->demo_table} LIMIT {$number_of_users_to_get}"; $sql = "SELECT * FROM {$this->demo_table} LIMIT {$number_of_users_to_get}";
$result = $this->dbh->query($sql); $result = $this->dbh->query($sql);
return \tts\database\help_load::fetch_lazy($result); return \tts\database\help_load::pdo_fetch_lazy($result);
} catch (\PDOException $e) { } catch (\PDOException $e) {
echo \tts\exceptions\DB_Exception::customMessage($e); echo \tts\exceptions\DB_Exception::customMessage($e);
} }

@ -1,17 +1,37 @@
<?php <?php
declare(strict_types=1);
namespace prj\{{.Root}}\outputs\{{.Subfolder}}; namespace prj\{{.Root}}\outputs\{{.Subfolder}};
use \tts\safer_io as SafeIO;
class {{.File}}_out { class {{.File}}_out {
public static function {{.Method}}(array $input): array { public static function {{.Method}}(array & $input): array {
$first_name = $input['html']['first_name'] ?? "";
$last_name = $input['html']['last_name'] ?? "";
$ret['main'] = ($input['errors']) ? "" : "Hello {$first_name}, {$last_name}!" . PHP_EOL;
$ret['errors'] = $input['errors'];
$ret['model'] = $input['model']; $ret['model'] = $input['model'];
unset($input['model']); // Required to work, as sanitize cannot take an Object
$html_output = [];
$errors = [];
foreach(SafeIO::html_sanitize($input) as $html) {
$key = $html['name'] ?? "";
$html_output[$key] = $html['html'];
if (\tts\common::get_count($html['errors'])) {
$errors[$key] = $html['errors'][$key];
}
}
$age = $html_output['age'] ?? 0;
$first_name = $html_output['first_name'] ?? "Unknown";
$last_name = $html_output['last_name'] ?? "Unknown";
$ret['main'] = (\tts\common::get_count($errors)) ? "" : "Hello {$first_name} {$last_name}, You are {$age} years old!" . PHP_EOL;
$ret['errors'] = $errors;
unset($input); // Free up some space
return $ret; return $ret;
} }
@ -35,7 +55,6 @@ class {{.File}}_out {
} }
\tts\html::show_table_from_generator( \tts\html::show_table_from_generator(
['ID', 'First Name', 'Last Name'], ['ID', 'First Name', 'Last Name'],
['id', 'first_name', 'last_name'],
$input $input
); );
} }

@ -1,3 +1,7 @@
<?php
declare(strict_types=1);
?>
<h2>Sample View file</h2> <h2>Sample View file</h2>
<?php <?php

@ -4,7 +4,6 @@ declare(strict_types=1);
namespace prj\mockup\inputs\app; namespace prj\mockup\inputs\app;
use \tts\safer_io as SafeIO;
use \tts\INPUTS; use \tts\INPUTS;
use \tts\FIELD_FILTER; use \tts\FIELD_FILTER;
use \tts\HTML_FLAG; use \tts\HTML_FLAG;
@ -13,7 +12,8 @@ use \tts\DB_FILTER;
class home_in { class home_in {
public static function name_demo(): array { public static function name_demo(): array {
$safer_data = SafeIO::sanitize([ \tts\safer_io::init_json(); // Staticly set JSON data if any
return [
'first_name' => 'first_name' =>
[ [
'input' => INPUTS::post, // Others: post, json (REQUIRED param) 'input' => INPUTS::post, // Others: post, json (REQUIRED param)
@ -38,9 +38,7 @@ class home_in {
'rule' => 'greater_than: 18', //'number_range: 18, 24' 'rule' => 'greater_than: 18', //'number_range: 18, 24'
'message' => ['greater_than' => 'The %s must be an Adult over %d!'], 'message' => ['greater_than' => 'The %s must be an Adult over %d!'],
], ],
]); ];
return $safer_data; // Makes an Array of: fields, html, and errrors
} }
} }

@ -63,7 +63,7 @@ class home_model {
} }
public function save_new_user(array $input): bool { public function save_new_user(array $input): bool {
$set = $this->db_helper->set_members_by_array($input); $set = $this->db_helper->set_members_by_generator($input);
if ($set === false) { if ($set === false) {
return false; // Found Validation issues in input return false; // Found Validation issues in input
} }
@ -85,7 +85,7 @@ class home_model {
try { try {
$sql = "SELECT * FROM {$this->name_demo_table} LIMIT {$number_of_users_to_get}"; $sql = "SELECT * FROM {$this->name_demo_table} LIMIT {$number_of_users_to_get}";
$result = $this->dbh->query($sql); $result = $this->dbh->query($sql);
return \tts\database\help_load::fetch_lazy($result); return \tts\database\help_load::pdo_fetch_lazy($result);
} catch (\PDOException $e) { } catch (\PDOException $e) {
echo \tts\exceptions\DB_Exception::customMessage($e); echo \tts\exceptions\DB_Exception::customMessage($e);
} }

@ -4,16 +4,34 @@ declare(strict_types=1);
namespace prj\mockup\outputs\app; namespace prj\mockup\outputs\app;
use \tts\safer_io as SafeIO;
class home_out { class home_out {
public static function name_demo(array $input): array { public static function name_demo(array & $input): array {
$age = $input['html']['age'];
$first_name = $input['html']['first_name'] ?? "Unknown";
$last_name = $input['html']['last_name'] ?? "Unknown";
$ret['main'] = ($input['errors']) ? "" : "Hello {$first_name} {$last_name}, You are {$age} years old!" . PHP_EOL;
$ret['errors'] = $input['errors'];
$ret['model'] = $input['model']; $ret['model'] = $input['model'];
unset($input['model']); // Required to work, as sanitize cannot take an Object
$html_output = [];
$errors = [];
foreach(SafeIO::html_sanitize($input) as $html) {
$key = $html['name'] ?? "";
$html_output[$key] = $html['html'];
if (\tts\common::get_count($html['errors'])) {
$errors[$key] = $html['errors'][$key];
}
}
$age = $html_output['age'] ?? 0;
$first_name = $html_output['first_name'] ?? "Unknown";
$last_name = $html_output['last_name'] ?? "Unknown";
$ret['main'] = (\tts\common::get_count($errors)) ? "" : "Hello {$first_name} {$last_name}, You are {$age} years old!" . PHP_EOL;
$ret['errors'] = $errors;
unset($input); // Free up some space
return $ret; return $ret;
} }
@ -37,7 +55,6 @@ class home_out {
} }
\tts\html::show_table_from_generator( \tts\html::show_table_from_generator(
['ID', 'First Name', 'Last Name', 'Age'], ['ID', 'First Name', 'Last Name', 'Age'],
['id', 'first_name', 'last_name', 'age'],
$input $input
); );
} }

@ -1,6 +1,7 @@
<?php <?php
declare(strict_types=1); declare(strict_types=1);
$route_name_demo = "app/home"; $route_name_demo = "app/home";
$method_name_demo = "name_demo"; $method_name_demo = "name_demo";
?> ?>

Loading…
Cancel
Save