Compare commits
10 Commits
6e6410f695
...
v1.0.4
| Author | SHA1 | Date | |
|---|---|---|---|
| 75e1f7979c | |||
| c8d150d752 | |||
| b7ee83e82a | |||
| d923c788f9 | |||
| 000ed4c093 | |||
| 2dc9c8df1a | |||
| 60f3508b36 | |||
| bd8c4f42eb | |||
| 7b807c015f | |||
| c6caa9e418 |
@@ -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
@@ -1,6 +1,6 @@
|
||||
<?php
|
||||
|
||||
namespace Mdesouza\Utils\DB;
|
||||
namespace Mdesouza\Utils;
|
||||
|
||||
/**
|
||||
* @file
|
||||
|
||||
+1
-3
@@ -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.
|
||||
*/
|
||||
|
||||
@@ -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>";
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
+45
-7
@@ -21,10 +21,19 @@ class TwigRender {
|
||||
*/
|
||||
private $template;
|
||||
|
||||
/**
|
||||
* Templates paths.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $templatesPath;
|
||||
|
||||
/**
|
||||
* Templates paths
|
||||
* Template variable name.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $templates_path;
|
||||
private $tname;
|
||||
|
||||
/**
|
||||
* Template data.
|
||||
@@ -36,10 +45,14 @@ class TwigRender {
|
||||
/**
|
||||
* Constructor.
|
||||
*/
|
||||
public function __construct(string $template, array $templates_path, array $data = []) {
|
||||
public function __construct(string $template, string $templates_path, array $data = [], string $tname = 'data') {
|
||||
$this->template = $template;
|
||||
$this->templates_path = $templates_path;
|
||||
$this->templatesPath = $templates_path;
|
||||
$this->data = $data;
|
||||
if (!isset($data['host'])) {
|
||||
$this->data['host'] = $_SERVER['HTTP_HOST'];
|
||||
}
|
||||
$this->tname = $tname;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -47,14 +60,15 @@ class TwigRender {
|
||||
*/
|
||||
public function render() : string {
|
||||
|
||||
$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());
|
||||
$html = $twig->render($this->template, ['data' => $this->data]);
|
||||
$html = $twig->render($this->template, [$this->tname => $this->data]);
|
||||
}
|
||||
catch (LoaderError $e) {
|
||||
$html .= "<br/><pre>" . print_r($e->getMessage());
|
||||
@@ -72,4 +86,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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user