6 Commits

Author SHA1 Message Date
mdesouza 000ed4c093 Release-v1.0.3 2025-09-25 11:52:03 -04:00
mdesouza 2dc9c8df1a Code improvements. 2025-09-25 11:47:43 -04:00
mdesouza 60f3508b36 Merge pull request 'Release-v1.0.2' (#3) from release-v1.0.2 into master
Reviewed-on: #3
2025-09-24 16:25:21 -04:00
mdesouza bd8c4f42eb Release-v1.0.2 2025-09-24 16:24:24 -04:00
mdesouza 7b807c015f fix paths. 2025-09-24 16:22:53 -04:00
mdesouza c6caa9e418 add some more utilities. 2025-09-24 16:07:50 -04:00
6 changed files with 146 additions and 13 deletions
+3 -1
View File
@@ -1,3 +1,5 @@
# mdesouza
Utils classes by mdesouza3@gmail.com
Utils classes
Release-v1.0.3
+57
View File
@@ -0,0 +1,57 @@
<?php
namespace Mdesouza\Utils;
/**
* @file
* Implementation of cookies.
*/
/**
* Class implementation for cookies.
*/
class Cookie {
/**
* Content of $_COOKIE array.
*
* @var array
*/
private $cookie = [];
/**
* Constructor.
*/
public function __construct() {
$this->cookie = $_COOKIE;
}
/**
* Checks if a cookie exists.
*/
public function cookieExists($name) {
return isset($this->cookie[$name]);
}
/**
* Get value of a cookie.
*/
public function getCookie($name) : string {
return $this->cookieExists($name) ? $this->cookie[$name] : "";
}
/**
* Set a cookie.
*/
public function setCookie(array $options) {
setcookie(
$options['name'],
$options['value'],
$options['expires'],
$options['path'],
$options['domain'],
$options['secure'],
$options['httponly']
);
}
}
+1 -1
View File
@@ -1,6 +1,6 @@
<?php
namespace Mdesouza\Utils\DB;
namespace Mdesouza\Utils;
/**
* @file
+1 -3
View File
@@ -1,14 +1,12 @@
<?php
namespace Mdesouza\Utils\DB;
namespace Mdesouza\Utils;
/**
* @file
* A database class around the PDO object.
*/
include_once 'db.inc.php';
/**
* A mysql class around the PDO object.
*/
+48
View File
@@ -0,0 +1,48 @@
<?php
namespace Mdesouza\Utils;
/**
* Show variables and exit or continue.
*/
class ShowVars {
/**
* Print vars and exit.
*/
public static function printAndExit($data, $trace = TRUE) {
$bt = debug_backtrace();
echo "<pre>";
if (TRUE == $trace) {
echo "Trace: " . PHP_EOL;
foreach ($bt as $atrace) {
echo "$atrace[file]:$atrace[line]" . PHP_EOL;
}
echo PHP_EOL;
}
print_r($data);
echo "</pre>";
die("");
}
/**
* Print vars and continue.
*/
public static function printAndContinue($data, $trace = TRUE) {
$bt = debug_backtrace();
echo "<pre>";
if (TRUE == $trace) {
echo "Trace: " . PHP_EOL;
foreach ($bt as $atrace) {
echo "$atrace[file]:$atrace[line]" . PHP_EOL;
}
echo PHP_EOL;
}
print_r($data);
echo "</pre>";
}
}
+36 -8
View File
@@ -21,10 +21,12 @@ class TwigRender {
*/
private $template;
/**
* Templates paths
*/
private $templates_path;
/**
* Templates paths.
*
* @var string
*/
private $templatesPath;
/**
* Template data.
@@ -36,9 +38,9 @@ class TwigRender {
/**
* Constructor.
*/
public function __construct(string $template, array $templates_path, array $data = []) {
public function __construct(string $template, string $templates_path, array $data = []) {
$this->template = $template;
$this->templates_path = $templates_path;
$this->templatesPath = $templates_path;
$this->data = $data;
}
@@ -47,10 +49,12 @@ class TwigRender {
*/
public function render() : string {
$data['host'] = $_SERVER['HTTP_HOST'];
$this->data['host'] = $_SERVER['HTTP_HOST'];
$html = "";
try {
$loader = new FilesystemLoader($this->templates_path);
$loader = new FilesystemLoader([]);
$loader = $this->addPaths($loader);
$twig = new Environment($loader, ['debug' => TRUE]);
$twig->addExtension(new DebugExtension());
$twig->addExtension(new MFDESTwigExtension());
@@ -72,4 +76,28 @@ class TwigRender {
return $html;
}
/**
* Recursively add all directories under the templates base path.
*/
private function addPaths(FilesystemLoader $loader) : FilesystemLoader {
// Add base path.
$loader->addPath($this->templatesPath);
// Create a recursive iterator to find all subdirectories.
$iterator = new \RecursiveIteratorIterator(
new \RecursiveDirectoryIterator($this->templatesPath, \FilesystemIterator::SKIP_DOTS),
\RecursiveIteratorIterator::SELF_FIRST
);
// Add each sub-dir under the base path.
foreach ($iterator as $info) {
if ($info->isDir()) {
$loader->addPath($info->getPathname());
}
}
return $loader;
}
}