Skip to content

Commit

Permalink
[UPD] refactor view module
Browse files Browse the repository at this point in the history
  • Loading branch information
bim-g committed Sep 2, 2024
1 parent db24620 commit e4ee9d0
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 57 deletions.
2 changes: 1 addition & 1 deletion router/route.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

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

$router = $app->router();
Expand Down
7 changes: 5 additions & 2 deletions src/Core/Controller.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
<?php
/*
* Copyright (c) 2023. wepesi dev framework
* Copyright (c) 2023-2024. Wepesi Dev Framework
*/

namespace Wepesi\Core;

use Wepesi\Core\Views\Provider\Contract\ViewsContract;
use Wepesi\Core\Views\View;

/**
*
*/
Expand All @@ -13,7 +16,7 @@ abstract class Controller
/**
* @var View
*/
protected View $view;
protected ViewsContract $view;

/**
*
Expand Down
40 changes: 40 additions & 0 deletions src/Core/Views/Provider/Contract/ViewsContract.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php
/*
* Copyright (c) 2024. Wepesi Dev Framework
*/

namespace Wepesi\Core\Views\Provider\Contract;

use Wepesi\Core\MetaData;

/**
*
*/
interface ViewsContract
{
/**
* Setup new folder location for layout template
* @param string $folder_name
* @return mixed
*/
public function setFolder(string $folder_name);

/**
* render html content
* @param string $view
* @return mixed
*/
public function display(string $view);

/**
* @param string $variable
* @param mixed $value
* @return mixed
*/
public function assign(string $variable, $value);

/**
* @return array
*/
public function getAssignData(): array;
}
55 changes: 55 additions & 0 deletions src/Core/Views/Provider/ViewBuilderProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php
/*
* Copyright (c) 2024. Wepesi Dev Framework
*/

namespace Wepesi\Core\Views\Provider;

use Wepesi\Core\Escape;

class ViewBuilderProvider implements Contract\ViewsContract
{
protected array $data = [];

/**
* @var string
*/
protected string $folder_name = '';

/**
* @param string $folder_name
* @return void
*/
public function setFolder(string $folder_name)
{
$this->folder_name = Escape::addSlashes($folder_name);
}

/**
* @inheritDoc
*/
public function display(string $view)
{
// TODO: Implement display() method.
}

/**
* assign variables data to be displayed on file_page
*
* @param string $variable
* @param $value
*/
public function assign(string $variable, $value)
{
$this->data[$variable] = $value;
}

/**
* List all data assigned before being displayed
* @return array
*/
public function getAssignData(): array
{
return $this->data;
}
}
83 changes: 29 additions & 54 deletions src/Core/View.php → src/Core/Views/View.php
Original file line number Diff line number Diff line change
@@ -1,18 +1,25 @@
<?php
/*
* Copyright (c) 2024. Wepesi Dev Framework
*/

namespace Wepesi\Core;
namespace Wepesi\Core\Views;

use \DOMDocument;
use \DOMXPath;
use DOMDocument;
use DOMXPath;
use Exception;
use Wepesi\Core\Application;
use Wepesi\Core\Escape;
use Wepesi\Core\Http\Response;
use Wepesi\Core\MetaData;
use Wepesi\Core\Views\Provider\ViewBuilderProvider;
use function libxml_clear_errors;
use function libxml_use_internal_errors;

/**
*
*/
class View
class View extends ViewBuilderProvider
{
/**
*
Expand All @@ -21,23 +28,16 @@ class View
/**
* @var array
*/
private static array $jslink = [];
private static array $js_link_path = [];
/**
* @var array
*/
private static array $stylelink = [];
private static array $style_link_path = [];
/**
* @var string|null
*/
private static ?string $metadata = null;
/**
* @var array
*/
private array $data = [];
/**
* @var string
*/
private string $folder_name = '';

/**
* @var string
*/
Expand All @@ -48,27 +48,27 @@ class View
private string $layout_content = '';

/**
* @param string $js_link
*
* 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
* @return void
*/
public static function setJsToHead(string $js_link, bool $external = false)
public static function setJsToHead(string $path, bool $external = false)
{
self::$jslink[] = [
'link' => $js_link,
'external' => $external
];
self::$js_link_path[] = $path;
}

/**
* @param string $style_link
* Provide css link path to set up stylesheet on the page header
*
* @param string $path
*
* @return void
* add
*/
public static function setStyleToHead(string $style_link)
public static function setStyleToHead(string $path)
{
self::$stylelink[] = $style_link;
self::$style_link_path[] = $path;
}

/**
Expand All @@ -81,15 +81,6 @@ public static function setMetaData(MetaData $metadata)
self::$metadata = $metadata->build();
}

/**
* @param string $folder_name
* @return void
*/
public function setFolder(string $folder_name)
{
$this->folder_name = Escape::addSlashes($folder_name);
}

/**
* call this method to display file content
*
Expand Down Expand Up @@ -122,6 +113,7 @@ private function buildFilePath(string $file_name): ?string
}

/**
* Render content from prided file
* @param string $view
*
* @return false|string|void
Expand Down Expand Up @@ -209,15 +201,14 @@ private function buildAssetHead($html)
$head = $xpath->query('//head/title');
$template = $dom->createDocumentFragment();
// add a style link to the head tag of the page
foreach (self::$stylelink as $k => $v) {
foreach (self::$style_link_path as $k => $v) {
$template->appendXML('<link rel="stylesheet" type="text/css" href="' . $v . '">');
$head[0]->parentNode->insertbefore($template, $head[0]->nextSibling);
}
// add a script link to the head of the page
foreach (self::$jslink as $k => $v) {
foreach (self::$js_link_path 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);
}
Expand All @@ -234,25 +225,9 @@ private function buildAssetHead($html)
}
}

/**
* assign variables data to be displayed on file_page
*
* @param string $variable
* @param $value
*/
public function assign(string $variable, $value)
{
$this->data[$variable] = $value;
}

/**
* List all data assigned before being displayed
* @return array
*/
public function getAssignData(): array
{
return $this->data;
}


/**
* you should provide the extension of your file,
* in another case the file will be missing
Expand Down

0 comments on commit e4ee9d0

Please sign in to comment.