diff --git a/controller/exampleController.php b/controller/indexController.php
similarity index 93%
rename from controller/exampleController.php
rename to controller/indexController.php
index f3082d9..a8064fa 100644
--- a/controller/exampleController.php
+++ b/controller/indexController.php
@@ -16,7 +16,7 @@
use Wepesi\Core\Http\Redirect;
use Wepesi\Core\Session;
-class exampleController extends Controller
+class indexController extends Controller
{
public function __construct()
{
diff --git a/router/route.php b/router/route.php
index 4eae98a..d382b9d 100644
--- a/router/route.php
+++ b/router/route.php
@@ -1,6 +1,6 @@
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';
\ No newline at end of file
diff --git a/src/Core/Application.php b/src/Core/Application.php
index 3cf85a0..51b5a25 100644
--- a/src/Core/Application.php
+++ b/src/Core/Application.php
@@ -8,7 +8,7 @@
use Wepesi\Core\Routing\Router;
/**
- * Application root
+ *
*/
class Application
{
@@ -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
*/
@@ -55,7 +59,7 @@ 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';
@@ -63,6 +67,38 @@ public function __construct(string $path, AppConfiguration $config)
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
@@ -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 ;
}
/**
@@ -101,4 +159,4 @@ public function run()
{
$this->router->run();
}
-}
+}
\ No newline at end of file
diff --git a/src/Core/View.php b/src/Core/View.php
index fb997ce..1ade45e 100644
--- a/src/Core/View.php
+++ b/src/Core/View.php
@@ -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;
/**
*
@@ -14,19 +16,19 @@ 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
*/
@@ -34,27 +36,22 @@ class View
/**
* @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();
}
/**
@@ -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);
}
@@ -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);
@@ -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
@@ -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('');
- $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 = '';
- 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('');
+ $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 = '';
+ 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()));
}
/**
@@ -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;
}
/**
@@ -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 = '';
+ }
}
\ No newline at end of file