Skip to content

Commit

Permalink
[UPD] update view component and setup view generics
Browse files Browse the repository at this point in the history
  • Loading branch information
bim-g committed Sep 21, 2024
1 parent dbf0576 commit 821f687
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 99 deletions.
8 changes: 4 additions & 4 deletions controller/indexController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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("/");
Expand Down
41 changes: 31 additions & 10 deletions src/Core/Views/Provider/Contract/ViewsContract.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<T> $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<T> $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<T> $variable
* @var <T> $value
*/
public function assign(string $variable, $value);

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

/**
* Render html string text
* @var class-string<T> $html
*/
public function renderHTML(string $html): void;

/**
* @var class-string<T> $template
*/
public function setLayout(string $template): void;

/**
* provide layout content name
* @var class-string<T> $layout_name
*/
public function setLayoutContent(string $layout_name): void;

/**
* reset view to default configuration
*/
public function flush(): void;
}
78 changes: 67 additions & 11 deletions src/Core/Views/Provider/ViewBuilderProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,41 +5,59 @@

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<T>
*/
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<T> $folder_name
*
*/
public function setFolder(string $folder_name)
public function setFolder(string $folder_name): void
{
$this->folder_name = Escape::addSlashes($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<T> $variable
* @param $value
*/
public function assign(string $variable, $value)
public function assign(string $variable, $value): void
{
$this->data[$variable] = $value;
}
Expand All @@ -52,4 +70,42 @@ public function getAssignData(): array
{
return $this->data;
}

/**
* render html string text
* @param class-string<T> $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<T> $template
*/
public function setLayout(string $template): void
{
$this->layout = Application::$ROOT_DIR . '/views' . $template;
}

/**
* @param class-string<T> $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 = '';
}
}
Loading

0 comments on commit 821f687

Please sign in to comment.