Skip to content

Commit

Permalink
Merge pull request #167 from kivudesign/version_boss
Browse files Browse the repository at this point in the history
[ENH] add dynamic use off many layout
  • Loading branch information
bim-g authored Feb 25, 2024
2 parents 70617ee + 1604295 commit 1985b3b
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
use Wepesi\Core\Http\Redirect;
use Wepesi\Core\Session;

class exampleController extends Controller
class indexController extends Controller
{
public function __construct()
{
Expand Down
6 changes: 3 additions & 3 deletions router/route.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

use Wepesi\Controller\exampleController;
use Wepesi\Controller\indexController;
use Wepesi\Core\View;
use Wepesi\Middleware\Validation\exampleValidation;

Expand All @@ -9,9 +9,9 @@
$router->get('/', function () {
(new View)->display('/home');
});
$router->get('/home', [\Wepesi\Controller\exampleController::class,'home']);
$router->get('/home', [\Wepesi\Controller\indexController::class,'home']);
//
$router->post('/changelang', [exampleController::class, 'changeLang'])
$router->post('/changelang', [indexController::class, 'changeLang'])
->middleware([exampleValidation::class, 'changeLang']);

include \Wepesi\Core\Application::$ROOT_DIR . './router/api.php';
72 changes: 65 additions & 7 deletions src/Core/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use Wepesi\Core\Routing\Router;

/**
* Application root
*
*/
class Application
{
Expand All @@ -32,11 +32,15 @@ class Application
/**
* @var string
*/
public static string $LAYOUT_CONTENT;
private static string $LAYOUT_CONTENT = 'layout_content';
/**
* @var string|null
* @var string
*/
private static string $LAYOUT = '';
/**
* @var string
*/
public static ?string $LAYOUT = null;
private static string $VIEW_FOLDER = '';
/**
* @var array
*/
Expand All @@ -55,14 +59,46 @@ public function __construct(string $path, AppConfiguration $config)
{

self::$ROOT_DIR = str_replace("\\", '/', $path);
self::$APP_DOMAIN = serverDomain()->domain;
self::$APP_DOMAIN = $this->domainSetup()->app_domain;
self::$params = $config->generate();
self::$APP_TEMPLATE = self::$params['app_template'] ?? null;
self::$APP_LANG = self::$params['lang'] ?? 'fr';
$this->router = new Router();
self::$LAYOUT_CONTENT = 'layout_content';
}

/**
* @return object
*/
private function domainSetup(): object
{
$server_name = $_SERVER['SERVER_NAME'];
$protocol = strtolower(explode('/', $_SERVER['SERVER_PROTOCOL'])[0]);
$domain = self::getDomainIp() === '127.0.0.1' ? "$protocol://$server_name" : $server_name;
return (object)[
'server_name' => $server_name,
'protocol' => $protocol,
'app_domain' => $domain,
];
}

/**
* use method to get domain ip
* @return string
*/
public static function getDomainIp() : string
{
$ip = $_SERVER['REMOTE_ADDR'];

if (! empty($_SERVER['HTTP_CLIENT_IP'])) {
$ip = $_SERVER['HTTP_CLIENT_IP'];
} elseif (! empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
} elseif ($ip == '::1') {
$ip = gethostbyname(getHostName());
}
return $ip;
}
/**
* simple builtin dumper for dump data
* @param $ex
Expand All @@ -83,7 +119,29 @@ public static function dumper($ex)
*/
public static function setLayout(string $layout)
{
self::$LAYOUT = self::$ROOT_DIR . '/views/' . $layout;
self::$LAYOUT = self::$ROOT_DIR.'/views/'.$layout;
}
public static function setLayoutContent(string $layout_name)
{
self::$LAYOUT_CONTENT = $layout_name;
}

public static function setViewFolder(string $folder_name)
{
self::$VIEW_FOLDER = $folder_name;
}
public static function getLayout()
{
return self::$LAYOUT ;
}
public static function getLayoutContent()
{
return self::$LAYOUT_CONTENT ;
}

public static function getViewFolder()
{
return self::$VIEW_FOLDER ;
}

/**
Expand All @@ -101,4 +159,4 @@ public function run()
{
$this->router->run();
}
}
}
124 changes: 71 additions & 53 deletions src/Core/View.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

namespace Wepesi\Core;

use DOMDocument;
use \DOMDocument;
use \DOMXPath;
use Exception;
use Wepesi\Core\Http\Response;
use function libxml_clear_errors;
use function libxml_use_internal_errors;

/**
*
Expand All @@ -14,47 +16,42 @@ class View
/**
*
*/
const ERROR_VIEW = '';
private bool $reset = false;
/**
* @var array
*/
private static array $jslink;
private static array $jslink = [];
/**
* @var array
*/
private static array $stylelink;
private static array $stylelink = [];
/**
* @var string|null
*/
private static ?string $metadata;
private static ?string $metadata = null;
/**
* @var array
*/
private array $data = [];
/**
* @var string
*/
private string $folder_name;
private string $folder_name = '';
/**
* @var string|null
* @var string
*/
private ?string $layout;
private string $layout = '';
/**
* @var string|null
*/
private ?string $layout_content;
private string $layout_content = '';

/**
* @param string|null $folder_name
*
*/
function __construct(?string $folder_name = '/')
public function __construct()
{
$this->folder_name = Escape::addSlaches($folder_name);
$this->layout = Application::$LAYOUT;
self::$jslink = [];
self::$stylelink = [];
self::$metadata = null;
$this->layout_content = Application::$LAYOUT_CONTENT;
$this->folder_name = Application::getViewFolder();
}

/**
Expand Down Expand Up @@ -95,7 +92,7 @@ public static function setMetaData(MetaData $metadata)
* @param string $folder_name
* @return void
*/
public function setFolder(string $folder_name = '/')
public function setFolder(string $folder_name)
{
$this->folder_name = Escape::addSlaches($folder_name);
}
Expand All @@ -109,7 +106,10 @@ function display(string $view)
{
$view_file = $this->buildFilePath($view);
$render = $this->renderView($view_file);
if ($this->layout) {
if ($this->layout === '' && !$this->reset) {
$this->layout = Application::getLayout();
}
if ($this->layout !== '') {
$render = $this->renderLayout($render);
}
$this->buildAssetHead($render);
Expand Down Expand Up @@ -164,6 +164,9 @@ private function renderNotDefined(string $file_name)
*/
protected function renderLayout(string $view)
{
if ($this->layout_content === '') {
$this->layout_content = Application::getLayoutContent();
}
if ($this->layout && is_file($this->layout)) {
$layout_data = $this->data;
// set the layout content variable to be used on the layout template
Expand All @@ -189,39 +192,43 @@ protected function renderLayout(string $view)
*/
private function buildAssetHead($html)
{
if (!$html) {
throw new Exception('Unable to render empty data');
}
$dom = new DOMDocument();
libxml_use_internal_errors(true);
$dom->loadHTML(
mb_convert_encoding($html, 'HTML-ENTITIES', 'UTF-8'),
LIBXML_HTML_NODEFDTD | LIBXML_HTML_NOIMPLIED
);
// $errors = libxml_get_errors();
libxml_clear_errors();
$xpath = new \DOMXPath($dom);
$head = $xpath->query('//head/title');
$template = $dom->createDocumentFragment();
// add style link to the head tag of the page
foreach (self::$stylelink as $k => $v) {
$template->appendXML('<link rel="stylesheet" type="text/css" href="' . $v . '">');
$head[0]->parentNode->insertbefore($template, $head[0]->nextSibling);
}
// add script link to the head of the page
foreach (self::$jslink as $k => $v) {
$link = $v['link'];
$src = '<script src="' . $link . '" type="text/javascript"></script>';
if (!$v['external']) $src = Bundles::insertJS($link, false, true);
$template->appendXML($src);
$head[0]->parentNode->insertbefore($template, $head[0]->nextSibling);
}
// add metadata to the head of the page
if (self::$metadata) {
$template->appendXML(self::$metadata);
$head[0]->parentNode->insertbefore($template, $head[0]->nextSibling);
try {
if (!$html) {
throw new Exception('Unable to render empty data');
}
$dom = new DOMDocument();
libxml_use_internal_errors(true);
$dom->loadHTML(
mb_convert_encoding($html, 'HTML-ENTITIES', 'UTF-8'),
LIBXML_HTML_NODEFDTD | LIBXML_HTML_NOIMPLIED
);
// $errors = libxml_get_errors();
libxml_clear_errors();
$xpath = new DOMXPath($dom);
$head = $xpath->query('//head/title');
$template = $dom->createDocumentFragment();
// add style link to the head tag of the page
foreach (self::$stylelink as $k => $v) {
$template->appendXML('<link rel="stylesheet" type="text/css" href="' . $v . '">');
$head[0]->parentNode->insertbefore($template, $head[0]->nextSibling);
}
// add script link to the head of the page
foreach (self::$jslink as $k => $v) {
$link = $v['link'];
$src = '<script src="' . $link . '" type="text/javascript"></script>';
if (!$v['external']) $src = Bundles::insertJS($link, false, true);
$template->appendXML($src);
$head[0]->parentNode->insertbefore($template, $head[0]->nextSibling);
}
// add metadata to the head of the page
if (self::$metadata) {
$template->appendXML(self::$metadata);
$head[0]->parentNode->insertbefore($template, $head[0]->nextSibling);
}
print(html_entity_decode($dom->saveHTML()));
} catch (Exception $ex) {
print_r(['exception' => $ex->getMessage()]);
}
print(html_entity_decode($dom->saveHTML()));
}

/**
Expand All @@ -236,13 +243,15 @@ public function assign(string $variable, $value)
}

/**
* you should provide the extension of your file,
* in another case the file will be missing
* @param string $template
*
* @return void
*/
public function setLayout(string $template)
{
$this->layout = Application::$ROOT_DIR . '/views/' . $template;
$this->layout = Application::$ROOT_DIR . '/views' . $template;
}

/**
Expand All @@ -253,4 +262,13 @@ public function setLayoutContent(string $layout_name)
{
$this->layout_content = $layout_name;
}

/**
* @return void
*/
public function resetConfig(){
$this->reset = true;
$this->layout = '';
$this->folder_name = '';
}
}

0 comments on commit 1985b3b

Please sign in to comment.