Code improvements.

This commit is contained in:
Marcos F. de Souza 2025-09-25 11:47:43 -04:00
parent 60f3508b36
commit 2dc9c8df1a
2 changed files with 29 additions and 5 deletions

View File

@ -7,8 +7,6 @@ namespace Mdesouza\Utils;
* A database class around the PDO object.
*/
include_once 'db.inc.php';
/**
* A mysql class around the PDO object.
*/

View File

@ -24,7 +24,7 @@ class TwigRender {
/**
* Templates paths.
*
* @var array
* @var string
*/
private $templatesPath;
@ -38,7 +38,7 @@ 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->templatesPath = $templates_path;
$this->data = $data;
@ -50,9 +50,11 @@ class TwigRender {
public function render() : string {
$this->data['host'] = $_SERVER['HTTP_HOST'];
$html = "";
try {
$loader = new FilesystemLoader($this->templatesPath);
$loader = new FilesystemLoader([]);
$loader = $this->addPaths($loader);
$twig = new Environment($loader, ['debug' => TRUE]);
$twig->addExtension(new DebugExtension());
$twig->addExtension(new MFDESTwigExtension());
@ -74,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;
}
}