From dbf05766538bc62cc57b717ee4e51700763d8f7b Mon Sep 17 00:00:00 2001 From: bim-g Date: Sat, 21 Sep 2024 23:15:57 +0200 Subject: [PATCH 1/2] [UPD] remove useless class --- src/Core/Views/Provider/Contract/ViewsContract.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/Core/Views/Provider/Contract/ViewsContract.php b/src/Core/Views/Provider/Contract/ViewsContract.php index 540aa88..6ae2887 100644 --- a/src/Core/Views/Provider/Contract/ViewsContract.php +++ b/src/Core/Views/Provider/Contract/ViewsContract.php @@ -5,8 +5,6 @@ namespace Wepesi\Core\Views\Provider\Contract; -use Wepesi\Core\MetaData; - /** * */ From 821f687c8ed2a36d73d37e2edaadbc197c5e6d11 Mon Sep 17 00:00:00 2001 From: bim-g Date: Sun, 22 Sep 2024 01:06:01 +0200 Subject: [PATCH 2/2] [UPD] update view component and setup view generics --- controller/indexController.php | 8 +- .../Views/Provider/Contract/ViewsContract.php | 41 +++++-- .../Views/Provider/ViewBuilderProvider.php | 78 ++++++++++-- src/Core/Views/View.php | 112 ++++++------------ 4 files changed, 140 insertions(+), 99 deletions(-) diff --git a/controller/indexController.php b/controller/indexController.php index 62fded5..3e4566c 100644 --- a/controller/indexController.php +++ b/controller/indexController.php @@ -24,17 +24,17 @@ public function __construct() } /** - * @return void + * */ - function home() + function home(): void { $this->view->display('home'); } /** - * @return void + * */ - function changeLang() + function changeLang(): void { Session::put('lang', Input::get("lang")); Redirect::to("/"); diff --git a/src/Core/Views/Provider/Contract/ViewsContract.php b/src/Core/Views/Provider/Contract/ViewsContract.php index 6ae2887..730a284 100644 --- a/src/Core/Views/Provider/Contract/ViewsContract.php +++ b/src/Core/Views/Provider/Contract/ViewsContract.php @@ -6,33 +6,54 @@ namespace Wepesi\Core\Views\Provider\Contract; /** - * + * @template T */ interface ViewsContract { /** * Setup new folder location for layout template - * @param string $folder_name - * @return mixed + * @var class-string $folder_name */ - public function setFolder(string $folder_name); + public function setFolder(string $folder_name): void; /** * render html content - * @param string $view - * @return mixed + * @var class-string $view */ public function display(string $view); /** - * @param string $variable - * @param mixed $value - * @return mixed + * assign variables data to be displayed on file_page + * @var class-string $variable + * @var $value */ public function assign(string $variable, $value); /** + * List all data assigned before being displayed * @return array */ public function getAssignData(): array; -} \ No newline at end of file + + /** + * Render html string text + * @var class-string $html + */ + public function renderHTML(string $html): void; + + /** + * @var class-string $template + */ + public function setLayout(string $template): void; + + /** + * provide layout content name + * @var class-string $layout_name + */ + public function setLayoutContent(string $layout_name): void; + + /** + * reset view to default configuration + */ + public function flush(): void; +} diff --git a/src/Core/Views/Provider/ViewBuilderProvider.php b/src/Core/Views/Provider/ViewBuilderProvider.php index 600a4e3..03923c8 100644 --- a/src/Core/Views/Provider/ViewBuilderProvider.php +++ b/src/Core/Views/Provider/ViewBuilderProvider.php @@ -5,22 +5,44 @@ namespace Wepesi\Core\Views\Provider; +use Wepesi\Core\Application; use Wepesi\Core\Escape; +use Wepesi\Core\Views\Provider\Contract\ViewsContract; -class ViewBuilderProvider implements Contract\ViewsContract +/** + * @template T + * @template-implements ViewsContract + */ +abstract class ViewBuilderProvider implements ViewsContract { + /** + * @var T[] + */ protected array $data = []; + /** + * $reset + * @var bool + */ + protected bool $reset = false; + /** + * @var string + */ + protected string $layout = ''; + /** + * @var string + */ + protected string $layout_content = ''; /** * @var string */ protected string $folder_name = ''; /** - * @param string $folder_name - * @return void + * @param class-string $folder_name + * */ - public function setFolder(string $folder_name) + public function setFolder(string $folder_name): void { $this->folder_name = Escape::addSlashes($folder_name); } @@ -28,18 +50,14 @@ public function setFolder(string $folder_name) /** * @inheritDoc */ - public function display(string $view) - { - // TODO: Implement display() method. - } - + abstract public function display(string $view); /** * assign variables data to be displayed on file_page * - * @param string $variable + * @param class-string $variable * @param $value */ - public function assign(string $variable, $value) + public function assign(string $variable, $value): void { $this->data[$variable] = $value; } @@ -52,4 +70,42 @@ public function getAssignData(): array { return $this->data; } + + /** + * render html string text + * @param class-string $html * + */ + public function renderHTML(string $html): void + { + print($html); + } + + /** + * you should provide the extension of your file, + * in another case the file will be missing + * @param class-string $template + */ + public function setLayout(string $template): void + { + $this->layout = Application::$ROOT_DIR . '/views' . $template; + } + + /** + * @param class-string $layout_name + * + */ + public function setLayoutContent(string $layout_name): void + { + $this->layout_content = $layout_name; + } + + /** + * Reset view to default configuration * + */ + public function flush(): void + { + $this->reset = true; + $this->layout = ''; + $this->folder_name = ''; + } } \ No newline at end of file diff --git a/src/Core/Views/View.php b/src/Core/Views/View.php index d45798a..92a7763 100644 --- a/src/Core/Views/View.php +++ b/src/Core/Views/View.php @@ -17,16 +17,13 @@ use function libxml_use_internal_errors; /** - * + * @template T + * @template-extends ViewBuilderProvider */ class View extends ViewBuilderProvider { /** - * - */ - private bool $reset = false; - /** - * @var array + * @var T[] */ private static array $js_link_path = []; /** @@ -38,35 +35,25 @@ class View extends ViewBuilderProvider */ private static ?string $metadata = null; - /** - * @var string - */ - private string $layout = ''; - /** - * @var string|null - */ - private string $layout_content = ''; /** * Provide js link to be set up on the page header - * @param string $path file path - * @param bool $external set to true for an external link + * @param class-string $path_file * @return void */ - public static function setJsToHead(string $path, bool $external = false) + public static function setJsToHead(string $path_file): void { - self::$js_link_path[] = $path; + self::$js_link_path[] = $path_file; } /** - * Provide css link path to set up stylesheet on the page header + * Provide CSS link path to set up stylesheet on the page header * * @param string $path - * * @return void - * add + * */ - public static function setStyleToHead(string $path) + public static function setStyleToHead(string $path): void { self::$style_link_path[] = $path; } @@ -76,7 +63,7 @@ public static function setStyleToHead(string $path) * * @return void */ - public static function setMetaData(MetaData $metadata) + public static function setMetaData(MetaData $metadata): void { self::$metadata = $metadata->build(); } @@ -84,9 +71,9 @@ public static function setMetaData(MetaData $metadata) /** * call this method to display file content * - * @param string $view + * @param class-string $view */ - public function display(string $view) + public function display(string $view): void { $view_file = $this->buildFilePath($view); $render = $this->renderView($view_file); @@ -100,7 +87,7 @@ public function display(string $view) } /** - * @param string $file_name + * @param class-string $file_name * * @return string|null */ @@ -113,8 +100,8 @@ private function buildFilePath(string $file_name): ?string } /** - * Render content from prided file - * @param string $view + * Render content from a provided file + * @param class-string $view * * @return false|string|void */ @@ -133,17 +120,17 @@ protected function renderView(string $view) } /** - * @param string $file_name + * @param class-string $file_name * * @return void */ - private function renderNotDefined(string $file_name) + private function renderNotDefined(string $file_name): void { Response::send(['exception' => "$file_name file path is not correct."],404); } /** - * @param string $view + * @param class-string $view * * @return false|string|void */ @@ -168,26 +155,19 @@ protected function renderLayout(string $view) } } + /** - * render html string text - * @param string $html - * @return void - */ - public function renderHTML(string $html) { - print($html); - } - /** + * this module will help to create a dom document to add + * asset js | CSS on the head of your file. * @param $html * * @return void - * this module will help to create a dom document to add - * asset js | css on the head of your file. - */ - private function buildAssetHead($html) + */ + private function buildAssetHead($html): void { try { if (!$html) { - throw new Exception('Unable to render empty data'); + throw new Exception('Unable to render empty file'); } $dom = new DOMDocument(); libxml_use_internal_errors(true); @@ -195,20 +175,20 @@ private function buildAssetHead($html) mb_convert_encoding($html, 'HTML-ENTITIES', 'UTF-8'), LIBXML_HTML_NODEFDTD | LIBXML_HTML_NOIMPLIED ); - // $errors = libxml_get_errors(); +// $errors = libxml_get_errors(); libxml_clear_errors(); $xpath = new DOMXPath($dom); $head = $xpath->query('//head/title'); $template = $dom->createDocumentFragment(); // add a style link to the head tag of the page - foreach (self::$style_link_path as $k => $v) { - $template->appendXML(''); + foreach (self::$style_link_path as $key => $value) { + $src = $this->generateStyleLink($value); + $template->appendXML($src); $head[0]->parentNode->insertbefore($template, $head[0]->nextSibling); } // add a script link to the head of the page - foreach (self::$js_link_path as $k => $v) { - $link = $v['link']; - $src = ''; + foreach (self::$js_link_path as $key => $value) { + $src = $this->generateStyleLink($value['link']); $template->appendXML($src); $head[0]->parentNode->insertbefore($template, $head[0]->nextSibling); } @@ -225,37 +205,21 @@ private function buildAssetHead($html) } } - - - /** - * you should provide the extension of your file, - * in another case the file will be missing - * @param string $template - * - * @return void + * @param class-string $path + * @return T */ - public function setLayout(string $template) + private function generateJSLink(string $path): string { - $this->layout = Application::$ROOT_DIR . '/views' . $template; + return ''; } /** - * @param string $layout_name - * @return void + * @param class-string $path + * @return T */ - public function setLayoutContent(string $layout_name) - { - $this->layout_content = $layout_name; + private function generateStyleLink(string $path): string { + return ''; } - /** - * Reset view to default configuration - * @return void - */ - public function flush(){ - $this->reset = true; - $this->layout = ''; - $this->folder_name = ''; - } }