You've already forked framexEngine-pro
Firts up
This commit is contained in:
44
core/classes/Engine.php
Normal file
44
core/classes/Engine.php
Normal file
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
class Engine
|
||||
{
|
||||
protected $appPath;
|
||||
protected $templatesPath;
|
||||
public $viewPath;
|
||||
|
||||
public function __construct($appPath = APP, $templatesPath = TEMPLATES)
|
||||
{
|
||||
$this->appPath = rtrim($appPath, '/') . '/';
|
||||
$this->templatesPath = rtrim($templatesPath, '/') . '/';
|
||||
|
||||
$this->viewPath = $this->determineViewPath($this->_urlParams());
|
||||
}
|
||||
|
||||
protected function determineViewPath($urlParams): string
|
||||
{
|
||||
$path1 = $this->appPath . "app/views" . $urlParams . '.php';
|
||||
$path2 = $this->appPath . "app/views" . $urlParams . '/index.php';
|
||||
$path3 = $this->appPath . "app/views" . $urlParams . '.md';
|
||||
$path4 = $this->appPath . "app/views" . $urlParams . '/index.md';
|
||||
$path404 = $this->templatesPath . 'error404.php';
|
||||
|
||||
if (file_exists($path1)) {
|
||||
return $path1;
|
||||
} elseif (file_exists($path2)) {
|
||||
return $path2;
|
||||
} elseif (file_exists($path3)) {
|
||||
return $path3;
|
||||
} elseif (file_exists($path4)) {
|
||||
return $path4;
|
||||
} else {
|
||||
return $path404;
|
||||
}
|
||||
}
|
||||
|
||||
private function _urlParams(): string
|
||||
{
|
||||
$serverURL = parse_url($_SERVER["REQUEST_URI"], PHP_URL_PATH);
|
||||
// Normalize URL by removing trailing slashes
|
||||
return rtrim($serverURL, "/");
|
||||
}
|
||||
}
|
||||
49
core/classes/Framex.php
Normal file
49
core/classes/Framex.php
Normal file
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
class Framex extends Engine
|
||||
{
|
||||
private $viewsPath;
|
||||
|
||||
public function __construct($appPath = APP, $templatesPath = TEMPLATES, $viewsPath = VIEWS)
|
||||
{
|
||||
parent::__construct($appPath, $templatesPath);
|
||||
$this->viewsPath = rtrim($viewsPath, '/') . '/';
|
||||
}
|
||||
|
||||
public function renderTemplate(string $file, array $data = []): void
|
||||
{
|
||||
$filePath = $this->appPath . "templates/" . $file . ".php";
|
||||
if (file_exists($filePath)) {
|
||||
if (!empty($data)) {
|
||||
extract($data);
|
||||
}
|
||||
require_once $filePath;
|
||||
} else {
|
||||
// Handle the error appropriately, e.g., log it or throw an exception
|
||||
echo "Template file not found: " . htmlspecialchars($filePath);
|
||||
}
|
||||
}
|
||||
|
||||
public function init(): void
|
||||
{
|
||||
$file = $this->viewPath;
|
||||
$data = [];
|
||||
|
||||
if (!file_exists($file)) {
|
||||
$file = $this->viewsPath . 'templates/404.php';
|
||||
}
|
||||
|
||||
if (strtolower(pathinfo($file, PATHINFO_EXTENSION)) === 'md') {
|
||||
$markdown = file_get_contents($file);
|
||||
$parsedown = new Parsedown();
|
||||
$data['view'] = '<article class="prose prose-shell">' . $parsedown->text($markdown) . '</article>';
|
||||
} else {
|
||||
ob_start();
|
||||
require_once $file;
|
||||
$data['view'] = ob_get_clean();
|
||||
}
|
||||
|
||||
$template = $data['template'] ?? 'main';
|
||||
$this->renderTemplate($template, $data);
|
||||
}
|
||||
}
|
||||
1995
core/classes/Parsedown.php
Normal file
1995
core/classes/Parsedown.php
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user